Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
4.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
6 years ago
8 years ago
  1. /*
  2. NTP MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. Module key prefix: ntp
  5. */
  6. #if NTP_SUPPORT
  7. #include <TimeLib.h>
  8. #include <NtpClientLib.h>
  9. #include <WiFiClient.h>
  10. #include <Ticker.h>
  11. unsigned long _ntp_start = 0;
  12. bool _ntp_update = false;
  13. bool _ntp_configure = false;
  14. // -----------------------------------------------------------------------------
  15. // NTP
  16. // -----------------------------------------------------------------------------
  17. #if WEB_SUPPORT
  18. void _ntpWebSocketOnSend(JsonObject& root) {
  19. root["ntpVisible"] = 1;
  20. root["ntpStatus"] = (timeStatus() == timeSet);
  21. root["ntpServer"] = getSetting("ntpServer", NTP_SERVER);
  22. root["ntpOffset"] = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  23. root["ntpDST"] = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
  24. root["ntpRegion"] = getSetting("ntpRegion", NTP_DST_REGION).toInt();
  25. if (ntpSynced()) root["now"] = now();
  26. }
  27. #endif
  28. void _ntpStart() {
  29. _ntp_start = 0;
  30. NTP.begin(getSetting("ntpServer", NTP_SERVER));
  31. NTP.setInterval(NTP_SYNC_INTERVAL, NTP_UPDATE_INTERVAL);
  32. NTP.setNTPTimeout(NTP_TIMEOUT);
  33. _ntpConfigure();
  34. }
  35. void _ntpConfigure() {
  36. _ntp_configure = false;
  37. int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  38. int sign = offset > 0 ? 1 : -1;
  39. offset = abs(offset);
  40. int tz_hours = sign * (offset / 60);
  41. int tz_minutes = sign * (offset % 60);
  42. if (NTP.getTimeZone() != tz_hours || NTP.getTimeZoneMinutes() != tz_minutes) {
  43. NTP.setTimeZone(tz_hours, tz_minutes);
  44. _ntp_update = true;
  45. }
  46. bool daylight = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
  47. if (NTP.getDayLight() != daylight) {
  48. NTP.setDayLight(daylight);
  49. _ntp_update = true;
  50. }
  51. String server = getSetting("ntpServer", NTP_SERVER);
  52. if (!NTP.getNtpServerName().equals(server)) {
  53. NTP.setNtpServerName(server);
  54. }
  55. uint8_t dst_region = getSetting("ntpRegion", NTP_DST_REGION).toInt();
  56. NTP.setDSTZone(dst_region);
  57. }
  58. void _ntpUpdate() {
  59. _ntp_update = false;
  60. #if WEB_SUPPORT
  61. wsSend(_ntpWebSocketOnSend);
  62. #endif
  63. if (ntpSynced()) {
  64. time_t t = now();
  65. DEBUG_MSG_P(PSTR("[NTP] UTC Time : %s\n"), (char *) ntpDateTime(ntpLocal2UTC(t)).c_str());
  66. DEBUG_MSG_P(PSTR("[NTP] Local Time: %s\n"), (char *) ntpDateTime(t).c_str());
  67. }
  68. }
  69. void _ntpLoop() {
  70. if (0 < _ntp_start && _ntp_start < millis()) _ntpStart();
  71. if (_ntp_configure) _ntpConfigure();
  72. if (_ntp_update) _ntpUpdate();
  73. now();
  74. #if BROKER_SUPPORT
  75. static unsigned char last_minute = 60;
  76. if (ntpSynced() && (minute() != last_minute)) {
  77. last_minute = minute();
  78. brokerPublish(MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
  79. }
  80. #endif
  81. }
  82. bool _ntpKeyCheck(const char * key) {
  83. return (strncmp(key, "ntp", 3) == 0);
  84. }
  85. void _ntpBackwards() {
  86. // 1.12.0 - 2018-01-21
  87. moveSetting("ntpServer1", "ntpServer");
  88. delSetting("ntpServer2");
  89. delSetting("ntpServer3");
  90. // 1.12.0 - 2018-01-20
  91. int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  92. if (-30 < offset && offset < 30) {
  93. offset *= 60;
  94. setSetting("ntpOffset", offset);
  95. }
  96. }
  97. // -----------------------------------------------------------------------------
  98. bool ntpSynced() {
  99. return (year() > 2017);
  100. }
  101. String ntpDateTime(time_t t) {
  102. char buffer[20];
  103. snprintf_P(buffer, sizeof(buffer),
  104. PSTR("%04d-%02d-%02d %02d:%02d:%02d"),
  105. year(t), month(t), day(t), hour(t), minute(t), second(t)
  106. );
  107. return String(buffer);
  108. }
  109. String ntpDateTime() {
  110. if (ntpSynced()) return ntpDateTime(now());
  111. return String();
  112. }
  113. time_t ntpLocal2UTC(time_t local) {
  114. int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  115. if (NTP.isSummerTime()) offset += 60;
  116. return local - offset * 60;
  117. }
  118. // -----------------------------------------------------------------------------
  119. void ntpSetup() {
  120. // Check backwards compatibility
  121. _ntpBackwards();
  122. NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
  123. if (error) {
  124. #if WEB_SUPPORT
  125. wsSend_P(PSTR("{\"ntpStatus\": false}"));
  126. #endif
  127. if (error == noResponse) {
  128. DEBUG_MSG_P(PSTR("[NTP] Error: NTP server not reachable\n"));
  129. } else if (error == invalidAddress) {
  130. DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n"));
  131. }
  132. } else {
  133. _ntp_update = true;
  134. }
  135. });
  136. wifiRegister([](justwifi_messages_t code, char * parameter) {
  137. if (code == MESSAGE_CONNECTED) _ntp_start = millis() + NTP_START_DELAY;
  138. });
  139. #if WEB_SUPPORT
  140. wsOnSendRegister(_ntpWebSocketOnSend);
  141. wsOnAfterParseRegister([]() { _ntp_configure = true; });
  142. #endif
  143. // Register
  144. espurnaRegisterLoop(_ntpLoop);
  145. settingsRegisterKeyCheck(_ntpKeyCheck);
  146. }
  147. #endif // NTP_SUPPORT