Skip to content

Commit 920dc48

Browse files
committed
fix: Update MQTT and LoRa last send timestamps to Unix epoch format
1 parent ce598ae commit 920dc48

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

src/comm/wifi/wifi.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,14 +831,46 @@ void handleApiStatus(void) {
831831
unsigned long mqttLastPublish = controller.getMqtt().getLastPublishTime();
832832
json += ",\"mqtt_enabled\":" + String(configService.getConfig().sendToMqtt ? "true" : "false");
833833
json += ",\"mqtt_connected\":" + String((mqttLastPublish > 0) ? "true" : "false");
834+
834835
if (mqttLastPublish > 0) {
835-
json += ",\"mqtt_last_publish\":" + String(mqttLastPublish);
836+
// Convert millis() to Unix epoch milliseconds
837+
time_t now = time(nullptr);
838+
unsigned long currentMillis = millis();
839+
840+
// Handle millis() overflow (occurs after ~49 days)
841+
unsigned long millisAgo;
842+
if (currentMillis >= mqttLastPublish) {
843+
millisAgo = currentMillis - mqttLastPublish;
844+
} else {
845+
// Overflow occurred
846+
millisAgo = (0xFFFFFFFFUL - mqttLastPublish) + currentMillis + 1;
847+
}
848+
849+
// Convert to Unix epoch in milliseconds
850+
unsigned long long epochMs = ((unsigned long long)now * 1000ULL) - millisAgo;
851+
json += ",\"mqtt_last_publish\":" + String(epochMs);
836852
}
837853

838854
// LoRa status
839855
if (lastLoRaSendTime > 0) {
856+
// Convert millis() to Unix epoch milliseconds
857+
time_t now = time(nullptr);
858+
unsigned long currentMillis = millis();
859+
860+
// Handle millis() overflow (occurs after ~49 days)
861+
unsigned long millisAgo;
862+
if (currentMillis >= lastLoRaSendTime) {
863+
millisAgo = currentMillis - lastLoRaSendTime;
864+
} else {
865+
// Overflow occurred
866+
millisAgo = (0xFFFFFFFFUL - lastLoRaSendTime) + currentMillis + 1;
867+
}
868+
869+
// Convert to Unix epoch in milliseconds
870+
unsigned long long epochMs = ((unsigned long long)now * 1000ULL) - millisAgo;
871+
840872
json += ",\"lora_enabled\":true";
841-
json += ",\"lora_last_send\":" + String(lastLoRaSendTime);
873+
json += ",\"lora_last_send\":" + String(epochMs);
842874
} else {
843875
json += ",\"lora_enabled\":" + String(configService.getConfig().sendToLora ? "true" : "false");
844876
}

0 commit comments

Comments
 (0)