From 7ac06913478c1fca83cd359b0361c19f8f1941b2 Mon Sep 17 00:00:00 2001 From: Yuri Trikoz Date: Sat, 27 Jun 2020 04:20:48 +0300 Subject: [PATCH] clock fix --- include/Clock.h | 12 ++++-- include/Global.h | 13 +++--- include/Utils/JsonUtils.h | 8 ++-- src/Cmd.cpp | 8 ++-- src/Init.cpp | 2 +- src/Utils/JsonUtils.cpp | 12 +++--- src/main.cpp | 88 ++++++++++++++++++--------------------- 7 files changed, 69 insertions(+), 74 deletions(-) diff --git a/include/Clock.h b/include/Clock.h index a18c51e8..d09d388d 100644 --- a/include/Clock.h +++ b/include/Clock.h @@ -113,7 +113,7 @@ class Clock { * Локальное время "дд.ММ.гг" */ const String getDateDigitalFormated() { - char buf[16]; + char buf[32]; sprintf(buf, "%02d.%02d.%02d", _time_local.day_of_month, _time_local.month, _time_local.year); return String(buf); } @@ -122,16 +122,22 @@ class Clock { * Локальное время "чч:мм:cc" */ const String getTime() { - char buf[16]; + char buf[32]; sprintf(buf, "%02d:%02d:%02d", _time_local.hour, _time_local.minute, _time_local.second); return String(buf); } + const String getTimeJson() { + char buf[32]; + sprintf(buf, "%02d-%02d-%02d", _time_local.hour, _time_local.minute, _time_local.second); + return String(buf); + } + /* * Локальное время "чч:мм" */ const String getTimeWOsec() { - char buf[16]; + char buf[32]; sprintf(buf, "%02d:%02d", _time_local.hour, _time_local.minute); return String(buf); } diff --git a/include/Global.h b/include/Global.h index c646f943..dceb7399 100644 --- a/include/Global.h +++ b/include/Global.h @@ -127,7 +127,7 @@ extern int sensors_reading_map[15]; */ // Cmd -extern void CMD_init(); +extern void cmd_init(); extern void button(); extern void buttonSet(); extern void buttonChange(); @@ -161,12 +161,11 @@ extern void fileExecute(const String& filename); extern void stringExecute(String& cmdStr); // Init extern void loadConfig(); -extern void All_init(); +extern void all_init(); extern void statistics_init(); extern void loadScenario(); extern void Device_init(); extern void prsets_init(); -extern void up_time(); // Logging extern void logging(); @@ -178,8 +177,10 @@ extern void choose_log_date_and_send(); extern void setChipId(); extern void saveConfig(); extern String getURL(const String& urls); - +extern void do_check_fs(); +extern void do_scan_bus(); extern void servo_(); +extern void clock_init(); extern void setLedStatus(LedStatus_t); @@ -251,7 +252,6 @@ extern void UDP_init(); extern void do_udp_data_parse(); extern void do_mqtt_send_settings_to_udp(); -// iot_firmware extern void addCommandLoop(const String& cmdStr); extern void loopSerial(); extern void loopCmd(); @@ -266,5 +266,4 @@ extern void uptime_init(); // Web extern void web_init(); - -extern void telemetry_init(); \ No newline at end of file +extern void telemetry_init(); diff --git a/include/Utils/JsonUtils.h b/include/Utils/JsonUtils.h index 762fc286..d5200cdb 100644 --- a/include/Utils/JsonUtils.h +++ b/include/Utils/JsonUtils.h @@ -6,10 +6,10 @@ String jsonReadStr(String& json, String name); int jsonReadInt(String& json, String name); -String jsonWriteStr(String& json, String name, String volume); +boolean jsonReadBool(String& json, String name); -String jsonWriteInt(String& json, String name, int volume); +String jsonWriteStr(String& json, String name, String value); -String jsonWriteFloat(String& json, String name, float volume); +String jsonWriteInt(String& json, String name, int value); -boolean jsonReadBool(String& json, String name); \ No newline at end of file +String jsonWriteFloat(String& json, String name, float value); diff --git a/src/Cmd.cpp b/src/Cmd.cpp index b9521860..fbfe3638 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -15,7 +15,7 @@ SoftwareSerial *mySerial = nullptr; void getData(); -void CMD_init() { +void cmd_init() { sCmd.addCommand("button", button); sCmd.addCommand("buttonSet", buttonSet); sCmd.addCommand("buttonChange", buttonChange); @@ -330,10 +330,8 @@ void timeSet() { void handle_time_init() { ts.add( TIME, 1000, [&](void *) { - String tmp = timeNow->getTime(); - jsonWriteStr(configLiveJson, "time", tmp); - tmp.replace(":", "-"); - jsonWriteStr(configLiveJson, "timenow", tmp); + jsonWriteStr(configLiveJson, "time", timeNow->getTime()); + jsonWriteStr(configLiveJson, "timenow", timeNow->getTimeJson()); eventGen("timenow", ""); }, nullptr, true); diff --git a/src/Init.cpp b/src/Init.cpp index 82ae3d60..2573b68a 100644 --- a/src/Init.cpp +++ b/src/Init.cpp @@ -17,7 +17,7 @@ void loadConfig() { Serial.println(configSetupJson); } -void All_init() { +void all_init() { Device_init(); loadScenario(); Timer_countdown_init(); diff --git a/src/Utils/JsonUtils.cpp b/src/Utils/JsonUtils.cpp index d814c409..df6ff668 100644 --- a/src/Utils/JsonUtils.cpp +++ b/src/Utils/JsonUtils.cpp @@ -20,28 +20,28 @@ int jsonReadInt(String& json, String name) { return root[name]; } -String jsonWriteStr(String& json, String name, String volume) { +String jsonWriteStr(String& json, String name, String value) { DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.parseObject(json); - root[name] = volume; + root[name] = value; json = ""; root.printTo(json); return json; } -String jsonWriteInt(String& json, String name, int volume) { +String jsonWriteInt(String& json, String name, int value) { DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.parseObject(json); - root[name] = volume; + root[name] = value; json = ""; root.printTo(json); return json; } -String jsonWriteFloat(String& json, String name, float volume) { +String jsonWriteFloat(String& json, String name, float value) { DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.parseObject(json); - root[name] = volume; + root[name] = value; json = ""; root.printTo(json); return json; diff --git a/src/main.cpp b/src/main.cpp index b39d48e1..192912fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,40 +11,6 @@ static const char* MODULE = "Main"; Timings metric; boolean initialized = false; -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; - } -} - void setup() { WiFi.setAutoConnect(false); WiFi.persistent(false); @@ -62,14 +28,17 @@ void setup() { pm.info("Config"); loadConfig(); + pm.info("Clock"); + clock_init(); + pm.info("Commands"); - CMD_init(); + cmd_init(); pm.info("Sensors"); sensors_init(); pm.info("Init"); - All_init(); + all_init(); pm.info("Network"); startSTAMode(); @@ -91,15 +60,12 @@ void setup() { pm.info("WebAdmin"); web_init(); - pm.info("Clock"); - clock_init(); - #ifdef UDP_ENABLED pm.info("Broadcast UDP"); UDP_init(); #endif ts.add( - TEST, 10000, [&](void*) { + TEST, 1000 * 60, [&](void*) { pm.info(printMemoryStatus()); }, nullptr, true); @@ -121,10 +87,8 @@ void loop() { #ifdef WS_enable ws.cleanupClients(); #endif - // metric.add(MT_ONE); not_async_actions(); - // metric.add(MT_TWO); MqttClient::loop(); loopCmd(); @@ -140,12 +104,6 @@ void loop() { loopSerial(); ts.update(); - - if (metric._counter > 100000) { - metric.print(); - } else { - metric.count(); - } } void not_async_actions() { @@ -241,3 +199,37 @@ void setLedStatus(LedStatus_t status) { } #endif #endif + +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; + } +}