Skip to content

Refactor time handling adding localtime/utc and ntp sync flags #32

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
54 changes: 38 additions & 16 deletions src/ArduinoCellular.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,38 @@
#include "Watchdog.h"
#endif

unsigned long ArduinoCellular::getTime() {
int year, month, day, hour, minute, second;
Time ArduinoCellular::getTimeStruct(bool localTime) {
int year = 1970;
int month = 1;
int day = 1;
int hour = 0;
int minute = 0;
int second = 0;
float tz;
modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz);
return Time(year, month, day, hour, minute, second).getUNIXTimestamp();
if (syncNTPServer() == 0) {
if (localTime) {
modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz);
} else {
modem.getNetworkUTCTime(&year, &month, &day, &hour, &minute, &second, &tz);
}
}
return Time(year, month, day, hour, minute, second);
}

unsigned long ArduinoCellular::getTime() {
return getTimeStruct().getUNIXTimestamp();
}

int ArduinoCellular::syncNTPServer(bool forceNTPSync) {
static bool needNTPSync = true;
if(!needNTPSync && !forceNTPSync) {
return 0;
}
if(modem.NTPServerSync() == 0) {
needNTPSync = false;
return 0;
}
return -1;
}

ArduinoCellular::ArduinoCellular() {
Expand Down Expand Up @@ -126,20 +153,15 @@ Time ArduinoCellular::getGPSTime(){
return Time(year, month, day, hour, minute, second);
}

Time ArduinoCellular::getCellularTime(){
int year = 1970;
int month = 1;
int day = 1;
int hour = 0;
int minute = 0;
int second = 0;
float tz;
if (modem.NTPServerSync() == 0) {
modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz);
}
return Time(year, month, day, hour, minute, second);
Time ArduinoCellular::getCellularTime(bool localTime){
// Get the current time from the network as localtime
return getTimeStruct(localTime);
}

bool ArduinoCellular::syncCellularTime(){
// Sync the time with the network NTP service
return syncNTPServer(true) == 0 ? true : false;
}

void ArduinoCellular::sendSMS(String number, String message){
modem.sendAT("+CMGF=1");
Expand Down
15 changes: 13 additions & 2 deletions src/ArduinoCellular.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,19 @@ class ArduinoCellular {
* @return The GPS location. If the location is not retrieved, the latitude and longitude will be 0.0.
*/
Geolocation getGPSLocation(unsigned long timeout = 60000);

/**
* @brief Gets the current time from the network.
* @param localTime If true, the time will be converted to local time. Default is true.
* @return The current time.
*/
Time getCellularTime();
Time getCellularTime(bool localTime = true);

/**
* @brief Sync the modem time using NTP service.
* @return True on success false otherwise.
*/
bool syncCellularTime();

/**
* @brief Gets the current time from the GPS module.
Expand Down Expand Up @@ -303,7 +310,11 @@ class ArduinoCellular {

static unsigned long getTime(); /** Callback for getting the current time as an unix timestamp. */

static Time getTimeStruct(bool localTime = false); /** Function for getting the current time as a Time object. */

static constexpr unsigned long waitForNetworkTimeout = 20000L; /**< Maximum wait time for network registration (In milliseconds). */

static int syncNTPServer(bool forceNTPSync = false); /** Function for synchronizing the NTP server. */
};


Expand Down
Loading