diff --git a/docs/api.md b/docs/api.md index 0a06cba9..1d34f21d 100644 --- a/docs/api.md +++ b/docs/api.md @@ -40,6 +40,11 @@ Toggle the matrix on or off: | ---------------- | ----------------------------- | ------------------------- | ----------- | | `[PREFIX]/power` | `http://[IP]/api/power` | `{"power": true}` or `{"power": false}` | POST | +Send the board in deep sleep mode (turns off the matrix as well), good for saving battery life: + +| MQTT Topic | HTTP URL | Payload/Body | HTTP Method | +| ---------------- | ----------------------------- | ------------------------- | ----------- | +| `[PREFIX]/sleep` | `http://[IP]/api/sleep` | `{"sleep": X}` where X is number of seconds | POST | ## Sound Playback diff --git a/src/MQTTManager.cpp b/src/MQTTManager.cpp index 46050f2a..86d5e9dd 100644 --- a/src/MQTTManager.cpp +++ b/src/MQTTManager.cpp @@ -8,6 +8,7 @@ #include "Dictionary.h" #include "PeripheryManager.h" #include "UpdateManager.h" +#include "PowerManager.h" WiFiClient espClient; HADevice device; @@ -283,6 +284,26 @@ void onMqttMessage(const char *topic, const uint8_t *payload, uint16_t length) return; } + if (strTopic.equals(MQTT_PREFIX + "/sleep")) + { + StaticJsonDocument<128> doc; + DeserializationError error = deserializeJson(doc, payload); + if (error) + { + if (DEBUG_MODE) + DEBUG_PRINTLN(F("Failed to parse json")); + return; + } + if (doc.containsKey("sleep")) + { + DisplayManager.setPower(false); + PowerManager.sleep(doc["sleep"].as()); + } + + delete[] payloadCopy; + return; + } + if (strTopic.equals(MQTT_PREFIX + "/indicator1")) { DisplayManager.indicatorParser(1, payloadCopy); @@ -360,6 +381,7 @@ void onMqttConnected() "/nextapp", "/apps", "/power", + "/sleep", "/indicator1", "/indicator2", "/indicator3", diff --git a/src/PowerManager.cpp b/src/PowerManager.cpp new file mode 100644 index 00000000..867a86f2 --- /dev/null +++ b/src/PowerManager.cpp @@ -0,0 +1,45 @@ +#include +#include +#include "Globals.h" + +#define uS_TO_S_FACTOR 1000000L + +// The getter for the instantiated singleton instance +PowerManager_ &PowerManager_::getInstance() +{ + static PowerManager_ instance; + return instance; +} + +// Initialize the global shared instance +PowerManager_ &PowerManager = PowerManager.getInstance(); + +void PowerManager_::setup() +{ + +} + +void PowerManager_::sleepParser(const char *json) +{ + StaticJsonDocument<128> doc; + DeserializationError error = deserializeJson(doc, json); + if (error) + { + if (DEBUG_MODE) + DEBUG_PRINTLN(F("Failed to parse json")); + return; + } + + if (doc.containsKey("sleep")) + { + uint64_t seconds = doc["sleep"].as(); + sleep(seconds); + } +} + +void PowerManager_::sleep(uint64_t seconds) +{ + esp_sleep_enable_timer_wakeup(seconds * uS_TO_S_FACTOR); + Serial.print("Going to sleep...\n"); + esp_deep_sleep_start(); +} \ No newline at end of file diff --git a/src/PowerManager.h b/src/PowerManager.h new file mode 100644 index 00000000..b9f9979c --- /dev/null +++ b/src/PowerManager.h @@ -0,0 +1,18 @@ +#ifndef PowerManager_h +#define PowerManager_h + +#include + +class PowerManager_ +{ +private: + PowerManager_() = default; +public: + static PowerManager_ &getInstance(); + void setup(); + void sleepParser(const char*); + void sleep(uint64_t); +}; + +extern PowerManager_ &PowerManager; +#endif diff --git a/src/ServerManager.cpp b/src/ServerManager.cpp index 1abb8404..650a546c 100644 --- a/src/ServerManager.cpp +++ b/src/ServerManager.cpp @@ -10,6 +10,7 @@ #include "DisplayManager.h" #include "UpdateManager.h" #include "PeripheryManager.h" +#include "PowerManager.h" #include WiFiUDP udp; @@ -46,6 +47,13 @@ void addHandler() { mws.addHandler("/api/power", HTTP_POST, []() { DisplayManager.powerStateParse(mws.webserver->arg("plain").c_str()); mws.webserver->send(200,F("text/plain"),F("OK")); }); + mws.addHandler( + "/api/sleep", HTTP_POST, []() + { + mws.webserver->send(200,F("text/plain"),F("OK")); + DisplayManager.setPower(false); + PowerManager.sleepParser(mws.webserver->arg("plain").c_str()); + }); mws.addHandler("/api/loop", HTTP_GET, []() { mws.webserver->send_P(200, "application/json", DisplayManager.getAppsAsJson().c_str()); }); mws.addHandler("/api/effects", HTTP_GET, []()