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
#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);
}


+ 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;
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;
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


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

@ -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() {


+ 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>
std::vector<void (*)()> _loop_callbacks;
std::vector<void (*)()> _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
// -----------------------------------------------------------------------------


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

@ -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);
}


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

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

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

@ -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);
}


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

@ -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

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

@ -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);
}


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

@ -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);
}


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

@ -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; });
}


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

@ -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));


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

@ -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());
}

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

@ -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);
}


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

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


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

@ -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);
}


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

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


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

@ -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);
}


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

@ -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);
}


+ 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_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;
// -----------------------------------------------------------------------------
@ -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);


Loading…
Cancel
Save