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.

104 lines
3.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
8 years ago
  1. /*
  2. NTP MODULE
  3. Copyright (C) 2016-2017 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. WiFiEventHandler _ntp_wifi_onSTA;
  11. Ticker _ntp_delay;
  12. // -----------------------------------------------------------------------------
  13. // NTP
  14. // -----------------------------------------------------------------------------
  15. void _ntpWebSocketOnSend(JsonObject& root) {
  16. root["time"] = ntpDateTime();
  17. root["ntpVisible"] = 1;
  18. root["ntpStatus"] = ntpConnected();
  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 _ntpDebug() {
  26. DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str());
  27. }
  28. void _ntpConfigure() {
  29. NTP.begin(
  30. getSetting("ntpServer1", NTP_SERVER),
  31. getSetting("ntpOffset", NTP_TIME_OFFSET).toInt(),
  32. getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1
  33. );
  34. if (getSetting("ntpServer2")) NTP.setNtpServerName(getSetting("ntpServer2"), 1);
  35. if (getSetting("ntpServer3")) NTP.setNtpServerName(getSetting("ntpServer3"), 2);
  36. NTP.setInterval(NTP_UPDATE_INTERVAL);
  37. }
  38. // -----------------------------------------------------------------------------
  39. bool ntpConnected() {
  40. return (timeStatus() == timeSet);
  41. }
  42. String ntpDateTime() {
  43. if (!ntpConnected()) return String("Not set");
  44. String value = NTP.getTimeDateString();
  45. int hour = value.substring(0, 2).toInt();
  46. int minute = value.substring(3, 5).toInt();
  47. int second = value.substring(6, 8).toInt();
  48. int day = value.substring(9, 11).toInt();
  49. int month = value.substring(12, 14).toInt();
  50. int year = value.substring(15, 19).toInt();
  51. char buffer[20];
  52. snprintf_P(buffer, sizeof(buffer), PSTR("%04d/%02d/%02d %02d:%02d:%02d"), year, month, day, hour, minute, second);
  53. return String(buffer);
  54. }
  55. void ntpSetup() {
  56. NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
  57. if (error) {
  58. #if WEB_SUPPORT
  59. wsSend_P(PSTR("{\"ntpStatus\": false}"));
  60. #endif
  61. if (error == noResponse) {
  62. DEBUG_MSG_P(PSTR("[NTP] Error: NTP server not reachable\n"));
  63. } else if (error == invalidAddress) {
  64. DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n"));
  65. }
  66. } else {
  67. #if WEB_SUPPORT
  68. wsSend_P(PSTR("{\"ntpStatus\": true}"));
  69. #endif
  70. _ntp_delay.once_ms(100, _ntpDebug);
  71. }
  72. });
  73. _ntp_wifi_onSTA = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP ipInfo) {
  74. _ntpConfigure();
  75. });
  76. #if WEB_SUPPORT
  77. wsOnSendRegister(_ntpWebSocketOnSend);
  78. wsOnAfterParseRegister(_ntpConfigure);
  79. #endif
  80. }
  81. void ntpLoop() {
  82. now();
  83. }
  84. #endif // NTP_SUPPORT