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.

179 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
7 years ago
7 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() {
  97. if (!ntpSynced()) return String();
  98. char buffer[20];
  99. time_t t = now();
  100. snprintf_P(buffer, sizeof(buffer),
  101. PSTR("%04d-%02d-%02d %02d:%02d:%02d"),
  102. year(t), month(t), day(t), hour(t), minute(t), second(t)
  103. );
  104. return String(buffer);
  105. }
  106. // -----------------------------------------------------------------------------
  107. void ntpSetup() {
  108. _ntpBackwards();
  109. NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
  110. if (error) {
  111. #if WEB_SUPPORT
  112. wsSend_P(PSTR("{\"ntpStatus\": false}"));
  113. #endif
  114. if (error == noResponse) {
  115. DEBUG_MSG_P(PSTR("[NTP] Error: NTP server not reachable\n"));
  116. } else if (error == invalidAddress) {
  117. DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n"));
  118. }
  119. } else {
  120. _ntp_update = true;
  121. }
  122. });
  123. wifiRegister([](justwifi_messages_t code, char * parameter) {
  124. if (code == MESSAGE_CONNECTED) _ntp_start = millis() + NTP_START_DELAY;
  125. });
  126. #if WEB_SUPPORT
  127. wsOnSendRegister(_ntpWebSocketOnSend);
  128. wsOnReceiveRegister(_ntpWebSocketOnReceive);
  129. wsOnAfterParseRegister([]() { _ntp_configure = true; });
  130. #endif
  131. // Register loop
  132. espurnaRegisterLoop(_ntpLoop);
  133. }
  134. #endif // NTP_SUPPORT