mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 03:49:13 +03:00
add weekday, IP, ESP_NAME
This commit is contained in:
@@ -143,7 +143,16 @@ class Clock {
|
|||||||
return String(buf);
|
return String(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Локальное дата время "день недели"
|
||||||
|
*/
|
||||||
|
const String getWeekday() {
|
||||||
|
char buf[32];
|
||||||
|
sprintf(buf, "%d", _time_local.day_of_week);
|
||||||
|
return String(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
/*
|
/*
|
||||||
* Локальное время "чч:мм:cc"
|
* Локальное время "чч:мм:cc"
|
||||||
*/
|
*/
|
||||||
|
|||||||
178
include/Clock.h.bak
Normal file
178
include/Clock.h.bak
Normal file
@@ -0,0 +1,178 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Clock.h"
|
||||||
|
#include "Global.h"
|
||||||
|
#include "Utils/TimeUtils.h"
|
||||||
|
#include "Utils\SerialPrint.h"
|
||||||
|
|
||||||
|
extern void clockInit();
|
||||||
|
|
||||||
|
#ifdef ESP8266
|
||||||
|
#include "sntp.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Clock {
|
||||||
|
private:
|
||||||
|
Time_t _time_local;
|
||||||
|
Time_t _time_utc;
|
||||||
|
unsigned long _uptime;
|
||||||
|
unsigned long _unixtime;
|
||||||
|
int _timezone;
|
||||||
|
String _ntp;
|
||||||
|
bool _hasSynced;
|
||||||
|
bool _configured;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Clock() : _uptime{0}, _timezone{0}, _ntp{""}, _hasSynced{false}, _configured{false} {};
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
unsigned long passed = millis_since(_uptime);
|
||||||
|
if (passed < ONE_SECOND_ms) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_uptime += passed;
|
||||||
|
|
||||||
|
// world time
|
||||||
|
time_t now = getSystemTime();
|
||||||
|
time_t estimated = _unixtime + (passed / ONE_SECOND_ms);
|
||||||
|
double drift = difftime(now, estimated);
|
||||||
|
if (drift > 1) {
|
||||||
|
// Обработать ситуации c дрифтом времени на значительные величины
|
||||||
|
}
|
||||||
|
|
||||||
|
_unixtime = now;
|
||||||
|
|
||||||
|
breakEpochToTime(_unixtime, _time_utc);
|
||||||
|
|
||||||
|
breakEpochToTime(_unixtime + getOffsetInSeconds(_timezone), _time_local);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool hasSync() {
|
||||||
|
if (!_hasSynced) {
|
||||||
|
startSync();
|
||||||
|
}
|
||||||
|
return _hasSynced;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setNtpPool(String ntp) {
|
||||||
|
if (!_ntp.equals(ntp)) {
|
||||||
|
_ntp = ntp;
|
||||||
|
_configured = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setTimezone(int timezone) {
|
||||||
|
if (_timezone != timezone) {
|
||||||
|
_timezone = timezone;
|
||||||
|
_configured = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void startSync() {
|
||||||
|
if (!_configured) {
|
||||||
|
SerialPrint("I", "NTP", "sync to: " + _ntp + " timezone: " + String(_timezone));
|
||||||
|
setupSntp();
|
||||||
|
_configured = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_hasSynced = hasTimeSynced();
|
||||||
|
if (_hasSynced) {
|
||||||
|
SerialPrint("I", "NTP", "synced " + getDateDotFormated() + " " + getTime());
|
||||||
|
} else {
|
||||||
|
SerialPrint("E", "NTP", F("failed to obtain time"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setupSntp() {
|
||||||
|
#ifdef ESP8266
|
||||||
|
sntp_setservername(0, _ntp.c_str());
|
||||||
|
sntp_setservername(1, "ru.pool.ntp.org");
|
||||||
|
sntp_setservername(2, "pool.ntp.org");
|
||||||
|
sntp_stop();
|
||||||
|
sntp_set_timezone(0);
|
||||||
|
sntp_init();
|
||||||
|
#else
|
||||||
|
configTime(0, 0, _ntp.c_str(), "ru.pool.ntp.org", "pool.ntp.org");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool hasTimeSynced() const {
|
||||||
|
return _unixtime > MIN_DATETIME;
|
||||||
|
}
|
||||||
|
|
||||||
|
time_t getSystemTime() const {
|
||||||
|
timeval tv{0, 0};
|
||||||
|
timezone tz = timezone{0, 0};
|
||||||
|
time_t epoch = 0;
|
||||||
|
if (gettimeofday(&tv, &tz) != -1) {
|
||||||
|
epoch = tv.tv_sec;
|
||||||
|
}
|
||||||
|
return epoch;
|
||||||
|
}
|
||||||
|
|
||||||
|
const String getTimeUnix() {
|
||||||
|
return String(_unixtime);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Локальное время "дд.ММ.гг"
|
||||||
|
*/
|
||||||
|
const String getDateDotFormated() {
|
||||||
|
char buf[32];
|
||||||
|
sprintf(buf, "%02d.%02d.%02d", _time_local.day_of_month, _time_local.month, _time_local.year);
|
||||||
|
return String(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Локальное дата время "дд.ММ.гг чч.мм.cc"
|
||||||
|
*/
|
||||||
|
const String getDateTimeDotFormated() {
|
||||||
|
char buf[32];
|
||||||
|
sprintf(buf, "%02d.%02d.%02d %02d:%02d:%02d", _time_local.day_of_month, _time_local.month, _time_local.year, _time_local.hour, _time_local.minute, _time_local.second);
|
||||||
|
return String(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Локальное дата время "дд.ММ.гг чч.мм.cc"
|
||||||
|
*/
|
||||||
|
const String getDateTimeDotFormated(Time_t timeNow) {
|
||||||
|
char buf[32];
|
||||||
|
sprintf(buf, "%02d.%02d.%02d %02d:%02d:%02d", timeNow.day_of_month, timeNow.month, timeNow.year, timeNow.hour, timeNow.minute, timeNow.second);
|
||||||
|
return String(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Локальное время "чч:мм:cc"
|
||||||
|
*/
|
||||||
|
const String getTime() {
|
||||||
|
char buf[32];
|
||||||
|
sprintf(buf, "%02d:%02d:%02d", _time_local.hour, _time_local.minute, _time_local.second);
|
||||||
|
return String(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
const String getTimeJson() {
|
||||||
|
char buf[32];
|
||||||
|
sprintf(buf, "%02d-%02d-%02d", _time_local.hour, _time_local.minute, _time_local.second);
|
||||||
|
return String(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Локальное время "чч:мм"
|
||||||
|
*/
|
||||||
|
const String getTimeWOsec() {
|
||||||
|
char buf[32];
|
||||||
|
sprintf(buf, "%02d:%02d", _time_local.hour, _time_local.minute);
|
||||||
|
return String(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Время с момента запуска "чч:мм:cc" далее "дд чч:мм"
|
||||||
|
*/
|
||||||
|
const String getUptime() {
|
||||||
|
return prettyMillis(_uptime);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
extern Clock* timeNow;
|
||||||
@@ -46,6 +46,9 @@ void outputExecute() {
|
|||||||
|
|
||||||
value.replace("#", " ");
|
value.replace("#", " ");
|
||||||
value.replace("%date%", timeNow->getDateTimeDotFormated());
|
value.replace("%date%", timeNow->getDateTimeDotFormated());
|
||||||
|
value.replace("%weekday%", timeNow->getWeekday());
|
||||||
|
value.replace("%IP%", jsonReadStr(configSetupJson, F("ip")));
|
||||||
|
value.replace("%name%", jsonReadStr(configSetupJson, F("name")));
|
||||||
|
|
||||||
int number = getKeyNum(key, output_KeyList);
|
int number = getKeyNum(key, output_KeyList);
|
||||||
|
|
||||||
|
|||||||
58
src/items/vOutput.cpp.bak
Normal file
58
src/items/vOutput.cpp.bak
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
#include "Consts.h"
|
||||||
|
#ifdef EnableOutput
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#include "BufferExecute.h"
|
||||||
|
#include "Class/LineParsing.h"
|
||||||
|
#include "Clock.h"
|
||||||
|
#include "Global.h"
|
||||||
|
#include "items/vOutput.h"
|
||||||
|
|
||||||
|
Output::Output(String key) {
|
||||||
|
_key = key;
|
||||||
|
String value = jsonReadStr(configLiveJson, key);
|
||||||
|
this->execute(value);
|
||||||
|
}
|
||||||
|
Output::~Output() {}
|
||||||
|
|
||||||
|
void Output::execute(String value) {
|
||||||
|
eventGen2(_key, value);
|
||||||
|
jsonWriteStr(configLiveJson, _key, value);
|
||||||
|
publishStatus(_key, value);
|
||||||
|
//publishLastUpdateTime(_key, timeNow->getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
MyOutputVector* myOutput = nullptr;
|
||||||
|
|
||||||
|
void outputValue() {
|
||||||
|
myLineParsing.update();
|
||||||
|
String key = myLineParsing.gkey();
|
||||||
|
myLineParsing.clear();
|
||||||
|
|
||||||
|
output_EnterCounter++;
|
||||||
|
addKey(key, output_KeyList, output_EnterCounter);
|
||||||
|
|
||||||
|
static bool firstTime = true;
|
||||||
|
if (firstTime) myOutput = new MyOutputVector();
|
||||||
|
firstTime = false;
|
||||||
|
myOutput->push_back(Output(key));
|
||||||
|
|
||||||
|
sCmd.addCommand(key.c_str(), outputExecute);
|
||||||
|
}
|
||||||
|
|
||||||
|
void outputExecute() {
|
||||||
|
String key = sCmd.order();
|
||||||
|
String value = sCmd.next();
|
||||||
|
|
||||||
|
value.replace("#", " ");
|
||||||
|
value.replace("%date%", timeNow->getDateTimeDotFormated());
|
||||||
|
|
||||||
|
int number = getKeyNum(key, output_KeyList);
|
||||||
|
|
||||||
|
if (myOutput != nullptr) {
|
||||||
|
if (number != -1) {
|
||||||
|
myOutput->at(number).execute(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user