mirror of
https://github.com/btclock/btclock_v3.git
synced 2024-11-19 02:30:01 +01:00
Monitor WS disconnects and try to reconnect
This commit is contained in:
parent
95c4aa3cad
commit
9cb4b97146
@ -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,
|
||||
|
23
src/main.cpp
23
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));
|
||||
|
Loading…
Reference in New Issue
Block a user