fixed time

This commit is contained in:
2025-11-04 21:09:40 +01:00
parent 8196ae4601
commit 4f02a8f3d0
14 changed files with 120 additions and 129 deletions

View File

@@ -6,10 +6,12 @@
#include <fetchOTA.h>
#include <AsyncWebSocket.h>
#include "log.h"
#include "esp_sntp.h"
extern Preferences prefs;
extern OTAStatus ota_status;
// extern AsyncWebSocket webSocket;
extern AsyncWebSocket webSocket;
extern Version current_spiffs_version;
@@ -43,7 +45,7 @@ void update_started() {
void update_finished() {
LOG(ELOG_LEVEL_DEBUG, "OTA Update finished");
ota_status.update_progress = -1;
// webSocket.textAll(String(-1).c_str());
webSocket.textAll(String(-1).c_str());
}
void update_progress(int cur, int total) {
@@ -52,7 +54,7 @@ void update_progress(int cur, int total) {
ota_status.update_progress = int(float(cur)/float(total)*100);
LOG(ELOG_LEVEL_DEBUG, "OTA Update progress: %d/%d, %i", cur, total, ota_status.update_progress);
}
// webSocket.textAll(String(ota_status.update_progress).c_str());
webSocket.textAll(String(ota_status.update_progress).c_str());
LOG(ELOG_LEVEL_DEBUG, "OTA Update progress: %d/%d", cur, total);
}
@@ -62,6 +64,7 @@ void update_error(int err) {
}
void check_update_task(void* parameter) {
LOG(ELOG_LEVEL_DEBUG, "Starting check Update Task");
ota_status.current_version = current_software_version;
ota_status.update_progress = -1;
#ifdef USE_INA226
@@ -127,14 +130,47 @@ void run_ota_update_task(void* parameter) {
vTaskDelete(NULL);
}
// void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
// void *arg, uint8_t *data, size_t len) {
// if (type == WS_EVT_CONNECT) {
// Serial.println("WebSocket client connected");
// } else if (type == WS_EVT_DISCONNECT) {
// Serial.println("WebSocket client disconnected");
// } else if (type == WS_EVT_DATA) {
// // Optionally process data received from the client
// }
// }
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
void *arg, uint8_t *data, size_t len) {
if (type == WS_EVT_CONNECT) {
Serial.println("WebSocket client connected");
} else if (type == WS_EVT_DISCONNECT) {
Serial.println("WebSocket client disconnected");
} else if (type == WS_EVT_DATA) {
// Optionally process data received from the client
}
}
void get_time_task(void* parameter) {
LOG(ELOG_LEVEL_DEBUG, "Starting GetTimeTask");
LOG(ELOG_LEVEL_DEBUG, "Trying to get time from Internet");
sntp_set_time_sync_notification_cb(onTimeSync);
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
sntp_set_sync_interval(1000 * 60 * 60); //re-sync every hour
configTzTime("UTC0", "pool.ntp.org", "1.europe.pool.ntp.org", " time.Windows.com");
waitForTime();
vTaskDelete(NULL);
}
// Time stuff
// Handler to handle successful time sync
void onTimeSync(struct timeval* tv) {
struct tm timeinfo;
getLocalTime(&timeinfo);
LOG(ELOG_LEVEL_DEBUG, "Time synchronized. New Time: %s", asctime(&timeinfo));
}
// wait for succesful time sync
bool waitForTime() {
time_t now;
do {
time(&now);
if (now > 8 * 3600) break;
delay(500);
LOG(ELOG_LEVEL_WARNING, "No valid time.");
} while (true);
LOG(ELOG_LEVEL_DEBUG, "Found valid time.");
return true;
}