Skip to content

Commit 8784783

Browse files
authored
Merge pull request #353 from pennam/time_service_refactor
TimeService update/refactor
2 parents 840f6bd + 77ea16d commit 8784783

File tree

10 files changed

+435
-127
lines changed

10 files changed

+435
-127
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/NTPUtils.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,11 @@ void NTPUtils::sendNTPpacket(UDP & udp)
9292

9393
int NTPUtils::getRandomPort(int const min_port, int const max_port)
9494
{
95-
#ifdef BOARD_HAS_ECCX08
95+
#if defined (BOARD_HAS_ECCX08)
9696
return ECCX08.random(min_port, max_port);
97+
#elif defined (ARDUINO_ARCH_ESP8266) || (ARDUINO_ARCH_ESP32)
98+
/* Uses HW Random Number Generator */
99+
return random(min_port, max_port);
97100
#else
98101
randomSeed(analogRead(0));
99102
return random(min_port, max_port);

src/utility/time/RTCMillis.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
#ifdef ARDUINO_ARCH_ESP8266
19+
20+
/**************************************************************************************
21+
* INCLUDE
22+
**************************************************************************************/
23+
24+
#include <Arduino.h>
25+
#include "RTCMillis.h"
26+
27+
/**************************************************************************************
28+
* CTOR/DTOR
29+
**************************************************************************************/
30+
31+
RTCMillis::RTCMillis()
32+
: _last_rtc_update_tick(0)
33+
, _last_rtc_update_value(0)
34+
{
35+
36+
}
37+
38+
/**************************************************************************************
39+
* PUBLIC MEMBER FUNCTIONS
40+
**************************************************************************************/
41+
42+
void RTCMillis::begin()
43+
{
44+
45+
}
46+
47+
void RTCMillis::set(unsigned long time)
48+
{
49+
_last_rtc_update_tick = millis();
50+
_last_rtc_update_value = time;
51+
}
52+
53+
unsigned long RTCMillis::get()
54+
{
55+
unsigned long current_tick = millis();
56+
unsigned long const elapsed_s = (current_tick - _last_rtc_update_tick) / 1000;
57+
if(elapsed_s) {
58+
set(_last_rtc_update_value + elapsed_s);
59+
}
60+
return _last_rtc_update_value;
61+
}
62+
63+
#endif /* ARDUINO_ARCH_ESP8266 */

src/utility/time/RTCMillis.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
#ifndef ARDUINO_IOT_CLOUD_RTC_MILLIS_H_
19+
#define ARDUINO_IOT_CLOUD_RTC_MILLIS_H_
20+
21+
#ifdef ARDUINO_ARCH_ESP8266
22+
23+
/**************************************************************************************
24+
* INCLUDE
25+
**************************************************************************************/
26+
27+
/**************************************************************************************
28+
* CLASS DECLARATION
29+
**************************************************************************************/
30+
31+
class RTCMillis
32+
{
33+
34+
public:
35+
36+
RTCMillis();
37+
38+
void begin();
39+
void set(unsigned long time);
40+
unsigned long get();
41+
42+
private:
43+
unsigned long _last_rtc_update_tick;
44+
unsigned long _last_rtc_update_value;
45+
46+
};
47+
48+
#endif /* ARDUINO_ARCH_ESP8266 */
49+
50+
#endif /* ARDUINO_IOT_CLOUD_RTC_MILLIS_H_ */

0 commit comments

Comments
 (0)