mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
time works
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
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;
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user