Unclutter main.cpp

This commit is contained in:
Djuri Baars 2024-12-21 17:46:10 +01:00
parent 4fdd6b6b4f
commit c44626cb42

View file

@ -22,152 +22,132 @@
uint wifiLostConnection; uint wifiLostConnection;
uint priceNotifyLostConnection = 0; uint priceNotifyLostConnection = 0;
uint blockNotifyLostConnection = 0; uint blockNotifyLostConnection = 0;
// char ptrTaskList[1500];
extern "C" void app_main() int64_t getUptime() {
{ return esp_timer_get_time() / 1000000;
initArduino(); }
Serial.begin(115200); void handlePriceNotifyDisconnection() {
setup(); if (priceNotifyLostConnection == 0) {
priceNotifyLostConnection = getUptime();
Serial.println(F("Lost price notification connection, trying to reconnect..."));
}
while (true) if ((getUptime() - priceNotifyLostConnection) > 300) { // 5 minutes timeout
{ Serial.println(F("Price notification connection lost for 5 minutes, restarting handler..."));
// vTaskList(ptrTaskList); restartPriceNotify();
// Serial.println(F("**********************************")); priceNotifyLostConnection = 0;
// Serial.println(F("Task State Prio Stack Num")); }
// Serial.println(F("**********************************")); }
// Serial.print(ptrTaskList);
// Serial.println(F("**********************************"));
if (eventSourceTaskHandle != NULL)
xTaskNotifyGive(eventSourceTaskHandle);
int64_t currentUptime = esp_timer_get_time() / 1000000; void handleBlockNotifyDisconnection() {
; if (blockNotifyLostConnection == 0) {
blockNotifyLostConnection = getUptime();
Serial.println(F("Lost block notification connection, trying to reconnect..."));
}
if (!getIsOTAUpdating()) if ((getUptime() - blockNotifyLostConnection) > 300) { // 5 minutes timeout
{ Serial.println(F("Block notification connection lost for 5 minutes, restarting handler..."));
restartBlockNotify();
blockNotifyLostConnection = 0;
}
}
void handleFrontlight() {
#ifdef HAS_FRONTLIGHT #ifdef HAS_FRONTLIGHT
if (hasLightLevel()) { if (hasLightLevel() && preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE) != 0) {
if (preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE) != 0) uint lightLevel = getLightLevel();
{ uint luxThreshold = preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE);
if (hasLightLevel() && getLightLevel() <= 1 && preferences.getBool("flOffWhenDark", DEFAULT_FL_OFF_WHEN_DARK))
{ if (lightLevel <= 1 && preferences.getBool("flOffWhenDark", DEFAULT_FL_OFF_WHEN_DARK)) {
if (frontlightIsOn()) { if (frontlightIsOn()) frontlightFadeOutAll();
frontlightFadeOutAll(); } else if (lightLevel < luxThreshold && !frontlightIsOn()) {
}
}
else if (hasLightLevel() && getLightLevel() < preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE) && !frontlightIsOn())
{
frontlightFadeInAll(); frontlightFadeInAll();
} } else if (frontlightIsOn() && lightLevel > luxThreshold) {
else if (frontlightIsOn() && getLightLevel() > preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE))
{
frontlightFadeOutAll(); frontlightFadeOutAll();
} }
} }
}
#endif #endif
}
if (!WiFi.isConnected()) void checkWiFiConnection() {
{ if (!WiFi.isConnected()) {
if (!wifiLostConnection) if (!wifiLostConnection) {
{ wifiLostConnection = getUptime();
wifiLostConnection = currentUptime;
Serial.println(F("Lost WiFi connection, trying to reconnect...")); Serial.println(F("Lost WiFi connection, trying to reconnect..."));
} }
if ((getUptime() - wifiLostConnection) > 600) {
if ((currentUptime - wifiLostConnection) > 600)
{
Serial.println(F("Still no connection after 10 minutes, restarting...")); Serial.println(F("Still no connection after 10 minutes, restarting..."));
delay(2000); delay(2000);
ESP.restart(); ESP.restart();
} }
WiFi.begin(); WiFi.begin();
} } else if (wifiLostConnection) {
else if (wifiLostConnection)
{
wifiLostConnection = 0; wifiLostConnection = 0;
Serial.println(F("Connection restored, reset timer.")); Serial.println(F("Connection restored, reset timer."));
} }
}
if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", DEFAULT_FETCH_EUR_PRICE) && !isPriceNotifyConnected()) void checkMissedBlocks() {
{
priceNotifyLostConnection++;
Serial.println(F("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(F("Restarting price handler..."));
restartPriceNotify();
// setupPriceNotify();
priceNotifyLostConnection = 0;
}
}
else if (priceNotifyLostConnection > 0 && isPriceNotifyConnected())
{
priceNotifyLostConnection = 0;
}
if (getBlockNotifyInit() && !isBlockNotifyConnected())
{
blockNotifyLostConnection++;
Serial.println(F("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(F("Restarting block handler..."));
restartBlockNotify();
// setupBlockNotify();
blockNotifyLostConnection = 0;
}
}
else if (blockNotifyLostConnection > 0 && isBlockNotifyConnected())
{
blockNotifyLostConnection = 0;
}
// if more than 5 price updates are missed, there is probably something wrong, reconnect
if ((getLastPriceUpdate(CURRENCY_USD) - currentUptime) > (preferences.getUInt("minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE) * 5))
{
Serial.println(F("Detected 5 missed price updates... restarting price handler."));
restartPriceNotify();
// setupPriceNotify();
priceNotifyLostConnection = 0;
}
// If after 45 minutes no mempool blocks, check the rest API
if ((getLastBlockUpdate() - currentUptime) > 45 * 60)
{
Serial.println(F("Long time (45 min) since last block, checking if I missed anything...")); Serial.println(F("Long time (45 min) since last block, checking if I missed anything..."));
int currentBlock = getBlockFetch(); int currentBlock = getBlockFetch();
if (currentBlock != -1) if (currentBlock != -1) {
{ if (currentBlock != getBlockHeight()) {
if (currentBlock != getBlockHeight())
{
Serial.println(F("Detected stuck block height... restarting block handler.")); Serial.println(F("Detected stuck block height... restarting block handler."));
// Mempool source stuck, restart
restartBlockNotify(); restartBlockNotify();
// setupBlockNotify();
} }
// set last block update so it doesn't fetch for 45 minutes setLastBlockUpdate(getUptime());
setLastBlockUpdate(currentUptime);
} }
}
void monitorDataConnections() {
// Price notification monitoring
if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", DEFAULT_FETCH_EUR_PRICE) && !isPriceNotifyConnected()) {
handlePriceNotifyDisconnection();
} else if (priceNotifyLostConnection > 0 && isPriceNotifyConnected()) {
priceNotifyLostConnection = 0;
} }
if (currentUptime - getLastTimeSync() > 24 * 60 * 60) // Block notification monitoring
{ if (getBlockNotifyInit() && !isBlockNotifyConnected()) {
handleBlockNotifyDisconnection();
} else if (blockNotifyLostConnection > 0 && isBlockNotifyConnected()) {
blockNotifyLostConnection = 0;
}
// Check for missed price updates
if ((getLastPriceUpdate(CURRENCY_USD) - getUptime()) > (preferences.getUInt("minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE) * 5)) {
Serial.println(F("Detected 5 missed price updates... restarting price handler."));
restartPriceNotify();
priceNotifyLostConnection = 0;
}
// Check for missed blocks
if ((getLastBlockUpdate() - getUptime()) > 45 * 60) {
checkMissedBlocks();
}
}
extern "C" void app_main() {
initArduino();
Serial.begin(115200);
setup();
while (true) {
if (eventSourceTaskHandle != NULL) {
xTaskNotifyGive(eventSourceTaskHandle);
}
if (!getIsOTAUpdating()) {
handleFrontlight();
checkWiFiConnection();
monitorDataConnections();
if (getUptime() - getLastTimeSync() > 24 * 60 * 60) {
Serial.println(F("Last time update is longer than 24 hours ago, sync again")); Serial.println(F("Last time update is longer than 24 hours ago, sync again"));
syncTime(); syncTime();
}; }
} }
vTaskDelay(pdMS_TO_TICKS(5000)); vTaskDelay(pdMS_TO_TICKS(5000));