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.

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