From 1b1687bbac21c2c15ee55b976e791ec3b48c49e8 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/emonesp.h | 2 ++ src/main.cpp | 12 ++++++++++++ src/time_man.cpp | 1 + 5 files changed, 22 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/emonesp.h b/src/emonesp.h index faf9113d..1fcdb255 100644 --- a/src/emonesp.h +++ b/src/emonesp.h @@ -173,4 +173,6 @@ extern String serial; void restart_system(); +uint64_t uptimeMillis(); + #endif // _EMONESP_H diff --git a/src/main.cpp b/src/main.cpp index f2a5fd3f..61d54a0c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -175,6 +175,8 @@ void loop() { Profile_Start(loop); + uptimeMillis(); + Profile_Start(Mongoose); Mongoose.poll(0); Profile_End(Mongoose, 10); @@ -340,3 +342,13 @@ void handle_serial() } } } + +// inspired from https://www.snad.cz/en/2018/12/21/uptime-and-esp8266/ +uint64_t uptimeMillis() +{ + static uint32_t low32, high32; + uint32_t new_low32 = millis(); + if (new_low32 < low32) high32++; + low32 = new_low32; + return (uint64_t) high32 << 32 | low32; +} diff --git a/src/time_man.cpp b/src/time_man.cpp index 71d7b182..7e3fc3e2 100644 --- a/src/time_man.cpp +++ b/src/time_man.cpp @@ -278,4 +278,5 @@ void TimeManager::serialise(JsonDocument &doc) doc["time"] = time; doc["local_time"] = local_time; doc["offset"] = offset; + doc["uptime"] = uptimeMillis() / 1000; }