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.

101 lines
2.6 KiB

8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 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. Ticker _ntp_delay;
  11. // -----------------------------------------------------------------------------
  12. // NTP
  13. // -----------------------------------------------------------------------------
  14. void _ntpWebSocketOnSend(JsonObject& root) {
  15. root["time"] = ntpDateTime();
  16. root["ntpVisible"] = 1;
  17. root["ntpStatus"] = ntpSynced();
  18. root["ntpServer1"] = getSetting("ntpServer1", NTP_SERVER);
  19. root["ntpServer2"] = getSetting("ntpServer2");
  20. root["ntpServer3"] = getSetting("ntpServer3");
  21. root["ntpOffset"] = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  22. root["ntpDST"] = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
  23. }
  24. void _ntpUpdate() {
  25. #if WEB_SUPPORT
  26. wsSend(_ntpWebSocketOnSend);
  27. #endif
  28. DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str());
  29. }
  30. void _ntpConfigure() {
  31. NTP.begin(
  32. getSetting("ntpServer1", NTP_SERVER),
  33. getSetting("ntpOffset", NTP_TIME_OFFSET).toInt(),
  34. getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1
  35. );
  36. if (getSetting("ntpServer2")) NTP.setNtpServerName(getSetting("ntpServer2"), 1);
  37. if (getSetting("ntpServer3")) NTP.setNtpServerName(getSetting("ntpServer3"), 2);
  38. NTP.setInterval(NTP_UPDATE_INTERVAL);
  39. }
  40. // -----------------------------------------------------------------------------
  41. bool ntpSynced() {
  42. return (timeStatus() == timeSet);
  43. }
  44. String ntpDateTime() {
  45. if (!ntpSynced()) return String();
  46. char buffer[20];
  47. time_t t = now();
  48. snprintf_P(buffer, sizeof(buffer),
  49. PSTR("%04d-%02d-%02d %02d:%02d:%02d"),
  50. year(t), month(t), day(t), hour(t), minute(t), second(t)
  51. );
  52. return String(buffer);
  53. }
  54. void ntpSetup() {
  55. NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
  56. if (error) {
  57. #if WEB_SUPPORT
  58. wsSend_P(PSTR("{\"ntpStatus\": false}"));
  59. #endif
  60. if (error == noResponse) {
  61. DEBUG_MSG_P(PSTR("[NTP] Error: NTP server not reachable\n"));
  62. } else if (error == invalidAddress) {
  63. DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n"));
  64. }
  65. } else {
  66. _ntp_delay.once_ms(100, _ntpUpdate);
  67. }
  68. });
  69. _ntpConfigure();
  70. #if WEB_SUPPORT
  71. wsOnSendRegister(_ntpWebSocketOnSend);
  72. wsOnAfterParseRegister(_ntpConfigure);
  73. #endif
  74. // Register loop
  75. espurnaRegisterLoop(ntpLoop);
  76. }
  77. void ntpLoop() {
  78. now();
  79. }
  80. #endif // NTP_SUPPORT