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.

182 lines
4.5 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
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. */
  5. #if NTP_SUPPORT
  6. #include <TimeLib.h>
  7. #include <NtpClientLib.h>
  8. #include <WiFiClient.h>
  9. #include <Ticker.h>
  10. unsigned long _ntp_start = 0;
  11. bool _ntp_update = false;
  12. bool _ntp_configure = false;
  13. // -----------------------------------------------------------------------------
  14. // NTP
  15. // -----------------------------------------------------------------------------
  16. #if WEB_SUPPORT
  17. bool _ntpWebSocketOnReceive(const char * key, JsonVariant& value) {
  18. return (strncmp(key, "ntp", 3) == 0);
  19. }
  20. void _ntpWebSocketOnSend(JsonObject& root) {
  21. root["ntpVisible"] = 1;
  22. root["ntpStatus"] = (timeStatus() == timeSet);
  23. root["ntpServer"] = getSetting("ntpServer", NTP_SERVER);
  24. root["ntpOffset"] = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  25. root["ntpDST"] = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
  26. root["ntpRegion"] = getSetting("ntpRegion", NTP_DST_REGION).toInt();
  27. if (ntpSynced()) root["now"] = now();
  28. }
  29. #endif
  30. void _ntpStart() {
  31. _ntp_start = 0;
  32. NTP.begin(getSetting("ntpServer", NTP_SERVER));
  33. NTP.setInterval(NTP_SYNC_INTERVAL, NTP_UPDATE_INTERVAL);
  34. NTP.setNTPTimeout(NTP_TIMEOUT);
  35. _ntpConfigure();
  36. }
  37. void _ntpConfigure() {
  38. _ntp_configure = false;
  39. int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  40. int sign = offset > 0 ? 1 : -1;
  41. offset = abs(offset);
  42. int tz_hours = sign * (offset / 60);
  43. int tz_minutes = sign * (offset % 60);
  44. if (NTP.getTimeZone() != tz_hours || NTP.getTimeZoneMinutes() != tz_minutes) {
  45. NTP.setTimeZone(tz_hours, tz_minutes);
  46. _ntp_update = true;
  47. }
  48. bool daylight = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
  49. if (NTP.getDayLight() != daylight) {
  50. NTP.setDayLight(daylight);
  51. _ntp_update = true;
  52. }
  53. String server = getSetting("ntpServer", NTP_SERVER);
  54. if (!NTP.getNtpServerName().equals(server)) {
  55. NTP.setNtpServerName(server);
  56. }
  57. uint8_t dst_region = getSetting("ntpRegion", NTP_DST_REGION).toInt();
  58. NTP.setDSTZone(dst_region);
  59. }
  60. void _ntpUpdate() {
  61. _ntp_update = false;
  62. #if WEB_SUPPORT
  63. wsSend(_ntpWebSocketOnSend);
  64. #endif
  65. if (strlen(ntpDateTime().c_str())) {
  66. DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().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. void _ntpBackwards() {
  83. moveSetting("ntpServer1", "ntpServer");
  84. delSetting("ntpServer2");
  85. delSetting("ntpServer3");
  86. int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  87. if (-30 < offset && offset < 30) {
  88. offset *= 60;
  89. setSetting("ntpOffset", offset);
  90. }
  91. }
  92. // -----------------------------------------------------------------------------
  93. bool ntpSynced() {
  94. return (year() > 2017);
  95. }
  96. String ntpDateTime(time_t t) {
  97. char buffer[20];
  98. snprintf_P(buffer, sizeof(buffer),
  99. PSTR("%04d-%02d-%02d %02d:%02d:%02d"),
  100. year(t), month(t), day(t), hour(t), minute(t), second(t)
  101. );
  102. return String(buffer);
  103. }
  104. String ntpDateTime() {
  105. if (ntpSynced()) return ntpDateTime(now());
  106. return String();
  107. }
  108. // -----------------------------------------------------------------------------
  109. void ntpSetup() {
  110. _ntpBackwards();
  111. NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
  112. if (error) {
  113. #if WEB_SUPPORT
  114. wsSend_P(PSTR("{\"ntpStatus\": false}"));
  115. #endif
  116. if (error == noResponse) {
  117. DEBUG_MSG_P(PSTR("[NTP] Error: NTP server not reachable\n"));
  118. } else if (error == invalidAddress) {
  119. DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n"));
  120. }
  121. } else {
  122. _ntp_update = true;
  123. }
  124. });
  125. wifiRegister([](justwifi_messages_t code, char * parameter) {
  126. if (code == MESSAGE_CONNECTED) _ntp_start = millis() + NTP_START_DELAY;
  127. });
  128. #if WEB_SUPPORT
  129. wsOnSendRegister(_ntpWebSocketOnSend);
  130. wsOnReceiveRegister(_ntpWebSocketOnReceive);
  131. wsOnAfterParseRegister([]() { _ntp_configure = true; });
  132. #endif
  133. // Register loop
  134. espurnaRegisterLoop(_ntpLoop);
  135. }
  136. #endif // NTP_SUPPORT