Fix lost data connection detection

This commit is contained in:
Djuri Baars 2024-01-31 23:45:26 +01:00
parent 9cb4b97146
commit e4a39de5fc
9 changed files with 80 additions and 10 deletions

View File

@ -3,6 +3,7 @@
char *wsServer; char *wsServer;
esp_websocket_client_handle_t blockNotifyClient = NULL; esp_websocket_client_handle_t blockNotifyClient = NULL;
uint currentBlockHeight = 816000; uint currentBlockHeight = 816000;
bool blockNotifyInit = false;
// const char *mempoolWsCert = R"(-----BEGIN CERTIFICATE----- // const char *mempoolWsCert = R"(-----BEGIN CERTIFICATE-----
// MIIHfTCCBmWgAwIBAgIRANFX3mhqRYDt1NFuENoSyaAwDQYJKoZIhvcNAQELBQAw // MIIHfTCCBmWgAwIBAgIRANFX3mhqRYDt1NFuENoSyaAwDQYJKoZIhvcNAQELBQAw
@ -105,6 +106,8 @@ void onWebsocketEvent(void *handler_args, esp_event_base_t base,
const String sub = "{\"action\": \"want\", \"data\":[\"blocks\"]}"; const String sub = "{\"action\": \"want\", \"data\":[\"blocks\"]}";
switch (event_id) { switch (event_id) {
case WEBSOCKET_EVENT_CONNECTED: case WEBSOCKET_EVENT_CONNECTED:
blockNotifyInit = true;
Serial.println(F("Connected to Mempool.space WebSocket")); Serial.println(F("Connected to Mempool.space WebSocket"));
Serial.println(sub); Serial.println(sub);
@ -181,7 +184,16 @@ bool isBlockNotifyConnected() {
return esp_websocket_client_is_connected(blockNotifyClient); return esp_websocket_client_is_connected(blockNotifyClient);
} }
bool getBlockNotifyInit() {
return blockNotifyInit;
}
void stopBlockNotify() { void stopBlockNotify() {
if (blockNotifyClient == NULL) return;
esp_websocket_client_close(blockNotifyClient, portMAX_DELAY);
esp_websocket_client_stop(blockNotifyClient); esp_websocket_client_stop(blockNotifyClient);
esp_websocket_client_destroy(blockNotifyClient); esp_websocket_client_destroy(blockNotifyClient);
blockNotifyClient = NULL;
} }

View File

@ -24,4 +24,5 @@ void onWebsocketMessage(esp_websocket_event_data_t *event_data);
void setBlockHeight(uint newBlockHeight); void setBlockHeight(uint newBlockHeight);
uint getBlockHeight(); uint getBlockHeight();
bool isBlockNotifyConnected(); bool isBlockNotifyConnected();
void stopBlockNotify(); void stopBlockNotify();
bool getBlockNotifyInit();

View File

@ -32,6 +32,12 @@ void ledTask(void *parameter) {
case LED_EFFECT_HEARTBEAT: case LED_EFFECT_HEARTBEAT:
blinkDelayColor(150, 2, 0, 0, 255); blinkDelayColor(150, 2, 0, 0, 255);
break; break;
case LED_DATA_BLOCK_ERROR:
blinkDelayColor(150, 2, 128, 0, 128);
break;
case LED_DATA_PRICE_ERROR:
blinkDelayColor(150, 2, 177, 90, 31);
break;
case LED_EFFECT_WIFI_CONNECT_SUCCESS: case LED_EFFECT_WIFI_CONNECT_SUCCESS:
case LED_FLASH_SUCCESS: case LED_FLASH_SUCCESS:
blinkDelayColor(150, 3, 0, 255, 0); blinkDelayColor(150, 3, 0, 255, 0);

View File

@ -27,10 +27,15 @@ const int LED_EFFECT_WIFI_CONNECTING = 101;
const int LED_EFFECT_WIFI_CONNECT_ERROR = 102; const int LED_EFFECT_WIFI_CONNECT_ERROR = 102;
const int LED_EFFECT_WIFI_CONNECT_SUCCESS = 103; const int LED_EFFECT_WIFI_CONNECT_SUCCESS = 103;
const int LED_EFFECT_WIFI_ERASE_SETTINGS = 104; const int LED_EFFECT_WIFI_ERASE_SETTINGS = 104;
const int LED_PROGRESS_25 = 200; const int LED_PROGRESS_25 = 200;
const int LED_PROGRESS_50 = 201; const int LED_PROGRESS_50 = 201;
const int LED_PROGRESS_75 = 202; const int LED_PROGRESS_75 = 202;
const int LED_PROGRESS_100 = 203; const int LED_PROGRESS_100 = 203;
const int LED_DATA_PRICE_ERROR = 300;
const int LED_DATA_BLOCK_ERROR = 301;
const int LED_POWER_TEST = 999; const int LED_POWER_TEST = 999;
extern TaskHandle_t ledTaskHandle; extern TaskHandle_t ledTaskHandle;
extern Adafruit_NeoPixel pixels; extern Adafruit_NeoPixel pixels;

View File

@ -51,7 +51,6 @@ 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,
@ -61,6 +60,8 @@ void onWebsocketPriceEvent(void *handler_args, esp_event_base_t base,
switch (event_id) { switch (event_id) {
case WEBSOCKET_EVENT_CONNECTED: case WEBSOCKET_EVENT_CONNECTED:
Serial.println(F("Connected to CoinCap.io WebSocket")); Serial.println(F("Connected to CoinCap.io WebSocket"));
priceNotifyInit = true;
break; break;
case WEBSOCKET_EVENT_DATA: case WEBSOCKET_EVENT_DATA:
onWebsocketPriceMessage(data); onWebsocketPriceMessage(data);
@ -113,7 +114,15 @@ bool isPriceNotifyConnected() {
return esp_websocket_client_is_connected(clientPrice); return esp_websocket_client_is_connected(clientPrice);
} }
bool getPriceNotifyInit() {
return priceNotifyInit;
}
void stopPriceNotify() { void stopPriceNotify() {
if (clientPrice == NULL) return;
esp_websocket_client_close(clientPrice, portMAX_DELAY);
esp_websocket_client_stop(clientPrice); esp_websocket_client_stop(clientPrice);
esp_websocket_client_destroy(clientPrice); esp_websocket_client_destroy(clientPrice);
clientPrice = NULL;
} }

View File

@ -18,4 +18,5 @@ uint getPrice();
void setPrice(uint newPrice); void setPrice(uint newPrice);
bool isPriceNotifyConnected(); bool isPriceNotifyConnected();
void stopPriceNotify(); void stopPriceNotify();
bool getPriceNotifyInit();

View File

@ -24,6 +24,10 @@ void setupWebserver() {
server.on("/api/full_refresh", HTTP_GET, onApiFullRefresh); server.on("/api/full_refresh", HTTP_GET, onApiFullRefresh);
server.on("/api/stop_datasources", HTTP_GET, onApiStopDataSources);
server.on("/api/restart_datasources", HTTP_GET, onApiRestartDataSources);
server.on("/api/action/pause", HTTP_GET, onApiActionPause); server.on("/api/action/pause", HTTP_GET, onApiActionPause);
server.on("/api/action/timer_restart", HTTP_GET, onApiActionTimerRestart); server.on("/api/action/timer_restart", HTTP_GET, onApiActionTimerRestart);
@ -694,6 +698,28 @@ void onApiLightsStatus(AsyncWebServerRequest *request) {
request->send(response); request->send(response);
} }
void onApiStopDataSources(AsyncWebServerRequest *request) {
AsyncResponseStream *response =
request->beginResponseStream("application/json");
stopPriceNotify();
stopBlockNotify();
request->send(response);
}
void onApiRestartDataSources(AsyncWebServerRequest *request) {
AsyncResponseStream *response =
request->beginResponseStream("application/json");
stopPriceNotify();
stopBlockNotify();
setupPriceNotify();
setupBlockNotify();
request->send(response);
}
void onApiLightsOff(AsyncWebServerRequest *request) { void onApiLightsOff(AsyncWebServerRequest *request) {
setLights(0, 0, 0); setLights(0, 0, 0);
request->send(200); request->send(200);

View File

@ -49,4 +49,7 @@ void onNotFound(AsyncWebServerRequest *request);
StaticJsonDocument<512> getLedStatusObject(); StaticJsonDocument<512> getLedStatusObject();
StaticJsonDocument<768> getStatusObject(); StaticJsonDocument<768> getStatusObject();
void eventSourceUpdate(); void eventSourceUpdate();
void eventSourceTask(void *pvParameters); void eventSourceTask(void *pvParameters);
void onApiStopDataSources(AsyncWebServerRequest *request);
void onApiRestartDataSources(AsyncWebServerRequest *request);

View File

@ -55,20 +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()) { } else if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", false) && !isPriceNotifyConnected()) {
priceNotifyLostConnection++; priceNotifyLostConnection++;
Serial.println("Lost price data connection...");
queueLedEffect(LED_DATA_PRICE_ERROR);
// if price WS connection does not come back after 6*5 seconds, destroy and recreate
if (priceNotifyLostConnection > 6) {
Serial.println("Restarting price handler...");
// if price WS connection does not come back after 60 seconds, destroy and recreate
if (priceNotifyLostConnection > 12) {
stopPriceNotify(); stopPriceNotify();
setupPriceNotify(); setupPriceNotify();
priceNotifyLostConnection = 0; priceNotifyLostConnection = 0;
} }
} else if (!isBlockNotifyConnected()) { } else if (getBlockNotifyInit() && !isBlockNotifyConnected()) {
blockNotifyLostConnection++; blockNotifyLostConnection++;
Serial.println("Lost block data connection...");
queueLedEffect(LED_DATA_BLOCK_ERROR);
// if mempool WS connection does not come back after 6*5 seconds, destroy and recreate
if (blockNotifyLostConnection > 6) {
Serial.println("Restarting block handler...");
// if mempool WS connection does not come back after 60 seconds, destroy and recreate
if (blockNotifyLostConnection > 12) {
stopBlockNotify(); stopBlockNotify();
setupBlockNotify(); setupBlockNotify();
blockNotifyLostConnection = 0; blockNotifyLostConnection = 0;