Files
IoTManager/src/main.cpp

236 lines
4.5 KiB
C++
Raw Normal View History

2020-06-18 23:43:06 +02:00
#include "Global.h"
2020-06-17 23:30:48 +03:00
2020-06-25 00:15:10 +03:00
#include "HttpServer.h"
2020-06-25 17:57:17 +03:00
#include "Bus/BusScanner.h"
2020-06-26 01:38:29 +03:00
#include "Utils/Timings.h"
2020-06-25 00:15:10 +03:00
2020-06-22 03:11:02 +03:00
void not_async_actions();
static const char* MODULE = "Main";
2020-06-27 01:21:58 +03:00
Timings metric;
boolean initialized = false;
2020-06-22 03:11:02 +03:00
void setup() {
WiFi.setAutoConnect(false);
WiFi.persistent(false);
Serial.begin(115200);
Serial.flush();
Serial.println();
Serial.println("--------------started----------------");
setChipId();
pm.info("FS");
fileSystemInit();
pm.info("Config");
loadConfig();
2020-06-27 04:20:48 +03:00
pm.info("Clock");
clock_init();
2020-06-22 03:11:02 +03:00
pm.info("Commands");
2020-06-27 04:20:48 +03:00
cmd_init();
2020-06-22 03:11:02 +03:00
pm.info("Sensors");
sensors_init();
pm.info("Init");
2020-06-27 04:20:48 +03:00
all_init();
2020-06-22 03:11:02 +03:00
pm.info("Network");
startSTAMode();
pm.info("Uptime");
uptime_init();
2020-06-22 14:01:12 +03:00
if (!TELEMETRY_UPDATE_INTERVAL) {
pm.info("Telemetry: Disabled");
}
2020-06-22 03:11:02 +03:00
telemetry_init();
pm.info("Updater");
2020-06-25 17:57:17 +03:00
initUpdater();
2020-06-22 03:11:02 +03:00
2020-06-25 00:15:10 +03:00
pm.info("HttpServer");
HttpServer::init();
2020-06-22 03:11:02 +03:00
pm.info("WebAdmin");
web_init();
#ifdef UDP_ENABLED
2020-06-27 01:21:58 +03:00
pm.info("Broadcast UDP");
2020-06-22 03:11:02 +03:00
UDP_init();
#endif
ts.add(
2020-06-27 04:20:48 +03:00
TEST, 1000 * 60, [&](void*) {
2020-06-25 09:21:42 +03:00
pm.info(printMemoryStatus());
2020-06-22 03:11:02 +03:00
},
nullptr, true);
just_load = false;
2020-06-27 01:21:58 +03:00
initialized = true;
2020-06-17 23:30:48 +03:00
}
2020-06-22 03:11:02 +03:00
void loop() {
2020-06-27 01:21:58 +03:00
if (!initialized) {
return;
}
timeNow->loop();
2020-06-22 03:11:02 +03:00
#ifdef OTA_UPDATES_ENABLED
ArduinoOTA.handle();
#endif
#ifdef WS_enable
ws.cleanupClients();
#endif
not_async_actions();
2020-06-24 01:16:00 +03:00
MqttClient::loop();
2020-06-22 17:40:29 +03:00
loopCmd();
2020-06-26 01:38:29 +03:00
2020-06-22 17:40:29 +03:00
loopButton();
2020-06-26 01:38:29 +03:00
2020-06-22 17:40:29 +03:00
loopScenario();
2020-06-26 01:38:29 +03:00
2020-06-22 03:11:02 +03:00
#ifdef UDP_ENABLED
2020-06-22 17:40:29 +03:00
loopUdp();
2020-06-22 03:11:02 +03:00
#endif
2020-06-24 02:11:08 +03:00
loopSerial();
2020-06-22 03:11:02 +03:00
ts.update();
}
void not_async_actions() {
2020-06-24 01:16:00 +03:00
if (mqttParamsChanged) {
MqttClient::reconnect();
mqttParamsChanged = false;
}
2020-06-27 01:21:58 +03:00
2020-06-25 17:57:17 +03:00
getLastVersion();
2020-06-27 01:21:58 +03:00
2020-06-25 17:57:17 +03:00
flashUpgrade();
2020-06-22 03:11:02 +03:00
#ifdef UDP_ENABLED
do_udp_data_parse();
do_mqtt_send_settings_to_udp();
#endif
2020-06-27 01:21:58 +03:00
do_scan_bus();
2020-06-25 05:09:11 +03:00
2020-06-27 01:21:58 +03:00
do_check_fs();
2020-06-22 03:11:02 +03:00
}
2020-06-19 07:48:31 +03:00
String getURL(const String& urls) {
2020-06-19 07:27:06 +03:00
String res = "";
2020-06-17 23:30:48 +03:00
HTTPClient http;
2020-06-19 07:27:06 +03:00
http.begin(urls);
2020-06-17 23:30:48 +03:00
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
2020-06-19 07:27:06 +03:00
res = http.getString();
2020-06-17 23:30:48 +03:00
} else {
2020-06-19 07:27:06 +03:00
res = "error";
2020-06-17 23:30:48 +03:00
}
http.end();
2020-06-19 07:27:06 +03:00
return res;
2020-06-17 23:30:48 +03:00
}
2020-06-19 07:27:06 +03:00
2020-06-18 23:43:06 +02:00
void safeDataToFile(String data, String Folder) {
2020-06-17 23:30:48 +03:00
String fileName;
fileName.toLowerCase();
fileName = deleteBeforeDelimiter(fileName, " ");
fileName.replace(" ", ".");
fileName.replace("..", ".");
fileName = Folder + "/" + fileName + ".txt";
2020-06-22 17:40:29 +03:00
2020-06-19 14:50:34 +02:00
jsonWriteStr(configLiveJson, "test", fileName);
2020-06-17 23:30:48 +03:00
}
2020-06-18 23:43:06 +02:00
2020-06-22 14:01:12 +03:00
void sendConfig(String topic, String widgetConfig, String key, String date) {
2020-06-17 23:30:48 +03:00
yield();
2020-06-22 14:01:12 +03:00
topic = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipId + "/" + topic + "/status";
2020-06-17 23:30:48 +03:00
String outer = "{\"widgetConfig\":";
String inner = "{\"";
inner = inner + key;
inner = inner + "\":\"";
inner = inner + date;
inner = inner + "\"";
inner = inner + "}}";
String t = outer + inner;
yield();
}
2020-06-21 15:20:40 +03:00
void setChipId() {
chipId = getChipId();
Serial.println(chipId);
}
2020-06-27 01:21:58 +03:00
void saveConfig() {
writeFile(String("config.json"), configSetupJson);
}
2020-06-17 23:30:48 +03:00
#ifdef ESP8266
2020-06-21 15:20:40 +03:00
#ifdef LED_PIN
2020-06-22 03:11:02 +03:00
void setLedStatus(LedStatus_t status) {
2020-06-21 03:43:15 +03:00
pinMode(LED_PIN, OUTPUT);
2020-06-22 03:11:02 +03:00
switch (status) {
case LED_OFF:
noTone(LED_PIN);
digitalWrite(LED_PIN, HIGH);
break;
case LED_ON:
noTone(LED_PIN);
digitalWrite(LED_PIN, LOW);
break;
case LED_SLOW:
tone(LED_PIN, 1);
break;
case LED_FAST:
tone(LED_PIN, 20);
break;
default:
break;
2020-06-17 23:30:48 +03:00
}
2020-06-20 22:51:14 +03:00
}
2020-06-17 23:30:48 +03:00
#endif
#endif
2020-06-27 04:20:48 +03:00
void do_fscheck(String& results) {
// TODO Проверка наличие важных файлов, возможно версии ФС
}
void clock_init() {
timeNow = new Clock();
timeNow->setNtpPool(jsonReadStr(configSetupJson, "ntp"));
timeNow->setTimezone(jsonReadStr(configSetupJson, "timezone").toInt());
ts.add(
TIME_SYNC, 30000, [&](void*) {
timeNow->hasSync();
},
nullptr, true);
}
void do_scan_bus() {
if (busScanFlag) {
String res = "";
BusScanner* scanner = BusScannerFactory::get(res, busToScan);
scanner->scan();
jsonWriteStr(configLiveJson, BusScannerFactory::label(busToScan), res);
busScanFlag = false;
}
}
void do_check_fs() {
if (fsCheckFlag) {
String buf;
do_fscheck(buf);
jsonWriteStr(configLiveJson, "fscheck", buf);
fsCheckFlag = false;
}
}