From a964d84c42b648b1277665a392af9be3eed5aeac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Mon, 9 Oct 2017 18:41:02 +0200 Subject: [PATCH] Check relayParsePayload output --- code/espurna/relay.ino | 21 +++++++++++++++------ code/espurna/web.ino | 12 ++++++++---- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 7f476572..c1c54a29 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -343,6 +343,7 @@ unsigned char relayParsePayload(const char * payload) { if (0 <= value && value <=3) return value; return 0xFF; + } //------------------------------------------------------------------------------ // REST API @@ -366,16 +367,22 @@ void relaySetupAPI() { snprintf_P(buffer, len, PSTR("%d"), relayStatus(relayID) ? 1 : 0); }, [relayID](const char * payload) { + unsigned char value = relayParsePayload(payload); + if (value == 0xFF) { DEBUG_MSG_P(PSTR("[RELAY] Wrong payload (%s)\n"), payload); return; } - if (value == 2) { + + if (value == 0) { + relayStatus(relayID, false); + } elseif (value == 1) { + relayStatus(relayID, true); + } elseif (value == 2) { relayToggle(relayID); - } else { - relayStatus(relayID, value == 1); } + } ); @@ -461,10 +468,12 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo } // Action to perform - if (value == 2) { + if (value == 0) { + relayStatus(relayID, false, mqttForward()); + } elseif (value == 1) { + relayStatus(relayID, true, mqttForward()); + } elseif (value == 2) { relayToggle(relayID); - } else { - relayStatus(relayID, value == 1, mqttForward()); } } diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 34a2e60c..4d3d2ba3 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -143,11 +143,12 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { if (data.containsKey("status")) { unsigned char value = relayParsePayload(data["status"]); + if (value == 3) { relayWS(); - } else { + } elseif (value < 3) { unsigned int relayID = 0; if (data.containsKey("id")) { @@ -155,10 +156,13 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { relayID = value.toInt(); } - if (value == 2) { + // Action to perform + if (value == 0) { + relayStatus(relayID, false); + } elseif (value == 1) { + relayStatus(relayID, true); + } elseif (value == 2) { relayToggle(relayID); - } else { - relayStatus(relayID, value == 1); } }