From ff75e5c400713209a5a7353d49dbe494f049eb65 Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Sun, 4 Aug 2019 13:41:05 +0300 Subject: [PATCH] Save a bit by using a single callback vector --- code/espurna/alexa.ino | 6 ++-- code/espurna/api.ino | 6 ++-- code/espurna/button.ino | 2 +- code/espurna/config/prototypes.h | 13 ++++---- code/espurna/debug.ino | 6 ++-- code/espurna/domoticz.ino | 6 ++-- code/espurna/homeassistant.ino | 8 +++-- code/espurna/influxdb.ino | 8 ++--- code/espurna/led.ino | 4 +-- code/espurna/light.ino | 8 ++--- code/espurna/lightfox.ino | 4 +-- code/espurna/mqtt.ino | 3 +- code/espurna/nofuss.ino | 8 ++--- code/espurna/ntp.ino | 3 +- code/espurna/relay.ino | 8 +++-- code/espurna/rfbridge.ino | 8 ++--- code/espurna/rfm69.ino | 8 ++--- code/espurna/scheduler.ino | 3 +- code/espurna/sensor.ino | 28 +++++++++++++----- code/espurna/system.ino | 2 +- code/espurna/telnet.ino | 3 +- code/espurna/thermostat.ino | 8 ++--- code/espurna/thinkspeak.ino | 3 +- code/espurna/wifi.ino | 4 +-- code/espurna/ws.ino | 51 +++++++++++++++++--------------- 25 files changed, 117 insertions(+), 94 deletions(-) diff --git a/code/espurna/alexa.ino b/code/espurna/alexa.ino index 78d2c46bfd..71b51571b5 100644 --- a/code/espurna/alexa.ino +++ b/code/espurna/alexa.ino @@ -123,8 +123,10 @@ void alexaSetup() { #if WEB_SUPPORT webBodyRegister(_alexaBodyCallback); webRequestRegister(_alexaRequestCallback); - wsOnConnectedRegister(_alexaWebSocketOnConnected); - wsOnKeyCheckRegister(_alexaWebSocketOnKeyCheck); + ws_callbacks_t callbacks; + callbacks.on_connected = _alexaWebSocketOnConnected; + callbacks.on_keycheck = _alexaWebSocketOnKeyCheck; + wsRegister(callbacks); #endif // Register wifi callback diff --git a/code/espurna/api.ino b/code/espurna/api.ino index 19c48264d3..3fe0b5f6b4 100644 --- a/code/espurna/api.ino +++ b/code/espurna/api.ino @@ -242,8 +242,10 @@ void apiRegister(const char * key, api_get_callback_f getFn, api_put_callback_f void apiSetup() { _apiConfigure(); - wsOnConnectedRegister(_apiWebSocketOnConnected); - wsOnKeyCheckRegister(_apiWebSocketOnKeyCheck); + ws_callbacks_t callbacks; + callbacks.on_connected = _apiWebSocketOnConnected; + callbacks.on_keycheck = _apiWebSocketOnKeyCheck; + wsRegister(callbacks); webRequestRegister(_apiRequestCallback); espurnaRegisterReload(_apiConfigure); } diff --git a/code/espurna/button.ino b/code/espurna/button.ino index dc981b0364..35cf528506 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -243,7 +243,7 @@ void buttonSetup() { // Websocket Callbacks #if WEB_SUPPORT - wsOnKeyCheckRegister(_buttonWebSocketOnKeyCheck); + wsRegister({ nullptr, nullptr, _buttonWebSocketOnKeyCheck }); #endif // Register loop diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index ff03d59a43..1455207a4e 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -212,15 +212,18 @@ void webRequestRegister(web_request_callback_f callback); using ws_on_action_callback_f = std::function; using ws_on_keycheck_callback_f = std::function; - void wsOnConnectedRegister(ws_on_send_callback_f callback); + struct ws_callbacks_t { + ws_on_send_callback_f on_connected; + ws_on_action_callback_f on_action; + ws_on_keycheck_callback_f on_keycheck; + }; + void wsRegister(const ws_callbacks_t&); + void wsRegister(const ws_callbacks_t&); + void wsSend(uint32_t, JsonObject& root); void wsSend(JsonObject& root); void wsSend(ws_on_send_callback_f sender); - - void wsOnActionRegister(ws_on_action_callback_f callback); - void wsOnKeyCheckRegister(ws_on_keycheck_callback_f callback); - bool wsConnected(); bool wsConnected(uint32_t); bool wsDebugSend(const char*, const char*); diff --git a/code/espurna/debug.ino b/code/espurna/debug.ino index cb86ff1054..9604fae55e 100644 --- a/code/espurna/debug.ino +++ b/code/espurna/debug.ino @@ -110,8 +110,10 @@ void _debugWebSocketOnAction(uint32_t client_id, const char * action, JsonObject void debugWebSetup() { - wsOnConnectedRegister(_debugWebSocketOnConnected); - wsOnActionRegister(_debugWebSocketOnAction); + ws_callbacks_t callbacks; + callbacks.on_connected = _debugWebSocketOnConnected; + callbacks.on_action = _debugWebSocketOnAction; + wsRegister(callbacks); #if DEBUG_UDP_SUPPORT #if DEBUG_UDP_PORT == 514 diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index 1e57b26424..184ab7777a 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -248,8 +248,10 @@ void domoticzSetup() { _domoticzConfigure(); #if WEB_SUPPORT - wsOnConnectedRegister(_domoticzWebSocketOnConnected); - wsOnKeyCheckRegister(_domoticzWebSocketOnKeyCheck); + ws_callbacks_t callbacks; + callbacks.on_connected = _domoticzWebSocketOnConnected; + callbacks.on_keycheck = _domoticzWebSocketOnKeyCheck; + wsRegister(callbacks); #endif #if BROKER_SUPPORT diff --git a/code/espurna/homeassistant.ino b/code/espurna/homeassistant.ino index 7461d56494..8c6cb62363 100644 --- a/code/espurna/homeassistant.ino +++ b/code/espurna/homeassistant.ino @@ -375,9 +375,11 @@ void haSetup() { _haConfigure(); #if WEB_SUPPORT - wsOnConnectedRegister(_haWebSocketOnConnected); - wsOnActionRegister(_haWebSocketOnAction); - wsOnKeyCheckRegister(_haWebSocketOnKeyCheck); + ws_callbacks_t callbacks; + callbacks.on_connected = _haWebSocketOnConnected; + callbacks.on_action = _haWebSocketOnAction; + callbacks.on_keycheck = _haWebSocketOnKeyCheck; + wsRegister(callbacks); espurnaRegisterLoop(_haLoop); #endif diff --git a/code/espurna/influxdb.ino b/code/espurna/influxdb.ino index 5a3b14222d..cde010cf45 100644 --- a/code/espurna/influxdb.ino +++ b/code/espurna/influxdb.ino @@ -17,11 +17,11 @@ SyncClientWrap * _idb_client; // ----------------------------------------------------------------------------- -bool _idbWebSocketOnReceive(const char * key, JsonVariant& value) { +bool _idbWebSocketOnKeyCheck(const char * key, JsonVariant& value) { return (strncmp(key, "idb", 3) == 0); } -void _idbWebSocketOnSend(JsonObject& root) { +void _idbWebSocketOnConnected(JsonObject& root) { root["idbVisible"] = 1; root["idbEnabled"] = getSetting("idbEnabled", INFLUXDB_ENABLED).toInt() == 1; root["idbHost"] = getSetting("idbHost", INFLUXDB_HOST); @@ -118,8 +118,8 @@ void idbSetup() { _idbConfigure(); #if WEB_SUPPORT - wsOnSendRegister(_idbWebSocketOnSend); - wsOnReceiveRegister(_idbWebSocketOnReceive); + wsOnConnectedRegister(_idbWebSocketOnConnected); + wsOnKeyCheckRegister(_idbWebSocketOnKeyCheck); #endif #if BROKER_SUPPORT diff --git a/code/espurna/led.ino b/code/espurna/led.ino index d8bc3c5015..445bc56b53 100644 --- a/code/espurna/led.ino +++ b/code/espurna/led.ino @@ -200,8 +200,8 @@ void ledSetup() { #endif #if WEB_SUPPORT - wsOnConnectedRegister(_ledWebSocketOnConnected); - wsOnKeyCheckRegister(_ledWebSocketOnKeyCheck); + ws_callbacks_t callbacks = { _ledWebSocketOnConnected, nullptr, _ledWebSocketOnKeyCheck }; + wsRegister(callbacks); #endif #if BROKER_SUPPORT diff --git a/code/espurna/light.ino b/code/espurna/light.ino index f3230951de..a8586cc437 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -921,7 +921,7 @@ void lightTransitionTime(unsigned long m) { #if WEB_SUPPORT -bool _lightWebSocketOnReceive(const char * key, JsonVariant& value) { +bool _lightWebSocketOnKeyCheck(const char * key, JsonVariant& value) { if (strncmp(key, "light", 5) == 0) return true; if (strncmp(key, "use", 3) == 0) return true; return false; @@ -946,7 +946,7 @@ void _lightWebSocketStatus(JsonObject& root) { root["brightness"] = lightBrightness(); } -void _lightWebSocketOnSend(JsonObject& root) { +void _lightWebSocketOnConnected(JsonObject& root) { root["colorVisible"] = 1; root["mqttGroupColor"] = getSetting("mqttGroupColor"); root["useColor"] = _light_has_color; @@ -1290,9 +1290,9 @@ void lightSetup() { } #if WEB_SUPPORT - wsOnSendRegister(_lightWebSocketOnSend); + wsOnConnectedRegister(_lightWebSocketOnConnected); wsOnActionRegister(_lightWebSocketOnAction); - wsOnReceiveRegister(_lightWebSocketOnReceive); + wsOnKeyCheckRegister(_lightWebSocketOnKeyCheck); #endif #if API_SUPPORT diff --git a/code/espurna/lightfox.ino b/code/espurna/lightfox.ino index 7814f1e811..b13c46c1ef 100644 --- a/code/espurna/lightfox.ino +++ b/code/espurna/lightfox.ino @@ -47,7 +47,7 @@ void lightfoxClear() { #if WEB_SUPPORT -void _lightfoxWebSocketOnSend(JsonObject& root) { +void _lightfoxWebSocketOnConnected(JsonObject& root) { root["lightfoxVisible"] = 1; uint8_t buttonsCount = _buttons.size(); root["lightfoxRelayCount"] = relayCount(); @@ -94,7 +94,7 @@ void _lightfoxInitCommands() { void lightfoxSetup() { #if WEB_SUPPORT - wsOnSendRegister(_lightfoxWebSocketOnSend); + wsOnConnectedRegister(_lightfoxWebSocketOnConnected); wsOnActionRegister(_lightfoxWebSocketOnAction); #endif diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index da15a18b3f..c84468df58 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -844,8 +844,7 @@ void mqttSetup() { mqttRegister(_mqttCallback); #if WEB_SUPPORT - wsOnConnectedRegister(_mqttWebSocketOnConnected); - wsOnKeyCheckRegister(_mqttWebSocketOnKeyCheck); + wsRegister((ws_callbacks_t) { _mqttWebSocketOnConnected, nullptr, _mqttWebSocketOnKeyCheck }); #endif #if TERMINAL_SUPPORT diff --git a/code/espurna/nofuss.ino b/code/espurna/nofuss.ino index aba702ded6..c47fa30821 100644 --- a/code/espurna/nofuss.ino +++ b/code/espurna/nofuss.ino @@ -20,11 +20,11 @@ bool _nofussEnabled = false; #if WEB_SUPPORT -bool _nofussWebSocketOnReceive(const char * key, JsonVariant& value) { +bool _nofussWebSocketOnKeyCheck(const char * key, JsonVariant& value) { return (strncmp(key, "nofuss", 6) == 0); } -void _nofussWebSocketOnSend(JsonObject& root) { +void _nofussWebSocketOnConnected(JsonObject& root) { root["nofussVisible"] = 1; root["nofussEnabled"] = getSetting("nofussEnabled", NOFUSS_ENABLED).toInt() == 1; root["nofussServer"] = getSetting("nofussServer", NOFUSS_SERVER); @@ -161,8 +161,8 @@ void nofussSetup() { }); #if WEB_SUPPORT - wsOnSendRegister(_nofussWebSocketOnSend); - wsOnReceiveRegister(_nofussWebSocketOnReceive); + wsOnConnectedRegister(_nofussWebSocketOnConnected); + wsOnKeyCheckRegister(_nofussWebSocketOnKeyCheck); #endif #if TERMINAL_SUPPORT diff --git a/code/espurna/ntp.ino b/code/espurna/ntp.ino index 6d88c93adf..49e200dcd4 100644 --- a/code/espurna/ntp.ino +++ b/code/espurna/ntp.ino @@ -255,8 +255,7 @@ void ntpSetup() { }); #if WEB_SUPPORT - wsOnConnectedRegister(_ntpWebSocketOnConnected); - wsOnKeyCheckRegister(_ntpWebSocketOnKeyCheck); + wsRegister((ws_callbacks_t) { _ntpWebSocketOnConnected, nullptr, _ntpWebSocketOnKeyCheck }); #endif // Main callbacks diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 7746bc9e0c..6fe96c8a66 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -727,9 +727,11 @@ void _relayWebSocketOnAction(uint32_t client_id, const char * action, JsonObject } void relaySetupWS() { - wsOnConnectedRegister(_relayWebSocketOnConnected); - wsOnActionRegister(_relayWebSocketOnAction); - wsOnKeyCheckRegister(_relayWebSocketOnKeyCheck); + ws_callbacks_t callbacks; + callbacks.on_connected = _relayWebSocketOnConnected; + callbacks.on_action = _relayWebSocketOnAction; + callbacks.on_keycheck = _relayWebSocketOnKeyCheck; + wsRegister(callbacks); } #endif // WEB_SUPPORT diff --git a/code/espurna/rfbridge.ino b/code/espurna/rfbridge.ino index 6f444aa61c..6d1bfcc273 100644 --- a/code/espurna/rfbridge.ino +++ b/code/espurna/rfbridge.ino @@ -118,7 +118,7 @@ void _rfbWebSocketSendCodes() { _rfbWebSocketSendCodeArray(0, relayCount()); } -void _rfbWebSocketOnSend(JsonObject& root) { +void _rfbWebSocketOnConnected(JsonObject& root) { root["rfbVisible"] = 1; root["rfbRepeat"] = getSetting("rfbRepeat", RF_SEND_TIMES).toInt(); root["rfbCount"] = relayCount(); @@ -136,7 +136,7 @@ void _rfbWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& if (strcmp(action, "rfbsend") == 0) rfbStore(data["id"], data["status"], data["data"].as()); } -bool _rfbWebSocketOnReceive(const char * key, JsonVariant& value) { +bool _rfbWebSocketOnKeyCheck(const char * key, JsonVariant& value) { return (strncmp(key, "rfb", 3) == 0); } @@ -768,9 +768,9 @@ void rfbSetup() { #endif #if WEB_SUPPORT - wsOnSendRegister(_rfbWebSocketOnSend); + wsOnConnectedRegister(_rfbWebSocketOnConnected); wsOnActionRegister(_rfbWebSocketOnAction); - wsOnReceiveRegister(_rfbWebSocketOnReceive); + wsOnKeyCheckRegister(_rfbWebSocketOnKeyCheck); #endif #if TERMINAL_SUPPORT diff --git a/code/espurna/rfm69.ino b/code/espurna/rfm69.ino index f24ca04cbe..2ec99746e5 100644 --- a/code/espurna/rfm69.ino +++ b/code/espurna/rfm69.ino @@ -35,7 +35,7 @@ unsigned long _rfm69_packet_count; #if WEB_SUPPORT -void _rfm69WebSocketOnSend(JsonObject& root) { +void _rfm69WebSocketOnConnected(JsonObject& root) { root["rfm69Visible"] = 1; root["rfm69Topic"] = getSetting("rfm69Topic", RFM69_DEFAULT_TOPIC); @@ -53,7 +53,7 @@ void _rfm69WebSocketOnSend(JsonObject& root) { } -bool _rfm69WebSocketOnReceive(const char * key, JsonVariant& value) { +bool _rfm69WebSocketOnKeyCheck(const char * key, JsonVariant& value) { if (strncmp(key, "rfm69", 5) == 0) return true; if (strncmp(key, "node", 4) == 0) return true; if (strncmp(key, "key", 3) == 0) return true; @@ -269,8 +269,8 @@ void rfm69Setup() { DEBUG_MSG_P(PSTR("[RFM69] Promiscuous mode %s\n"), RFM69_PROMISCUOUS ? "ON" : "OFF"); #if WEB_SUPPORT - wsOnSendRegister(_rfm69WebSocketOnSend); - wsOnReceiveRegister(_rfm69WebSocketOnReceive); + wsOnConnectedRegister(_rfm69WebSocketOnConnected); + wsOnKeyCheckRegister(_rfm69WebSocketOnKeyCheck); wsOnActionRegister(_rfm69WebSocketOnAction); #endif diff --git a/code/espurna/scheduler.ino b/code/espurna/scheduler.ino index 47b130d851..0f15c61426 100644 --- a/code/espurna/scheduler.ino +++ b/code/espurna/scheduler.ino @@ -229,8 +229,7 @@ void schSetup() { // Update websocket clients #if WEB_SUPPORT - wsOnConnectedRegister(_schWebSocketOnConnected); - wsOnKeyCheckRegister(_schWebSocketOnKeyCheck); + wsRegister((ws_callbacks_t) { _schWebSocketOnConnected, nullptr, _schWebSocketOnKeyCheck }); #endif // Main callbacks diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 45fb55c20c..e6117a37a5 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -111,14 +111,14 @@ double _magnitudeProcess(unsigned char type, unsigned char decimals, double valu #if WEB_SUPPORT +//void _sensorWebSocketMagnitudes(JsonObject& root, const String& ws_name, const String& conf_name) { template void _sensorWebSocketMagnitudes(JsonObject& root, T prefix) { // ws produces flat list Magnitudes - String ws_name = String(prefix); - ws_name.concat("Magnitudes"); + const String ws_name = String(prefix) + "Magnitudes"; // config uses Magnitude (cut 's') - String conf_name = ws_name.substring(0, ws_name.length() - 1); + const String conf_name = ws_name.substring(0, ws_name.length() - 1); JsonObject& list = root.createNestedObject(ws_name); list["size"] = magnitudeCount(); @@ -136,7 +136,21 @@ template void _sensorWebSocketMagnitudes(JsonObject& root, T prefix) } } -bool _sensorWebSocketOnReceive(const char * key, JsonVariant& value) { +/* +template void _sensorWebSocketMagnitudes(JsonObject& root, T prefix) { + + // ws produces flat list Magnitudes + const String ws_name = String(prefix) + "Magnitudes"; + + // config uses Magnitude (cut 's') + const String conf_name = ws_name.substring(0, ws_name.length() - 1); + + _sensorWebSocketMagnitudes(root, ws_name, conf_name); + +} +*/ + +bool _sensorWebSocketOnKeyCheck(const char * key, JsonVariant& value) { if (strncmp(key, "pwr", 3) == 0) return true; if (strncmp(key, "sns", 3) == 0) return true; if (strncmp(key, "tmp", 3) == 0) return true; @@ -200,7 +214,7 @@ void _sensorWebSocketSendData(JsonObject& root) { } -void _sensorWebSocketStart(JsonObject& root) { +void _sensorWebSocketOnConnected(JsonObject& root) { for (unsigned char i=0; i<_sensors.size(); i++) { @@ -1437,9 +1451,7 @@ void sensorSetup() { // Websockets #if WEB_SUPPORT - wsOnSendRegister(_sensorWebSocketStart); - wsOnReceiveRegister(_sensorWebSocketOnReceive); - wsOnSendRegister(_sensorWebSocketSendData); + wsRegister({ _sensorWebSocketOnConnected, nullptr, _sensorWebSocketOnKeyCheck }); #endif // API diff --git a/code/espurna/system.ino b/code/espurna/system.ino index 28a1c857de..8e4a10d9a0 100644 --- a/code/espurna/system.ino +++ b/code/espurna/system.ino @@ -261,7 +261,7 @@ void systemSetup() { #endif #if WEB_SUPPORT - wsOnKeyCheckRegister(_systemWebSocketOnKeyCheck); + wsRegister((ws_callbacks_t) { nullptr, nullptr, _systemWebSocketOnKeyCheck }); #endif // Init device-specific hardware diff --git a/code/espurna/telnet.ino b/code/espurna/telnet.ino index cc067716cb..c258aa1123 100644 --- a/code/espurna/telnet.ino +++ b/code/espurna/telnet.ino @@ -313,8 +313,7 @@ void telnetSetup() { #endif #if WEB_SUPPORT - wsOnConnectedRegister(_telnetWebSocketOnConnected); - wsOnKeyCheckRegister(_telnetWebSocketOnKeyCheck); + wsRegister((ws_callbacks_t) { _telnetWebSocketOnConnected, nullptr, _telnetWebSocketOnKeyCheck }); #endif espurnaRegisterReload(_telnetConfigure); diff --git a/code/espurna/thermostat.ino b/code/espurna/thermostat.ino index cc9be25dd0..f52382702b 100644 --- a/code/espurna/thermostat.ino +++ b/code/espurna/thermostat.ino @@ -298,7 +298,7 @@ void _thermostatReload() { #if WEB_SUPPORT //------------------------------------------------------------------------------ -void _thermostatWebSocketOnSend(JsonObject& root) { +void _thermostatWebSocketOnConnected(JsonObject& root) { root["thermostatEnabled"] = thermostatEnabled(); root["thermostatMode"] = thermostatModeCooler(); root["thermostatVisible"] = 1; @@ -328,7 +328,7 @@ void _thermostatWebSocketOnSend(JsonObject& root) { } //------------------------------------------------------------------------------ -bool _thermostatWebSocketOnReceive(const char * key, JsonVariant& value) { +bool _thermostatWebSocketOnKeyCheck(const char * key, JsonVariant& value) { if (strncmp(key, NAME_THERMOSTAT_ENABLED, strlen(NAME_THERMOSTAT_ENABLED)) == 0) return true; if (strncmp(key, NAME_THERMOSTAT_MODE, strlen(NAME_THERMOSTAT_MODE)) == 0) return true; if (strncmp(key, NAME_TEMP_RANGE_MIN, strlen(NAME_TEMP_RANGE_MIN)) == 0) return true; @@ -358,8 +358,8 @@ void thermostatSetup() { // Websockets #if WEB_SUPPORT - wsOnSendRegister(_thermostatWebSocketOnSend); - wsOnReceiveRegister(_thermostatWebSocketOnReceive); + wsOnConnectedRegister(_thermostatWebSocketOnConnected); + wsOnKeyCheckRegister(_thermostatWebSocketOnKeyCheck); wsOnActionRegister(_thermostatWebSocketOnAction); #endif diff --git a/code/espurna/thinkspeak.ino b/code/espurna/thinkspeak.ino index 1ff0abfd76..5aa55c21a6 100644 --- a/code/espurna/thinkspeak.ino +++ b/code/espurna/thinkspeak.ino @@ -386,8 +386,7 @@ void tspkSetup() { _tspkConfigure(); #if WEB_SUPPORT - wsOnConnectedRegister(_tspkWebSocketOnConnected); - wsOnKeyCheckRegister(_tspkWebSocketOnKeyCheck); + wsRegister((ws_callbacks_t) { _tspkWebSocketOnConnected, nullptr, _tspkWebSocketOnKeyCheck }); #endif #if BROKER_SUPPORT diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index a7c1f5c2fd..5ecb7ca176 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -645,9 +645,7 @@ void wifiSetup() { #endif #if WEB_SUPPORT - wsOnConnectedRegister(_wifiWebSocketOnConnected); - wsOnKeyCheckRegister(_wifiWebSocketOnKeyCheck); - wsOnActionRegister(_wifiWebSocketOnAction); + wsRegister((ws_callbacks_t) { _wifiWebSocketOnConnected, _wifiWebSocketOnAction, _wifiWebSocketOnKeyCheck }); #endif #if TERMINAL_SUPPORT diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index 0ae0455ce8..a501beddf1 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -18,9 +18,7 @@ Copyright (C) 2016-2019 by Xose PĂ©rez AsyncWebSocket _ws("/ws"); Ticker _web_defer; -std::vector _ws_on_connected_callbacks; -std::vector _ws_on_action_callbacks; -std::vector _ws_on_keycheck_callbacks; +std::vector _ws_callbacks; // ----------------------------------------------------------------------------- // Private methods @@ -29,7 +27,7 @@ std::vector _ws_on_keycheck_callbacks; struct ws_ticket_t { IPAddress ip; unsigned long timestamp = 0; -}; +}; ws_ticket_t _ws_tickets[WS_BUFFER_SIZE]; std::queue _ws_clients; @@ -87,7 +85,7 @@ bool wsDebugSend(const char* prefix, const char* message) { // via: https://arduinojson.org/v6/assistant/ // we use 1 object for "weblog", 2nd one for "message". "prefix", optional - StaticJsonBuffer jsonBuffer; + StaticJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); JsonObject& weblog = root.createNestedObject("weblog"); @@ -149,8 +147,9 @@ bool _wsStore(String key, JsonArray& value) { } bool _wsCheckKey(const String& key, JsonVariant& value) { - for (auto& callback : _ws_on_keycheck_callbacks) { - if (callback(key.c_str(), value)) return true; + for (auto& callbacks : _ws_callbacks) { + if (!callbacks.on_keycheck) continue; + if (callbacks.on_keycheck(key.c_str(), value)) return true; // TODO: remove this to call all OnKeyCheckCallbacks with the // current key/value } @@ -165,7 +164,7 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { uint32_t client_id = client->id(); // Parse JSON input - DynamicJsonBuffer jsonBuffer(1024); + DynamicJsonBuffer jsonBuffer(512); JsonObject& root = jsonBuffer.parseObject((char *) payload); if (!root.success()) { DEBUG_MSG_P(PSTR("[WEBSOCKET] JSON parsing error\n")); @@ -201,8 +200,9 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { if (data.success()) { // Callbacks - for (auto& callback : _ws_on_action_callbacks) { - callback(client_id, action, data); + for (auto& callbacks : _ws_callbacks) { + if (!callbacks.on_action) continue; + callbacks.on_action(client_id, action, data); } // Restore configuration via websockets @@ -474,20 +474,27 @@ void _wsNewClient() { } // TODO: v6 has memoryUsage, how to check v5? + //Serial.printf("BEFORE heap=%u\n", ESP.getFreeHeap()); DynamicJsonBuffer jsonBuffer(4096); JsonObject& root = jsonBuffer.createObject(); - for (auto& callback : _ws_on_connected_callbacks) { - callback(root); + //Serial.printf("MKJSON heap=%u\n", ESP.getFreeHeap()); + for (auto& callbacks : _ws_callbacks) { + if (!callbacks.on_connected) continue; + callbacks.on_connected(root); + Serial.printf("CALL heap=%u ptr=%p\n", ESP.getFreeHeap(), &callbacks.on_connected); } + //Serial.printf("MKSEND heap=%u \n", ESP.getFreeHeap()); wsSend(client, root); jsonBuffer.clear(); + //Serial.printf("SENT heap=%u \n", ESP.getFreeHeap()); _ws_clients.pop(); yield(); // finally, allow incoming messages ws_client->_tempObject = new WebSocketIncommingBuffer(_wsParse, true); + //Serial.printf("%u is now receiving\n", client); } void _wsLoop() { @@ -509,16 +516,9 @@ bool wsConnected(uint32_t client_id) { return _ws.hasClient(client_id); } -void wsOnConnectedRegister(ws_on_send_callback_f callback) { - _ws_on_connected_callbacks.push_back(callback); -} - -void wsOnKeyCheckRegister(ws_on_keycheck_callback_f callback) { - _ws_on_keycheck_callbacks.push_back(callback); -} - -void wsOnActionRegister(ws_on_action_callback_f callback) { - _ws_on_action_callbacks.push_back(callback); +void wsRegister(const ws_callbacks_t& callbacks) { + DEBUG_MSG("register %p\n", &callbacks); + _ws_callbacks.push_back(callbacks); } void wsSend(ws_on_send_callback_f callback) { @@ -589,8 +589,11 @@ void wsSetup() { mqttRegister(_wsMQTTCallback); #endif - wsOnConnectedRegister(_wsOnConnected); - wsOnKeyCheckRegister(_wsOnKeyCheck); + ws_callbacks_t callbacks; + callbacks.on_connected = _wsOnConnected; + callbacks.on_keycheck = _wsOnKeyCheck; + wsRegister(callbacks); + espurnaRegisterLoop(_wsLoop); }