btclock_v3/src/main.cpp

175 lines
5.6 KiB
C++
Raw Normal View History

2023-11-30 21:38:01 +00:00
/*
2024-03-30 10:40:58 +00:00
* Copyright 2023-2024 Djuri Baars
2023-11-30 21:38:01 +00:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2023-11-06 19:16:07 +00:00
#include "Arduino.h"
#include <WiFiManager.h>
#define WEBSERVER_H
2023-11-30 21:38:01 +00:00
#include "ESPAsyncWebServer.h"
2023-11-07 00:11:12 +00:00
#include "lib/config.hpp"
uint wifiLostConnection;
uint priceNotifyLostConnection = 0;
uint blockNotifyLostConnection = 0;
2024-06-29 00:19:25 +00:00
// char ptrTaskList[1500];
2024-03-30 10:40:58 +00:00
extern "C" void app_main()
{
2023-11-30 21:38:01 +00:00
initArduino();
Serial.begin(115200);
setup();
2024-03-30 10:40:58 +00:00
while (true)
{
2023-11-30 21:38:01 +00:00
// vTaskList(ptrTaskList);
// Serial.println(F("**********************************"));
// Serial.println(F("Task State Prio Stack Num"));
// Serial.println(F("**********************************"));
// Serial.print(ptrTaskList);
// Serial.println(F("**********************************"));
if (eventSourceTaskHandle != NULL)
xTaskNotifyGive(eventSourceTaskHandle);
2024-03-30 10:40:58 +00:00
int64_t currentUptime = esp_timer_get_time() / 1000000;
;
2024-07-11 20:08:42 +00:00
2024-06-29 00:19:25 +00:00
if (!getIsOTAUpdating())
2024-03-30 10:40:58 +00:00
{
2024-06-29 00:19:25 +00:00
#ifdef HAS_FRONTLIGHT
2024-07-29 18:49:46 +00:00
if (hasLightLevel()) {
if (preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE) != 0)
2024-06-29 00:19:25 +00:00
{
2024-12-10 14:13:17 +00:00
if (hasLightLevel() && getLightLevel() <= 1 && preferences.getBool("flOffWhenDark", DEFAULT_FL_OFF_WHEN_DARK))
2024-07-29 18:49:46 +00:00
{
if (frontlightIsOn()) {
frontlightFadeOutAll();
}
}
else if (hasLightLevel() && getLightLevel() < preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE) && !frontlightIsOn())
{
frontlightFadeInAll();
}
else if (frontlightIsOn() && getLightLevel() > preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE))
{
2024-06-29 00:19:25 +00:00
frontlightFadeOutAll();
}
}
2023-11-30 21:38:01 +00:00
}
2024-06-29 00:19:25 +00:00
#endif
if (!WiFi.isConnected())
2024-03-30 10:40:58 +00:00
{
2024-06-29 00:19:25 +00:00
if (!wifiLostConnection)
{
wifiLostConnection = currentUptime;
Serial.println(F("Lost WiFi connection, trying to reconnect..."));
}
2023-11-30 21:38:01 +00:00
2024-06-29 00:19:25 +00:00
if ((currentUptime - wifiLostConnection) > 600)
{
Serial.println(F("Still no connection after 10 minutes, restarting..."));
delay(2000);
ESP.restart();
}
WiFi.begin();
}
else if (wifiLostConnection)
{
wifiLostConnection = 0;
Serial.println(F("Connection restored, reset timer."));
}
2024-01-31 22:45:26 +00:00
if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", DEFAULT_FETCH_EUR_PRICE) && !isPriceNotifyConnected())
2024-03-30 10:40:58 +00:00
{
2024-06-29 00:19:25 +00:00
priceNotifyLostConnection++;
Serial.println(F("Lost price data connection..."));
queueLedEffect(LED_DATA_PRICE_ERROR);
2024-06-29 00:19:25 +00:00
// 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;
}
2024-04-11 22:17:40 +00:00
2024-06-29 00:19:25 +00:00
if (getBlockNotifyInit() && !isBlockNotifyConnected())
2024-03-30 10:40:58 +00:00
{
2024-06-29 00:19:25 +00:00
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..."));
2024-06-29 00:19:25 +00:00
restartBlockNotify();
// setupBlockNotify();
blockNotifyLostConnection = 0;
}
}
else if (blockNotifyLostConnection > 0 && isBlockNotifyConnected())
{
blockNotifyLostConnection = 0;
}
2023-11-30 21:38:01 +00:00
2024-06-29 00:19:25 +00:00
// 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))
2024-06-29 00:19:25 +00:00
{
Serial.println(F("Detected 5 missed price updates... restarting price handler."));
2024-06-29 00:19:25 +00:00
restartPriceNotify();
// setupPriceNotify();
2024-03-30 10:40:58 +00:00
2024-06-29 00:19:25 +00:00
priceNotifyLostConnection = 0;
}
2024-03-30 10:40:58 +00:00
2024-06-29 00:19:25 +00:00
// If after 45 minutes no mempool blocks, check the rest API
if ((getLastBlockUpdate() - currentUptime) > 45 * 60)
2024-03-30 10:40:58 +00:00
{
2024-06-29 00:19:25 +00:00
Serial.println(F("Long time (45 min) since last block, checking if I missed anything..."));
int currentBlock = getBlockFetch();
if (currentBlock != -1)
2024-03-30 10:40:58 +00:00
{
2024-06-29 00:19:25 +00:00
if (currentBlock != getBlockHeight())
{
Serial.println(F("Detected stuck block height... restarting block handler."));
// Mempool source stuck, restart
restartBlockNotify();
// setupBlockNotify();
}
// set last block update so it doesn't fetch for 45 minutes
setLastBlockUpdate(currentUptime);
2024-03-30 10:40:58 +00:00
}
}
2024-06-29 00:19:25 +00:00
if (currentUptime - getLastTimeSync() > 24 * 60 * 60)
{
Serial.println(F("Last time update is longer than 24 hours ago, sync again"));
syncTime();
};
}
2024-04-11 22:17:40 +00:00
2023-11-30 21:38:01 +00:00
vTaskDelay(pdMS_TO_TICKS(5000));
}
2023-11-06 19:16:07 +00:00
}