Added Sync Time to LCD, Fixed UTC Timestamp issues

This commit is contained in:
2026-01-28 10:28:05 +02:00
parent 094b1a9620
commit 53c55d2726
5 changed files with 478 additions and 117 deletions

View File

@@ -283,6 +283,8 @@ void CommandHandler::handleSystemInfoCommand(JsonVariant contents, const Message
handleNetworkInfoCommand(context);
} else if (action == "get_full_settings") {
handleGetFullSettingsCommand(context);
} else if (action == "sync_time_to_lcd") {
handleSyncTimeToLcdCommand(context);
} else {
LOG_WARNING("Unknown system info action: %s", action.c_str());
sendErrorResponse("system_info", "Unknown action: " + action, context);
@@ -636,19 +638,26 @@ void CommandHandler::handleSetClockEnabledCommand(JsonVariant contents, const Me
}
void CommandHandler::handleGetDeviceTimeCommand(const MessageContext& context) {
StaticJsonDocument<256> response;
StaticJsonDocument<384> response;
response["status"] = "SUCCESS";
response["type"] = "device_time";
if (_timeKeeper) {
// Get Unix timestamp from Timekeeper
unsigned long timestamp = _timeKeeper->getTime();
response["payload"]["timestamp"] = timestamp;
// RTC stores LOCAL time (already timezone-adjusted)
unsigned long localTimestamp = _timeKeeper->getTime();
// Get timezone offset to calculate UTC
const auto& timeConfig = _configManager.getTimeConfig();
long totalOffset = timeConfig.gmtOffsetSec + timeConfig.daylightOffsetSec;
unsigned long utcTimestamp = localTimestamp - totalOffset;
response["payload"]["local_timestamp"] = localTimestamp;
response["payload"]["utc_timestamp"] = utcTimestamp;
response["payload"]["rtc_available"] = true;
// Convert to readable format
time_t rawTime = (time_t)timestamp;
struct tm* timeInfo = localtime(&rawTime);
// Convert LOCAL timestamp to readable format using gmtime (no additional offset)
time_t rawTime = (time_t)localTimestamp;
struct tm* timeInfo = gmtime(&rawTime); // Use gmtime to avoid double-offset
response["payload"]["year"] = timeInfo->tm_year + 1900;
response["payload"]["month"] = timeInfo->tm_mon + 1;
response["payload"]["day"] = timeInfo->tm_mday;
@@ -656,15 +665,16 @@ void CommandHandler::handleGetDeviceTimeCommand(const MessageContext& context) {
response["payload"]["minute"] = timeInfo->tm_min;
response["payload"]["second"] = timeInfo->tm_sec;
} else {
response["payload"]["timestamp"] = millis() / 1000;
response["payload"]["local_timestamp"] = millis() / 1000;
response["payload"]["utc_timestamp"] = millis() / 1000;
response["payload"]["rtc_available"] = false;
LOG_WARNING("TimeKeeper reference not set for device time request");
}
String responseStr;
serializeJson(response, responseStr);
sendResponse(responseStr, context);
LOG_DEBUG("Device time requested");
}
@@ -858,7 +868,37 @@ void CommandHandler::handleGetFullSettingsCommand(const MessageContext& context)
LOG_DEBUG("Full settings sent (%d bytes)", responseStr.length());
}
void CommandHandler::handleSyncTimeToLcdCommand(const MessageContext& context) {
StaticJsonDocument<256> response;
response["status"] = "SUCCESS";
response["type"] = "sync_time_to_lcd";
// Get the local timestamp from TimeKeeper (RTC stores local time)
unsigned long localTimestamp = 0;
if (_timeKeeper) {
localTimestamp = _timeKeeper->getTime();
} else {
// Fallback to millis if TimeKeeper not available
localTimestamp = millis() / 1000;
LOG_WARNING("TimeKeeper not available for LCD time sync");
}
// Get timezone offset from ConfigManager (in seconds)
const auto& timeConfig = _configManager.getTimeConfig();
long totalOffset = timeConfig.gmtOffsetSec + timeConfig.daylightOffsetSec;
// Calculate UTC timestamp by subtracting the offset from local time
unsigned long utcTimestamp = localTimestamp - totalOffset;
response["payload"]["timestamp"] = utcTimestamp;
response["payload"]["offset"] = totalOffset;
String responseStr;
serializeJson(response, responseStr);
sendResponse(responseStr, context);
LOG_DEBUG("LCD time sync: UTC=%lu, offset=%ld", utcTimestamp, totalOffset);
}
void CommandHandler::handleSetNetworkConfigCommand(JsonVariant contents, const MessageContext& context) {
// Validate that we have at least one parameter to update

View File

@@ -140,6 +140,7 @@ private:
void handleGetFirmwareStatusCommand(const MessageContext& context);
void handleNetworkInfoCommand(const MessageContext& context);
void handleGetFullSettingsCommand(const MessageContext& context);
void handleSyncTimeToLcdCommand(const MessageContext& context);
// Network configuration
void handleSetNetworkConfigCommand(JsonVariant contents, const MessageContext& context);