mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 20:09:14 +03:00
@@ -9,8 +9,7 @@ class Clock {
|
|||||||
const char* MODULE = "Clock";
|
const char* MODULE = "Clock";
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Clock() : _timezone{0}, _ntp{}, _hasSynced{false}, _configured{false} {
|
Clock() : _timezone{0}, _hasSynced{false}, _configured{false} {}
|
||||||
}
|
|
||||||
|
|
||||||
bool hasSync() {
|
bool hasSync() {
|
||||||
if (!_hasSynced) {
|
if (!_hasSynced) {
|
||||||
@@ -42,7 +41,7 @@ class Clock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setupSntp() {
|
void setupSntp() {
|
||||||
int tzs = getBiasInSeconds();
|
int tzs = getOffsetInSeconds(_timezone);
|
||||||
int tzh = tzs / 3600;
|
int tzh = tzs / 3600;
|
||||||
tzs -= tzh * 3600;
|
tzs -= tzh * 3600;
|
||||||
int tzm = tzs / 60;
|
int tzm = tzs / 60;
|
||||||
@@ -61,36 +60,23 @@ class Clock {
|
|||||||
// i++;
|
// i++;
|
||||||
// delay(1000);
|
// delay(1000);
|
||||||
// }
|
// }
|
||||||
// #endif
|
// #endifr
|
||||||
|
|
||||||
bool hasTimeSynced() {
|
bool hasTimeSynced() {
|
||||||
int uptime = millis() / 1000;
|
return getSystemTime() > 30000;
|
||||||
return getSystemTime() > uptime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
time_t getSystemTime() {
|
time_t getSystemTime() {
|
||||||
timeval tv{0, 0};
|
timeval tv{0, 0};
|
||||||
timezone tz = getTimeZone(getBiasInMinutes());
|
timezone tz = timezone{getOffsetInMinutes(_timezone), 0};
|
||||||
time_t epoch = 0;
|
|
||||||
if (gettimeofday(&tv, &tz) != -1) {
|
if (gettimeofday(&tv, &tz) != -1) {
|
||||||
epoch = tv.tv_sec;
|
_epoch = tv.tv_sec;
|
||||||
}
|
}
|
||||||
return epoch + getBiasInSeconds();
|
return _epoch;
|
||||||
}
|
|
||||||
|
|
||||||
int getBiasInSeconds() {
|
|
||||||
return getBiasInMinutes() * 60;
|
|
||||||
}
|
|
||||||
|
|
||||||
int getBiasInMinutes() {
|
|
||||||
return _timezone * 60;
|
|
||||||
}
|
|
||||||
|
|
||||||
const timezone getTimeZone(int minutes) {
|
|
||||||
return timezone{minutes, 0};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
time_t _epoch;
|
||||||
int _timezone;
|
int _timezone;
|
||||||
String _ntp;
|
String _ntp;
|
||||||
bool _hasSynced;
|
bool _hasSynced;
|
||||||
|
|||||||
@@ -1,16 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
#include <TZ.h>
|
#include <time.h>
|
||||||
#endif
|
#endif
|
||||||
|
#include <Arduino.h>
|
||||||
void Time_Init();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Получение текущего времени
|
* Получение текущего времени
|
||||||
*/
|
*/
|
||||||
String getTime();
|
String getTime();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Получаем время в формате linux gmt
|
* Получаем время в формате linux gmt
|
||||||
*/
|
*/
|
||||||
@@ -36,11 +34,15 @@ int timeToMin(String Time);
|
|||||||
const String prettyMillis(unsigned long time_ms = millis());
|
const String prettyMillis(unsigned long time_ms = millis());
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Время (мс) прошедщее с @simce
|
* Время (мс) прошедщее с @since
|
||||||
*/
|
*/
|
||||||
unsigned long millis_since(unsigned long sinse);
|
unsigned long millis_since(unsigned long sinse);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Интерввал времени (мс) между @start и @fimish
|
* Интерввал времени (мс) между @start и @finish
|
||||||
*/
|
*/
|
||||||
unsigned long millis_passed(unsigned long start, unsigned long finish);
|
unsigned long millis_passed(unsigned long start, unsigned long finish);
|
||||||
|
|
||||||
|
int getOffsetInSeconds(int timezone);
|
||||||
|
|
||||||
|
int getOffsetInMinutes(int timezone);
|
||||||
|
|||||||
71
include/Utils/Timings.h
Normal file
71
include/Utils/Timings.h
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "Utils\StringUtils.h"
|
#include "Utils\StringUtils.h"
|
||||||
|
|
||||||
#define ONE_MINUTE_s 60
|
#define ONE_MINUTE_s 60
|
||||||
|
#define ONE_HOUR_m 60
|
||||||
#define ONE_HOUR_s 60 * ONE_MINUTE_s
|
#define ONE_HOUR_s 60 * ONE_MINUTE_s
|
||||||
|
|
||||||
time_t t;
|
time_t t;
|
||||||
@@ -13,22 +14,20 @@ String getTimeUnix() {
|
|||||||
t = time(NULL);
|
t = time(NULL);
|
||||||
tm = localtime(&t);
|
tm = localtime(&t);
|
||||||
Serial.printf("%04d/%02d/%02d(%s) %02d:%02d:%02d\n",
|
Serial.printf("%04d/%02d/%02d(%s) %02d:%02d:%02d\n",
|
||||||
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
|
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, wd[tm->tm_wday],
|
||||||
wd[tm->tm_wday],
|
|
||||||
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
||||||
delay(1000);
|
delay(1000);
|
||||||
time_t now = time(nullptr);
|
time_t now = time(nullptr);
|
||||||
if (now < 30000) {
|
if (now < 30000) {
|
||||||
return "failed";
|
return "failed";
|
||||||
} else {
|
|
||||||
return String(now);
|
|
||||||
}
|
}
|
||||||
|
return String(now);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean getUnixTimeStr(String& res) {
|
boolean getUnixTimeStr(String& res) {
|
||||||
time_t now = time(nullptr);
|
time_t now = time(nullptr);
|
||||||
res = String(now);
|
res = String(now);
|
||||||
return now < 30000;
|
return now > 30000;
|
||||||
}
|
}
|
||||||
|
|
||||||
String getTime() {
|
String getTime() {
|
||||||
@@ -158,3 +157,11 @@ unsigned long millis_passed(unsigned long start, unsigned long finish) {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int getOffsetInSeconds(int timezone) {
|
||||||
|
return getOffsetInMinutes(timezone) * ONE_MINUTE_s;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getOffsetInMinutes(int timezone) {
|
||||||
|
return timezone * ONE_HOUR_m;
|
||||||
|
}
|
||||||
|
|||||||
16
src/main.cpp
16
src/main.cpp
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "HttpServer.h"
|
#include "HttpServer.h"
|
||||||
#include "Bus/BusScanner.h"
|
#include "Bus/BusScanner.h"
|
||||||
|
#include "Utils/Timings.h"
|
||||||
|
|
||||||
void not_async_actions();
|
void not_async_actions();
|
||||||
|
|
||||||
@@ -84,22 +85,27 @@ void saveConfig() {
|
|||||||
writeFile(String("config.json"), configSetupJson);
|
writeFile(String("config.json"), configSetupJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Timings metric;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
#ifdef OTA_UPDATES_ENABLED
|
#ifdef OTA_UPDATES_ENABLED
|
||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef WS_enable
|
#ifdef WS_enable
|
||||||
ws.cleanupClients();
|
ws.cleanupClients();
|
||||||
#endif
|
#endif
|
||||||
|
metric.add(MT_ONE);
|
||||||
not_async_actions();
|
not_async_actions();
|
||||||
|
|
||||||
|
metric.add(MT_TWO);
|
||||||
MqttClient::loop();
|
MqttClient::loop();
|
||||||
|
|
||||||
loopCmd();
|
loopCmd();
|
||||||
|
|
||||||
loopButton();
|
loopButton();
|
||||||
|
|
||||||
loopScenario();
|
loopScenario();
|
||||||
|
|
||||||
#ifdef UDP_ENABLED
|
#ifdef UDP_ENABLED
|
||||||
loopUdp();
|
loopUdp();
|
||||||
#endif
|
#endif
|
||||||
@@ -107,6 +113,12 @@ void loop() {
|
|||||||
loopSerial();
|
loopSerial();
|
||||||
|
|
||||||
ts.update();
|
ts.update();
|
||||||
|
|
||||||
|
if (metric._counter > 100000) {
|
||||||
|
metric.print();
|
||||||
|
} else {
|
||||||
|
metric.count();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void not_async_actions() {
|
void not_async_actions() {
|
||||||
|
|||||||
Reference in New Issue
Block a user