Merge pull request #21 from ytrikoz/dev

time works
This commit is contained in:
Dmitry Borisenko
2020-06-26 01:04:43 +02:00
committed by GitHub
5 changed files with 115 additions and 37 deletions

View File

@@ -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;

View File

@@ -1,16 +1,14 @@
#pragma once
#include <Arduino.h>
#ifdef ESP8266
#include <TZ.h>
#include <time.h>
#endif
void Time_Init();
#include <Arduino.h>
/*
* Получение текущего времени
*/
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);
int getOffsetInSeconds(int timezone);
int getOffsetInMinutes(int timezone);

71
include/Utils/Timings.h Normal file
View 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;
};
};

View File

@@ -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;
}

View File

@@ -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() {