Skip to content

TimeService update/refactor #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions extras/test/src/test_CloudSchedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@
unsigned long time_now = 1;

/**************************************************************************************
* TimeService Fake CTOR/DTOR
* TimeServiceClass Fake CTOR
**************************************************************************************/

TimeService::TimeService() {}
TimeServiceClass::TimeServiceClass() {}

/**************************************************************************************
* TimeService Fake Methods
* TimeServiceClass Fake Methods
**************************************************************************************/

unsigned long TimeService::getLocalTime() {return time_now;}
unsigned long TimeServiceClass::getLocalTime() {return time_now;}

/**************************************************************************************
* TimeService Fake local instance
**************************************************************************************/

TimeServiceClass TimeService;

/**************************************************************************************
TEST CODE
Expand Down
5 changes: 0 additions & 5 deletions extras/test/src/util/PropertyTestUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,3 @@ unsigned long getTime()
{
return 0;
}

TimeService & ArduinoIoTCloudTimeService() {
static TimeService _timeService_instance;
return _timeService_instance;
}
2 changes: 1 addition & 1 deletion src/ArduinoIoTCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
ArduinoIoTCloudClass::ArduinoIoTCloudClass()
: _connection{nullptr}
, _last_checked_property_index{0}
, _time_service(ArduinoIoTCloudTimeService())
, _time_service(TimeService)
, _tz_offset{0}
, _tz_dst_until{0}
, _thing_id{""}
Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoIoTCloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class ArduinoIoTCloudClass
PropertyContainer _device_property_container;
PropertyContainer _thing_property_container;
unsigned int _last_checked_property_index;
TimeService & _time_service;
TimeServiceClass & _time_service;
int _tz_offset;
unsigned int _tz_dst_until;
String _thing_id;
Expand Down
3 changes: 1 addition & 2 deletions src/property/types/CloudSchedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Schedule {

bool isActive() {

ScheduleTimeType now = _schedule_time_service.getLocalTime();
ScheduleTimeType now = TimeService.getLocalTime();

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

ScheduleUnit getScheduleUnit(ScheduleConfigurationType msk) {
return static_cast<ScheduleUnit>((msk & SCHEDULE_UNIT_MASK) >> SCHEDULE_UNIT_SHIFT);
Expand Down
5 changes: 4 additions & 1 deletion src/utility/time/NTPUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ void NTPUtils::sendNTPpacket(UDP & udp)

int NTPUtils::getRandomPort(int const min_port, int const max_port)
{
#ifdef BOARD_HAS_ECCX08
#if defined (BOARD_HAS_ECCX08)
return ECCX08.random(min_port, max_port);
#elif defined (ARDUINO_ARCH_ESP8266) || (ARDUINO_ARCH_ESP32)
/* Uses HW Random Number Generator */
return random(min_port, max_port);
#else
randomSeed(analogRead(0));
return random(min_port, max_port);
Expand Down
63 changes: 63 additions & 0 deletions src/utility/time/RTCMillis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
This file is part of ArduinoIoTCloud.

Copyright 2020 ARDUINO SA (http://www.arduino.cc/)

This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html

You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to license@arduino.cc.
*/

#ifdef ARDUINO_ARCH_ESP8266

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include <Arduino.h>
#include "RTCMillis.h"

/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/

RTCMillis::RTCMillis()
: _last_rtc_update_tick(0)
, _last_rtc_update_value(0)
{

}

/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/

void RTCMillis::begin()
{

}

void RTCMillis::set(unsigned long time)
{
_last_rtc_update_tick = millis();
_last_rtc_update_value = time;
}

unsigned long RTCMillis::get()
{
unsigned long current_tick = millis();
unsigned long const elapsed_s = (current_tick - _last_rtc_update_tick) / 1000;
if(elapsed_s) {
set(_last_rtc_update_value + elapsed_s);
}
return _last_rtc_update_value;
}

#endif /* ARDUINO_ARCH_ESP8266 */
50 changes: 50 additions & 0 deletions src/utility/time/RTCMillis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
This file is part of ArduinoIoTCloud.

Copyright 2020 ARDUINO SA (http://www.arduino.cc/)

This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html

You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to license@arduino.cc.
*/

#ifndef ARDUINO_IOT_CLOUD_RTC_MILLIS_H_
#define ARDUINO_IOT_CLOUD_RTC_MILLIS_H_

#ifdef ARDUINO_ARCH_ESP8266

/**************************************************************************************
* INCLUDE
**************************************************************************************/

/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/

class RTCMillis
{

public:

RTCMillis();

void begin();
void set(unsigned long time);
unsigned long get();

private:
unsigned long _last_rtc_update_tick;
unsigned long _last_rtc_update_value;

};

#endif /* ARDUINO_ARCH_ESP8266 */

#endif /* ARDUINO_IOT_CLOUD_RTC_MILLIS_H_ */
Loading