From 37c57b7d97e2aa6b21f22bec8a76eccef10d5218 Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Sun, 17 Mar 2024 23:16:15 +0100 Subject: [PATCH] Add extra check for missing price updates --- src/lib/price_notify.cpp | 4 ++++ src/lib/price_notify.hpp | 3 ++- src/main.cpp | 14 ++++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/lib/price_notify.cpp b/src/lib/price_notify.cpp index c07143e..b6ab9a3 100644 --- a/src/lib/price_notify.cpp +++ b/src/lib/price_notify.cpp @@ -105,6 +105,10 @@ void onWebsocketPriceMessage(esp_websocket_event_data_t *event_data) { } } +uint getLastPriceUpdate() { + return lastPriceUpdate; +} + uint getPrice() { return currentPrice; } void setPrice(uint newPrice) { currentPrice = newPrice; } diff --git a/src/lib/price_notify.hpp b/src/lib/price_notify.hpp index 8bcb207..acdd898 100644 --- a/src/lib/price_notify.hpp +++ b/src/lib/price_notify.hpp @@ -19,4 +19,5 @@ void setPrice(uint newPrice); bool isPriceNotifyConnected(); void stopPriceNotify(); -bool getPriceNotifyInit(); \ No newline at end of file +bool getPriceNotifyInit(); +uint getLastPriceUpdate(); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 012bf5f..47e73fb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,13 +39,15 @@ extern "C" void app_main() { if (eventSourceTaskHandle != NULL) xTaskNotifyGive(eventSourceTaskHandle); + int64_t currentUptime = esp_timer_get_time() / 1000000;; + if (!WiFi.isConnected()) { if (!wifiLostConnection) { - wifiLostConnection = esp_timer_get_time() / 1000000; + wifiLostConnection = currentUptime; Serial.println("Lost WiFi connection, trying to reconnect..."); } - if (((esp_timer_get_time() / 1000000) - wifiLostConnection) > 600) { + if ((currentUptime - wifiLostConnection) > 600) { Serial.println("Still no connection after 10 minutes, restarting..."); delay(2000); ESP.restart(); @@ -85,6 +87,14 @@ extern "C" void app_main() { priceNotifyLostConnection = 0; } + // if more than 5 price updates are missed, there is probably something wrong, reconnect + if ((getLastPriceUpdate() - currentUptime) > (preferences.getUInt("minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE)*5)) { + stopPriceNotify(); + setupPriceNotify(); + + priceNotifyLostConnection = 0; + } + vTaskDelay(pdMS_TO_TICKS(5000)); } } \ No newline at end of file