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.

274 lines
7.2 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
6 years ago
8 years ago
  1. /*
  2. NTP MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if NTP_SUPPORT
  6. #include <TimeLib.h>
  7. #include <WiFiClient.h>
  8. #include <Ticker.h>
  9. #include "libs/NtpClientWrap.h"
  10. Ticker _ntp_defer;
  11. bool _ntp_report = false;
  12. bool _ntp_configure = false;
  13. bool _ntp_want_sync = false;
  14. // -----------------------------------------------------------------------------
  15. // NTP
  16. // -----------------------------------------------------------------------------
  17. #if WEB_SUPPORT
  18. bool _ntpWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  19. return (strncmp(key, "ntp", 3) == 0);
  20. }
  21. void _ntpWebSocketOnVisible(JsonObject& root) {
  22. root["ntpVisible"] = 1;
  23. }
  24. void _ntpWebSocketOnData(JsonObject& root) {
  25. root["ntpStatus"] = (timeStatus() == timeSet);
  26. }
  27. void _ntpWebSocketOnConnected(JsonObject& root) {
  28. root["ntpServer"] = getSetting("ntpServer", NTP_SERVER);
  29. root["ntpOffset"] = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  30. root["ntpDST"] = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
  31. root["ntpRegion"] = getSetting("ntpRegion", NTP_DST_REGION).toInt();
  32. }
  33. #endif
  34. time_t _ntpSyncProvider() {
  35. _ntp_want_sync = true;
  36. return 0;
  37. }
  38. void _ntpWantSync() {
  39. _ntp_want_sync = true;
  40. }
  41. // Randomized in time to avoid clogging the server with simultaious requests from multiple devices
  42. // (for example, when multiple devices start up at the same time)
  43. int inline _ntpSyncInterval() {
  44. return secureRandom(NTP_SYNC_INTERVAL, NTP_SYNC_INTERVAL * 2);
  45. }
  46. int inline _ntpUpdateInterval() {
  47. return secureRandom(NTP_UPDATE_INTERVAL, NTP_UPDATE_INTERVAL * 2);
  48. }
  49. void _ntpStart() {
  50. _ntpConfigure();
  51. // short (initial) and long (after sync) intervals
  52. NTPw.setInterval(_ntpSyncInterval(), _ntpUpdateInterval());
  53. DEBUG_MSG_P(PSTR("[NTP] Update intervals: %us / %us\n"),
  54. NTPw.getShortInterval(), NTPw.getLongInterval());
  55. // setSyncProvider will immediatly call given function by setting next sync time to the current time.
  56. // Avoid triggering sync immediatly by canceling sync provider flag and resetting sync interval again
  57. setSyncProvider(_ntpSyncProvider);
  58. _ntp_want_sync = false;
  59. setSyncInterval(NTPw.getShortInterval());
  60. }
  61. void _ntpConfigure() {
  62. _ntp_configure = false;
  63. int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  64. int sign = offset > 0 ? 1 : -1;
  65. offset = abs(offset);
  66. int tz_hours = sign * (offset / 60);
  67. int tz_minutes = sign * (offset % 60);
  68. if (NTPw.getTimeZone() != tz_hours || NTPw.getTimeZoneMinutes() != tz_minutes) {
  69. NTPw.setTimeZone(tz_hours, tz_minutes);
  70. _ntp_report = true;
  71. }
  72. bool daylight = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
  73. if (NTPw.getDayLight() != daylight) {
  74. NTPw.setDayLight(daylight);
  75. _ntp_report = true;
  76. }
  77. String server = getSetting("ntpServer", NTP_SERVER);
  78. if (!NTPw.getNtpServerName().equals(server)) {
  79. NTPw.setNtpServerName(server);
  80. }
  81. uint8_t dst_region = getSetting("ntpRegion", NTP_DST_REGION).toInt();
  82. NTPw.setDSTZone(dst_region);
  83. // Some remote servers can be slow to respond, increase accordingly
  84. // TODO does this need upper constrain?
  85. NTPw.setNTPTimeout(getSetting("ntpTimeout", NTP_TIMEOUT).toInt());
  86. }
  87. void _ntpReport() {
  88. _ntp_report = false;
  89. if (ntpSynced()) {
  90. time_t t = now();
  91. DEBUG_MSG_P(PSTR("[NTP] UTC Time : %s\n"), ntpDateTime(ntpLocal2UTC(t)).c_str());
  92. DEBUG_MSG_P(PSTR("[NTP] Local Time: %s\n"), ntpDateTime(t).c_str());
  93. }
  94. }
  95. #if BROKER_SUPPORT
  96. void inline _ntpBroker() {
  97. static unsigned char last_minute = 60;
  98. if (ntpSynced() && (minute() != last_minute)) {
  99. last_minute = minute();
  100. brokerPublish(BROKER_MSG_TYPE_DATETIME, MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
  101. }
  102. }
  103. #endif
  104. void _ntpLoop() {
  105. // Disable ntp sync when softAP is active. This will not crash, but instead spam debug-log with pointless sync failures.
  106. if (!wifiConnected()) return;
  107. if (_ntp_configure) _ntpConfigure();
  108. // NTPClientLib will trigger callback with sync status
  109. // see: NTPw.onNTPSyncEvent([](NTPSyncEvent_t error){ ... }) below
  110. if (_ntp_want_sync) {
  111. _ntp_want_sync = false;
  112. NTPw.getTime();
  113. }
  114. // Print current time whenever configuration changes or after successful sync
  115. if (_ntp_report) _ntpReport();
  116. #if BROKER_SUPPORT
  117. _ntpBroker();
  118. #endif
  119. }
  120. // TODO: remove me!
  121. void _ntpBackwards() {
  122. moveSetting("ntpServer1", "ntpServer");
  123. delSetting("ntpServer2");
  124. delSetting("ntpServer3");
  125. int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  126. if (-30 < offset && offset < 30) {
  127. offset *= 60;
  128. setSetting("ntpOffset", offset);
  129. }
  130. }
  131. // -----------------------------------------------------------------------------
  132. bool ntpSynced() {
  133. #if NTP_WAIT_FOR_SYNC
  134. // Has synced at least once
  135. return (NTPw.getFirstSync() > 0);
  136. #else
  137. // TODO: runtime setting?
  138. return true;
  139. #endif
  140. }
  141. String ntpDateTime(time_t t) {
  142. char buffer[20];
  143. snprintf_P(buffer, sizeof(buffer),
  144. PSTR("%04d-%02d-%02d %02d:%02d:%02d"),
  145. year(t), month(t), day(t), hour(t), minute(t), second(t)
  146. );
  147. return String(buffer);
  148. }
  149. String ntpDateTime() {
  150. if (ntpSynced()) return ntpDateTime(now());
  151. return String();
  152. }
  153. // XXX: returns garbage during DST switch
  154. time_t ntpLocal2UTC(time_t local) {
  155. int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  156. if (NTPw.isSummerTime()) offset += 60;
  157. return local - offset * 60;
  158. }
  159. // -----------------------------------------------------------------------------
  160. void ntpSetup() {
  161. _ntpBackwards();
  162. #if TERMINAL_SUPPORT
  163. terminalRegisterCommand(F("NTP"), [](Embedis* e) {
  164. if (ntpSynced()) {
  165. _ntpReport();
  166. terminalOK();
  167. } else {
  168. DEBUG_MSG_P(PSTR("[NTP] Not synced\n"));
  169. }
  170. });
  171. terminalRegisterCommand(F("NTP.SYNC"), [](Embedis* e) {
  172. _ntpWantSync();
  173. terminalOK();
  174. });
  175. #endif
  176. NTPw.onNTPSyncEvent([](NTPSyncEvent_t error) {
  177. if (error) {
  178. if (error == noResponse) {
  179. DEBUG_MSG_P(PSTR("[NTP] Error: NTP server not reachable\n"));
  180. } else if (error == invalidAddress) {
  181. DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n"));
  182. }
  183. wsPost(_ntpWebSocketOnData);
  184. } else {
  185. _ntp_report = true;
  186. setTime(NTPw.getLastNTPSync());
  187. }
  188. });
  189. wifiRegister([](justwifi_messages_t code, char * parameter) {
  190. if (code == MESSAGE_CONNECTED) {
  191. if (!ntpSynced()) {
  192. _ntp_defer.once_ms(secureRandom(NTP_START_DELAY, NTP_START_DELAY * 15), _ntpWantSync);
  193. }
  194. }
  195. });
  196. #if WEB_SUPPORT
  197. wsRegister()
  198. .onVisible(_ntpWebSocketOnVisible)
  199. .onConnected(_ntpWebSocketOnConnected)
  200. .onData(_ntpWebSocketOnData)
  201. .onKeyCheck(_ntpWebSocketOnKeyCheck);
  202. #endif
  203. // Main callbacks
  204. espurnaRegisterLoop(_ntpLoop);
  205. espurnaRegisterReload([]() { _ntp_configure = true; });
  206. // Sets up NTP instance, installs ours sync provider
  207. _ntpStart();
  208. }
  209. #endif // NTP_SUPPORT