diff --git a/include/Global.h b/include/Global.h index bcab371a..dadf273d 100644 --- a/include/Global.h +++ b/include/Global.h @@ -1,6 +1,10 @@ #pragma once +//внешние глобальные директории #include #include #include -#include \ No newline at end of file +#include + +//внутренние глобальные директории проекта +#include "Utils\SerialPrint.h" \ No newline at end of file diff --git a/include/utils/SerialPrint.h b/include/utils/SerialPrint.h new file mode 100644 index 00000000..afbacc06 --- /dev/null +++ b/include/utils/SerialPrint.h @@ -0,0 +1,5 @@ +#pragma once +#include "Global.h" +#include "Utils/TimeUtils.h" + +void SerialPrint(String errorLevel, String module, String msg); \ No newline at end of file diff --git a/include/utils/TimeUtils.h b/include/utils/TimeUtils.h new file mode 100644 index 00000000..505883fa --- /dev/null +++ b/include/utils/TimeUtils.h @@ -0,0 +1,65 @@ +#pragma once + +#include "Global.h" + +#define ONE_MINUTE_s 60 +#define ONE_HOUR_m 60 +#define ONE_HOUR_s 60 * ONE_MINUTE_s +#define LEAP_YEAR(Y) (((1970 + Y) > 0) && !((1970 + Y) % 4) && (((1970 + Y) % 100) || !((1970 + Y) % 400))) +#define MIN_DATETIME 1575158400 +#define ONE_SECOND_ms 1000 + +/* + * Время (мс) прошедщее с @since + */ +unsigned long millis_since(unsigned long sinse); + +/* + * Интерввал времени (мс) между @start и @finish + */ +unsigned long millis_passed(unsigned long start, unsigned long finish); + +/* + * Форматиронное время интервала (мс) + * "чч:мм:cc", + * "дд чч:мм", если > 24 часов + */ +const String prettyMillis(unsigned long time_ms = millis()); + +/* + * Форматиронное время интервала (c) + * "чч:мм:cc", + * "дд чч:мм", если > 24 часов + */ +const String prettySeconds(unsigned long time_s); + +/* + * Тайм зона в секундах + */ +int getOffsetInSeconds(int timezone); + +/* + * Тайм зона в минутах + */ +int getOffsetInMinutes(int timezone); + +/* + * Разбивает время на составляющие + */ + +struct Time_t { + uint8_t second; + uint8_t minute; + uint8_t hour; + uint8_t day_of_week; + uint8_t day_of_month; + uint8_t month; + uint16_t day_of_year; + uint16_t year; + unsigned long days; + unsigned long valid; +}; + +void breakEpochToTime(unsigned long epoch, Time_t& tm); + +// void timeInit(); \ No newline at end of file diff --git a/src/EspFileSystem.cpp b/src/EspFileSystem.cpp index c464a1dc..0087f9fe 100644 --- a/src/EspFileSystem.cpp +++ b/src/EspFileSystem.cpp @@ -2,10 +2,10 @@ bool fileSystemInit() { if (!FileFS.begin()) { - Serial.println("FS Init ERROR, may be FS was not flashed"); + SerialPrint(F("E"), F("FS"), F("Init ERROR, may be FS was not flashed")); return false; } - Serial.println("FS Init completed"); + SerialPrint(F("i"), F("FS"), F("Init completed")); return true; } @@ -13,7 +13,7 @@ File seekFile(const String& filename, size_t position) { String path = filepath(filename); auto file = FileFS.open(path, "r"); if (!file) { - Serial.println("[E] file error"); + SerialPrint(F("E"), F("FS"), F("seek file error")); } file.seek(position, SeekSet); return file; @@ -53,9 +53,9 @@ const String filepath(const String& filename) { bool cutFile(const String& src, const String& dst) { String srcPath = filepath(src); String dstPath = filepath(dst); - Serial.println("cut " + srcPath + " to " + dstPath); + SerialPrint(F("i"), F("FS"), "cut " + srcPath + " to " + dstPath); if (!FileFS.exists(srcPath)) { - Serial.println("not exist: " + srcPath); + SerialPrint(F("E"), F("FS"), "not exist: " + srcPath); return false; } if (FileFS.exists(dstPath)) { @@ -63,7 +63,6 @@ bool cutFile(const String& src, const String& dst) { } auto srcFile = FileFS.open(srcPath, "r"); auto dstFile = FileFS.open(dstPath, "w"); - uint8_t buf[512]; while (srcFile.available()) { size_t len = srcFile.read(buf, 512); diff --git a/src/utils/SerialPrint.cpp b/src/utils/SerialPrint.cpp new file mode 100644 index 00000000..dce41c16 --- /dev/null +++ b/src/utils/SerialPrint.cpp @@ -0,0 +1,9 @@ + +#include "Utils\SerialPrint.h" + +void SerialPrint(String errorLevel, String module, String msg) { + Serial.println(prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg); + String tosend = "[" + errorLevel + "] [" + module + "] " + msg; + + // ws.textAll(tosend); +} \ No newline at end of file diff --git a/src/utils/TimeUtils.cpp b/src/utils/TimeUtils.cpp new file mode 100644 index 00000000..6aa08f05 --- /dev/null +++ b/src/utils/TimeUtils.cpp @@ -0,0 +1,132 @@ +#include "Utils/TimeUtils.h" + +static const uint8_t days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + +static const char* TIME_FORMAT PROGMEM = "%02d:%02d:%02d"; +static const char* TIME_FORMAT_WITH_DAYS PROGMEM = "%dd %02d:%02d"; + +const String prettySeconds(unsigned long time_s) { + unsigned long tmp = time_s; + unsigned long seconds; + unsigned long minutes; + unsigned long hours; + unsigned long days; + seconds = tmp % 60; + tmp = tmp / 60; + + minutes = tmp % 60; + tmp = tmp / 60; + + hours = tmp % 24; + days = tmp / 24; + + char buf[32]; + + if (days) { + sprintf_P(buf, TIME_FORMAT_WITH_DAYS, days, hours, minutes, seconds); + } else { + sprintf_P(buf, TIME_FORMAT, hours, minutes, seconds); + } + return String(buf); +} + +const String prettyMillis(unsigned long time_ms) { + return prettySeconds(time_ms / 1000); +} + +unsigned long millis_since(unsigned long sinse) { + return millis_passed(sinse, millis()); +} + +unsigned long millis_passed(unsigned long start, unsigned long finish) { + unsigned long result = 0; + if (start <= finish) { + unsigned long passed = finish - start; + if (passed <= __LONG_MAX__) { + result = static_cast(passed); + } else { + result = static_cast((__LONG_MAX__ - finish) + start + 1u); + } + } else { + unsigned long passed = start - finish; + if (passed <= __LONG_MAX__) { + result = static_cast(passed); + result = -1 * result; + } else { + result = static_cast((__LONG_MAX__ - start) + finish + 1u); + result = -1 * result; + } + } + return result; +} + +int getOffsetInSeconds(int timezone) { + return getOffsetInMinutes(timezone) * ONE_MINUTE_s; +} + +int getOffsetInMinutes(int timezone) { + return timezone * ONE_HOUR_m; +} + +void breakEpochToTime(unsigned long epoch, Time_t& tm) { + unsigned long time = epoch; + tm.second = time % 60; + time /= 60; // now it is minutes + tm.minute = time % 60; + time /= 60; // now it is hours + tm.hour = time % 24; + time /= 24; // now it is days + tm.days = time; + tm.day_of_week = ((time + 4) % 7) + 1; // Sunday is day 1 + + uint8_t year = 0; + unsigned long days = 0; + + while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) { + year++; + } + tm.year = year - 30; + + days -= LEAP_YEAR(year) ? 366 : 365; + time -= days; // now it is days in this year, starting at 0 + tm.day_of_year = time; + + uint8_t month; + uint8_t month_length; + for (month = 0; month < 12; month++) { + if (1 == month) { // february + if (LEAP_YEAR(year)) { + month_length = 29; + } else { + month_length = 28; + } + } else { + month_length = days_in_month[month]; + } + + if (time >= month_length) { + time -= month_length; + } else { + break; + } + } + tm.month = month + 1; + tm.day_of_month = time + 1; + tm.valid = (epoch > MIN_DATETIME); +} + +//void timeInit() { +// ts.add( +// TIME, 1000, [&](void*) { +// String timenow = timeNow->getTimeWOsec(); +// static String prevTime; +// if (prevTime != timenow) { +// prevTime = timenow; +// jsonWriteStr(configLiveJson, "timenow", timenow); +// eventGen2("timenow", timenow); +// SerialPrint("I", F("NTP"), timenow); +// } +// }, +// nullptr, true); +// SerialPrint("I", F("NTP"), F("Handle time init")); +//}