diff --git a/code/espurna/api.ino b/code/espurna/api.ino index 309fcba4..e60dd701 100644 --- a/code/espurna/api.ino +++ b/code/espurna/api.ino @@ -11,6 +11,7 @@ Copyright (C) 2016-2019 by Xose PĂ©rez #include "api.h" #include "system.h" #include "web.h" +#include "rpc.h" #include "ws.h" typedef struct { @@ -150,12 +151,12 @@ void _onRPC(AsyncWebServerRequest *request) { if (request->hasParam("action")) { AsyncWebParameter* p = request->getParam("action"); - String action = p->value(); + + const auto action = p->value(); DEBUG_MSG_P(PSTR("[RPC] Action: %s\n"), action.c_str()); - if (action.equals("reboot")) { - response = 200; - deferredReset(100, CUSTOM_RESET_RPC); + if (rpcHandleAction(action)) { + response = 204; } } diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index e89e68ef..f103b98b 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -52,6 +52,7 @@ along with this program. If not, see . #include "ota.h" #include "relay.h" #include "rfm69.h" +#include "rpc.h" #include "rpnrules.h" #include "rtcmem.h" #include "thermostat.h" diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 3a210ab3..06c79f67 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -18,6 +18,7 @@ Updated secure client support by Niek van der Maas < mail at niekvandermaas dot #include "system.h" #include "mqtt.h" #include "ntp.h" +#include "rpc.h" #include "ws.h" #include "libs/SecureClientHelpers.h" @@ -523,9 +524,7 @@ void _mqttCallback(unsigned int type, const char * topic, const char * payload) // Actions if (t.equals(MQTT_TOPIC_ACTION)) { - if (strcmp(payload, MQTT_ACTION_RESET) == 0) { - deferredReset(100, CUSTOM_RESET_MQTT); - } + rpcHandleAction(payload); } } diff --git a/code/espurna/rpc.h b/code/espurna/rpc.h new file mode 100644 index 00000000..e93294da --- /dev/null +++ b/code/espurna/rpc.h @@ -0,0 +1,9 @@ +/* + +Part of MQTT and API modules + +*/ + +#pragma once + +bool rpcHandleAction(const String& action); diff --git a/code/espurna/rpc.ino b/code/espurna/rpc.ino new file mode 100644 index 00000000..a6ba5ed8 --- /dev/null +++ b/code/espurna/rpc.ino @@ -0,0 +1,29 @@ +/* + +Part of MQTT and API modules + +*/ + +#if MQTT_SUPPORT || API_SUPPORT + +#include + +#include "system.h" +#include "utils.h" +#include "rpc.h" + +bool rpcHandleAction(const String& action) { + bool result = false; + if (action.equals("reboot")) { + result = true; + schedule_function([]() { + deferredReset(100, CUSTOM_RESET_RPC); + }); + } else if (action.equals("heartbeat")) { + result = true; + schedule_function(heartbeat); + } + return result; +} + +#endif // MQTT_SUPPORT || API_SUPPORT