2023-11-30 21:38:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2023 Djuri Baars
|
|
|
|
*
|
|
|
|
* 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"
|
2023-11-10 12:02:05 +00:00
|
|
|
#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"
|
2023-11-13 11:27:34 +00:00
|
|
|
|
2023-11-25 23:51:54 +00:00
|
|
|
uint wifiLostConnection;
|
2024-01-27 14:54:31 +00:00
|
|
|
uint priceNotifyLostConnection = 0;
|
|
|
|
uint blockNotifyLostConnection = 0;
|
2023-11-25 23:51:54 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
extern "C" void app_main() {
|
|
|
|
initArduino();
|
|
|
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
setup();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// 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-17 22:16:15 +00:00
|
|
|
int64_t currentUptime = esp_timer_get_time() / 1000000;;
|
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
if (!WiFi.isConnected()) {
|
|
|
|
if (!wifiLostConnection) {
|
2024-03-17 22:16:15 +00:00
|
|
|
wifiLostConnection = currentUptime;
|
2023-11-30 21:38:01 +00:00
|
|
|
Serial.println("Lost WiFi connection, trying to reconnect...");
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:16:15 +00:00
|
|
|
if ((currentUptime - wifiLostConnection) > 600) {
|
2023-11-30 21:38:01 +00:00
|
|
|
Serial.println("Still no connection after 10 minutes, restarting...");
|
|
|
|
delay(2000);
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
WiFi.begin();
|
|
|
|
} else if (wifiLostConnection) {
|
|
|
|
wifiLostConnection = 0;
|
|
|
|
Serial.println("Connection restored, reset timer.");
|
2024-01-31 22:45:26 +00:00
|
|
|
} else if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", false) && !isPriceNotifyConnected()) {
|
2024-01-27 14:54:31 +00:00
|
|
|
priceNotifyLostConnection++;
|
2024-01-31 22:45:26 +00:00
|
|
|
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...");
|
2024-01-27 14:54:31 +00:00
|
|
|
|
|
|
|
stopPriceNotify();
|
|
|
|
setupPriceNotify();
|
|
|
|
priceNotifyLostConnection = 0;
|
|
|
|
}
|
2024-01-31 22:45:26 +00:00
|
|
|
} else if (getBlockNotifyInit() && !isBlockNotifyConnected()) {
|
2024-01-27 14:54:31 +00:00
|
|
|
blockNotifyLostConnection++;
|
2024-01-31 22:45:26 +00:00
|
|
|
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...");
|
2024-01-27 14:54:31 +00:00
|
|
|
|
|
|
|
stopBlockNotify();
|
|
|
|
setupBlockNotify();
|
|
|
|
blockNotifyLostConnection = 0;
|
|
|
|
}
|
|
|
|
} else if (blockNotifyLostConnection > 0 || priceNotifyLostConnection > 0) {
|
|
|
|
blockNotifyLostConnection = 0;
|
|
|
|
priceNotifyLostConnection = 0;
|
2023-11-07 00:11:12 +00:00
|
|
|
}
|
2023-11-30 21:38:01 +00:00
|
|
|
|
2024-03-17 22:16:15 +00:00
|
|
|
// 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();
|
|
|
|
|
|
|
|
priceNotifyLostConnection = 0;
|
|
|
|
}
|
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(5000));
|
|
|
|
}
|
2023-11-06 19:16:07 +00:00
|
|
|
}
|