diff --git a/include/Clock.h b/include/Clock.h index 00ece8f8..3f361122 100644 --- a/include/Clock.h +++ b/include/Clock.h @@ -9,8 +9,7 @@ class Clock { const char* MODULE = "Clock"; public: - Clock() : _timezone{0}, _ntp{}, _hasSynced{false}, _configured{false} { - } + Clock() : _timezone{0}, _hasSynced{false}, _configured{false} {} bool hasSync() { if (!_hasSynced) { @@ -42,7 +41,7 @@ class Clock { } void setupSntp() { - int tzs = getBiasInSeconds(); + int tzs = getOffsetInSeconds(_timezone); int tzh = tzs / 3600; tzs -= tzh * 3600; int tzm = tzs / 60; @@ -61,36 +60,23 @@ class Clock { // i++; // delay(1000); // } - // #endif + // #endifr bool hasTimeSynced() { - int uptime = millis() / 1000; - return getSystemTime() > uptime; + return getSystemTime() > 30000; } time_t getSystemTime() { timeval tv{0, 0}; - timezone tz = getTimeZone(getBiasInMinutes()); - time_t epoch = 0; + timezone tz = timezone{getOffsetInMinutes(_timezone), 0}; if (gettimeofday(&tv, &tz) != -1) { - epoch = tv.tv_sec; + _epoch = tv.tv_sec; } - return epoch + getBiasInSeconds(); - } - - int getBiasInSeconds() { - return getBiasInMinutes() * 60; - } - - int getBiasInMinutes() { - return _timezone * 60; - } - - const timezone getTimeZone(int minutes) { - return timezone{minutes, 0}; + return _epoch; } private: + time_t _epoch; int _timezone; String _ntp; bool _hasSynced; diff --git a/include/Utils/TimeUtils.h b/include/Utils/TimeUtils.h index 658634d9..7cf508a5 100644 --- a/include/Utils/TimeUtils.h +++ b/include/Utils/TimeUtils.h @@ -1,16 +1,14 @@ #pragma once - -#include #ifdef ESP8266 -#include +#include #endif - -void Time_Init(); +#include /* * Получение текущего времени */ String getTime(); + /* * Получаем время в формате linux gmt */ @@ -36,11 +34,15 @@ int timeToMin(String Time); const String prettyMillis(unsigned long time_ms = millis()); /* -* Время (мс) прошедщее с @simce +* Время (мс) прошедщее с @since */ unsigned long millis_since(unsigned long sinse); /* -* Интерввал времени (мс) между @start и @fimish +* Интерввал времени (мс) между @start и @finish */ -unsigned long millis_passed(unsigned long start, unsigned long finish); \ No newline at end of file +unsigned long millis_passed(unsigned long start, unsigned long finish); + +int getOffsetInSeconds(int timezone); + +int getOffsetInMinutes(int timezone); diff --git a/include/Utils/Timings.h b/include/Utils/Timings.h new file mode 100644 index 00000000..c992eeee --- /dev/null +++ b/include/Utils/Timings.h @@ -0,0 +1,71 @@ +#include + +enum Timings_t { MT_ONE, + MT_TWO, + NUM_TIMINGS }; + +struct Timing { + unsigned long _total_mu; + unsigned long _min_mu; + unsigned long _max_mu; + + Timing() : _total_mu{0}, _min_mu{999999}, _max_mu{0} {}; + + void reset() { + _total_mu = 0; + _min_mu = 999999; + _max_mu = 0; + } + + void add(unsigned long time_mu) { + if (time_mu == 0) return; + + _total_mu += time_mu; + + if (_min_mu > time_mu) { + _min_mu = time_mu; + } + + if (_max_mu < time_mu) { + _max_mu = time_mu; + } + } +}; + +static const char* module_name[NUM_TIMINGS] = {"strings", "boolean"}; + +struct Timings { + Timing mu[NUM_TIMINGS]; + + unsigned long _counter; + unsigned long _start; + unsigned long long _total; + + Timings() : _counter{0}, _start{0} {}; + + void add(size_t module, unsigned long now = micros()) { + unsigned long time = now - _start; + _total += time; + mu[module].add(time); + _start = now; + } + + void count() { + _counter++; + _start = micros(); + } + + void print() { + if (!_counter) { + return; + }; + Serial.printf("lp/ms: %llu ", _counter / _total); + for (size_t i = 0; i < NUM_TIMINGS; i++) { + Serial.printf("%s: %.2f%% ", module_name[i], ((float)mu[i]._total_mu / _total) * 100); + mu[i].reset(); + } + Serial.println(); + _counter = 0; + _total = 0; + }; +}; \ No newline at end of file diff --git a/src/Utils/TimeUtils.cpp b/src/Utils/TimeUtils.cpp index 59aa09cc..09c7185d 100644 --- a/src/Utils/TimeUtils.cpp +++ b/src/Utils/TimeUtils.cpp @@ -4,6 +4,7 @@ #include "Utils\StringUtils.h" #define ONE_MINUTE_s 60 +#define ONE_HOUR_m 60 #define ONE_HOUR_s 60 * ONE_MINUTE_s time_t t; @@ -13,22 +14,20 @@ String getTimeUnix() { t = time(NULL); tm = localtime(&t); Serial.printf("%04d/%02d/%02d(%s) %02d:%02d:%02d\n", - tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, - wd[tm->tm_wday], + tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, wd[tm->tm_wday], tm->tm_hour, tm->tm_min, tm->tm_sec); delay(1000); time_t now = time(nullptr); if (now < 30000) { return "failed"; - } else { - return String(now); } + return String(now); } boolean getUnixTimeStr(String& res) { time_t now = time(nullptr); res = String(now); - return now < 30000; + return now > 30000; } String getTime() { @@ -158,3 +157,11 @@ unsigned long millis_passed(unsigned long start, unsigned long finish) { } return result; } + +int getOffsetInSeconds(int timezone) { + return getOffsetInMinutes(timezone) * ONE_MINUTE_s; +} + +int getOffsetInMinutes(int timezone) { + return timezone * ONE_HOUR_m; +} diff --git a/src/main.cpp b/src/main.cpp index 09e3fb7d..00737fff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include "HttpServer.h" #include "Bus/BusScanner.h" +#include "Utils/Timings.h" void not_async_actions(); @@ -84,22 +85,27 @@ void saveConfig() { writeFile(String("config.json"), configSetupJson); } +Timings metric; + void loop() { #ifdef OTA_UPDATES_ENABLED ArduinoOTA.handle(); #endif - #ifdef WS_enable ws.cleanupClients(); #endif - + metric.add(MT_ONE); not_async_actions(); + metric.add(MT_TWO); MqttClient::loop(); loopCmd(); + loopButton(); + loopScenario(); + #ifdef UDP_ENABLED loopUdp(); #endif @@ -107,6 +113,12 @@ void loop() { loopSerial(); ts.update(); + + if (metric._counter > 100000) { + metric.print(); + } else { + metric.count(); + } } void not_async_actions() {