Skip to content

Commit fa22ff8

Browse files
committed
Switch to TimeServiceClass
Rename TimeService into TimeServiceClass and use a more standard way to instantiate it.
1 parent c9cfdbe commit fa22ff8

File tree

7 files changed

+46
-41
lines changed

7 files changed

+46
-41
lines changed

extras/test/src/test_CloudSchedule.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@
1313
unsigned long time_now = 1;
1414

1515
/**************************************************************************************
16-
* TimeService Fake CTOR/DTOR
16+
* TimeServiceClass Fake CTOR
1717
**************************************************************************************/
1818

19-
TimeService::TimeService() {}
19+
TimeServiceClass::TimeServiceClass() {}
2020

2121
/**************************************************************************************
22-
* TimeService Fake Methods
22+
* TimeServiceClass Fake Methods
2323
**************************************************************************************/
2424

25-
unsigned long TimeService::getLocalTime() {return time_now;}
25+
unsigned long TimeServiceClass::getLocalTime() {return time_now;}
26+
27+
/**************************************************************************************
28+
* TimeService Fake local instance
29+
**************************************************************************************/
30+
31+
TimeServiceClass TimeService;
2632

2733
/**************************************************************************************
2834
TEST CODE

extras/test/src/util/PropertyTestUtil.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,3 @@ unsigned long getTime()
1818
{
1919
return 0;
2020
}
21-
22-
TimeService & ArduinoIoTCloudTimeService() {
23-
static TimeService _timeService_instance;
24-
return _timeService_instance;
25-
}

src/ArduinoIoTCloud.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
ArduinoIoTCloudClass::ArduinoIoTCloudClass()
2929
: _connection{nullptr}
3030
, _last_checked_property_index{0}
31-
, _time_service(ArduinoIoTCloudTimeService())
31+
, _time_service(TimeService)
3232
, _tz_offset{0}
3333
, _tz_dst_until{0}
3434
, _thing_id{""}

src/ArduinoIoTCloud.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class ArduinoIoTCloudClass
156156
PropertyContainer _device_property_container;
157157
PropertyContainer _thing_property_container;
158158
unsigned int _last_checked_property_index;
159-
TimeService & _time_service;
159+
TimeServiceClass & _time_service;
160160
int _tz_offset;
161161
unsigned int _tz_dst_until;
162162
String _thing_id;

src/property/types/CloudSchedule.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Schedule {
115115

116116
bool isActive() {
117117

118-
ScheduleTimeType now = _schedule_time_service.getLocalTime();
118+
ScheduleTimeType now = TimeService.getLocalTime();
119119

120120
if(checkTimeValid(now)) {
121121
/* We have to wait RTC configuration and Timezone setting from the cloud */
@@ -201,7 +201,6 @@ class Schedule {
201201
return !(operator==(aSchedule));
202202
}
203203
private:
204-
TimeService & _schedule_time_service = ArduinoIoTCloudTimeService();
205204

206205
ScheduleUnit getScheduleUnit(ScheduleConfigurationType msk) {
207206
return static_cast<ScheduleUnit>((msk & SCHEDULE_UNIT_MASK) >> SCHEDULE_UNIT_SHIFT);

src/utility/time/TimeService.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static time_t const EPOCH = 0;
9999
* CTOR/DTOR
100100
**************************************************************************************/
101101

102-
TimeService::TimeService()
102+
TimeServiceClass::TimeServiceClass()
103103
: _con_hdl(nullptr)
104104
, _is_rtc_configured(false)
105105
, _is_tz_configured(false)
@@ -116,7 +116,7 @@ TimeService::TimeService()
116116
* PUBLIC MEMBER FUNCTIONS
117117
**************************************************************************************/
118118

119-
void TimeService::begin(ConnectionHandler * con_hdl)
119+
void TimeServiceClass::begin(ConnectionHandler * con_hdl)
120120
{
121121
_con_hdl = con_hdl;
122122
initRTC();
@@ -125,7 +125,7 @@ void TimeService::begin(ConnectionHandler * con_hdl)
125125
#endif
126126
}
127127

128-
unsigned long TimeService::getTime()
128+
unsigned long TimeServiceClass::getTime()
129129
{
130130
/* Check if it's time to sync */
131131
unsigned long const current_tick = millis();
@@ -139,12 +139,12 @@ unsigned long TimeService::getTime()
139139
return isTimeValid(utc) ? utc : EPOCH_AT_COMPILE_TIME;
140140
}
141141

142-
void TimeService::setTime(unsigned long time)
142+
void TimeServiceClass::setTime(unsigned long time)
143143
{
144144
setRTC(time);
145145
}
146146

147-
bool TimeService::sync()
147+
bool TimeServiceClass::sync()
148148
{
149149
_is_rtc_configured = false;
150150

@@ -170,29 +170,29 @@ bool TimeService::sync()
170170
return _is_rtc_configured;
171171
}
172172

173-
void TimeService::setSyncInterval(unsigned long seconds)
173+
void TimeServiceClass::setSyncInterval(unsigned long seconds)
174174
{
175175
_sync_interval_ms = seconds * 1000;
176176
}
177177

178-
void TimeService::setSyncFunction(syncTimeFunctionPtr sync_func)
178+
void TimeServiceClass::setSyncFunction(syncTimeFunctionPtr sync_func)
179179
{
180180
if(sync_func) {
181181
_sync_func = sync_func;
182182
}
183183
}
184184

185-
void TimeService::setTimeZoneData(long offset, unsigned long dst_until)
185+
void TimeServiceClass::setTimeZoneData(long offset, unsigned long dst_until)
186186
{
187187
if(_timezone_offset != offset || _timezone_dst_until != dst_until) {
188-
DEBUG_DEBUG("TimeService::%s offset: %d dst_unitl %ul", __FUNCTION__, offset, dst_until);
188+
DEBUG_DEBUG("TimeServiceClass::%s offset: %d dst_unitl %ul", __FUNCTION__, offset, dst_until);
189189
_timezone_offset = offset;
190190
_timezone_dst_until = dst_until;
191191
_is_tz_configured = true;
192192
}
193193
}
194194

195-
unsigned long TimeService::getLocalTime()
195+
unsigned long TimeServiceClass::getLocalTime()
196196
{
197197
unsigned long utc = getTime();
198198
if(_is_tz_configured) {
@@ -202,7 +202,7 @@ unsigned long TimeService::getLocalTime()
202202
}
203203
}
204204

205-
unsigned long TimeService::getTimeFromString(const String& input)
205+
unsigned long TimeServiceClass::getTimeFromString(const String& input)
206206
{
207207
struct tm t =
208208
{
@@ -224,29 +224,29 @@ unsigned long TimeService::getTimeFromString(const String& input)
224224
static const int expected_parameters = 6;
225225

226226
if(input == nullptr || input.length() != expected_length) {
227-
DEBUG_ERROR("TimeService::%s invalid input length", __FUNCTION__);
227+
DEBUG_ERROR("TimeServiceClass::%s invalid input length", __FUNCTION__);
228228
return 0;
229229
}
230230

231231
int scanned_parameters = sscanf(input.c_str(), "%d %s %d %d:%d:%d", &year, s_month, &day, &hour, &min, &sec);
232232

233233
if(scanned_parameters != expected_parameters) {
234-
DEBUG_ERROR("TimeService::%s invalid input parameters number", __FUNCTION__);
234+
DEBUG_ERROR("TimeServiceClass::%s invalid input parameters number", __FUNCTION__);
235235
return 0;
236236
}
237237

238238
char * s_month_position = strstr(month_names, s_month);
239239

240240
if(s_month_position == nullptr || strlen(s_month) != 3) {
241-
DEBUG_ERROR("TimeService::%s invalid month name, use %s", __FUNCTION__, month_names);
241+
DEBUG_ERROR("TimeServiceClass::%s invalid month name, use %s", __FUNCTION__, month_names);
242242
return 0;
243243
}
244244

245245
month = (s_month_position - month_names) / 3;
246246

247247
if(month < 0 || month > 11 || day < 1 || day > 31 || year < 1900 || hour < 0 ||
248248
hour > 24 || min < 0 || min > 60 || sec < 0 || sec > 60) {
249-
DEBUG_ERROR("TimeService::%s invalid date values", __FUNCTION__);
249+
DEBUG_ERROR("TimeServiceClass::%s invalid date values", __FUNCTION__);
250250
return 0;
251251
}
252252

@@ -265,7 +265,7 @@ unsigned long TimeService::getTimeFromString(const String& input)
265265
**************************************************************************************/
266266

267267
#ifdef HAS_TCP
268-
bool TimeService::connected()
268+
bool TimeServiceClass::connected()
269269
{
270270
if(_con_hdl == nullptr) {
271271
return false;
@@ -274,7 +274,7 @@ bool TimeService::connected()
274274
}
275275
}
276276

277-
unsigned long TimeService::getRemoteTime()
277+
unsigned long TimeServiceClass::getRemoteTime()
278278
{
279279
if(connected()) {
280280
/* At first try to see if a valid time can be obtained
@@ -307,12 +307,12 @@ unsigned long TimeService::getRemoteTime()
307307

308308
#endif /* HAS_TCP */
309309

310-
bool TimeService::isTimeValid(unsigned long const time)
310+
bool TimeServiceClass::isTimeValid(unsigned long const time)
311311
{
312312
return (time > EPOCH_AT_COMPILE_TIME);
313313
}
314314

315-
void TimeService::initRTC()
315+
void TimeServiceClass::initRTC()
316316
{
317317
#if defined (ARDUINO_ARCH_SAMD)
318318
samd_initRTC();
@@ -329,7 +329,7 @@ void TimeService::initRTC()
329329
#endif
330330
}
331331

332-
void TimeService::setRTC(unsigned long time)
332+
void TimeServiceClass::setRTC(unsigned long time)
333333
{
334334
#if defined (ARDUINO_ARCH_SAMD)
335335
samd_setRTC(time);
@@ -346,7 +346,7 @@ void TimeService::setRTC(unsigned long time)
346346
#endif
347347
}
348348

349-
unsigned long TimeService::getRTC()
349+
unsigned long TimeServiceClass::getRTC()
350350
{
351351
#if defined (ARDUINO_ARCH_SAMD)
352352
return samd_getRTC();
@@ -483,7 +483,8 @@ unsigned long esp8266_getRTC()
483483
}
484484
#endif
485485

486-
TimeService & ArduinoIoTCloudTimeService() {
487-
static TimeService _timeService_instance;
488-
return _timeService_instance;
489-
}
486+
/******************************************************************************
487+
* EXTERN DEFINITION
488+
******************************************************************************/
489+
490+
TimeServiceClass TimeService;

src/utility/time/TimeService.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ typedef unsigned long(*syncTimeFunctionPtr)(void);
3535
* CLASS DECLARATION
3636
**************************************************************************************/
3737

38-
class TimeService
38+
class TimeServiceClass
3939
{
4040

4141
public:
4242

43-
TimeService();
43+
TimeServiceClass();
4444

4545
void begin (ConnectionHandler * con_hdl);
4646
unsigned long getTime();
@@ -77,6 +77,10 @@ class TimeService
7777

7878
};
7979

80-
TimeService & ArduinoIoTCloudTimeService();
80+
/******************************************************************************
81+
* EXTERN DECLARATION
82+
******************************************************************************/
83+
84+
extern TimeServiceClass TimeService;
8185

8286
#endif /* ARDUINO_IOT_CLOUD_TIME_SERVICE_H_ */

0 commit comments

Comments
 (0)