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.

94 lines
2.5 KiB

  1. /*
  2. RTC support module
  3. Copyright (C) 2018 by Pavel Chauzov <poulch at mail dot ru>
  4. in debug terminal - RTC return current rtc time
  5. - RTC YYYY-MM-DD DoW HH:MM:SS setup rtc
  6. */
  7. #if RTC_SUPPORT
  8. static int _rtc_recovery = 0;
  9. static time_t ntp_getTime() {
  10. time_t tm = 0;
  11. if(_rtc_recovery == 0) { tm = NTP.getTime(); }
  12. if((_rtc_recovery !=0) || (tm == 0)) {
  13. _rtc_recovery++;
  14. tm = getTime_rtc();
  15. // signal ntp loop to update clock but not rtc...
  16. _ntp_update = true;
  17. #if RTC_SUPPORT && RTC_NTP_SYNC_ENA
  18. _rtc_update = false;
  19. #endif
  20. }
  21. if(_rtc_recovery > RTC_RECOVERY_CNT) { _rtc_recovery = RTC_RECOVERY_CNT ? 0:1; }
  22. DEBUG_MSG_P(PSTR("[NTP] RTC Time : %s\n"), (char *) ntpDateTime(tm).c_str());
  23. return tm;
  24. }
  25. //------------------------------------------------------------------------------
  26. // Settings
  27. //------------------------------------------------------------------------------
  28. #if TERMINAL_SUPPORT
  29. String _rtc_getValue(String data, char sep, int idx) {
  30. int found = 0;
  31. int si[] = {0, -1};
  32. int maxi = data.length()-1;
  33. for(int i=0; i<=maxi && found<=idx; i++) {
  34. if(data.charAt(i)==sep || i==maxi) {
  35. found++;
  36. si[0] = si[1]+1;
  37. si[1] = (i == maxi) ? i+1 : i;
  38. }
  39. }
  40. return found>idx ? data.substring(si[0], si[1]) : "0";
  41. }
  42. void _rtcInitCommands() {
  43. settingsRegisterCommand(F("RTC"), [](Embedis* e) {
  44. String rtc;
  45. time_t t;
  46. tmElements_t tm;
  47. if (e->argc == 1) {
  48. t = getTime_rtc();
  49. DEBUG_MSG_P(PSTR("[NTP] GET RTC Local Time: %s\n"), (char *) ntpDateTime(t).c_str());
  50. }
  51. if (e->argc > 3) {
  52. String sdate = String(e->argv[1]);
  53. String sdow = String(e->argv[2]);
  54. String stime = String(e->argv[3]);
  55. tm.Second = _rtc_getValue(stime,':',2).toInt();
  56. tm.Minute = _rtc_getValue(stime,':',1).toInt();
  57. tm.Hour = _rtc_getValue(stime,':',0).toInt();
  58. tm.Wday = sdow.toInt();
  59. tm.Day = _rtc_getValue(sdate,'-',2).toInt();
  60. tm.Month = _rtc_getValue(sdate,'-',1).toInt();
  61. tm.Year = y2kYearToTm(_rtc_getValue(sdate,'-',0).toInt()-2000);
  62. t = makeTime(tm);
  63. setTime_rtc(t);
  64. setTime(t);
  65. DEBUG_MSG_P(PSTR("[NTP] SET RTC Local Time: %s\n"), (char *) ntpDateTime(t).c_str());
  66. }
  67. DEBUG_MSG_P(PSTR("+OK\n"));
  68. });
  69. }
  70. #endif // TERMINAL_SUPPORT
  71. #endif