Ability to update time without requiring Internet Access

This commit is contained in:
4rianton
2025-09-06 14:59:49 +02:00
parent 2a5094993c
commit ab48cbe747
2 changed files with 24 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
#include <Arduino.h>
#include <HardwareSerial.h>
#include <system.h>
#include <WiFi.h>
#include "commstructs.h"
#include "contentmanager.h"

View File

@@ -26,6 +26,7 @@
#include "tag_db.h"
#include "udp.h"
#include "wifimanager.h"
#include <sys/time.h>
#ifdef HAS_EXT_FLASHER
#include "webflasher.h"
@@ -648,6 +649,28 @@ void init_web() {
request->send(200, "text/plain", "Ok, saved");
});
// Allow external time sync (e.g., from Home Assistant) without Internet
// Usage: POST /set_time with form field 'epoch' (UNIX time seconds)
server.on("/set_time", HTTP_POST, [](AsyncWebServerRequest *request) {
if (request->hasParam("epoch", true)) {
time_t epoch = static_cast<time_t>(request->getParam("epoch", true)->value().toInt());
if (epoch > 1600000000) { // basic sanity check (~2020-09-13)
struct timeval tv;
tv.tv_sec = epoch;
tv.tv_usec = 0;
settimeofday(&tv, nullptr);
logLine("Time set via /set_time");
wsSendSysteminfo();
request->send(200, "text/plain", "ok");
return;
} else {
request->send(400, "text/plain", "invalid epoch");
return;
}
}
request->send(400, "text/plain", "missing 'epoch'");
});
server.on("/set_var", HTTP_POST, [](AsyncWebServerRequest *request) {
if (request->hasParam("key", true) && request->hasParam("val", true)) {
std::string key = request->getParam("key", true)->value().c_str();