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("Время синхронизировано"));
29 RTC.adjust(DateTime(now));
36 void setupHandlers() {
37 for (int i=0; i<maxTimeHandlers; i++) {
38 tHandlerNames[i] = nullptr;
39 tHandlerTypes[i] = ' ';
40 tTimeHandlers[i] = nullptr;
45 configTime(cfg.getCharValue(F("tz")),cfg.getCharValue(F("ntp_server")));
46 settimeofday_cb(&timeIsSet);
49 void registerTimeHandler(const char* handlerName, const char handlerType, std::function<void()> timeHandler) {
50 for (int i=0; i<maxTimeHandlers; i++) {
51 if (!tHandlerNames[i]) {
53 tHandlerNames[i] = handlerName;
54 tHandlerTypes[i] = handlerType;
55 tTimeHandlers[i] = timeHandler;
61 void registerTimeHandler(const __FlashStringHelper* handlerName, const char handlerType, std::function<void()> timeHandler) {
62 for (int i=0; i<maxTimeHandlers; i++) {
63 if (!tHandlerNames[i]) {
65 tHandlerNames[i] = copystr(handlerName);
66 tHandlerTypes[i] = handlerType;
67 tTimeHandlers[i] = timeHandler;
73 void unregisterTimeHandler(const char* handlerName) {
74 for (int i=0; i<maxTimeHandlers; i++) {
75 if (tHandlerNames[i] && strcmp(tHandlerNames[i],handlerName) == 0) {
76 tHandlerNames[i] = nullptr;
77 tHandlerTypes[i] = ' ';
78 tTimeHandlers[i] = nullptr;
84 void unregisterTimeHandler(const __FlashStringHelper* handlerName) {
85 for (int i=0; i<maxTimeHandlers; i++) {
86 if (tHandlerNames[i] && strcmp_P(tHandlerNames[i],(PGM_P)handlerName) == 0) {
87 tHandlerNames[i] = nullptr;
88 tHandlerTypes[i] = ' ';
89 tTimeHandlers[i] = nullptr;
95 void runHandlers(char handlerType) {
96 for (int i=0; i<maxTimeHandlers; i++) {
97 if (tTimeHandlers[i] && tHandlerTypes[i]==handlerType) {
104 static int prevD = 0, prevH = 0, prevM = 0, prevS = 0;
106 struct tm* timeinfo = localtime(&now);
107 hh = timeinfo->tm_hour;
108 mi = timeinfo->tm_min;
109 ss = timeinfo->tm_sec;
110 dw = timeinfo->tm_wday;
111 dd = timeinfo->tm_mday;
112 mm = timeinfo->tm_mon+1;
113 yy = timeinfo->tm_year+1900;
133 int day_from = cfg.getIntValue(F("day_from"));
134 int night_from = cfg.getIntValue(F("night_from"));
135 if (day_from<night_from) { // night ... day ... night
136 return hh<day_from || hh>=night_from;
137 } else { /// late day ... night .... day till mindnight
138 return (hh>=night_from && hh<day_from);