Browse Source

Check relayParsePayload output

fastled
Xose Pérez 7 years ago
parent
commit
a964d84c42
2 changed files with 23 additions and 10 deletions
  1. +15
    -6
      code/espurna/relay.ino
  2. +8
    -4
      code/espurna/web.ino

+ 15
- 6
code/espurna/relay.ino View File

@ -343,6 +343,7 @@ unsigned char relayParsePayload(const char * payload) {
if (0 <= value && value <=3) return value; if (0 <= value && value <=3) return value;
return 0xFF; return 0xFF;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// REST API // REST API
@ -366,16 +367,22 @@ void relaySetupAPI() {
snprintf_P(buffer, len, PSTR("%d"), relayStatus(relayID) ? 1 : 0); snprintf_P(buffer, len, PSTR("%d"), relayStatus(relayID) ? 1 : 0);
}, },
[relayID](const char * payload) { [relayID](const char * payload) {
unsigned char value = relayParsePayload(payload); unsigned char value = relayParsePayload(payload);
if (value == 0xFF) { if (value == 0xFF) {
DEBUG_MSG_P(PSTR("[RELAY] Wrong payload (%s)\n"), payload); DEBUG_MSG_P(PSTR("[RELAY] Wrong payload (%s)\n"), payload);
return; return;
} }
if (value == 2) {
if (value == 0) {
relayStatus(relayID, false);
} elseif (value == 1) {
relayStatus(relayID, true);
} elseif (value == 2) {
relayToggle(relayID); 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 // 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); relayToggle(relayID);
} else {
relayStatus(relayID, value == 1, mqttForward());
} }
} }


+ 8
- 4
code/espurna/web.ino View File

@ -143,11 +143,12 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
if (data.containsKey("status")) { if (data.containsKey("status")) {
unsigned char value = relayParsePayload(data["status"]); unsigned char value = relayParsePayload(data["status"]);
if (value == 3) { if (value == 3) {
relayWS(); relayWS();
} else {
} elseif (value < 3) {
unsigned int relayID = 0; unsigned int relayID = 0;
if (data.containsKey("id")) { if (data.containsKey("id")) {
@ -155,10 +156,13 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
relayID = value.toInt(); 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); relayToggle(relayID);
} else {
relayStatus(relayID, value == 1);
} }
} }


Loading…
Cancel
Save