From 90617963e3960a8f17919a7d5e9bb7040712e00f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 25 Aug 2017 12:39:37 +0200 Subject: [PATCH] Method to send websocket messages from progmem --- code/espurna/ntp.ino | 4 ++-- code/espurna/ota.ino | 4 ++-- code/espurna/relay.ino | 2 +- code/espurna/rfbridge.ino | 2 +- code/espurna/web.ino | 38 ++++++++++++++++++++++++++------------ 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/code/espurna/ntp.ino b/code/espurna/ntp.ino index 02a6520f..f260f542 100644 --- a/code/espurna/ntp.ino +++ b/code/espurna/ntp.ino @@ -52,12 +52,12 @@ void ntpSetup() { DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n")); } #if WEB_SUPPORT - wsSend("{\"ntpStatus\": false}"); + wsSend_P(PSTR("{\"ntpStatus\": false}")); #endif } else { DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str()); #if WEB_SUPPORT - wsSend("{\"ntpStatus\": true}"); + wsSend_P(PSTR("{\"ntpStatus\": true}")); #endif } }); diff --git a/code/espurna/ota.ino b/code/espurna/ota.ino index 2b5724ad..17116b0e 100644 --- a/code/espurna/ota.ino +++ b/code/espurna/ota.ino @@ -25,7 +25,7 @@ void otaSetup() { ArduinoOTA.onStart([]() { DEBUG_MSG_P(PSTR("[OTA] Start\n")); #if WEB_SUPPORT - wsSend("{\"message\": \"OTA update started\"}"); + wsSend_P(PSTR("{\"message\": \"OTA update started\"}")); #endif }); @@ -33,7 +33,7 @@ void otaSetup() { customReset(CUSTOM_RESET_OTA); DEBUG_MSG_P(PSTR("\n[OTA] End\n")); #if WEB_SUPPORT - wsSend("{\"action\": \"reload\"}"); + wsSend_P(PSTR("{\"action\": \"reload\"}")); #endif delay(100); }); diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 687bbdcf..27572891 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -153,7 +153,7 @@ void relayPulseMode(unsigned int value, bool report) { #if WEB_SUPPORT char message[20]; - sprintf_P(message, PSTR("{\"relayPulseMode\": %d}"), value); + snprintf_P(message, strlen(message), PSTR("{\"relayPulseMode\": %d}"), value); wsSend(message); #endif diff --git a/code/espurna/rfbridge.ino b/code/espurna/rfbridge.ino index beb572fe..83b9fde4 100644 --- a/code/espurna/rfbridge.ino +++ b/code/espurna/rfbridge.ino @@ -102,7 +102,7 @@ void _rfbDecode() { _rfbAck(); DEBUG_MSG_P(PSTR("[RFBRIDGE] Learn timeout\n")); #if WEB_SUPPORT - wsSend("{\"action\": \"rfbTimeout\"}"); + wsSend_P(PSTR("{\"action\": \"rfbTimeout\"}")); #endif } diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 7d457dc4..b5004458 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -52,11 +52,11 @@ std::vector _apis; void _wsMQTTCallback(unsigned int type, const char * topic, const char * payload) { if (type == MQTT_CONNECT_EVENT) { - wsSend("{\"mqttStatus\": true}"); + wsSend_P(PSTR("{\"mqttStatus\": true}")); } if (type == MQTT_DISCONNECT_EVENT) { - wsSend("{\"mqttStatus\": false}"); + wsSend_P(PSTR("{\"mqttStatus\": false}")); } } @@ -68,7 +68,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { JsonObject& root = jsonBuffer.parseObject((char *) payload); if (!root.success()) { DEBUG_MSG_P(PSTR("[WEBSOCKET] Error parsing data\n")); - wsSend(client_id, "{\"message\": \"Error parsing data!\"}"); + wsSend_P(client_id, PSTR("{\"message\": \"Error parsing data!\"}")); return; } @@ -103,7 +103,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { JsonObject& data = root["data"]; if (!data.containsKey("app") || (data["app"] != APP_NAME)) { - wsSend(client_id, "{\"message\": \"The file does not look like a valid configuration backup.\"}"); + wsSend_P(client_id, PSTR("{\"message\": \"The file does not look like a valid configuration backup.\"}")); return; } @@ -119,7 +119,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { saveSettings(); - wsSend(client_id, "{\"message\": \"Changes saved. You should reboot your board now.\"}"); + wsSend_P(client_id, PSTR("{\"message\": \"Changes saved. You should reboot your board now.\"}")); } @@ -264,11 +264,11 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { } if (key == "adminPass2") { if (!value.equals(adminPass)) { - wsSend(client_id, "{\"message\": \"Passwords do not match!\"}"); + wsSend_P(client_id, PSTR("{\"message\": \"Passwords do not match!\"}")); return; } if (value.length() == 0) continue; - wsSend(client_id, "{\"action\": \"reload\"}"); + wsSend_P(client_id, PSTR("{\"action\": \"reload\"}")); key = String("adminPass"); } @@ -371,9 +371,9 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { } if (changed) { - wsSend(client_id, "{\"message\": \"Changes saved\"}"); + wsSend_P(client_id, PSTR("{\"message\": \"Changes saved\"}")); } else { - wsSend(client_id, "{\"message\": \"No changes detected\"}"); + wsSend_P(client_id, PSTR("{\"message\": \"No changes detected\"}")); } } @@ -608,7 +608,7 @@ bool _wsAuth(AsyncWebSocketClient * client) { if (index == WS_BUFFER_SIZE) { DEBUG_MSG_P(PSTR("[WEBSOCKET] Validation check failed\n")); - wsSend(client->id(), "{\"message\": \"Session expired, please reload page...\"}"); + wsSend_P(client->id(), PSTR("{\"message\": \"Session expired, please reload page...\"}")); return false; } @@ -663,16 +663,30 @@ bool wsConnected() { return (_ws.count() > 0); } -bool wsSend(const char * payload) { +void wsSend(const char * payload) { if (_ws.count() > 0) { _ws.textAll(payload); } } -bool wsSend(uint32_t client_id, const char * payload) { +void wsSend_P(PGM_P payload) { + if (_ws.count() > 0) { + char buffer[strlen_P(payload)]; + strcpy_P(buffer, payload); + _ws.textAll(buffer); + } +} + +void wsSend(uint32_t client_id, const char * payload) { _ws.text(client_id, payload); } +void wsSend_P(uint32_t client_id, PGM_P payload) { + char buffer[strlen_P(payload)]; + strcpy_P(buffer, payload); + _ws.text(client_id, buffer); +} + void wsSetup() { _ws.onEvent(_wsEvent); mqttRegister(_wsMQTTCallback);