Skip to content

Commit

Permalink
Save a bit by using a single callback vector
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr committed Aug 4, 2019
1 parent 11e65b3 commit ff75e5c
Show file tree
Hide file tree
Showing 25 changed files with 117 additions and 94 deletions.
6 changes: 4 additions & 2 deletions code/espurna/alexa.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions code/espurna/api.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/button.ino
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void buttonSetup() {

// Websocket Callbacks
#if WEB_SUPPORT
wsOnKeyCheckRegister(_buttonWebSocketOnKeyCheck);
wsRegister({ nullptr, nullptr, _buttonWebSocketOnKeyCheck });
#endif

// Register loop
Expand Down
13 changes: 8 additions & 5 deletions code/espurna/config/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,18 @@ void webRequestRegister(web_request_callback_f callback);
using ws_on_action_callback_f = std::function<void(uint32_t, const char *, JsonObject&)>;
using ws_on_keycheck_callback_f = std::function<bool(const char *, JsonVariant&)>;

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*);
Expand Down
6 changes: 4 additions & 2 deletions code/espurna/debug.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions code/espurna/domoticz.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions code/espurna/homeassistant.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions code/espurna/influxdb.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -118,8 +118,8 @@ void idbSetup() {
_idbConfigure();

#if WEB_SUPPORT
wsOnSendRegister(_idbWebSocketOnSend);
wsOnReceiveRegister(_idbWebSocketOnReceive);
wsOnConnectedRegister(_idbWebSocketOnConnected);
wsOnKeyCheckRegister(_idbWebSocketOnKeyCheck);
#endif

#if BROKER_SUPPORT
Expand Down
4 changes: 2 additions & 2 deletions code/espurna/led.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions code/espurna/light.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -1290,9 +1290,9 @@ void lightSetup() {
}

#if WEB_SUPPORT
wsOnSendRegister(_lightWebSocketOnSend);
wsOnConnectedRegister(_lightWebSocketOnConnected);
wsOnActionRegister(_lightWebSocketOnAction);
wsOnReceiveRegister(_lightWebSocketOnReceive);
wsOnKeyCheckRegister(_lightWebSocketOnKeyCheck);
#endif

#if API_SUPPORT
Expand Down
4 changes: 2 additions & 2 deletions code/espurna/lightfox.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -94,7 +94,7 @@ void _lightfoxInitCommands() {
void lightfoxSetup() {

#if WEB_SUPPORT
wsOnSendRegister(_lightfoxWebSocketOnSend);
wsOnConnectedRegister(_lightfoxWebSocketOnConnected);
wsOnActionRegister(_lightfoxWebSocketOnAction);
#endif

Expand Down
3 changes: 1 addition & 2 deletions code/espurna/mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions code/espurna/nofuss.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -161,8 +161,8 @@ void nofussSetup() {
});

#if WEB_SUPPORT
wsOnSendRegister(_nofussWebSocketOnSend);
wsOnReceiveRegister(_nofussWebSocketOnReceive);
wsOnConnectedRegister(_nofussWebSocketOnConnected);
wsOnKeyCheckRegister(_nofussWebSocketOnKeyCheck);
#endif

#if TERMINAL_SUPPORT
Expand Down
3 changes: 1 addition & 2 deletions code/espurna/ntp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ void ntpSetup() {
});

#if WEB_SUPPORT
wsOnConnectedRegister(_ntpWebSocketOnConnected);
wsOnKeyCheckRegister(_ntpWebSocketOnKeyCheck);
wsRegister((ws_callbacks_t) { _ntpWebSocketOnConnected, nullptr, _ntpWebSocketOnKeyCheck });
#endif

// Main callbacks
Expand Down
8 changes: 5 additions & 3 deletions code/espurna/relay.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions code/espurna/rfbridge.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<const char*>());
}

bool _rfbWebSocketOnReceive(const char * key, JsonVariant& value) {
bool _rfbWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
return (strncmp(key, "rfb", 3) == 0);
}

Expand Down Expand Up @@ -768,9 +768,9 @@ void rfbSetup() {
#endif

#if WEB_SUPPORT
wsOnSendRegister(_rfbWebSocketOnSend);
wsOnConnectedRegister(_rfbWebSocketOnConnected);
wsOnActionRegister(_rfbWebSocketOnAction);
wsOnReceiveRegister(_rfbWebSocketOnReceive);
wsOnKeyCheckRegister(_rfbWebSocketOnKeyCheck);
#endif

#if TERMINAL_SUPPORT
Expand Down
8 changes: 4 additions & 4 deletions code/espurna/rfm69.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions code/espurna/scheduler.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 20 additions & 8 deletions code/espurna/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T> void _sensorWebSocketMagnitudes(JsonObject& root, T prefix) {

// ws produces flat list <prefix>Magnitudes
String ws_name = String(prefix);
ws_name.concat("Magnitudes");
const String ws_name = String(prefix) + "Magnitudes";

// config uses <prefix>Magnitude<index> (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();
Expand All @@ -136,7 +136,21 @@ template<typename T> void _sensorWebSocketMagnitudes(JsonObject& root, T prefix)
}
}

bool _sensorWebSocketOnReceive(const char * key, JsonVariant& value) {
/*
template<typename T> void _sensorWebSocketMagnitudes(JsonObject& root, T prefix) {
// ws produces flat list <prefix>Magnitudes
const String ws_name = String(prefix) + "Magnitudes";
// config uses <prefix>Magnitude<index> (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;
Expand Down Expand Up @@ -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++) {

Expand Down Expand Up @@ -1437,9 +1451,7 @@ void sensorSetup() {

// Websockets
#if WEB_SUPPORT
wsOnSendRegister(_sensorWebSocketStart);
wsOnReceiveRegister(_sensorWebSocketOnReceive);
wsOnSendRegister(_sensorWebSocketSendData);
wsRegister({ _sensorWebSocketOnConnected, nullptr, _sensorWebSocketOnKeyCheck });
#endif

// API
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/system.ino
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ void systemSetup() {
#endif

#if WEB_SUPPORT
wsOnKeyCheckRegister(_systemWebSocketOnKeyCheck);
wsRegister((ws_callbacks_t) { nullptr, nullptr, _systemWebSocketOnKeyCheck });
#endif

// Init device-specific hardware
Expand Down
3 changes: 1 addition & 2 deletions code/espurna/telnet.ino
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,7 @@ void telnetSetup() {
#endif

#if WEB_SUPPORT
wsOnConnectedRegister(_telnetWebSocketOnConnected);
wsOnKeyCheckRegister(_telnetWebSocketOnKeyCheck);
wsRegister((ws_callbacks_t) { _telnetWebSocketOnConnected, nullptr, _telnetWebSocketOnKeyCheck });
#endif

espurnaRegisterReload(_telnetConfigure);
Expand Down
Loading

0 comments on commit ff75e5c

Please sign in to comment.