From 7d09f59952597d07c042e41867c1928f0a41520d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Tue, 28 Aug 2018 11:09:04 +0200 Subject: [PATCH] Handle configure callbacks outside the ws module --- code/espurna/alexa.ino | 4 ++-- code/espurna/config/prototypes.h | 4 ---- code/espurna/domoticz.ino | 7 ++++++- code/espurna/espurna.ino | 13 ++++++++++++- code/espurna/homeassistant.ino | 11 ++++++----- code/espurna/influxdb.ino | 7 ++++++- code/espurna/led.ino | 4 ++-- code/espurna/light.ino | 14 ++++++++------ code/espurna/mqtt.ino | 6 +++--- code/espurna/nofuss.ino | 4 ++-- code/espurna/ntp.ino | 4 ++-- code/espurna/ota.ino | 9 +++------ code/espurna/relay.ino | 7 ++++--- code/espurna/rfm69.ino | 4 ++-- code/espurna/scheduler.ino | 4 ++-- code/espurna/sensor.ino | 4 ++-- code/espurna/settings.ino | 2 +- code/espurna/thinkspeak.ino | 4 ++-- code/espurna/wifi.ino | 4 ++-- code/espurna/ws.ino | 16 +--------------- 20 files changed, 68 insertions(+), 64 deletions(-) diff --git a/code/espurna/alexa.ino b/code/espurna/alexa.ino index f1d552a3..84e5ea2e 100644 --- a/code/espurna/alexa.ino +++ b/code/espurna/alexa.ino @@ -82,7 +82,6 @@ void alexaSetup() { // Websockets #if WEB_SUPPORT wsOnSendRegister(_alexaWebSocketOnSend); - wsOnAfterParseRegister(_alexaConfigure); wsOnReceiveRegister(_alexaWebSocketOnReceive); #endif @@ -102,8 +101,9 @@ void alexaSetup() { _alexa_queue.push(element); }); - // Register loop + // Register main callbacks espurnaRegisterLoop(alexaLoop); + espurnaRegisterReload(_alexaConfigure); } diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index c6052810..fc34a11d 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -156,15 +156,11 @@ void webRequestRegister(web_request_callback_f callback); typedef std::function ws_on_action_callback_f; void wsOnActionRegister(ws_on_action_callback_f callback); - typedef std::function ws_on_after_parse_callback_f; - void wsOnAfterParseRegister(ws_on_after_parse_callback_f callback); - typedef std::function ws_on_receive_callback_f; void wsOnReceiveRegister(ws_on_receive_callback_f callback); #else #define ws_on_send_callback_f void * #define ws_on_action_callback_f void * - #define ws_on_after_parse_callback_f void * #define ws_on_receive_callback_f void * #endif diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index d2622eca..23386529 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -157,13 +157,18 @@ unsigned int domoticzIdx(unsigned char relayID) { } void domoticzSetup() { + _domoticzConfigure(); + #if WEB_SUPPORT wsOnSendRegister(_domoticzWebSocketOnSend); - wsOnAfterParseRegister(_domoticzConfigure); wsOnReceiveRegister(_domoticzWebSocketOnReceive); #endif + + // Callbacks mqttRegister(_domoticzMqtt); + espurnaRegisterReload(_domoticzConfigure); + } bool domoticzEnabled() { diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 7ab6c080..a4fcf432 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -23,15 +23,26 @@ along with this program. If not, see . #include std::vector _loop_callbacks; +std::vector _reload_callbacks; // ----------------------------------------------------------------------------- -// REGISTER +// GENERAL CALLBACKS // ----------------------------------------------------------------------------- void espurnaRegisterLoop(void (*callback)()) { _loop_callbacks.push_back(callback); } +void espurnaRegisterReload(void (*callback)()) { + _reload_callbacks.push_back(callback); +} + +void espurnaReload() { + for (unsigned char i = 0; i < _reload_callbacks.size(); i++) { + (_reload_callbacks[i])(); + } +} + // ----------------------------------------------------------------------------- // BOOTING // ----------------------------------------------------------------------------- diff --git a/code/espurna/homeassistant.ino b/code/espurna/homeassistant.ino index 5d725f80..d0b6ffe8 100644 --- a/code/espurna/homeassistant.ino +++ b/code/espurna/homeassistant.ino @@ -286,20 +286,21 @@ void haSetup() { #if WEB_SUPPORT wsOnSendRegister(_haWebSocketOnSend); - wsOnAfterParseRegister(_haConfigure); wsOnActionRegister(_haWebSocketOnAction); wsOnReceiveRegister(_haWebSocketOnReceive); #endif + #if TERMINAL_SUPPORT + _haInitCommands(); + #endif + // On MQTT connect check if we have something to send mqttRegister([](unsigned int type, const char * topic, const char * payload) { if (type == MQTT_CONNECT_EVENT) _haSend(); }); - #if TERMINAL_SUPPORT - _haInitCommands(); - #endif - + // Main callbacks + espurnaRegisterReload(_haConfigure); } diff --git a/code/espurna/influxdb.ino b/code/espurna/influxdb.ino index 041ce6c6..4d6e7f07 100644 --- a/code/espurna/influxdb.ino +++ b/code/espurna/influxdb.ino @@ -100,12 +100,17 @@ bool idbEnabled() { } void idbSetup() { + _idbConfigure(); + #if WEB_SUPPORT wsOnSendRegister(_idbWebSocketOnSend); - wsOnAfterParseRegister(_idbConfigure); wsOnReceiveRegister(_idbWebSocketOnReceive); #endif + + // Main callbacks + espurnaRegisterReload(_idbConfigure); + } #endif diff --git a/code/espurna/led.ino b/code/espurna/led.ino index ecfe5eca..7fe6716a 100644 --- a/code/espurna/led.ino +++ b/code/espurna/led.ino @@ -170,14 +170,14 @@ void ledSetup() { #if WEB_SUPPORT wsOnSendRegister(_ledWebSocketOnSend); - wsOnAfterParseRegister(_ledConfigure); wsOnReceiveRegister(_ledWebSocketOnReceive); #endif DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size()); - // Register loop + // Main callbacks espurnaRegisterLoop(ledLoop); + espurnaRegisterReload(_ledConfigure); } diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 1870869e..d2456dca 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -1076,12 +1076,6 @@ void lightSetup() { wsOnSendRegister(_lightWebSocketOnSend); wsOnActionRegister(_lightWebSocketOnAction); wsOnReceiveRegister(_lightWebSocketOnReceive); - wsOnAfterParseRegister([]() { - #if LIGHT_SAVE_ENABLED == 0 - lightSave(); - #endif - _lightConfigure(); - }); #endif #if API_SUPPORT @@ -1096,6 +1090,14 @@ void lightSetup() { _lightInitCommands(); #endif + // Main callbacks + espurnaRegisterReload([]() { + #if LIGHT_SAVE_ENABLED == 0 + lightSave(); + #endif + _lightConfigure(); + }); + } #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index fa02afde..b4ec306a 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -751,7 +751,7 @@ void mqttReset() { void mqttSetup() { _mqttBackwards(); - + DEBUG_MSG_P(PSTR("[MQTT] Async %s, SSL %s, Autoconnect %s\n"), MQTT_USE_ASYNC ? "ENABLED" : "DISABLED", ASYNC_TCP_SSL_ENABLED ? "ENABLED" : "DISABLED", @@ -809,7 +809,6 @@ void mqttSetup() { #if WEB_SUPPORT wsOnSendRegister(_mqttWebSocketOnSend); - wsOnAfterParseRegister(_mqttConfigure); wsOnReceiveRegister(_mqttWebSocketOnReceive); #endif @@ -817,8 +816,9 @@ void mqttSetup() { _mqttInitCommands(); #endif - // Register loop + // Main callbacks espurnaRegisterLoop(mqttLoop); + espurnaRegisterReload(_mqttConfigure); } diff --git a/code/espurna/nofuss.ino b/code/espurna/nofuss.ino index c9247986..af1163ce 100644 --- a/code/espurna/nofuss.ino +++ b/code/espurna/nofuss.ino @@ -154,7 +154,6 @@ void nofussSetup() { #if WEB_SUPPORT wsOnSendRegister(_nofussWebSocketOnSend); - wsOnAfterParseRegister(_nofussConfigure); wsOnReceiveRegister(_nofussWebSocketOnReceive); #endif @@ -162,8 +161,9 @@ void nofussSetup() { _nofussInitCommands(); #endif - // Register loop + // Main callbacks espurnaRegisterLoop(nofussLoop); + espurnaRegisterReload(_nofussConfigure); } diff --git a/code/espurna/ntp.ino b/code/espurna/ntp.ino index 2a8134a6..5bf2f591 100644 --- a/code/espurna/ntp.ino +++ b/code/espurna/ntp.ino @@ -179,11 +179,11 @@ void ntpSetup() { #if WEB_SUPPORT wsOnSendRegister(_ntpWebSocketOnSend); wsOnReceiveRegister(_ntpWebSocketOnReceive); - wsOnAfterParseRegister([]() { _ntp_configure = true; }); #endif - // Register loop + // Main callbacks espurnaRegisterLoop(_ntpLoop); + espurnaRegisterReload([]() { _ntp_configure = true; }); } diff --git a/code/espurna/ota.ino b/code/espurna/ota.ino index 5579fcb1..e192764a 100644 --- a/code/espurna/ota.ino +++ b/code/espurna/ota.ino @@ -203,16 +203,13 @@ void otaSetup() { _otaConfigure(); - #if WEB_SUPPORT - wsOnAfterParseRegister(_otaConfigure); - #endif - #if TERMINAL_SUPPORT _otaInitCommands(); #endif - // Register loop + // Main callbacks espurnaRegisterLoop(_otaLoop); + espurnaRegisterReload(_otaConfigure); // ------------------------------------------------------------------------- @@ -238,7 +235,7 @@ void otaSetup() { deferredReset(100, CUSTOM_RESET_OTA); }); - ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { static unsigned int _progOld; unsigned int _prog = (progress / (total / 100)); diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index d05b7ff4..0ca7a83b 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -618,7 +618,6 @@ void _relayWebSocketOnAction(uint32_t client_id, const char * action, JsonObject void relaySetupWS() { wsOnSendRegister(_relayWebSocketOnStart); wsOnActionRegister(_relayWebSocketOnAction); - wsOnAfterParseRegister(_relayConfigure); wsOnReceiveRegister(_relayWebSocketOnReceive); } @@ -1004,8 +1003,6 @@ void relaySetup() { _relayBoot(); _relayLoop(); - espurnaRegisterLoop(_relayLoop); - #if WEB_SUPPORT relaySetupWS(); #endif @@ -1019,6 +1016,10 @@ void relaySetup() { _relayInitCommands(); #endif + // Main callbacks + espurnaRegisterLoop(_relayLoop); + espurnaRegisterReload(_relayConfigure); + DEBUG_MSG_P(PSTR("[RELAY] Number of relays: %d\n"), _relays.size()); } diff --git a/code/espurna/rfm69.ino b/code/espurna/rfm69.ino index 5ab362a2..9f9d9e09 100644 --- a/code/espurna/rfm69.ino +++ b/code/espurna/rfm69.ino @@ -268,12 +268,12 @@ void rfm69Setup() { #if WEB_SUPPORT wsOnSendRegister(_rfm69WebSocketOnSend); wsOnReceiveRegister(_rfm69WebSocketOnReceive); - wsOnAfterParseRegister(_rfm69Configure); wsOnActionRegister(_rfm69WebSocketOnAction); #endif - // Register loop + // Main callbacks espurnaRegisterLoop(_rfm69Loop); + espurnaRegisterReload(_rfm69Configure); } diff --git a/code/espurna/scheduler.ino b/code/espurna/scheduler.ino index 003a6f69..28bce0c4 100644 --- a/code/espurna/scheduler.ino +++ b/code/espurna/scheduler.ino @@ -216,11 +216,11 @@ void schSetup() { #if WEB_SUPPORT wsOnSendRegister(_schWebSocketOnSend); wsOnReceiveRegister(_schWebSocketOnReceive); - wsOnAfterParseRegister(_schConfigure); #endif - // Register loop + // Main callbacks espurnaRegisterLoop(_schLoop); + espurnaRegisterReload(_schConfigure); } diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index d074ef69..a3d0ef3c 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -1071,7 +1071,6 @@ void sensorSetup() { wsOnSendRegister(_sensorWebSocketStart); wsOnReceiveRegister(_sensorWebSocketOnReceive); wsOnSendRegister(_sensorWebSocketSendData); - wsOnAfterParseRegister(_sensorConfigure); #endif // API @@ -1084,8 +1083,9 @@ void sensorSetup() { _sensorInitCommands(); #endif - // Register loop + // Main callbacks espurnaRegisterLoop(sensorLoop); + espurnaRegisterReload(_sensorConfigure); } diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index 51459a15..5f934722 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -276,7 +276,7 @@ void _settingsInitCommands() { #if WEB_SUPPORT settingsRegisterCommand(F("RELOAD"), [](Embedis* e) { - wsReload(); + espurnaReload(); DEBUG_MSG_P(PSTR("+OK\n")); }); #endif diff --git a/code/espurna/thinkspeak.ino b/code/espurna/thinkspeak.ino index 04b0c85c..f06b1d00 100644 --- a/code/espurna/thinkspeak.ino +++ b/code/espurna/thinkspeak.ino @@ -259,7 +259,6 @@ void tspkSetup() { #if WEB_SUPPORT wsOnSendRegister(_tspkWebSocketOnSend); - wsOnAfterParseRegister(_tspkConfigure); wsOnReceiveRegister(_tspkWebSocketOnReceive); #endif @@ -268,8 +267,9 @@ void tspkSetup() { THINGSPEAK_USE_SSL ? "ENABLED" : "DISABLED" ); - // Register loop + // Main callbacks espurnaRegisterLoop(tspkLoop); + espurnaRegisterReload(_tspkConfigure); } diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index d72396e7..f41a315f 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -613,7 +613,6 @@ void wifiSetup() { #if WEB_SUPPORT wsOnSendRegister(_wifiWebSocketOnSend); wsOnReceiveRegister(_wifiWebSocketOnReceive); - wsOnAfterParseRegister(_wifiConfigure); wsOnActionRegister(_wifiWebSocketOnAction); #endif @@ -621,8 +620,9 @@ void wifiSetup() { _wifiInitCommands(); #endif - // Register loop + // Main callbacks espurnaRegisterLoop(wifiLoop); + espurnaRegisterReload(_wifiConfigure); } diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index aab9be2e..e70d15c8 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -20,7 +20,6 @@ Ticker _web_defer; std::vector _ws_on_send_callbacks; std::vector _ws_on_action_callbacks; -std::vector _ws_on_after_parse_callbacks; std::vector _ws_on_receive_callbacks; // ----------------------------------------------------------------------------- @@ -256,7 +255,7 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { if (save) { // Callbacks - wsReload(); + espurnaReload(); // This should got to callback as well // but first change management has to be in place @@ -429,10 +428,6 @@ void wsOnActionRegister(ws_on_action_callback_f callback) { _ws_on_action_callbacks.push_back(callback); } -void wsOnAfterParseRegister(ws_on_after_parse_callback_f callback) { - _ws_on_after_parse_callbacks.push_back(callback); -} - void wsSend(ws_on_send_callback_f callback) { if (_ws.count() > 0) { DynamicJsonBuffer jsonBuffer; @@ -479,15 +474,6 @@ void wsSend_P(uint32_t client_id, PGM_P payload) { _ws.text(client_id, buffer); } -// This method being public makes -// _ws_on_after_parse_callbacks strange here, -// it should belong somewhere else. -void wsReload() { - for (unsigned char i = 0; i < _ws_on_after_parse_callbacks.size(); i++) { - (_ws_on_after_parse_callbacks[i])(); - } -} - void wsSetup() { _ws.onEvent(_wsEvent);