From 1d3f36c165d8cb943080171c6217415fdb954e09 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Fri, 21 Jul 2023 10:29:05 +0200 Subject: [PATCH] Fix #434: Implement Uptime --- api.yml | 4 ++++ models/Status.yaml | 3 +++ src/time_man.cpp | 13 +++++++++++++ 3 files changed, 20 insertions(+) diff --git a/api.yml b/api.yml index 30f43c17..00277612 100644 --- a/api.yml +++ b/api.yml @@ -773,10 +773,14 @@ paths: x-stoplight: id: 7h1cpgfjj5j9j format: date-time + uptime: + type: integer + description: EVSE gateway uptime, in seconds required: - time - offset - local_time + - uptime operationId: get-time description: Gets the time set on the OpenEVSE post: diff --git a/models/Status.yaml b/models/Status.yaml index 374a103d..8f2d1f14 100644 --- a/models/Status.yaml +++ b/models/Status.yaml @@ -175,3 +175,6 @@ properties: limit_version: type: integer description: /limit endpoint current version + uptime: + type: integer + description: EVSE gateway uptime, in seconds diff --git a/src/time_man.cpp b/src/time_man.cpp index 71d7b182..6181efe0 100644 --- a/src/time_man.cpp +++ b/src/time_man.cpp @@ -258,6 +258,18 @@ String time_format_time(tm &time) return String(output); } + +// inspired from https://www.snad.cz/en/2018/12/21/uptime-and-esp8266/ +// this works if this method is called at least once every 47 days, which is the case (each 8 hours at least based on TIME_POLL_TIME) +uint64_t millis64() +{ + static uint32_t low32, high32; + uint32_t new_low32 = millis(); + if (new_low32 < low32) high32++; + low32 = new_low32; + return (uint64_t) high32 << 32 | low32; +} + void TimeManager::serialise(JsonDocument &doc) { // get the current time @@ -278,4 +290,5 @@ void TimeManager::serialise(JsonDocument &doc) doc["time"] = time; doc["local_time"] = local_time; doc["offset"] = offset; + doc["uptime"] = millis64() / 1000; }