Add second block source check

This commit is contained in:
Djuri 2024-03-30 11:40:58 +01:00
parent f84ae969d4
commit 91fc474a1f
3 changed files with 177 additions and 84 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright 2023 Djuri Baars
* Copyright 2023-2024 Djuri Baars
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,13 +23,15 @@ uint wifiLostConnection;
uint priceNotifyLostConnection = 0;
uint blockNotifyLostConnection = 0;
extern "C" void app_main() {
extern "C" void app_main()
{
initArduino();
Serial.begin(115200);
setup();
while (true) {
while (true)
{
// vTaskList(ptrTaskList);
// Serial.println(F("**********************************"));
// Serial.println(F("Task State Prio Stack Num"));
@ -39,60 +41,96 @@ extern "C" void app_main() {
if (eventSourceTaskHandle != NULL)
xTaskNotifyGive(eventSourceTaskHandle);
int64_t currentUptime = esp_timer_get_time() / 1000000;;
int64_t currentUptime = esp_timer_get_time() / 1000000;
;
if (!WiFi.isConnected()) {
if (!wifiLostConnection) {
if (!WiFi.isConnected())
{
if (!wifiLostConnection)
{
wifiLostConnection = currentUptime;
Serial.println("Lost WiFi connection, trying to reconnect...");
}
if ((currentUptime - wifiLostConnection) > 600) {
if ((currentUptime - wifiLostConnection) > 600)
{
Serial.println("Still no connection after 10 minutes, restarting...");
delay(2000);
ESP.restart();
}
WiFi.begin();
} else if (wifiLostConnection) {
}
else if (wifiLostConnection)
{
wifiLostConnection = 0;
Serial.println("Connection restored, reset timer.");
} else if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", false) && !isPriceNotifyConnected()) {
}
else if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", false) && !isPriceNotifyConnected())
{
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) {
if (priceNotifyLostConnection > 6)
{
Serial.println("Restarting price handler...");
stopPriceNotify();
setupPriceNotify();
priceNotifyLostConnection = 0;
}
} else if (getBlockNotifyInit() && !isBlockNotifyConnected()) {
}
else if (getBlockNotifyInit() && !isBlockNotifyConnected())
{
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) {
if (blockNotifyLostConnection > 6)
{
Serial.println("Restarting block handler...");
stopBlockNotify();
setupBlockNotify();
blockNotifyLostConnection = 0;
}
} else if (blockNotifyLostConnection > 0 || priceNotifyLostConnection > 0) {
}
else if (blockNotifyLostConnection > 0 || priceNotifyLostConnection > 0)
{
blockNotifyLostConnection = 0;
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();
if ((getLastPriceUpdate() - currentUptime) > (preferences.getUInt("minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE) * 5))
{
Serial.println("Detected 5 missed price updates... restarting price handler.");
priceNotifyLostConnection = 0;
stopPriceNotify();
setupPriceNotify();
priceNotifyLostConnection = 0;
}
// If after 45 minutes no mempool blocks, check the rest API
if ((getLastBlockUpdate() - currentUptime) > 45 * 60)
{
Serial.println("Long time (45 min) since last block, checking if I missed anything...");
int currentBlock = getBlockFetch();
if (currentBlock != -1)
{
if (currentBlock != getBlockHeight())
{
Serial.println("Detected stuck block height... restarting block handler.");
// Mempool source stuck, restart
stopBlockNotify();
setupBlockNotify();
}
// set last block update so it doesn't fetch for 45 minutes
setLastBlockUpdate(currentUptime);
}
}
vTaskDelay(pdMS_TO_TICKS(5000));