This commit is contained in:
Yuri Trikoz
2020-06-25 09:21:42 +03:00
parent 5ed1c23c62
commit 7c61ff2810
17 changed files with 182 additions and 254 deletions

View File

@@ -1,7 +1,99 @@
#pragma once
#include "Global.h"
#include "Utils/TimeUtils.h"
#include "Utils/PrintMessage.h"
void startTimeSync();
#include "time.h"
void reconfigTime();
class Clock {
const char* MODULE = "Clock";
public:
Clock() : _timezone{0}, _ntp{}, _hasSynced{false}, _configured{false} {
}
bool hasSync() {
if (!_hasSynced) {
startSync();
}
return _hasSynced;
}
void
setNtpPool(String ntp) {
_ntp = ntp;
}
void setTimezone(int timezone) {
_timezone = timezone;
}
time_t getSystemTime() {
timeval tv{0, 0};
timezone tz = getTimeZone(getBiasInMinutes());
time_t epoch = 0;
if (gettimeofday(&tv, &tz) != -1)
epoch = tv.tv_sec;
return epoch;
}
void startSync() {
if (!_configured) {
pm.info("sync to: " + _ntp + " time zone: " + String(_timezone));
setupSntp();
_configured = true;
}
_hasSynced = hasTimeSynced();
if (_hasSynced) {
pm.info("synced " + getDateDigitalFormated() + " " + getTime());
} else {
pm.error("failed to obtain");
}
}
void setupSntp() {
int tzs = getBiasInSeconds();
int tzh = tzs / 3600;
tzs -= tzh * 3600;
int tzm = tzs / 60;
tzs -= tzm * 60;
char tzstr[64];
snprintf(tzstr, sizeof tzstr, "ESPUSER<%+d:%02d:%02d>", tzh, tzm, tzs);
pm.info(String(tzstr));
configTime(tzstr, _ntp.c_str());
}
// #ifdef ESP32
// uint8_t i = 0;
// struct tm timeinfo;
// while (!getLocalTime(&timeinfo) && i <= 4) {
// Serial.print(".");
// i++;
// delay(1000);
// }
// #endif
private:
bool hasTimeSynced() {
unsigned long now = time(nullptr);
return now > millis();
}
int getBiasInSeconds() {
return getBiasInMinutes() * 60;
}
int getBiasInMinutes() {
return _timezone * 60;
}
const timezone getTimeZone(int minutes) {
return timezone{minutes, 0};
}
private:
int _timezone;
String _ntp;
bool _hasSynced;
bool _configured;
};

View File

@@ -9,6 +9,7 @@
#define MQTT_RECONNECT_INTERVAL 20000
// 1000 * 60 * 60 * 2
#define TELEMETRY_UPDATE_INTERVAL 0
#define DEVICE_CONFIG_FILE "dev_conf.txt"
#define DEVICE_SCENARIO_FILE "dev_scen.txt"
#define DEFAULT_PRESET 100

View File

@@ -21,7 +21,6 @@
#include "Utils\JsonUtils.h"
#include "Utils\StringUtils.h"
#include "Utils\SysUtils.h"
#include "Utils\TimeUtils.h"
#include "Utils\PrintMessage.h"
#include "Utils\WiFiUtils.h"
@@ -45,6 +44,8 @@ extern AsyncWebSocket ws;
//extern AsyncEventSource events;
#endif
extern Clock* rtc;
extern TickerScheduler ts;
extern WiFiClient espClient;
@@ -165,7 +166,6 @@ extern void choose_log_date_and_send();
// Main
extern void setChipId();
extern void printMemoryStatus(String text);
extern void saveConfig();
extern String getURL(const String& urls);
@@ -229,9 +229,10 @@ extern int readTimer(int number);
extern void init_updater();
// widget
extern void createWidget(String widget_name, String page_name, String page_number, String file, String topic);
extern void createWidgetByType(String widget_name, String page_name, String page_number, String file, String topic);
extern void createWidgetParam(String widget_name, String page_name, String page_number, String file, String topic, String name1, String param1, String name2, String param2, String name3, String param3);
extern void createWidgetByType(String widget_name, String page_name, String page_number, String type, String topik);
extern void createWidget(String widget_name, String page_name, String page_number, String type, String topik);
extern void createChart(String widget_name, String page_name, String page_number, String file, String topic, String maxCount);
// PushingBox

View File

@@ -11,3 +11,5 @@ String jsonWriteStr(String& json, String name, String volume);
String jsonWriteInt(String& json, String name, int volume);
String jsonWriteFloat(String& json, String name, float volume);
boolean jsonReadBool(String& json, String name);

View File

@@ -14,11 +14,11 @@ class PrintMessage {
_module = module;
}
void error(const String str) {
void error(const String& str) {
print(EL_ERROR, str);
}
void info(const String str) {
void info(const String& str) {
print(EL_INFO, str);
}

View File

@@ -4,6 +4,6 @@
const String getChipId();
void printMemoryStatus(String text = "");
const String printMemoryStatus();
String getHeapStats();
const String getHeapStats();

View File

@@ -35,12 +35,6 @@ int timeToMin(String Time);
const String prettyMillis(unsigned long time_ms = millis());
int timeZoneInSeconds(const byte timeZone);
bool hasTimeSynced();
int getBiasInSeconds();
/*
* Время (мс) прошедщее с @simce
*/