5 bool isTimeSet = false;
19 #define maxTimeHandlers 8
20 const char* tHandlerNames[maxTimeHandlers];
21 char tHandlerTypes[maxTimeHandlers];
22 std::function<void()> tTimeHandlers[maxTimeHandlers];
24 void timeIsSet(bool ntp) {
26 Serial.println(F("Время синхронизировано"));
27 message(F("Время синхронизировано"));
28 reportMessage(F("Время синхронизировано"));
30 RTC.adjust(DateTime(now));
37 void setupHandlers() {
38 for (int i=0; i<maxTimeHandlers; i++) {
39 tHandlerNames[i] = nullptr;
40 tHandlerTypes[i] = ' ';
41 tTimeHandlers[i] = nullptr;
46 configTime(cfg.getCharValue(F("tz")),cfg.getCharValue(F("ntp_server")));
47 settimeofday_cb(&timeIsSet);
50 void registerTimeHandler(const char* handlerName, const char handlerType, std::function<void()> timeHandler) {
51 for (int i=0; i<maxTimeHandlers; i++) {
52 if (!tHandlerNames[i]) {
54 tHandlerNames[i] = handlerName;
55 tHandlerTypes[i] = handlerType;
56 tTimeHandlers[i] = timeHandler;
62 void registerTimeHandler(const __FlashStringHelper* handlerName, const char handlerType, std::function<void()> timeHandler) {
63 for (int i=0; i<maxTimeHandlers; i++) {
64 if (!tHandlerNames[i]) {
66 tHandlerNames[i] = copystr(handlerName);
67 tHandlerTypes[i] = handlerType;
68 tTimeHandlers[i] = timeHandler;
74 void unregisterTimeHandler(const char* handlerName) {
75 for (int i=0; i<maxTimeHandlers; i++) {
76 if (tHandlerNames[i] && strcmp(tHandlerNames[i],handlerName) == 0) {
77 tHandlerNames[i] = nullptr;
78 tHandlerTypes[i] = ' ';
79 tTimeHandlers[i] = nullptr;
85 void unregisterTimeHandler(const __FlashStringHelper* handlerName) {
86 for (int i=0; i<maxTimeHandlers; i++) {
87 if (tHandlerNames[i] && strcmp_P(tHandlerNames[i],(PGM_P)handlerName) == 0) {
88 tHandlerNames[i] = nullptr;
89 tHandlerTypes[i] = ' ';
90 tTimeHandlers[i] = nullptr;
96 void runHandlers(char handlerType) {
97 for (int i=0; i<maxTimeHandlers; i++) {
98 if (tTimeHandlers[i] && tHandlerTypes[i]==handlerType) {
105 static int prevD = 0, prevH = 0, prevM = 0, prevS = 0;
107 struct tm* timeinfo = localtime(&now);
108 hh = timeinfo->tm_hour;
109 mi = timeinfo->tm_min;
110 ss = timeinfo->tm_sec;
111 dw = timeinfo->tm_wday;
112 dd = timeinfo->tm_mday;
113 mm = timeinfo->tm_mon+1;
114 yy = timeinfo->tm_year+1900;
134 int day_from = cfg.getIntValue(F("day_from"));
135 int night_from = cfg.getIntValue(F("night_from"));
136 if (day_from<night_from) { // night ... day ... night
137 return hh<day_from || hh>=night_from;
138 } else { /// late day ... night .... day till mindnight
139 return (hh>=night_from && hh<day_from);