From 9cb4b97146b7856ace11de2e826f156291a8421b Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Sat, 27 Jan 2024 15:54:31 +0100 Subject: [PATCH] Monitor WS disconnects and try to reconnect --- src/lib/price_notify.cpp | 2 ++ src/main.cpp | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/lib/price_notify.cpp b/src/lib/price_notify.cpp index f901cc0..00ff2a7 100644 --- a/src/lib/price_notify.cpp +++ b/src/lib/price_notify.cpp @@ -37,6 +37,7 @@ const char *wsServerPrice = "wss://ws.coincap.io/prices?assets=bitcoin"; esp_websocket_client_handle_t clientPrice = NULL; uint currentPrice = 30000; unsigned long int lastPriceUpdate; +bool priceNotifyInit = false; void setupPriceNotify() { // currentPrice = preferences.get("lastPrice", 30000); @@ -50,6 +51,7 @@ void setupPriceNotify() { esp_websocket_register_events(clientPrice, WEBSOCKET_EVENT_ANY, onWebsocketPriceEvent, clientPrice); esp_websocket_client_start(clientPrice); + priceNotifyInit = true; } void onWebsocketPriceEvent(void *handler_args, esp_event_base_t base, diff --git a/src/main.cpp b/src/main.cpp index 79ed62d..de96dc8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,8 @@ #include "lib/config.hpp" uint wifiLostConnection; +uint priceNotifyLostConnection = 0; +uint blockNotifyLostConnection = 0; extern "C" void app_main() { initArduino(); @@ -53,6 +55,27 @@ extern "C" void app_main() { } else if (wifiLostConnection) { wifiLostConnection = 0; Serial.println("Connection restored, reset timer."); + } else if (preferences.getBool("fetchEurPrice", false) && !isPriceNotifyConnected()) { + priceNotifyLostConnection++; + + // if price WS connection does not come back after 60 seconds, destroy and recreate + if (priceNotifyLostConnection > 12) { + stopPriceNotify(); + setupPriceNotify(); + priceNotifyLostConnection = 0; + } + } else if (!isBlockNotifyConnected()) { + blockNotifyLostConnection++; + + // if mempool WS connection does not come back after 60 seconds, destroy and recreate + if (blockNotifyLostConnection > 12) { + stopBlockNotify(); + setupBlockNotify(); + blockNotifyLostConnection = 0; + } + } else if (blockNotifyLostConnection > 0 || priceNotifyLostConnection > 0) { + blockNotifyLostConnection = 0; + priceNotifyLostConnection = 0; } vTaskDelay(pdMS_TO_TICKS(5000));