Monitor WS disconnects and try to reconnect

This commit is contained in:
Djuri Baars 2024-01-27 15:54:31 +01:00
parent 95c4aa3cad
commit 9cb4b97146
2 changed files with 25 additions and 0 deletions

View File

@ -37,6 +37,7 @@ const char *wsServerPrice = "wss://ws.coincap.io/prices?assets=bitcoin";
esp_websocket_client_handle_t clientPrice = NULL; esp_websocket_client_handle_t clientPrice = NULL;
uint currentPrice = 30000; uint currentPrice = 30000;
unsigned long int lastPriceUpdate; unsigned long int lastPriceUpdate;
bool priceNotifyInit = false;
void setupPriceNotify() { void setupPriceNotify() {
// currentPrice = preferences.get("lastPrice", 30000); // currentPrice = preferences.get("lastPrice", 30000);
@ -50,6 +51,7 @@ void setupPriceNotify() {
esp_websocket_register_events(clientPrice, WEBSOCKET_EVENT_ANY, esp_websocket_register_events(clientPrice, WEBSOCKET_EVENT_ANY,
onWebsocketPriceEvent, clientPrice); onWebsocketPriceEvent, clientPrice);
esp_websocket_client_start(clientPrice); esp_websocket_client_start(clientPrice);
priceNotifyInit = true;
} }
void onWebsocketPriceEvent(void *handler_args, esp_event_base_t base, void onWebsocketPriceEvent(void *handler_args, esp_event_base_t base,

View File

@ -20,6 +20,8 @@
#include "lib/config.hpp" #include "lib/config.hpp"
uint wifiLostConnection; uint wifiLostConnection;
uint priceNotifyLostConnection = 0;
uint blockNotifyLostConnection = 0;
extern "C" void app_main() { extern "C" void app_main() {
initArduino(); initArduino();
@ -53,6 +55,27 @@ extern "C" void app_main() {
} else if (wifiLostConnection) { } else if (wifiLostConnection) {
wifiLostConnection = 0; wifiLostConnection = 0;
Serial.println("Connection restored, reset timer."); 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)); vTaskDelay(pdMS_TO_TICKS(5000));