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.

168 lines
4.3 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. void _ntpWebSocketOnSend(JsonObject& root) {
  17. root["ntpVisible"] = 1;
  18. root["ntpStatus"] = (timeStatus() == timeSet);
  19. root["ntpServer"] = getSetting("ntpServer", NTP_SERVER);
  20. root["ntpOffset"] = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  21. root["ntpDST"] = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
  22. root["ntpRegion"] = getSetting("ntpRegion", NTP_DST_REGION).toInt();
  23. if (ntpSynced()) root["now"] = now();
  24. }
  25. void _ntpStart() {
  26. _ntp_start = 0;
  27. NTP.begin(getSetting("ntpServer", NTP_SERVER));
  28. NTP.setInterval(NTP_SYNC_INTERVAL, NTP_UPDATE_INTERVAL);
  29. NTP.setNTPTimeout(NTP_TIMEOUT);
  30. _ntpConfigure();
  31. }
  32. void _ntpConfigure() {
  33. _ntp_configure = false;
  34. int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  35. int sign = offset > 0 ? 1 : -1;
  36. offset = abs(offset);
  37. int tz_hours = sign * (offset / 60);
  38. int tz_minutes = sign * (offset % 60);
  39. if (NTP.getTimeZone() != tz_hours || NTP.getTimeZoneMinutes() != tz_minutes) {
  40. NTP.setTimeZone(tz_hours, tz_minutes);
  41. _ntp_update = true;
  42. }
  43. bool daylight = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
  44. if (NTP.getDayLight() != daylight) {
  45. NTP.setDayLight(daylight);
  46. _ntp_update = true;
  47. }
  48. String server = getSetting("ntpServer", NTP_SERVER);
  49. if (!NTP.getNtpServerName().equals(server)) {
  50. NTP.setNtpServerName(server);
  51. }
  52. uint8_t dst_region = getSetting("ntpRegion", NTP_DST_REGION).toInt();
  53. NTP.setDSTZone(dst_region);
  54. }
  55. void _ntpUpdate() {
  56. _ntp_update = false;
  57. #if WEB_SUPPORT
  58. wsSend(_ntpWebSocketOnSend);
  59. #endif
  60. DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str());
  61. }
  62. void _ntpLoop() {
  63. if (0 < _ntp_start && _ntp_start < millis()) _ntpStart();
  64. if (_ntp_configure) _ntpConfigure();
  65. if (_ntp_update) _ntpUpdate();
  66. now();
  67. #if BROKER_SUPPORT
  68. static unsigned char last_minute = 60;
  69. if (ntpSynced() && (minute() != last_minute)) {
  70. last_minute = minute();
  71. brokerPublish(MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
  72. }
  73. #endif
  74. }
  75. void _ntpBackwards() {
  76. moveSetting("ntpServer1", "ntpServer");
  77. delSetting("ntpServer2");
  78. delSetting("ntpServer3");
  79. int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  80. if (-30 < offset && offset < 30) {
  81. offset *= 60;
  82. setSetting("ntpOffset", offset);
  83. }
  84. }
  85. // -----------------------------------------------------------------------------
  86. bool ntpSynced() {
  87. return (year() > 2017);
  88. }
  89. String ntpDateTime() {
  90. if (!ntpSynced()) return String();
  91. char buffer[20];
  92. time_t t = now();
  93. snprintf_P(buffer, sizeof(buffer),
  94. PSTR("%04d-%02d-%02d %02d:%02d:%02d"),
  95. year(t), month(t), day(t), hour(t), minute(t), second(t)
  96. );
  97. return String(buffer);
  98. }
  99. // -----------------------------------------------------------------------------
  100. void ntpSetup() {
  101. _ntpBackwards();
  102. NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
  103. if (error) {
  104. #if WEB_SUPPORT
  105. wsSend_P(PSTR("{\"ntpStatus\": false}"));
  106. #endif
  107. if (error == noResponse) {
  108. DEBUG_MSG_P(PSTR("[NTP] Error: NTP server not reachable\n"));
  109. } else if (error == invalidAddress) {
  110. DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n"));
  111. }
  112. } else {
  113. _ntp_update = true;
  114. }
  115. });
  116. wifiRegister([](justwifi_messages_t code, char * parameter) {
  117. if (code == MESSAGE_CONNECTED) _ntp_start = millis() + NTP_START_DELAY;
  118. });
  119. #if WEB_SUPPORT
  120. wsOnSendRegister(_ntpWebSocketOnSend);
  121. wsOnAfterParseRegister([]() { _ntp_configure = true; });
  122. #endif
  123. // Register loop
  124. espurnaRegisterLoop(_ntpLoop);
  125. }
  126. #endif // NTP_SUPPORT