Browse Source

Handle configure callbacks outside the ws module

ech1560
Xose Pérez 6 years ago
parent
commit
7d09f59952
20 changed files with 68 additions and 64 deletions
  1. +2
    -2
      code/espurna/alexa.ino
  2. +0
    -4
      code/espurna/config/prototypes.h
  3. +6
    -1
      code/espurna/domoticz.ino
  4. +12
    -1
      code/espurna/espurna.ino
  5. +6
    -5
      code/espurna/homeassistant.ino
  6. +6
    -1
      code/espurna/influxdb.ino
  7. +2
    -2
      code/espurna/led.ino
  8. +8
    -6
      code/espurna/light.ino
  9. +3
    -3
      code/espurna/mqtt.ino
  10. +2
    -2
      code/espurna/nofuss.ino
  11. +2
    -2
      code/espurna/ntp.ino
  12. +3
    -6
      code/espurna/ota.ino
  13. +4
    -3
      code/espurna/relay.ino
  14. +2
    -2
      code/espurna/rfm69.ino
  15. +2
    -2
      code/espurna/scheduler.ino
  16. +2
    -2
      code/espurna/sensor.ino
  17. +1
    -1
      code/espurna/settings.ino
  18. +2
    -2
      code/espurna/thinkspeak.ino
  19. +2
    -2
      code/espurna/wifi.ino
  20. +1
    -15
      code/espurna/ws.ino

+ 2
- 2
code/espurna/alexa.ino View File

@ -82,7 +82,6 @@ void alexaSetup() {
// Websockets // Websockets
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_alexaWebSocketOnSend); wsOnSendRegister(_alexaWebSocketOnSend);
wsOnAfterParseRegister(_alexaConfigure);
wsOnReceiveRegister(_alexaWebSocketOnReceive); wsOnReceiveRegister(_alexaWebSocketOnReceive);
#endif #endif
@ -102,8 +101,9 @@ void alexaSetup() {
_alexa_queue.push(element); _alexa_queue.push(element);
}); });
// Register loop
// Register main callbacks
espurnaRegisterLoop(alexaLoop); espurnaRegisterLoop(alexaLoop);
espurnaRegisterReload(_alexaConfigure);
} }


+ 0
- 4
code/espurna/config/prototypes.h View File

@ -156,15 +156,11 @@ void webRequestRegister(web_request_callback_f callback);
typedef std::function<void(uint32_t, const char *, JsonObject&)> ws_on_action_callback_f; typedef std::function<void(uint32_t, const char *, JsonObject&)> ws_on_action_callback_f;
void wsOnActionRegister(ws_on_action_callback_f callback); void wsOnActionRegister(ws_on_action_callback_f callback);
typedef std::function<void(void)> ws_on_after_parse_callback_f;
void wsOnAfterParseRegister(ws_on_after_parse_callback_f callback);
typedef std::function<bool(const char *, JsonVariant&)> ws_on_receive_callback_f; typedef std::function<bool(const char *, JsonVariant&)> ws_on_receive_callback_f;
void wsOnReceiveRegister(ws_on_receive_callback_f callback); void wsOnReceiveRegister(ws_on_receive_callback_f callback);
#else #else
#define ws_on_send_callback_f void * #define ws_on_send_callback_f void *
#define ws_on_action_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 * #define ws_on_receive_callback_f void *
#endif #endif


+ 6
- 1
code/espurna/domoticz.ino View File

@ -157,13 +157,18 @@ unsigned int domoticzIdx(unsigned char relayID) {
} }
void domoticzSetup() { void domoticzSetup() {
_domoticzConfigure(); _domoticzConfigure();
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_domoticzWebSocketOnSend); wsOnSendRegister(_domoticzWebSocketOnSend);
wsOnAfterParseRegister(_domoticzConfigure);
wsOnReceiveRegister(_domoticzWebSocketOnReceive); wsOnReceiveRegister(_domoticzWebSocketOnReceive);
#endif #endif
// Callbacks
mqttRegister(_domoticzMqtt); mqttRegister(_domoticzMqtt);
espurnaRegisterReload(_domoticzConfigure);
} }
bool domoticzEnabled() { bool domoticzEnabled() {


+ 12
- 1
code/espurna/espurna.ino View File

@ -23,15 +23,26 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <vector> #include <vector>
std::vector<void (*)()> _loop_callbacks; std::vector<void (*)()> _loop_callbacks;
std::vector<void (*)()> _reload_callbacks;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// REGISTER
// GENERAL CALLBACKS
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void espurnaRegisterLoop(void (*callback)()) { void espurnaRegisterLoop(void (*callback)()) {
_loop_callbacks.push_back(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 // BOOTING
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------


+ 6
- 5
code/espurna/homeassistant.ino View File

@ -286,20 +286,21 @@ void haSetup() {
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_haWebSocketOnSend); wsOnSendRegister(_haWebSocketOnSend);
wsOnAfterParseRegister(_haConfigure);
wsOnActionRegister(_haWebSocketOnAction); wsOnActionRegister(_haWebSocketOnAction);
wsOnReceiveRegister(_haWebSocketOnReceive); wsOnReceiveRegister(_haWebSocketOnReceive);
#endif #endif
#if TERMINAL_SUPPORT
_haInitCommands();
#endif
// On MQTT connect check if we have something to send // On MQTT connect check if we have something to send
mqttRegister([](unsigned int type, const char * topic, const char * payload) { mqttRegister([](unsigned int type, const char * topic, const char * payload) {
if (type == MQTT_CONNECT_EVENT) _haSend(); if (type == MQTT_CONNECT_EVENT) _haSend();
}); });
#if TERMINAL_SUPPORT
_haInitCommands();
#endif
// Main callbacks
espurnaRegisterReload(_haConfigure);
} }


+ 6
- 1
code/espurna/influxdb.ino View File

@ -100,12 +100,17 @@ bool idbEnabled() {
} }
void idbSetup() { void idbSetup() {
_idbConfigure(); _idbConfigure();
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_idbWebSocketOnSend); wsOnSendRegister(_idbWebSocketOnSend);
wsOnAfterParseRegister(_idbConfigure);
wsOnReceiveRegister(_idbWebSocketOnReceive); wsOnReceiveRegister(_idbWebSocketOnReceive);
#endif #endif
// Main callbacks
espurnaRegisterReload(_idbConfigure);
} }
#endif #endif

+ 2
- 2
code/espurna/led.ino View File

@ -170,14 +170,14 @@ void ledSetup() {
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_ledWebSocketOnSend); wsOnSendRegister(_ledWebSocketOnSend);
wsOnAfterParseRegister(_ledConfigure);
wsOnReceiveRegister(_ledWebSocketOnReceive); wsOnReceiveRegister(_ledWebSocketOnReceive);
#endif #endif
DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size()); DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size());
// Register loop
// Main callbacks
espurnaRegisterLoop(ledLoop); espurnaRegisterLoop(ledLoop);
espurnaRegisterReload(_ledConfigure);
} }


+ 8
- 6
code/espurna/light.ino View File

@ -1076,12 +1076,6 @@ void lightSetup() {
wsOnSendRegister(_lightWebSocketOnSend); wsOnSendRegister(_lightWebSocketOnSend);
wsOnActionRegister(_lightWebSocketOnAction); wsOnActionRegister(_lightWebSocketOnAction);
wsOnReceiveRegister(_lightWebSocketOnReceive); wsOnReceiveRegister(_lightWebSocketOnReceive);
wsOnAfterParseRegister([]() {
#if LIGHT_SAVE_ENABLED == 0
lightSave();
#endif
_lightConfigure();
});
#endif #endif
#if API_SUPPORT #if API_SUPPORT
@ -1096,6 +1090,14 @@ void lightSetup() {
_lightInitCommands(); _lightInitCommands();
#endif #endif
// Main callbacks
espurnaRegisterReload([]() {
#if LIGHT_SAVE_ENABLED == 0
lightSave();
#endif
_lightConfigure();
});
} }
#endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE

+ 3
- 3
code/espurna/mqtt.ino View File

@ -751,7 +751,7 @@ void mqttReset() {
void mqttSetup() { void mqttSetup() {
_mqttBackwards(); _mqttBackwards();
DEBUG_MSG_P(PSTR("[MQTT] Async %s, SSL %s, Autoconnect %s\n"), DEBUG_MSG_P(PSTR("[MQTT] Async %s, SSL %s, Autoconnect %s\n"),
MQTT_USE_ASYNC ? "ENABLED" : "DISABLED", MQTT_USE_ASYNC ? "ENABLED" : "DISABLED",
ASYNC_TCP_SSL_ENABLED ? "ENABLED" : "DISABLED", ASYNC_TCP_SSL_ENABLED ? "ENABLED" : "DISABLED",
@ -809,7 +809,6 @@ void mqttSetup() {
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_mqttWebSocketOnSend); wsOnSendRegister(_mqttWebSocketOnSend);
wsOnAfterParseRegister(_mqttConfigure);
wsOnReceiveRegister(_mqttWebSocketOnReceive); wsOnReceiveRegister(_mqttWebSocketOnReceive);
#endif #endif
@ -817,8 +816,9 @@ void mqttSetup() {
_mqttInitCommands(); _mqttInitCommands();
#endif #endif
// Register loop
// Main callbacks
espurnaRegisterLoop(mqttLoop); espurnaRegisterLoop(mqttLoop);
espurnaRegisterReload(_mqttConfigure);
} }


+ 2
- 2
code/espurna/nofuss.ino View File

@ -154,7 +154,6 @@ void nofussSetup() {
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_nofussWebSocketOnSend); wsOnSendRegister(_nofussWebSocketOnSend);
wsOnAfterParseRegister(_nofussConfigure);
wsOnReceiveRegister(_nofussWebSocketOnReceive); wsOnReceiveRegister(_nofussWebSocketOnReceive);
#endif #endif
@ -162,8 +161,9 @@ void nofussSetup() {
_nofussInitCommands(); _nofussInitCommands();
#endif #endif
// Register loop
// Main callbacks
espurnaRegisterLoop(nofussLoop); espurnaRegisterLoop(nofussLoop);
espurnaRegisterReload(_nofussConfigure);
} }


+ 2
- 2
code/espurna/ntp.ino View File

@ -179,11 +179,11 @@ void ntpSetup() {
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_ntpWebSocketOnSend); wsOnSendRegister(_ntpWebSocketOnSend);
wsOnReceiveRegister(_ntpWebSocketOnReceive); wsOnReceiveRegister(_ntpWebSocketOnReceive);
wsOnAfterParseRegister([]() { _ntp_configure = true; });
#endif #endif
// Register loop
// Main callbacks
espurnaRegisterLoop(_ntpLoop); espurnaRegisterLoop(_ntpLoop);
espurnaRegisterReload([]() { _ntp_configure = true; });
} }


+ 3
- 6
code/espurna/ota.ino View File

@ -203,16 +203,13 @@ void otaSetup() {
_otaConfigure(); _otaConfigure();
#if WEB_SUPPORT
wsOnAfterParseRegister(_otaConfigure);
#endif
#if TERMINAL_SUPPORT #if TERMINAL_SUPPORT
_otaInitCommands(); _otaInitCommands();
#endif #endif
// Register loop
// Main callbacks
espurnaRegisterLoop(_otaLoop); espurnaRegisterLoop(_otaLoop);
espurnaRegisterReload(_otaConfigure);
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
@ -238,7 +235,7 @@ void otaSetup() {
deferredReset(100, CUSTOM_RESET_OTA); 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; static unsigned int _progOld;
unsigned int _prog = (progress / (total / 100)); unsigned int _prog = (progress / (total / 100));


+ 4
- 3
code/espurna/relay.ino View File

@ -618,7 +618,6 @@ void _relayWebSocketOnAction(uint32_t client_id, const char * action, JsonObject
void relaySetupWS() { void relaySetupWS() {
wsOnSendRegister(_relayWebSocketOnStart); wsOnSendRegister(_relayWebSocketOnStart);
wsOnActionRegister(_relayWebSocketOnAction); wsOnActionRegister(_relayWebSocketOnAction);
wsOnAfterParseRegister(_relayConfigure);
wsOnReceiveRegister(_relayWebSocketOnReceive); wsOnReceiveRegister(_relayWebSocketOnReceive);
} }
@ -1004,8 +1003,6 @@ void relaySetup() {
_relayBoot(); _relayBoot();
_relayLoop(); _relayLoop();
espurnaRegisterLoop(_relayLoop);
#if WEB_SUPPORT #if WEB_SUPPORT
relaySetupWS(); relaySetupWS();
#endif #endif
@ -1019,6 +1016,10 @@ void relaySetup() {
_relayInitCommands(); _relayInitCommands();
#endif #endif
// Main callbacks
espurnaRegisterLoop(_relayLoop);
espurnaRegisterReload(_relayConfigure);
DEBUG_MSG_P(PSTR("[RELAY] Number of relays: %d\n"), _relays.size()); DEBUG_MSG_P(PSTR("[RELAY] Number of relays: %d\n"), _relays.size());
} }

+ 2
- 2
code/espurna/rfm69.ino View File

@ -268,12 +268,12 @@ void rfm69Setup() {
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_rfm69WebSocketOnSend); wsOnSendRegister(_rfm69WebSocketOnSend);
wsOnReceiveRegister(_rfm69WebSocketOnReceive); wsOnReceiveRegister(_rfm69WebSocketOnReceive);
wsOnAfterParseRegister(_rfm69Configure);
wsOnActionRegister(_rfm69WebSocketOnAction); wsOnActionRegister(_rfm69WebSocketOnAction);
#endif #endif
// Register loop
// Main callbacks
espurnaRegisterLoop(_rfm69Loop); espurnaRegisterLoop(_rfm69Loop);
espurnaRegisterReload(_rfm69Configure);
} }


+ 2
- 2
code/espurna/scheduler.ino View File

@ -216,11 +216,11 @@ void schSetup() {
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_schWebSocketOnSend); wsOnSendRegister(_schWebSocketOnSend);
wsOnReceiveRegister(_schWebSocketOnReceive); wsOnReceiveRegister(_schWebSocketOnReceive);
wsOnAfterParseRegister(_schConfigure);
#endif #endif
// Register loop
// Main callbacks
espurnaRegisterLoop(_schLoop); espurnaRegisterLoop(_schLoop);
espurnaRegisterReload(_schConfigure);
} }


+ 2
- 2
code/espurna/sensor.ino View File

@ -1071,7 +1071,6 @@ void sensorSetup() {
wsOnSendRegister(_sensorWebSocketStart); wsOnSendRegister(_sensorWebSocketStart);
wsOnReceiveRegister(_sensorWebSocketOnReceive); wsOnReceiveRegister(_sensorWebSocketOnReceive);
wsOnSendRegister(_sensorWebSocketSendData); wsOnSendRegister(_sensorWebSocketSendData);
wsOnAfterParseRegister(_sensorConfigure);
#endif #endif
// API // API
@ -1084,8 +1083,9 @@ void sensorSetup() {
_sensorInitCommands(); _sensorInitCommands();
#endif #endif
// Register loop
// Main callbacks
espurnaRegisterLoop(sensorLoop); espurnaRegisterLoop(sensorLoop);
espurnaRegisterReload(_sensorConfigure);
} }


+ 1
- 1
code/espurna/settings.ino View File

@ -276,7 +276,7 @@ void _settingsInitCommands() {
#if WEB_SUPPORT #if WEB_SUPPORT
settingsRegisterCommand(F("RELOAD"), [](Embedis* e) { settingsRegisterCommand(F("RELOAD"), [](Embedis* e) {
wsReload();
espurnaReload();
DEBUG_MSG_P(PSTR("+OK\n")); DEBUG_MSG_P(PSTR("+OK\n"));
}); });
#endif #endif


+ 2
- 2
code/espurna/thinkspeak.ino View File

@ -259,7 +259,6 @@ void tspkSetup() {
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_tspkWebSocketOnSend); wsOnSendRegister(_tspkWebSocketOnSend);
wsOnAfterParseRegister(_tspkConfigure);
wsOnReceiveRegister(_tspkWebSocketOnReceive); wsOnReceiveRegister(_tspkWebSocketOnReceive);
#endif #endif
@ -268,8 +267,9 @@ void tspkSetup() {
THINGSPEAK_USE_SSL ? "ENABLED" : "DISABLED" THINGSPEAK_USE_SSL ? "ENABLED" : "DISABLED"
); );
// Register loop
// Main callbacks
espurnaRegisterLoop(tspkLoop); espurnaRegisterLoop(tspkLoop);
espurnaRegisterReload(_tspkConfigure);
} }


+ 2
- 2
code/espurna/wifi.ino View File

@ -613,7 +613,6 @@ void wifiSetup() {
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_wifiWebSocketOnSend); wsOnSendRegister(_wifiWebSocketOnSend);
wsOnReceiveRegister(_wifiWebSocketOnReceive); wsOnReceiveRegister(_wifiWebSocketOnReceive);
wsOnAfterParseRegister(_wifiConfigure);
wsOnActionRegister(_wifiWebSocketOnAction); wsOnActionRegister(_wifiWebSocketOnAction);
#endif #endif
@ -621,8 +620,9 @@ void wifiSetup() {
_wifiInitCommands(); _wifiInitCommands();
#endif #endif
// Register loop
// Main callbacks
espurnaRegisterLoop(wifiLoop); espurnaRegisterLoop(wifiLoop);
espurnaRegisterReload(_wifiConfigure);
} }


+ 1
- 15
code/espurna/ws.ino View File

@ -20,7 +20,6 @@ Ticker _web_defer;
std::vector<ws_on_send_callback_f> _ws_on_send_callbacks; std::vector<ws_on_send_callback_f> _ws_on_send_callbacks;
std::vector<ws_on_action_callback_f> _ws_on_action_callbacks; std::vector<ws_on_action_callback_f> _ws_on_action_callbacks;
std::vector<ws_on_after_parse_callback_f> _ws_on_after_parse_callbacks;
std::vector<ws_on_receive_callback_f> _ws_on_receive_callbacks; std::vector<ws_on_receive_callback_f> _ws_on_receive_callbacks;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -256,7 +255,7 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) {
if (save) { if (save) {
// Callbacks // Callbacks
wsReload();
espurnaReload();
// This should got to callback as well // This should got to callback as well
// but first change management has to be in place // 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); _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) { void wsSend(ws_on_send_callback_f callback) {
if (_ws.count() > 0) { if (_ws.count() > 0) {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
@ -479,15 +474,6 @@ void wsSend_P(uint32_t client_id, PGM_P payload) {
_ws.text(client_id, buffer); _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() { void wsSetup() {
_ws.onEvent(_wsEvent); _ws.onEvent(_wsEvent);


Loading…
Cancel
Save