diff --git a/include/Global.h b/include/Global.h index 96baa6fc..6eecf0f8 100644 --- a/include/Global.h +++ b/include/Global.h @@ -129,6 +129,8 @@ struct Time_t { unsigned long valid; }; +extern unsigned long unixTime; +extern bool isTimeSynch; extern Time_t _time_local; extern Time_t _time_utc; extern bool _time_isTrust; diff --git a/include/utils/FileUtils.h b/include/utils/FileUtils.h index 4fbcf93d..6d51ad15 100644 --- a/include/utils/FileUtils.h +++ b/include/utils/FileUtils.h @@ -6,8 +6,8 @@ extern void writeFileUint8tByFrames(const String& filename, uint8_t*& big_buf, s extern void writeFileUint8tByByte(const String& filename, uint8_t*& payload, size_t length, size_t headerLenth); extern File seekFile(const String& filename, size_t position = 0); extern const String writeFile(const String& filename, const String& str); +extern const String addFileLn(const String& filename, const String& str); extern const String readFile(const String& filename, size_t max_size); extern const String filepath(const String& filename); extern bool cutFile(const String& src, const String& dst); -extern const String addFileLn(const String& filename, const String& str); extern void onFlashWrite(); \ No newline at end of file diff --git a/src/Global.cpp b/src/Global.cpp index 43c88086..931123f2 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -65,6 +65,8 @@ String scenario = ""; String mqttRootDevice = ""; // Time +unsigned long unixTime = 0; +bool isTimeSynch = false; Time_t _time_local; Time_t _time_utc; bool _time_isTrust = false; diff --git a/src/NTP.cpp b/src/NTP.cpp index 71abee66..296f2b2e 100644 --- a/src/NTP.cpp +++ b/src/NTP.cpp @@ -8,14 +8,16 @@ void ntpInit() { ts.add( TIME, 1000, [&](void*) { - unsigned long unixTime = getSystemTime(); + unixTime = getSystemTime(); if (unixTime < MIN_DATETIME) { + isTimeSynch = false; // SerialPrint("E", "NTP", "Time not synched"); synchTime(); return; } breakEpochToTime(unixTime + jsonReadInt(settingsFlashJson, F("timezone")) * 60 * 60, _time_local); breakEpochToTime(unixTime, _time_utc); + isTimeSynch = true; String timenow = getTimeLocal_hhmm(); static String prevTime; if (prevTime != timenow) { @@ -25,7 +27,7 @@ void ntpInit() { jsonWriteStr_(errorsHeapJson, F("timenow"), dateAndTime); SerialPrint("I", F("NTP"), "✔ " + dateAndTime); } - _time_isTrust = true; // доверяем значению времени + _time_isTrust = true; // доверяем значению времени }, nullptr, true); diff --git a/src/modules/virtual/Logging/Loging.cpp b/src/modules/virtual/Logging/Loging.cpp index 06443cb3..06a20f0f 100644 --- a/src/modules/virtual/Logging/Loging.cpp +++ b/src/modules/virtual/Logging/Loging.cpp @@ -20,9 +20,13 @@ class Loging : public IoTItem { if (value == "") { SerialPrint("E", F("Logging"), F("no value set")); } else { - regEvent(value, "Logging"); - String logData = String(_time_local.second) + " " + value + "\r\n"; - writeFile("log.txt", logData); + if (isTimeSynch) { + regEvent(value, "Logging"); + String logData = String(unixTime) + " " + value + "\r\n"; + addFileLn("/log.txt", logData); + } else { + SerialPrint("E", F("Logging"), F("cant logging - no time Synch")); + } } } }; diff --git a/src/utils/FileUtils.cpp b/src/utils/FileUtils.cpp index f097d61c..e5a13450 100644 --- a/src/utils/FileUtils.cpp +++ b/src/utils/FileUtils.cpp @@ -101,6 +101,18 @@ const String writeFile(const String& filename, const String& str) { onFlashWrite(); } +const String addFileLn(const String& filename, const String& str) { + String path = filepath(filename); + auto file = FileFS.open(path, "a"); + if (!file) { + return "failed"; + } + file.println(str); + file.close(); + return "sucсess"; + onFlashWrite(); +} + const String readFile(const String& filename, size_t max_size) { String path = filepath(filename); auto file = FileFS.open(path, "r"); @@ -146,17 +158,7 @@ bool cutFile(const String& src, const String& dst) { return true; } -const String addFileLn(const String& filename, const String& str) { - String path = filepath(filename); - auto file = FileFS.open(path, "a"); - if (!file) { - return "failed"; - } - file.println(str); - file.close(); - onFlashWrite(); - return "sucсess"; -} + void onFlashWrite() { flashWriteNumber++;