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.

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