Skip to content

Commit 14736a1

Browse files
committed
Make TimeService instance reusable
1 parent d4cfdd6 commit 14736a1

File tree

8 files changed

+38
-23
lines changed

8 files changed

+38
-23
lines changed

examples/ArduinoIoTCloud-Schedule/ArduinoIoTCloud-Schedule.ino

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void setup() {
6363
*/
6464
void setupOneShotSchedule() {
6565

66-
unsigned int startingFrom = Schedule::getTimeFromString("2021 Nov 01 17:00:00");
66+
unsigned int startingFrom = TimeService::getTimeFromString("2021 Nov 01 17:00:00");
6767
unsigned int until = startingFrom + ( DAYS * 1 );
6868
unsigned int activePeriod = MINUTES * 5;
6969

@@ -79,7 +79,7 @@ void setupOneShotSchedule() {
7979
*/
8080
void setupMinuteSchedule() {
8181

82-
unsigned int startingFrom = Schedule::getTimeFromString("2021 Nov 01 17:00:00");
82+
unsigned int startingFrom = TimeService::getTimeFromString("2021 Nov 01 17:00:00");
8383
unsigned int until = startingFrom + ( DAYS * 1 );
8484
unsigned int activePeriod = SECONDS * 15;
8585
unsigned int repetitionPeriod = 1;
@@ -96,8 +96,8 @@ void setupMinuteSchedule() {
9696
*/
9797
void setupHourlySchedule() {
9898

99-
unsigned int startingFrom = Schedule::getTimeFromString("2021 Nov 01 17:00:00");
100-
unsigned int until = Schedule::getTimeFromString("2021 Nov 15 13:00:00");
99+
unsigned int startingFrom = TimeService::getTimeFromString("2021 Nov 01 17:00:00");
100+
unsigned int until = TimeService::getTimeFromString("2021 Nov 15 13:00:00");
101101
unsigned int activePeriod = MINUTES * 20;
102102
unsigned int repetitionPeriod = 1;
103103

@@ -113,8 +113,8 @@ void setupHourlySchedule() {
113113
*/
114114
void setupDailySchedule() {
115115

116-
unsigned int startingFrom = Schedule::getTimeFromString("2021 Nov 01 17:00:00");
117-
unsigned int until = Schedule::getTimeFromString("2021 Nov 15 13:00:00");
116+
unsigned int startingFrom = TimeService::getTimeFromString("2021 Nov 01 17:00:00");
117+
unsigned int until = TimeService::getTimeFromString("2021 Nov 15 13:00:00");
118118
unsigned int activePeriod = HOURS * 2;
119119
unsigned int repetitionPeriod = 1;
120120

@@ -137,7 +137,8 @@ void setupDailySchedule() {
137137
* Saturday -> Inactive
138138
*/
139139
void setupWeeklySchedule() {
140-
unsigned int startingFrom = Schedule::getTimeFromString("2021 Nov 01 17:00:00");
140+
141+
unsigned int startingFrom = TimeService::getTimeFromString("2021 Nov 01 17:00:00");
141142
unsigned int until = startingFrom + ( DAYS * 30 );
142143
unsigned int executionPeriod = MINUTES * 3;
143144

@@ -162,8 +163,8 @@ void setupWeeklySchedule() {
162163
*/
163164
void setupMonthlySchedule() {
164165

165-
unsigned int startingFrom = Schedule::getTimeFromString("2021 Nov 01 17:00:00");
166-
unsigned int until = Schedule::getTimeFromString("2021 Nov 15 13:00:00");
166+
unsigned int startingFrom = TimeService::getTimeFromString("2021 Nov 01 17:00:00");
167+
unsigned int until = TimeService::getTimeFromString("2021 Nov 15 13:00:00");
167168
unsigned int activePeriod = DAYS * 1;
168169
unsigned int dayOfMonth = 3;
169170

@@ -179,8 +180,8 @@ void setupMonthlySchedule() {
179180
*/
180181
void setupYearlySchedule() {
181182

182-
unsigned int startingFrom = Schedule::getTimeFromString("2021 Nov 06 17:00:00");
183-
unsigned int until = Schedule::getTimeFromString("2041 Nov 06 13:00:00");
183+
unsigned int startingFrom = TimeService::getTimeFromString("2021 Nov 06 17:00:00");
184+
unsigned int until = TimeService::getTimeFromString("2041 Nov 06 13:00:00");
184185
unsigned int activePeriod = DAYS * 2;
185186
unsigned int dayOfMonth = 6;
186187

extras/test/src/util/PropertyTestUtil.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ unsigned long TimeService::getLocalTime()
2323
{
2424
return getTime();
2525
}
26+
27+
TimeService* ArduinoIoTCloudTimeService() {
28+
static TimeService _timeService_instance;
29+
return &_timeService_instance;
30+
}

src/ArduinoIoTCloud.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ class ArduinoIoTCloudClass
9797

9898
inline ConnectionHandler * getConnection() { return _connection; }
9999

100-
inline unsigned long getInternalTime() { return _time_service.getTime(); }
101-
inline unsigned long getLocalTime() { return _time_service.getLocalTime(); }
102-
inline void updateInternalTimezoneInfo() { _time_service.setTimeZoneData(_tz_offset, _tz_dst_until); }
100+
inline unsigned long getInternalTime() { return _time_service->getTime(); }
101+
inline unsigned long getLocalTime() { return _time_service->getLocalTime(); }
102+
inline void updateInternalTimezoneInfo() { _time_service->setTimeZoneData(_tz_offset, _tz_dst_until); }
103103

104104
void addCallback(ArduinoIoTCloudEvent const event, OnCloudEventCallback callback);
105105

@@ -147,7 +147,7 @@ class ArduinoIoTCloudClass
147147

148148
ConnectionHandler * _connection = nullptr;
149149
PropertyContainer _property_container;
150-
TimeService _time_service;
150+
TimeService * _time_service = ArduinoIoTCloudTimeService();
151151
int _tz_offset = 0;
152152
unsigned int _tz_dst_until = 0;
153153

src/ArduinoIoTCloudLPWAN.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ int ArduinoIoTCloudLPWAN::begin(ConnectionHandler& connection, bool retry)
6868
{
6969
_connection = &connection;
7070
_retryEnable = retry;
71-
_time_service.begin(nullptr);
71+
_time_service->begin(nullptr);
7272
return 1;
7373
}
7474

@@ -105,7 +105,7 @@ ArduinoIoTCloudLPWAN::State ArduinoIoTCloudLPWAN::handle_ConnectPhy()
105105

106106
ArduinoIoTCloudLPWAN::State ArduinoIoTCloudLPWAN::handle_SyncTime()
107107
{
108-
unsigned long const internal_posix_time = _time_service.getTime();
108+
unsigned long const internal_posix_time = _time_service->getTime();
109109
DEBUG_VERBOSE("ArduinoIoTCloudLPWAN::%s internal clock configured to posix timestamp %d", __FUNCTION__, internal_posix_time);
110110
DEBUG_INFO("Connected to Arduino IoT Cloud");
111111
return State::Connected;

src/ArduinoIoTCloudTCP.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
115115
_connection = &connection;
116116
_brokerAddress = brokerAddress;
117117
_brokerPort = brokerPort;
118-
_time_service.begin(&connection);
118+
_time_service->begin(&connection);
119119
return begin(enable_watchdog, _brokerAddress, _brokerPort);
120120
}
121121

@@ -371,7 +371,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy()
371371

372372
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime()
373373
{
374-
unsigned long const internal_posix_time = _time_service.getTime();
374+
unsigned long const internal_posix_time = _time_service->getTime();
375375
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s internal clock configured to posix timestamp %d", __FUNCTION__, internal_posix_time);
376376
return State::ConnectMqttBroker;
377377
}
@@ -535,7 +535,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
535535
*/
536536
sendPropertiesToCloud();
537537

538-
unsigned long const internal_posix_time = _time_service.getTime();
538+
unsigned long const internal_posix_time = _time_service->getTime();
539539
if(internal_posix_time < _tz_dst_until) {
540540
return State::Connected;
541541
} else {
@@ -568,7 +568,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
568568
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values received", __FUNCTION__, millis());
569569
CBORDecoder::decode(_property_container, (uint8_t*)bytes, length, true);
570570
sendPropertiesToCloud();
571-
_time_service.setTimeZoneData(_tz_offset, _tz_dst_until);
571+
_time_service->setTimeZoneData(_tz_offset, _tz_dst_until);
572572
execCloudEventCallback(ArduinoIoTCloudEvent::SYNC);
573573
_last_sync_request_cnt = 0;
574574
_last_sync_request_tick = 0;

src/property/types/CloudSchedule.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ typedef struct ScheduleWeeklyMask {
105105
/******************************************************************************
106106
CLASS DECLARATION
107107
******************************************************************************/
108-
class Schedule : public TimeService {
108+
class Schedule {
109109
public:
110110
unsigned int frm, to, len, msk;
111111
Schedule(unsigned int s, unsigned int e, unsigned int d, unsigned int m): frm(s), to(e), len(d), msk(m) {}
112112

113113
bool isActive() {
114114

115-
unsigned int now = getLocalTime();
115+
unsigned int now = _schedule_time_service->getLocalTime();
116116
if(checkSchedulePeriod(now, frm, to)) {
117117
/* We are in the schedule range */
118118

@@ -193,6 +193,8 @@ class Schedule : public TimeService {
193193
return !(operator==(aSchedule));
194194
}
195195
private:
196+
TimeService * _schedule_time_service = ArduinoIoTCloudTimeService();
197+
196198
ScheduleUnit getScheduleUnit(unsigned int msk) {
197199
return static_cast<ScheduleUnit>((msk & SCHEDULE_UNIT_MASK) >> SCHEDULE_UNIT_SHIFT);
198200
}

src/utility/time/TimeService.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,8 @@ time_t cvt_time(char const * time)
247247

248248
return mktime(&t);
249249
}
250+
251+
TimeService* ArduinoIoTCloudTimeService() {
252+
static TimeService _timeService_instance;
253+
return &_timeService_instance;
254+
}

src/utility/time/TimeService.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,6 @@ class TimeService
7070

7171
};
7272

73+
TimeService* ArduinoIoTCloudTimeService();
74+
7375
#endif /* ARDUINO_IOT_CLOUD_TIME_SERVICE_H_ */

0 commit comments

Comments
 (0)