From 73b1246cfaeefa1bb0d3ddcad0dfa76c4789056f Mon Sep 17 00:00:00 2001 From: Simonmicro Date: Mon, 31 May 2021 22:16:41 +0200 Subject: [PATCH] Implemented auto NTP feature --- include/osw_config_keys.h | 1 + include/services/OswServiceTaskWiFi.h | 4 +++- src/osw_config_keys.cpp | 6 ++++-- src/services/OswServiceTaskWiFi.cpp | 27 +++++++++++++++++++++++++-- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/include/osw_config_keys.h b/include/osw_config_keys.h index 481865e9e..df1af78aa 100644 --- a/include/osw_config_keys.h +++ b/include/osw_config_keys.h @@ -22,6 +22,7 @@ class OswConfigKeyRGB; namespace OswConfigAllKeys { extern OswConfigKeyString hostname; extern OswConfigKeyBool wifiBootEnabled; +extern OswConfigKeyBool wifiAlwaysNTPEnabled; extern OswConfigKeyString wifiSsid; extern OswConfigKeyPassword wifiPass; extern OswConfigKeyRGB themeBackgroundColor; diff --git a/include/services/OswServiceTaskWiFi.h b/include/services/OswServiceTaskWiFi.h index 87d8923db..7d1b967e0 100644 --- a/include/services/OswServiceTaskWiFi.h +++ b/include/services/OswServiceTaskWiFi.h @@ -22,7 +22,7 @@ class OswServiceTaskWiFi : public OswServiceTask { WiFiClass* getNativeHandler(); bool isConnected(); IPAddress getIP(); /// Either get ip of this ap client it connected and enabled or station if enabled - //TODO general ssid&pass method (if pass is from client it will return a masked string instead) + void queueTimeUpdateViaNTP(); //WiFi (client) bool isWiFiEnabled(); @@ -47,6 +47,8 @@ class OswServiceTaskWiFi : public OswServiceTask { bool m_enableClient = false; bool m_enableStation = false; bool m_enabledStationByAutoAP = false; + bool m_queuedNTPUpdate = false; //Will be set to true it this feature is active + bool m_waitingForNTPUpdate = false; time_t m_autoAPTimeout = 0; String m_hostname; String m_clientSSID; diff --git a/src/osw_config_keys.cpp b/src/osw_config_keys.cpp index b56472cd7..7183df6de 100644 --- a/src/osw_config_keys.cpp +++ b/src/osw_config_keys.cpp @@ -10,6 +10,7 @@ namespace OswConfigAllKeys { // TODO Translate all this? OswConfigKeyString hostname("i", "WiFi", "Hostname", "Used e.g. for the wifi station", DEVICE_NAME); OswConfigKeyBool wifiBootEnabled("j", "WiFi", "Enable on boot", "This will drain your battery faster!", false); +OswConfigKeyBool wifiAlwaysNTPEnabled("k", "WiFi", "Always fetch time (when connected)", nullptr, true); OswConfigKeyString wifiSsid("a", "WiFi", "SSID", "Your wifi name", CONFIG_WIFI_SSID); OswConfigKeyPassword wifiPass("b", "WiFi", "Password", nullptr, CONFIG_WIFI_PASS); @@ -48,10 +49,11 @@ OswConfigKeyShort timeZone("h", "Date & Time", "Timezone", "Number of offset hou } // namespace OswConfigAllKeys // ...and also here, if you want to load them during boot and make them available in the configuration ui -const unsigned char oswConfigKeysCount = 25; // <------------- DON'T FORGET THIS ONE IF YOU EDIT BELOW ;) +const unsigned char oswConfigKeysCount = 26; // <------------- DON'T FORGET THIS ONE IF YOU EDIT BELOW ;) OswConfigKey* oswConfigKeys[] = { // wifi (2) - &OswConfigAllKeys::hostname, &OswConfigAllKeys::wifiSsid, &OswConfigAllKeys::wifiPass, &OswConfigAllKeys::wifiBootEnabled, + &OswConfigAllKeys::hostname, &OswConfigAllKeys::wifiSsid, &OswConfigAllKeys::wifiPass, + &OswConfigAllKeys::wifiBootEnabled, &OswConfigAllKeys::wifiAlwaysNTPEnabled, // display (8) &OswConfigAllKeys::settingDisplayTimeout, &OswConfigAllKeys::settingDisplayBrightness, &OswConfigAllKeys::settingDisplayOverlays, &OswConfigAllKeys::settingDisplayOverlaysOnWatchScreen, diff --git a/src/services/OswServiceTaskWiFi.cpp b/src/services/OswServiceTaskWiFi.cpp index 6778c19dd..07c0b3fd0 100644 --- a/src/services/OswServiceTaskWiFi.cpp +++ b/src/services/OswServiceTaskWiFi.cpp @@ -45,6 +45,25 @@ void OswServiceTaskWiFi::loop(OswHal* hal) { Serial.println(String(__FILE__) + ": [AutoAP] Active (password is " + this->m_stationPass + ")."); #endif } + + if(this->m_queuedNTPUpdate and WiFi.status() == WL_CONNECTED) { + configTime(OswConfigAllKeys::timeZone.get() * 3600 + 3600, OswConfigAllKeys::daylightOffset.get() * 3600, "pool.ntp.org", "time.nist.gov"); + this->m_queuedNTPUpdate = false; + this->m_waitingForNTPUpdate = true; +#ifdef DEBUG + Serial.println(String(__FILE__) + ": [TimeViaNTP] Started update..."); +#endif + } + + // sometimes time(nullptr) returns seconds since boot + // so check the request was resolved + if (this->m_waitingForNTPUpdate and time(nullptr) > 1600000000) { + this->m_waitingForNTPUpdate = false; +#ifdef DEBUG + Serial.println(String(__FILE__) + ": [TimeViaNTP] Update finished (time of " + time(nullptr) + ")!"); +#endif + hal->setUTCTime(time(nullptr)); + } } if(this->m_enabledStationByAutoAP and (WiFi.status() == WL_CONNECTED or !this->m_enableClient)) { @@ -54,8 +73,6 @@ void OswServiceTaskWiFi::loop(OswHal* hal) { Serial.println(String(__FILE__) + ": [AutoAP] Inactive."); #endif } - - //TODO autoreconnect } void OswServiceTaskWiFi::stop(OswHal* hal) { @@ -101,6 +118,10 @@ IPAddress OswServiceTaskWiFi::getIP() { return IPAddress(); } +void OswServiceTaskWiFi::queueTimeUpdateViaNTP() { + this->m_queuedNTPUpdate = true; +} + bool OswServiceTaskWiFi::isWiFiEnabled() { return this->m_enableClient; } @@ -115,6 +136,8 @@ void OswServiceTaskWiFi::connectWiFi() { this->updateWiFiConfig(); WiFi.begin(this->m_clientSSID.c_str(), this->m_clientPass.c_str()); this->m_autoAPTimeout = 0; + if(!this->m_queuedNTPUpdate) + this->m_queuedNTPUpdate = OswConfigAllKeys::wifiAlwaysNTPEnabled.get(); #ifdef DEBUG Serial.println(String(__FILE__) + ": Connecting to SSID " + OswConfigAllKeys::wifiSsid.get() + "..."); #endif