supla-device
Loading...
Searching...
No Matches
DS1307RTC.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15*/
16
17#ifndef SRC_SUPLA_CLOCK_DS1307RTC_H_
18#define SRC_SUPLA_CLOCK_DS1307RTC_H_
19
20/*
21Dependency: https://github.com/adafruit/RTClib,
22use library manager to install it
23*/
24
25#include <supla/log_wrapper.h>
26#include <time.h>
27#include "clock.h"
28
29#include "RTClib.h"
30
31namespace Supla {
32
33class DS1307RTC : public Clock {
34 public:
35 DS1307RTC() {}
36
37 void onInit() {
38 if (!rtc.begin()) {
39 SUPLA_LOG_DEBUG("Unable to find RTC");
40 } else {
41 struct tm timeinfo {};
42 isRTCReady = true;
43 RTCLostPower = (rtc.isrunning()) ? false : true;
44 DateTime now = rtc.now();
45
46 timeinfo.tm_year = now.year() - 1900;
47 timeinfo.tm_mon = now.month() - 1;
48 timeinfo.tm_mday = now.day();
49 timeinfo.tm_hour = now.hour();
50 timeinfo.tm_min = now.minute();
51 timeinfo.tm_sec = now.second();
52
53 localtime = mktime(&timeinfo);
54
55#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
56 timeval tv = {localtime, 0};
57 settimeofday(&tv, nullptr);
58#elif defined(ARDUINO_ARCH_AVR)
59 set_system_time(mktime(&timeinfo));
60#endif
61 printCurrentTime("from RTC:");
62
63 if (getYear() >= 2023) {
64 isClockReady = true;
65 } else {
66 SUPLA_LOG_DEBUG("Clock is not ready");
67 }
68 }
69 }
70
71 bool rtcIsReady() {
72 return isRTCReady;
73 }
74
75 bool getRTCLostPowerFlag() {
76 return RTCLostPower;
77 }
78
79 void resetRTCLostPowerFlag() {
80 RTCLostPower = false;
81 }
82
83 void parseLocaltimeFromServer(TSDC_UserLocalTimeResult *result) {
84 struct tm timeinfo {};
85
86 isClockReady = true;
87
88 printCurrentTime("current");
89
90 timeinfo.tm_year = result->year - 1900;
91 timeinfo.tm_mon = result->month - 1;
92 timeinfo.tm_mday = result->day;
93 timeinfo.tm_hour = result->hour;
94 timeinfo.tm_min = result->min;
95 timeinfo.tm_sec = result->sec;
96
97 localtime = mktime(&timeinfo);
98
99#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
100 timeval tv = {localtime, 0};
101 settimeofday(&tv, nullptr);
102#elif defined(ARDUINO_ARCH_AVR)
103 set_system_time(mktime(&timeinfo));
104#endif
105 printCurrentTime("new");
106
107 // Update RTC if minutes or seconds are different
108 // from the time obtained from the server
109 if (isRTCReady) {
110 DateTime now = rtc.now();
111 if ((now.year() != getYear()) || (now.month() != getMonth()) ||
112 (now.day() != getDay()) || (now.hour() != getHour()) ||
113 (now.minute() != getMin()) || (now.second() - getSec() > 5) ||
114 (now.second() - getSec() < -5)) {
115 rtc.adjust(DateTime(getTimeStamp()));
116 SUPLA_LOG_DEBUG("Update RTC time from server");
117 }
118 }
119}
120
121 protected:
122 RTC_DS1307 rtc;
123 bool RTCLostPower = false;
124 bool isRTCReady = false;
125};
126
127}; // namespace Supla
128
129#endif // SRC_SUPLA_CLOCK_DS1307RTC_H_
void onInit()
Third method called on element in SuplaDevice.begin()
Definition DS1307RTC.h:37