Skip to content

Commit e9e0434

Browse files
committed
Rename TimeService into TimeServiceClass and use a more standard way to instantiate it
1 parent 2d7c0cf commit e9e0434

File tree

7 files changed

+36
-31
lines changed

7 files changed

+36
-31
lines changed

extras/test/src/test_CloudSchedule.cpp

Lines changed: 9 additions & 3 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+
* TimeService Fake CTOR
1717
**************************************************************************************/
1818

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

2121
/**************************************************************************************
2222
* TimeService 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
@@ -170,7 +170,7 @@ class ArduinoIoTCloudClass
170170
PropertyContainer _device_property_container;
171171
PropertyContainer _thing_property_container;
172172
unsigned int _last_checked_property_index;
173-
TimeService & _time_service;
173+
TimeServiceClass & _time_service;
174174
int _tz_offset;
175175
unsigned int _tz_dst_until;
176176
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: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static time_t const EPOCH = 0;
5555
* CTOR/DTOR
5656
**************************************************************************************/
5757

58-
TimeService::TimeService()
58+
TimeServiceClass::TimeServiceClass()
5959
: _con_hdl(nullptr)
6060
#ifdef HAS_RTC
6161
, _is_rtc_configured(false)
@@ -73,15 +73,15 @@ TimeService::TimeService()
7373
* PUBLIC MEMBER FUNCTIONS
7474
**************************************************************************************/
7575

76-
void TimeService::begin(ConnectionHandler * con_hdl)
76+
void TimeServiceClass::begin(ConnectionHandler * con_hdl)
7777
{
7878
_con_hdl = con_hdl;
7979
#ifdef HAS_RTC
8080
rtc_begin();
8181
#endif
8282
}
8383

84-
unsigned long TimeService::getTime()
84+
unsigned long TimeServiceClass::getTime()
8585
{
8686
/* If RTC is available try to get current time from
8787
* there as first choice. RTC configuration is managed
@@ -111,7 +111,7 @@ unsigned long TimeService::getTime()
111111
return EPOCH_AT_COMPILE_TIME;
112112
}
113113

114-
void TimeService::setTimeZoneData(long offset, unsigned long dst_until)
114+
void TimeServiceClass::setTimeZoneData(long offset, unsigned long dst_until)
115115
{
116116
#ifdef HAS_TCP
117117
if(_timezone_offset != offset || _timezone_dst_until != dst_until) {
@@ -125,7 +125,7 @@ void TimeService::setTimeZoneData(long offset, unsigned long dst_until)
125125
#endif
126126
}
127127

128-
unsigned long TimeService::getLocalTime()
128+
unsigned long TimeServiceClass::getLocalTime()
129129
{
130130
#ifdef HAS_TCP
131131
unsigned long utc = getTime();
@@ -140,7 +140,7 @@ unsigned long TimeService::getLocalTime()
140140
#endif
141141
}
142142

143-
unsigned long TimeService::getTimeFromString(const String& input)
143+
unsigned long TimeServiceClass::getTimeFromString(const String& input)
144144
{
145145
struct tm t =
146146
{
@@ -203,7 +203,7 @@ unsigned long TimeService::getTimeFromString(const String& input)
203203
**************************************************************************************/
204204

205205
#ifdef HAS_TCP
206-
bool TimeService::connected()
206+
bool TimeServiceClass::connected()
207207
{
208208
if(_con_hdl == nullptr) {
209209
return false;
@@ -212,7 +212,7 @@ bool TimeService::connected()
212212
}
213213
}
214214

215-
unsigned long TimeService::getRemoteTime()
215+
unsigned long TimeServiceClass::getRemoteTime()
216216
{
217217
if(connected()) {
218218
/* At first try to see if a valid time can be obtained
@@ -245,7 +245,7 @@ unsigned long TimeService::getRemoteTime()
245245
#endif /* HAS_TCP */
246246

247247
#ifdef HAS_RTC
248-
unsigned long TimeService::getRTC()
248+
unsigned long TimeServiceClass::getRTC()
249249
{
250250
if(!_is_rtc_configured) {
251251
/* If RTC is not yet configured try to get a valid time value
@@ -256,7 +256,7 @@ unsigned long TimeService::getRTC()
256256
return rtc_get();
257257
}
258258

259-
void TimeService::configureRTC()
259+
void TimeServiceClass::configureRTC()
260260
{
261261
#ifdef HAS_TCP
262262
/* For devices with a TCP connection we can try to get a valid
@@ -278,7 +278,7 @@ void TimeService::configureRTC()
278278
}
279279
#endif /* HAS_RTC */
280280

281-
bool TimeService::isTimeValid(unsigned long const time)
281+
bool TimeServiceClass::isTimeValid(unsigned long const time)
282282
{
283283
return (time > EPOCH_AT_COMPILE_TIME);
284284
}
@@ -349,7 +349,8 @@ unsigned long rtc_get() {
349349
}
350350
#endif /* HAS_RTC */
351351

352-
TimeService & ArduinoIoTCloudTimeService() {
353-
static TimeService _timeService_instance;
354-
return _timeService_instance;
355-
}
352+
/******************************************************************************
353+
* EXTERN DEFINITION
354+
******************************************************************************/
355+
356+
TimeServiceClass TimeService;

src/utility/time/TimeService.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040
* CLASS DECLARATION
4141
**************************************************************************************/
4242

43-
class TimeService
43+
class TimeServiceClass
4444
{
4545

4646
public:
4747

48-
TimeService();
49-
48+
TimeServiceClass();
49+
virtual ~TimeServiceClass() { }
5050

5151
void begin (ConnectionHandler * con_hdl);
5252
unsigned long getTime();
@@ -82,6 +82,10 @@ class TimeService
8282

8383
};
8484

85-
TimeService & ArduinoIoTCloudTimeService();
85+
/******************************************************************************
86+
* EXTERN DECLARATION
87+
******************************************************************************/
88+
89+
extern TimeServiceClass TimeService;
8690

8791
#endif /* ARDUINO_IOT_CLOUD_TIME_SERVICE_H_ */

0 commit comments

Comments
 (0)