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.

146 lines
3.6 KiB

8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 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. bool _ntp_update = false;
  11. bool _ntp_configure = false;
  12. // -----------------------------------------------------------------------------
  13. // NTP
  14. // -----------------------------------------------------------------------------
  15. void _ntpWebSocketOnSend(JsonObject& root) {
  16. root["time"] = ntpDateTime();
  17. root["ntpVisible"] = 1;
  18. root["ntpStatus"] = ntpSynced();
  19. root["ntpServer1"] = getSetting("ntpServer1", NTP_SERVER);
  20. root["ntpServer2"] = getSetting("ntpServer2");
  21. root["ntpServer3"] = getSetting("ntpServer3");
  22. root["ntpOffset"] = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  23. root["ntpDST"] = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
  24. }
  25. void _ntpUpdate() {
  26. _ntp_update = false;
  27. #if WEB_SUPPORT
  28. wsSend(_ntpWebSocketOnSend);
  29. #endif
  30. DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str());
  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. NTP.begin(
  38. getSetting("ntpServer", 1, NTP_SERVER).c_str(),
  39. sign * (offset / 60),
  40. getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1,
  41. sign * (offset % 60)
  42. );
  43. if (hasSetting("ntpServer", 2)) NTP.setNtpServerName(getSetting("ntpServer", 2).c_str(), 1);
  44. if (hasSetting("ntpServer", 3)) NTP.setNtpServerName(getSetting("ntpServer", 3).c_str(), 2);
  45. NTP.setInterval(NTP_UPDATE_INTERVAL);
  46. _ntp_update = true;
  47. }
  48. void _ntpLoop() {
  49. if (_ntp_configure) _ntpConfigure();
  50. if (_ntp_update) _ntpUpdate();
  51. now();
  52. #if BROKER_SUPPORT
  53. static unsigned char last_minute = 60;
  54. if (ntpSynced() && (minute() != last_minute)) {
  55. last_minute = minute();
  56. brokerPublish(MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
  57. }
  58. #endif
  59. }
  60. void _ntpBackwards() {
  61. int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  62. if (-30 < offset && offset < 30) {
  63. offset *= 60;
  64. setSetting("ntpOffset", offset);
  65. }
  66. }
  67. // -----------------------------------------------------------------------------
  68. bool ntpSynced() {
  69. return (timeStatus() == timeSet);
  70. }
  71. String ntpDateTime() {
  72. if (!ntpSynced()) return String();
  73. char buffer[20];
  74. time_t t = now();
  75. snprintf_P(buffer, sizeof(buffer),
  76. PSTR("%04d-%02d-%02d %02d:%02d:%02d"),
  77. year(t), month(t), day(t), hour(t), minute(t), second(t)
  78. );
  79. return String(buffer);
  80. }
  81. // -----------------------------------------------------------------------------
  82. void ntpSetup() {
  83. _ntpBackwards();
  84. NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
  85. if (error) {
  86. #if WEB_SUPPORT
  87. wsSend_P(PSTR("{\"ntpStatus\": false}"));
  88. #endif
  89. if (error == noResponse) {
  90. DEBUG_MSG_P(PSTR("[NTP] Error: NTP server not reachable\n"));
  91. } else if (error == invalidAddress) {
  92. DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n"));
  93. }
  94. } else {
  95. _ntp_update = true;
  96. }
  97. });
  98. wifiRegister([](justwifi_messages_t code, char * parameter) {
  99. if (code == MESSAGE_CONNECTED) _ntp_configure = true;
  100. });
  101. #if WEB_SUPPORT
  102. wsOnSendRegister(_ntpWebSocketOnSend);
  103. wsOnAfterParseRegister([]() { _ntp_configure = true; });
  104. #endif
  105. // Register loop
  106. espurnaRegisterLoop(_ntpLoop);
  107. }
  108. #endif // NTP_SUPPORT