From 26289673e27a810e7b49d26723a8ad9b0c1d3010 Mon Sep 17 00:00:00 2001 From: BlueAndi Date: Sun, 5 Jan 2025 01:49:43 +0100 Subject: [PATCH] "reset" renamed to "restart" in the websocket API and the REST API. Additional its available now via MQTT too. Attention, the URL changed from "/rest/api/v1/reset" to "/rest/api/v1/device/restart". --- README.md | 2 +- data/debug.html | 14 ++--- data/extra/restart.json | 12 +++++ data/js/rest.js | 6 +-- data/js/ws.js | 4 +- data/update.html | 4 +- doc/WEBSOCKET.md | 6 +-- .../src/TopicHandlerService.cpp | 2 +- src/General/MiniTerminal.cpp | 51 +++++++++---------- src/General/MiniTerminal.h | 4 +- src/Topics/Topics.cpp | 28 +++++++++- src/Web/RestApi.cpp | 38 -------------- src/Web/WebSocket.cpp | 6 +-- .../{WsCmdReset.cpp => WsCmdRestart.cpp} | 8 +-- .../{WsCmdReset.h => WsCmdRestart.h} | 22 ++++---- 15 files changed, 103 insertions(+), 104 deletions(-) create mode 100644 data/extra/restart.json rename src/Web/WsCommand/{WsCmdReset.cpp => WsCmdRestart.cpp} (92%) rename src/Web/WsCommand/{WsCmdReset.h => WsCmdRestart.h} (86%) diff --git a/README.md b/README.md index 9dc1c992..09fb311f 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ Use the following default credentials to get access to the PIXELIX web interface Connect PIXELIX with your PC via usb and start a terminal. Use the following commands to set the wifi SSID and passphrase of your home wifi network: * Write wifi passphrase: ```write wifi passphrase ``` * Write wifi SSID: ```write wifi ssid ``` -* Restart PIXELIX: ```reset``` +* Restart PIXELIX: ```restart``` * Get IP-address: ```get ip``` * Get status: ```get status``` * A status of 0 means everything is ok. diff --git a/data/debug.html b/data/debug.html index 52cc471e..c7e777d4 100644 --- a/data/debug.html +++ b/data/debug.html @@ -52,7 +52,7 @@

Debug

@@ -96,9 +96,9 @@

Debug

-
-

Reset the system now:

-

+
+

Restart the system now:

+

@@ -262,11 +262,11 @@

Debug

}); } - /* Reset device */ - function reset() { + /* Restart device */ + function restart() { disableUI(); - wsClient.reset().then(function(rsp) { + wsClient.restart().then(function(rsp) { return dialog.showInfo("

Ok.

"); }).catch(function(err) { return dialog.showError("

Error: " + err + "

"); diff --git a/data/extra/restart.json b/data/extra/restart.json new file mode 100644 index 00000000..3836389e --- /dev/null +++ b/data/extra/restart.json @@ -0,0 +1,12 @@ +{ + "ha": { + "component": "button", + "discovery": { + "name": "Restart", + "ic": "mdi:restart", + "ent_cat": "config", + "dev_cla": "restart", + "pl_prs": "{}" + } + } +} diff --git a/data/js/rest.js b/data/js/rest.js index fc599a3f..ea04e82b 100644 --- a/data/js/rest.js +++ b/data/js/rest.js @@ -229,10 +229,10 @@ pixelix.rest.Client.prototype.setSetting = function(key, value) { return promise; }; -pixelix.rest.Client.prototype.reset = function() { +pixelix.rest.Client.prototype.restart = function() { return utils.makeRequest({ - method: "GET", - url: "/rest/api/v1/reset", + method: "POST", + url: "/rest/api/v1/device/restart", isJsonResponse: true }); }; diff --git a/data/js/ws.js b/data/js/ws.js index 412d1c80..88baa7cf 100644 --- a/data/js/ws.js +++ b/data/js/ws.js @@ -289,13 +289,13 @@ pixelix.ws.Client.prototype.getSlots = function() { }.bind(this)); }; -pixelix.ws.Client.prototype.reset = function() { +pixelix.ws.Client.prototype.restart = function() { return new Promise(function(resolve, reject) { if (null === this._socket) { reject(); } else { this._sendCmd({ - name: "RESET", + name: "RESTART", par: null, resolve: resolve, reject: reject diff --git a/data/update.html b/data/update.html index 27e8ea2d..787d39ba 100644 --- a/data/update.html +++ b/data/update.html @@ -344,12 +344,12 @@

Update

progress = 90; updateProgressBar("progressBarRestore", progress); }).then(function() { - return restClient.reset(); + return restClient.restart(); }).then(function() { progress = 100; updateProgressBar("progressBarRestore", progress); - return dialog.showInfo("

Reset performed to apply restored configuration.

"); + return dialog.showInfo("

Restart performed to apply restored configuration.

"); }).catch(function() { dialog.showError("

Restore failed.

"); }); diff --git a/doc/WEBSOCKET.md b/doc/WEBSOCKET.md index a90f394a..5300b4bc 100644 --- a/doc/WEBSOCKET.md +++ b/doc/WEBSOCKET.md @@ -7,7 +7,7 @@ * [Get display pixel colors](#get-display-pixel-colors) * [Get slots information](#get-slots-information) -* [Reset](#reset) +* [Restart](#restart) * [Brightness](#brightness) * [Get brightness information](#get-brightness-information) * [Set brightness](#set-brightness) @@ -62,8 +62,8 @@ Response: * Failed: * ```NACK``` -# Reset -Command: ```RESET``` +# Restart +Command: ```RESTART``` Parameter: * N/A diff --git a/lib/TopicHandlerService/src/TopicHandlerService.cpp b/lib/TopicHandlerService/src/TopicHandlerService.cpp index ad5ea61f..418ee40b 100644 --- a/lib/TopicHandlerService/src/TopicHandlerService.cpp +++ b/lib/TopicHandlerService/src/TopicHandlerService.cpp @@ -259,7 +259,7 @@ void TopicHandlerService::registerTopic(const String& deviceId, const String& en isReadAccess = true; } - if ((nullptr != setTopicFunc) && + if ((nullptr != setTopicFunc) || (nullptr != uploadReqFunc)) { isWriteAccess = true; diff --git a/src/General/MiniTerminal.cpp b/src/General/MiniTerminal.cpp index a5e5d14a..e7f475c1 100644 --- a/src/General/MiniTerminal.cpp +++ b/src/General/MiniTerminal.cpp @@ -61,31 +61,31 @@ * Local Variables *****************************************************************************/ -/** Command: reset */ -static const char RESET[] = "reset"; +/** Command: restart */ +static const char RESTART[] = "restart"; /** Command: write wifi passphrase */ -static const char WRITE_WIFI_PASSPHRASE[] = "write wifi passphrase "; +static const char WRITE_WIFI_PASSPHRASE[] = "write wifi passphrase "; /** Command: write wifi ssid */ -static const char WRITE_WIFI_SSID[] = "write wifi ssid "; +static const char WRITE_WIFI_SSID[] = "write wifi ssid "; /** Command: get ip */ -static const char GET_IP[] = "get ip"; +static const char GET_IP[] = "get ip"; /** Command: status */ -static const char GET_STATUS[] = "get status"; +static const char GET_STATUS[] = "get status"; /** Command: help */ -static const char HELP[] = "help"; +static const char HELP[] = "help"; const MiniTerminal::CmdTableEntry MiniTerminal::m_cmdTable[] = { - { RESET, &MiniTerminal::cmdReset }, - { WRITE_WIFI_PASSPHRASE, &MiniTerminal::cmdWriteWifiPassphrase }, - { WRITE_WIFI_SSID, &MiniTerminal::cmdWriteWifiSSID }, - { GET_IP, &MiniTerminal::cmdGetIPAddress }, - { GET_STATUS, &MiniTerminal::cmdGetStatus }, - { HELP, &MiniTerminal::cmdHelp }, + { RESTART, &MiniTerminal::cmdRestart }, + { WRITE_WIFI_PASSPHRASE, &MiniTerminal::cmdWriteWifiPassphrase }, + { WRITE_WIFI_SSID, &MiniTerminal::cmdWriteWifiSSID }, + { GET_IP, &MiniTerminal::cmdGetIPAddress }, + { GET_STATUS, &MiniTerminal::cmdGetStatus }, + { HELP, &MiniTerminal::cmdHelp }, }; /****************************************************************************** @@ -94,12 +94,12 @@ const MiniTerminal::CmdTableEntry MiniTerminal::m_cmdTable[] = { void MiniTerminal::process() { - char buffer[LOCAL_BUFFER_SIZE]; - size_t read = m_stream.readBytes(buffer, LOCAL_BUFFER_SIZE); - size_t idx = 0U; + char buffer[LOCAL_BUFFER_SIZE]; + size_t read = m_stream.readBytes(buffer, LOCAL_BUFFER_SIZE); + size_t idx = 0U; /* Process the read input data. */ - while(read > idx) + while (read > idx) { char currentChar = buffer[idx]; @@ -107,8 +107,8 @@ void MiniTerminal::process() if (ASCII_LF == currentChar) { /* Don't echo mechanism, because its too late in case the - * command may write a result too. - */ + * command may write a result too. + */ (void)m_stream.write(currentChar); m_input[m_writeIndex] = '\0'; @@ -118,9 +118,9 @@ void MiniTerminal::process() { executeCommand(m_input); } - + /* Reset command line buffer */ - m_writeIndex = 0U; + m_writeIndex = 0U; m_input[m_writeIndex] = '\0'; } /* Remove the last character from command line? */ @@ -129,8 +129,7 @@ void MiniTerminal::process() { if (0 < m_writeIndex) { - static const char removeSeq[] = - { + static const char removeSeq[] = { ASCII_BS, ASCII_SP, ASCII_BS @@ -192,7 +191,7 @@ void MiniTerminal::executeCommand(const char* cmdLine) for (idx = 0U; UTIL_ARRAY_NUM(m_cmdTable) > idx; ++idx) { const CmdTableEntry entry = m_cmdTable[idx]; - const size_t len = strlen(entry.cmdStr); + const size_t len = strlen(entry.cmdStr); if (0 == strncmp(cmdLine, entry.cmdStr, len)) { @@ -207,7 +206,7 @@ void MiniTerminal::executeCommand(const char* cmdLine) } } -void MiniTerminal::cmdReset(const char* par) +void MiniTerminal::cmdRestart(const char* par) { UTIL_NOT_USED(par); m_isRestartRequested = true; @@ -282,7 +281,7 @@ void MiniTerminal::cmdGetStatus(const char* par) { UTIL_NOT_USED(par); - ErrorState::ErrorId status = ErrorState::getInstance().getErrorId(); + ErrorState::ErrorId status = ErrorState::getInstance().getErrorId(); String result; result += static_cast(status); diff --git a/src/General/MiniTerminal.h b/src/General/MiniTerminal.h index e0d6021f..e3537e88 100644 --- a/src/General/MiniTerminal.h +++ b/src/General/MiniTerminal.h @@ -154,11 +154,11 @@ class MiniTerminal void executeCommand(const char* cmdLine); /** - * Reset the device. + * Restart the device. * * @param[in] par Parameter */ - void cmdReset(const char* par); + void cmdRestart(const char* par); /** * Write wifi passphrase. diff --git a/src/Topics/Topics.cpp b/src/Topics/Topics.cpp index c9c1c7bf..0eec3c3e 100644 --- a/src/Topics/Topics.cpp +++ b/src/Topics/Topics.cpp @@ -37,6 +37,7 @@ #include #include #include "DisplayMgr.h" +#include "UpdateMgr.h" /****************************************************************************** * Compiler Switches @@ -69,6 +70,7 @@ typedef struct static bool getDisplayState(const String& topic, JsonObject& value); static bool hasDisplayStateChanged(const String& topic); static bool setDisplayState(const String& topic, const JsonObjectConst& value); +static bool restart(const String& topic, const JsonObjectConst& value); /****************************************************************************** * Local Variables @@ -83,7 +85,8 @@ static String gDeviceId; * List of topics. */ static TopicElem gTopicList[] = { - { "display", "/power", getDisplayState, hasDisplayStateChanged, setDisplayState, "/extra/display.json" } + { "display", "/power", getDisplayState, hasDisplayStateChanged, setDisplayState, "/extra/display.json" }, + { "device", "/restart", nullptr, nullptr, restart, "/extra/restart.json" } }; /** @@ -263,3 +266,26 @@ static bool setDisplayState(const String& topic, const JsonObjectConst& value) return isSuccessful; } + +/** + * Restart the device. + * + * @param[in] topic Topic + * @param[in] value Value + * + * @return If successful, it will return true otherwise false. + */ +static bool restart(const String& topic, const JsonObjectConst& value) +{ + const uint32_t RESTART_DELAY = 100U; /* ms */ + + UTIL_NOT_USED(topic); + UTIL_NOT_USED(value); + + /* To ensure that a positive response will be sent before the device restarts, + * a short delay is necessary. + */ + UpdateMgr::getInstance().reqRestart(RESTART_DELAY); + + return true; +} diff --git a/src/Web/RestApi.cpp b/src/Web/RestApi.cpp index 6dd83feb..e3826f85 100644 --- a/src/Web/RestApi.cpp +++ b/src/Web/RestApi.cpp @@ -43,7 +43,6 @@ #include "RestUtil.h" #include "SlotList.h" #include "ButtonActions.h" -#include "UpdateMgr.h" #include #include @@ -120,7 +119,6 @@ static void handleSlot(AsyncWebServerRequest* request); static void handlePluginInstall(AsyncWebServerRequest* request); static void handlePluginUninstall(AsyncWebServerRequest* request); static void handlePlugins(AsyncWebServerRequest* request); -static void handleReset(AsyncWebServerRequest* request); static void handleSensors(AsyncWebServerRequest* request); static void handleSettings(AsyncWebServerRequest* request); static void handleSetting(AsyncWebServerRequest* request); @@ -178,7 +176,6 @@ void RestApi::init(AsyncWebServer& srv) (void)srv.on("/rest/api/v1/plugin/install", handlePluginInstall); (void)srv.on("/rest/api/v1/plugin/uninstall", handlePluginUninstall); (void)srv.on("/rest/api/v1/plugins", handlePlugins); - (void)srv.on("/rest/api/v1/reset", handleReset); (void)srv.on("/rest/api/v1/sensors", handleSensors); (void)srv.on("/rest/api/v1/settings", handleSettings); (void)srv.on("/rest/api/v1/setting", handleSetting); @@ -780,41 +777,6 @@ static void handlePlugins(AsyncWebServerRequest* request) RestUtil::sendJsonRsp(request, jsonDoc, httpStatusCode); } -/** - * Perform a reset request. - * GET \c "/api/v1/reset" - * - * @param[in] request HTTP request - */ -static void handleReset(AsyncWebServerRequest* request) -{ - const size_t JSON_DOC_SIZE = 512U; - DynamicJsonDocument jsonDoc(JSON_DOC_SIZE); - uint32_t httpStatusCode = HttpStatus::STATUS_CODE_OK; - - if (nullptr == request) - { - return; - } - - if (HTTP_GET != request->method()) - { - RestUtil::prepareRspErrorHttpMethodNotSupported(jsonDoc); - httpStatusCode = HttpStatus::STATUS_CODE_NOT_FOUND; - } - else - { - /* To ensure the positive response will be sent. */ - const uint32_t RESTART_DELAY = 100U; /* ms */ - - (void)RestUtil::prepareRspSuccess(jsonDoc); - - UpdateMgr::getInstance().reqRestart(RESTART_DELAY); - } - - RestUtil::sendJsonRsp(request, jsonDoc, httpStatusCode); -} - /** * List all sensors. * GET \c "/api/v1/sensors" diff --git a/src/Web/WebSocket.cpp b/src/Web/WebSocket.cpp index 8b81213f..aad7a0e0 100644 --- a/src/Web/WebSocket.cpp +++ b/src/Web/WebSocket.cpp @@ -44,7 +44,7 @@ #include "WsCmdLog.h" #include "WsCmdMove.h" #include "WsCmdPlugins.h" -#include "WsCmdReset.h" +#include "WsCmdRestart.h" #include "WsCmdSlotDuration.h" #include "WsCmdSlot.h" #include "WsCmdSlots.h" @@ -93,7 +93,7 @@ static WsCmdInstall gWsCmdInstall; static WsCmdUninstall gWsCmdUninstall; /** Websocket reset command */ -static WsCmdReset gWsCmdReset; +static WsCmdRestart gWsCmdRestart; /** Websocket get/set brightness command */ static WsCmdBrightness gWsCmdBrightness; @@ -132,7 +132,7 @@ static WsCmd* gWsCommands[] = &gWsCmdPlugins, &gWsCmdInstall, &gWsCmdUninstall, - &gWsCmdReset, + &gWsCmdRestart, &gWsCmdBrightness, &gWsCmdLog, &gWsCmdMove, diff --git a/src/Web/WsCommand/WsCmdReset.cpp b/src/Web/WsCommand/WsCmdRestart.cpp similarity index 92% rename from src/Web/WsCommand/WsCmdReset.cpp rename to src/Web/WsCommand/WsCmdRestart.cpp index a5f2bd20..792601a4 100644 --- a/src/Web/WsCommand/WsCmdReset.cpp +++ b/src/Web/WsCommand/WsCmdRestart.cpp @@ -25,14 +25,14 @@ DESCRIPTION *******************************************************************************/ /** - * @brief Websocket command to reset system + * @brief Websocket command to restart the system * @author Andreas Merkle */ /****************************************************************************** * Includes *****************************************************************************/ -#include "WsCmdReset.h" +#include "WsCmdRestart.h" #include "UpdateMgr.h" #include @@ -61,7 +61,7 @@ * Public Methods *****************************************************************************/ -void WsCmdReset::execute(AsyncWebSocket* server, uint32_t clientId) +void WsCmdRestart::execute(AsyncWebSocket* server, uint32_t clientId) { if (nullptr == server) { @@ -86,7 +86,7 @@ void WsCmdReset::execute(AsyncWebSocket* server, uint32_t clientId) m_isError = false; } -void WsCmdReset::setPar(const char* par) +void WsCmdRestart::setPar(const char* par) { UTIL_NOT_USED(par); diff --git a/src/Web/WsCommand/WsCmdReset.h b/src/Web/WsCommand/WsCmdRestart.h similarity index 86% rename from src/Web/WsCommand/WsCmdReset.h rename to src/Web/WsCommand/WsCmdRestart.h index 66d5b00e..2f2a3d8e 100644 --- a/src/Web/WsCommand/WsCmdReset.h +++ b/src/Web/WsCommand/WsCmdRestart.h @@ -25,7 +25,7 @@ DESCRIPTION *******************************************************************************/ /** - * @brief Websocket command to reset system + * @brief Websocket command to restart the system * @author Andreas Merkle * * @addtogroup WEB @@ -33,8 +33,8 @@ * @{ */ -#ifndef WSCMDRESET_H -#define WSCMDRESET_H +#ifndef WS_CMD_RESTART_H +#define WS_CMD_RESTART_H /****************************************************************************** * Compile Switches @@ -54,17 +54,17 @@ *****************************************************************************/ /** - * Websocket command to reset system + * Websocket command to restart system */ -class WsCmdReset: public WsCmd +class WsCmdRestart: public WsCmd { public: /** * Constructs the websocket command. */ - WsCmdReset() : - WsCmd("RESET"), + WsCmdRestart() : + WsCmd("RESTART"), m_isError(false) { } @@ -72,7 +72,7 @@ class WsCmdReset: public WsCmd /** * Destroys websocket command. */ - ~WsCmdReset() + ~WsCmdRestart() { } @@ -95,14 +95,14 @@ class WsCmdReset: public WsCmd bool m_isError; /**< Any error happened during parameter reception? */ - WsCmdReset(const WsCmdReset& cmd); - WsCmdReset& operator=(const WsCmdReset& cmd); + WsCmdRestart(const WsCmdRestart& cmd); + WsCmdRestart& operator=(const WsCmdRestart& cmd); }; /****************************************************************************** * Functions *****************************************************************************/ -#endif /* WSCMDRESET_H */ +#endif /* WS_CMD_RESTART_H */ /** @} */ \ No newline at end of file