From 9ca377870f63fd5991ba788a6ac3a32157531ed0 Mon Sep 17 00:00:00 2001 From: technyon Date: Sun, 20 Oct 2024 14:25:04 +0200 Subject: [PATCH] add espMillis() to replace esp_timer_get_time() / 1000 --- clion/CMakeLists.txt | 1 + src/EspMillis.h | 7 +++++ src/NukiNetwork.cpp | 8 +++--- src/NukiNetwork.h | 1 + src/NukiNetworkLock.h | 1 + src/NukiNetworkOpener.cpp | 4 +-- src/NukiNetworkOpener.h | 1 + src/NukiOpenerWrapper.cpp | 18 ++++++------- src/NukiWrapper.cpp | 38 +++++++++++++-------------- src/NukiWrapper.h | 1 + src/WebCfgServer.cpp | 4 +-- src/main.cpp | 11 ++++---- src/networkDevices/EthernetDevice.cpp | 6 ++--- src/networkDevices/NetworkDevice.h | 1 + src/networkDevices/WifiDevice.cpp | 8 +++--- 15 files changed, 62 insertions(+), 48 deletions(-) create mode 100644 src/EspMillis.h diff --git a/clion/CMakeLists.txt b/clion/CMakeLists.txt index 1a2e7bf..508f2a0 100644 --- a/clion/CMakeLists.txt +++ b/clion/CMakeLists.txt @@ -51,6 +51,7 @@ set(SRCFILES ../src/util/NetworkDeviceInstantiator.cpp ../src/NukiOfficial.cpp ../src/NukiPublisher.cpp + ../src/EspMillis.h ) file(GLOB_RECURSE SRCFILESREC diff --git a/src/EspMillis.h b/src/EspMillis.h new file mode 100644 index 0000000..95c7c0e --- /dev/null +++ b/src/EspMillis.h @@ -0,0 +1,7 @@ +#pragma once +#include + +inline int64_t espMillis() +{ + return esp_timer_get_time() / 1000; +} \ No newline at end of file diff --git a/src/NukiNetwork.cpp b/src/NukiNetwork.cpp index 9eec5bb..dd73ca4 100644 --- a/src/NukiNetwork.cpp +++ b/src/NukiNetwork.cpp @@ -359,7 +359,7 @@ void NukiNetwork::readSettings() bool NukiNetwork::update() { - int64_t ts = (esp_timer_get_time() / 1000); + int64_t ts = espMillis(); _device->update(); if(!_mqttEnabled || _device->isApOpen()) @@ -375,7 +375,7 @@ bool NukiNetwork::update() { forceEnableWebServer = true; } - if(_restartOnDisconnect && (esp_timer_get_time() / 1000) > 60000) + if(_restartOnDisconnect && espMillis() > 60000) { restartEsp(RestartReason::RestartOnDisconnectWatchdog); } @@ -604,7 +604,7 @@ bool NukiNetwork::update() { uint8_t pin = gpioTs.first; int64_t ts = gpioTs.second; - if(ts != 0 && (((esp_timer_get_time() / 1000) - ts) >= GPIO_DEBOUNCE_TIME)) + if(ts != 0 && ((espMillis() - ts) >= GPIO_DEBOUNCE_TIME)) { _gpioTs[pin] = 0; @@ -814,7 +814,7 @@ void NukiNetwork::parseGpioTopics(char* topic, int topic_len, char* data, int da void NukiNetwork::gpioActionCallback(const GpioAction &action, const int &pin) { - _gpioTs[pin] = (esp_timer_get_time() / 1000); + _gpioTs[pin] = espMillis(); } void NukiNetwork::disableAutoRestarts() diff --git a/src/NukiNetwork.h b/src/NukiNetwork.h index 79872d7..6908686 100644 --- a/src/NukiNetwork.h +++ b/src/NukiNetwork.h @@ -7,6 +7,7 @@ #include "networkDevices/IPConfiguration.h" #include "enums/NetworkDeviceType.h" #include "util/NetworkUtil.h" +#include "EspMillis.h" #ifndef NUKI_HUB_UPDATER #include "MqttReceiver.h" diff --git a/src/NukiNetworkLock.h b/src/NukiNetworkLock.h index 505d8b7..fd045a1 100644 --- a/src/NukiNetworkLock.h +++ b/src/NukiNetworkLock.h @@ -14,6 +14,7 @@ #include "LockActionResult.h" #include "NukiOfficial.h" #include "NukiPublisher.h" +#include "EspMillis.h" class NukiNetworkLock : public MqttReceiver { diff --git a/src/NukiNetworkOpener.cpp b/src/NukiNetworkOpener.cpp index 1d34d46..71069c1 100644 --- a/src/NukiNetworkOpener.cpp +++ b/src/NukiNetworkOpener.cpp @@ -131,7 +131,7 @@ void NukiNetworkOpener::initialize() void NukiNetworkOpener::update() { - if(_resetRingStateTs != 0 && (esp_timer_get_time() / 1000) >= _resetRingStateTs) + if(_resetRingStateTs != 0 && espMillis() >= _resetRingStateTs) { _resetRingStateTs = 0; publishString(mqtt_topic_lock_binary_ring, "standby", true); @@ -441,7 +441,7 @@ void NukiNetworkOpener::publishRing(const bool locked) } publishString(mqtt_topic_lock_binary_ring, "ring", true); - _resetRingStateTs = (esp_timer_get_time() / 1000) + 2000; + _resetRingStateTs = espMillis() + 2000; } void NukiNetworkOpener::publishState(NukiOpener::OpenerState lockState) diff --git a/src/NukiNetworkOpener.h b/src/NukiNetworkOpener.h index 5b9e394..08aafd3 100644 --- a/src/NukiNetworkOpener.h +++ b/src/NukiNetworkOpener.h @@ -6,6 +6,7 @@ #include "NukiConstants.h" #include "NukiOpenerConstants.h" #include "NukiNetworkLock.h" +#include "EspMillis.h" class NukiNetworkOpener : public MqttReceiver { diff --git a/src/NukiOpenerWrapper.cpp b/src/NukiOpenerWrapper.cpp index 69ee1ae..56b7733 100644 --- a/src/NukiOpenerWrapper.cpp +++ b/src/NukiOpenerWrapper.cpp @@ -197,7 +197,7 @@ void NukiOpenerWrapper::update() } int64_t lastReceivedBeaconTs = _nukiOpener.getLastReceivedBeaconTs(); - int64_t ts = (esp_timer_get_time() / 1000); + int64_t ts = espMillis(); uint8_t queryCommands = _network->queryCommands(); if(_restartBeaconTimeout > 0 && @@ -435,7 +435,7 @@ void NukiOpenerWrapper::updateKeyTurnerState() postponeBleWatchdog(); if(_retryLockstateCount < _nrOfRetries + 1) { - _nextLockStateUpdateTs = (esp_timer_get_time() / 1000) + _retryDelay; + _nextLockStateUpdateTs = espMillis() + _retryDelay; } return; } @@ -638,7 +638,7 @@ void NukiOpenerWrapper::updateConfig() { ++_retryConfigCount; Log->println(F("Invalid/Unexpected opener config and/or advanced config recieved, retrying in 10 seconds")); - int64_t ts = (esp_timer_get_time() / 1000); + int64_t ts = espMillis(); _nextConfigUpdateTs = ts + 10000; } } @@ -675,7 +675,7 @@ void NukiOpenerWrapper::updateAuthData(bool retrieved) printCommandResult(result); if(result == Nuki::CmdResult::Success) { - _waitAuthLogUpdateTs = (esp_timer_get_time() / 1000) + 5000; + _waitAuthLogUpdateTs = espMillis() + 5000; delay(100); std::list log; @@ -760,7 +760,7 @@ void NukiOpenerWrapper::updateKeypad(bool retrieved) printCommandResult(result); if(result == Nuki::CmdResult::Success) { - _waitKeypadUpdateTs = (esp_timer_get_time() / 1000) + 5000; + _waitKeypadUpdateTs = espMillis() + 5000; } } else @@ -837,7 +837,7 @@ void NukiOpenerWrapper::updateTimeControl(bool retrieved) printCommandResult(result); if(result == Nuki::CmdResult::Success) { - _waitTimeControlUpdateTs = (esp_timer_get_time() / 1000) + 5000; + _waitTimeControlUpdateTs = espMillis() + 5000; } } else @@ -951,7 +951,7 @@ void NukiOpenerWrapper::updateAuth(bool retrieved) void NukiOpenerWrapper::postponeBleWatchdog() { - _disableBleWatchdogTs = (esp_timer_get_time() / 1000) + 15000; + _disableBleWatchdogTs = espMillis() + 15000; } NukiOpener::LockAction NukiOpenerWrapper::lockActionToEnum(const char *str) @@ -2284,7 +2284,7 @@ void NukiOpenerWrapper::onConfigUpdateReceived(const char *value) jsonResult["general"] = "noChange"; } - _nextConfigUpdateTs = (esp_timer_get_time() / 1000) + 300; + _nextConfigUpdateTs = espMillis() + 300; serializeJson(jsonResult, _resbuf, sizeof(_resbuf)); _network->publishConfigCommandResult(_resbuf); @@ -3283,7 +3283,7 @@ void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value) _network->publishTimeControlCommandResult(resultStr); } - _nextConfigUpdateTs = (esp_timer_get_time() / 1000) + 300; + _nextConfigUpdateTs = espMillis() + 300; } else { diff --git a/src/NukiWrapper.cpp b/src/NukiWrapper.cpp index b710dc8..8c7e2ef 100644 --- a/src/NukiWrapper.cpp +++ b/src/NukiWrapper.cpp @@ -275,7 +275,7 @@ void NukiWrapper::update() } int64_t lastReceivedBeaconTs = _nukiLock.getLastReceivedBeaconTs(); - int64_t ts = (esp_timer_get_time() / 1000); + int64_t ts = espMillis(); uint8_t queryCommands = _network->queryCommands(); if(_restartBeaconTimeout > 0 && @@ -521,7 +521,7 @@ void NukiWrapper::updateKeyTurnerState() Log->print(F("Query lock state retrying in ")); Log->print(_retryDelay); Log->println("ms"); - _nextLockStateUpdateTs = (esp_timer_get_time() / 1000) + _retryDelay; + _nextLockStateUpdateTs = espMillis() + _retryDelay; } return; } @@ -532,7 +532,7 @@ void NukiWrapper::updateKeyTurnerState() if(lockState != _lastKeyTurnerState.lockState) { - _statusUpdatedTs = esp_timer_get_time() / 1000; + _statusUpdatedTs = espMillis(); } if(lockState == NukiLock::LockState::Locked || @@ -550,7 +550,7 @@ void NukiWrapper::updateKeyTurnerState() updateGpioOutputs(); } - else if(!_nukiOfficial->getOffConnected() && (esp_timer_get_time() / 1000) < _statusUpdatedTs + 10000) + else if(!_nukiOfficial->getOffConnected() && espMillis() < _statusUpdatedTs + 10000) { _statusUpdated = true; Log->println(F("Lock: Keep updating status on intermediate lock state")); @@ -723,7 +723,7 @@ void NukiWrapper::updateConfig() { ++_retryConfigCount; Log->println(F("Invalid/Unexpected lock config and/or advanced config recieved, retrying in 10 seconds")); - int64_t ts = (esp_timer_get_time() / 1000); + int64_t ts = espMillis(); _nextConfigUpdateTs = ts + 10000; } } @@ -758,7 +758,7 @@ void NukiWrapper::updateAuthData(bool retrieved) printCommandResult(result); if(result == Nuki::CmdResult::Success) { - _waitAuthLogUpdateTs = (esp_timer_get_time() / 1000) + 5000; + _waitAuthLogUpdateTs = espMillis() + 5000; delay(100); std::list log; @@ -842,7 +842,7 @@ void NukiWrapper::updateKeypad(bool retrieved) printCommandResult(result); if(result == Nuki::CmdResult::Success) { - _waitKeypadUpdateTs = (esp_timer_get_time() / 1000) + 5000; + _waitKeypadUpdateTs = espMillis() + 5000; } } else @@ -918,7 +918,7 @@ void NukiWrapper::updateTimeControl(bool retrieved) printCommandResult(result); if(result == Nuki::CmdResult::Success) { - _waitTimeControlUpdateTs = (esp_timer_get_time() / 1000) + 5000; + _waitTimeControlUpdateTs = espMillis() + 5000; } } else @@ -1032,7 +1032,7 @@ void NukiWrapper::updateAuth(bool retrieved) void NukiWrapper::postponeBleWatchdog() { - _disableBleWatchdogTs = (esp_timer_get_time() / 1000) + 15000; + _disableBleWatchdogTs = espMillis() + 15000; } NukiLock::LockAction NukiWrapper::lockActionToEnum(const char *str) @@ -1118,7 +1118,7 @@ LockActionResult NukiWrapper::onLockActionReceived(const char *value) { if(_preferences->getBool(preference_official_hybrid_actions, false)) { - _nukiOfficial->setOffCommandExecutedTs((esp_timer_get_time() / 1000) + 2000); + _nukiOfficial->setOffCommandExecutedTs(espMillis() + 2000); _offCommand = action; _network->publishOffAction((int)action); } @@ -2349,7 +2349,7 @@ void NukiWrapper::onConfigUpdateReceived(const char *value) jsonResult["general"] = "noChange"; } - _nextConfigUpdateTs = (esp_timer_get_time() / 1000) + 300; + _nextConfigUpdateTs = espMillis() + 300; serializeJson(jsonResult, _resbuf, sizeof(_resbuf)); _network->publishConfigCommandResult(_resbuf); @@ -2394,7 +2394,7 @@ void NukiWrapper::onGpioActionReceived(const GpioAction &action, const int &pin) } else { - _nukiOfficial->setOffCommandExecutedTs((esp_timer_get_time() / 1000) + 2000); + _nukiOfficial->setOffCommandExecutedTs(espMillis() + 2000); _offCommand = NukiLock::LockAction::Lock; _network->publishOffAction(2); } @@ -2406,7 +2406,7 @@ void NukiWrapper::onGpioActionReceived(const GpioAction &action, const int &pin) } else { - _nukiOfficial->setOffCommandExecutedTs((esp_timer_get_time() / 1000) + 2000); + _nukiOfficial->setOffCommandExecutedTs(espMillis() + 2000); _offCommand = NukiLock::LockAction::Unlock; _network->publishOffAction(1); } @@ -2418,7 +2418,7 @@ void NukiWrapper::onGpioActionReceived(const GpioAction &action, const int &pin) } else { - _nukiOfficial->setOffCommandExecutedTs((esp_timer_get_time() / 1000) + 2000); + _nukiOfficial->setOffCommandExecutedTs(espMillis() + 2000); _offCommand = NukiLock::LockAction::Unlatch; _network->publishOffAction(3); } @@ -2430,7 +2430,7 @@ void NukiWrapper::onGpioActionReceived(const GpioAction &action, const int &pin) } else { - _nukiOfficial->setOffCommandExecutedTs((esp_timer_get_time() / 1000) + 2000); + _nukiOfficial->setOffCommandExecutedTs(espMillis() + 2000); _offCommand = NukiLock::LockAction::LockNgo; _network->publishOffAction(4); } @@ -2442,7 +2442,7 @@ void NukiWrapper::onGpioActionReceived(const GpioAction &action, const int &pin) } else { - _nukiOfficial->setOffCommandExecutedTs((esp_timer_get_time() / 1000) + 2000); + _nukiOfficial->setOffCommandExecutedTs(espMillis() + 2000); _offCommand = NukiLock::LockAction::LockNgoUnlatch; _network->publishOffAction(5); } @@ -3398,7 +3398,7 @@ void NukiWrapper::onTimeControlCommandReceived(const char *value) _network->publishTimeControlCommandResult(resultStr); } - _nextConfigUpdateTs = (esp_timer_get_time() / 1000) + 300; + _nextConfigUpdateTs = espMillis() + 300; } else { @@ -3967,7 +3967,7 @@ void NukiWrapper::notify(Nuki::EventType eventType) { if(!_nukiOfficial->getOffConnected()) { - if(_nukiOfficial->getOffEnabled() && _intervalHybridLockstate > 0 && (esp_timer_get_time() / 1000) > (_intervalHybridLockstate * 1000)) + if(_nukiOfficial->getOffEnabled() && _intervalHybridLockstate > 0 && espMillis() > (_intervalHybridLockstate * 1000)) { Log->println("OffKeyTurnerStatusUpdated"); _statusUpdated = true; @@ -3978,7 +3978,7 @@ void NukiWrapper::notify(Nuki::EventType eventType) { Log->println("KeyTurnerStatusUpdated"); _statusUpdated = true; - _statusUpdatedTs = esp_timer_get_time() / 1000; + _statusUpdatedTs = espMillis(); _network->publishStatusUpdated(_statusUpdated); } } diff --git a/src/NukiWrapper.h b/src/NukiWrapper.h index ee695f9..1a29641 100644 --- a/src/NukiWrapper.h +++ b/src/NukiWrapper.h @@ -9,6 +9,7 @@ #include "LockActionResult.h" #include "NukiDeviceId.h" #include "NukiOfficial.h" +#include "EspMillis.h" class NukiWrapper : public Nuki::SmartlockEventHandler { diff --git a/src/WebCfgServer.cpp b/src/WebCfgServer.cpp index d4cf0d0..0c97b2d 100644 --- a/src/WebCfgServer.cpp +++ b/src/WebCfgServer.cpp @@ -1012,7 +1012,7 @@ esp_err_t WebCfgServer::handleOtaUpload(PsychicRequest *request, const String& f return(ESP_FAIL); } - _otaStartTs = esp_timer_get_time() / 1000; + _otaStartTs = espMillis(); esp_task_wdt_config_t twdt_config = { .timeout_ms = 30000, @@ -3985,7 +3985,7 @@ esp_err_t WebCfgServer::buildInfoHtml(PsychicRequest *request) response.print("\nUpdater build date: "); response.print(_preferences->getString(preference_updater_date, "")); response.print("\nUptime (min): "); - response.print(esp_timer_get_time() / 1000 / 1000 / 60); + response.print(espMillis() / 1000 / 60); response.print("\nConfig version: "); response.print(_preferences->getInt(preference_config_version)); response.print("\nLast restart reason FW: "); diff --git a/src/main.cpp b/src/main.cpp index 173acd6..0d2cbf1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include "esp_https_ota.h" #include "esp_task_wdt.h" #include "Config.h" +#include "EspMillis.h" #ifndef NUKI_HUB_UPDATER #include "NukiWrapper.h" @@ -131,7 +132,7 @@ void networkTask(void *pvParameters) } while(true) { - int64_t ts = (esp_timer_get_time() / 1000); + int64_t ts = espMillis(); if(ts > 120000 && ts < 125000) { if(bootloopCounter > 0) @@ -162,10 +163,10 @@ void networkTask(void *pvParameters) } #endif - if((esp_timer_get_time() / 1000) - networkLoopTs > 120000) + if(espMillis() - networkLoopTs > 120000) { Log->println("networkTask is running"); - networkLoopTs = esp_timer_get_time() / 1000; + networkLoopTs = espMillis(); } esp_task_wdt_reset(); @@ -212,10 +213,10 @@ void nukiTask(void *pvParameters) nukiOpener->update(); } - if((esp_timer_get_time() / 1000) - nukiLoopTs > 120000) + if(espMillis() - nukiLoopTs > 120000) { Log->println("nukiTask is running"); - nukiLoopTs = esp_timer_get_time() / 1000; + nukiLoopTs = espMillis(); } esp_task_wdt_reset(); diff --git a/src/networkDevices/EthernetDevice.cpp b/src/networkDevices/EthernetDevice.cpp index 1a0c78b..4ec1070 100644 --- a/src/networkDevices/EthernetDevice.cpp +++ b/src/networkDevices/EthernetDevice.cpp @@ -87,7 +87,7 @@ void EthernetDevice::initialize() criticalEthFailure = false; if(!_ipConfiguration->dhcpEnabled()) { - _checkIpTs = (esp_timer_get_time() / 1000) + 2000; + _checkIpTs = espMillis() + 2000; } } #endif @@ -126,7 +126,7 @@ void EthernetDevice::update() { Log->println(F("ETH Set static IP")); ETH.config(_ipConfiguration->ipAddress(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet(), _ipConfiguration->dnsServer()); - _checkIpTs = (esp_timer_get_time() / 1000) + 2000; + _checkIpTs = espMillis() + 2000; } else { @@ -213,7 +213,7 @@ bool EthernetDevice::isApOpen() void EthernetDevice::onDisconnected() { - if(_preferences->getBool(preference_restart_on_disconnect, false) && ((esp_timer_get_time() / 1000) > 60000)) + if(_preferences->getBool(preference_restart_on_disconnect, false) && (espMillis() > 60000)) { restartEsp(RestartReason::RestartOnDisconnectWatchdog); } diff --git a/src/networkDevices/NetworkDevice.h b/src/networkDevices/NetworkDevice.h index 7c32fc4..abbade2 100644 --- a/src/networkDevices/NetworkDevice.h +++ b/src/networkDevices/NetworkDevice.h @@ -1,5 +1,6 @@ #pragma once #include "IPConfiguration.h" +#include "../EspMillis.h" class NetworkDevice { diff --git a/src/networkDevices/WifiDevice.cpp b/src/networkDevices/WifiDevice.cpp index 46513af..8625db3 100644 --- a/src/networkDevices/WifiDevice.cpp +++ b/src/networkDevices/WifiDevice.cpp @@ -235,7 +235,7 @@ bool WifiDevice::connect() { Log->print("No network found with SSID: "); Log->println(ssid); - if(_preferences->getBool(preference_restart_on_disconnect, false) && ((esp_timer_get_time() / 1000) > 60000)) + if(_preferences->getBool(preference_restart_on_disconnect, false) && (espMillis() > 60000)) { restartEsp(RestartReason::RestartOnDisconnectWatchdog); } @@ -287,7 +287,7 @@ bool WifiDevice::connect() if (status != WL_CONNECTED) { - if(_preferences->getBool(preference_restart_on_disconnect, false) && ((esp_timer_get_time() / 1000) > 60000)) + if(_preferences->getBool(preference_restart_on_disconnect, false) && (espMillis() > 60000)) { restartEsp(RestartReason::RestartOnDisconnectWatchdog); _connecting = false; @@ -349,7 +349,7 @@ void WifiDevice::onDisconnected() if(_connected) { _connected = false; - _disconnectTs = (esp_timer_get_time() / 1000); + _disconnectTs = espMillis(); Log->println(F("Wi-Fi disconnected")); //QUICK RECONNECT @@ -377,7 +377,7 @@ void WifiDevice::onDisconnected() if(!isConnected()) { - if(_preferences->getBool(preference_restart_on_disconnect, false) && ((esp_timer_get_time() / 1000) > 60000)) + if(_preferences->getBool(preference_restart_on_disconnect, false) && (espMillis() > 60000)) { restartEsp(RestartReason::RestartOnDisconnectWatchdog); }