Skip to content

Commit

Permalink
Check which keys should be saved and which ones shouldn't (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Apr 3, 2018
1 parent 8a9e72e commit fa20c47
Show file tree
Hide file tree
Showing 22 changed files with 2,545 additions and 2,386 deletions.
6 changes: 6 additions & 0 deletions code/espurna/alexa.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ static std::queue<AlexaDevChange> _alexa_dev_changes;
// -----------------------------------------------------------------------------
// ALEXA
// -----------------------------------------------------------------------------

bool _alexaWebSocketOnReceive(const char * key, JsonVariant& value) {
return (strncmp(key, "alexa", 5) == 0);
}

void _alexaWebSocketOnSend(JsonObject& root) {
root["alexaVisible"] = 1;
root["alexaEnabled"] = getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1;
Expand All @@ -46,6 +51,7 @@ void alexaSetup() {
// Websockets
wsOnSendRegister(_alexaWebSocketOnSend);
wsOnAfterParseRegister(_alexaConfigure);
wsOnReceiveRegister(_alexaWebSocketOnReceive);

#endif

Expand Down
5 changes: 5 additions & 0 deletions code/espurna/api.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ std::vector<web_api_t> _apis;

// -----------------------------------------------------------------------------

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

void _apiWebSocketOnSend(JsonObject& root) {
root["apiEnabled"] = getSetting("apiEnabled", API_ENABLED).toInt() == 1;
root["apiKey"] = getSetting("apiKey");
Expand Down Expand Up @@ -187,6 +191,7 @@ void apiSetup() {
webServer()->on("/apis", HTTP_GET, _onAPIs);
webServer()->on("/rpc", HTTP_GET, _onRPC);
wsOnSendRegister(_apiWebSocketOnSend);
wsOnReceiveRegister(_apiWebSocketOnReceive);
}

#endif // WEB_SUPPORT
15 changes: 14 additions & 1 deletion code/espurna/button.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ void buttonMQTT(unsigned char id, uint8_t event) {
if (id >= _buttons.size()) return;
char payload[2];
itoa(event, payload, 10);
mqttSend(MQTT_TOPIC_BUTTON, id, payload, false, false); // 1st bool = force, 2nd = retain
mqttSend(MQTT_TOPIC_BUTTON, id, payload, false, false); // 1st bool = force, 2nd = retain
}

#endif

#if WEB_SUPPORT

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

#endif
Expand Down Expand Up @@ -182,6 +190,11 @@ void buttonSetup() {

DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), _buttons.size());

// Websocket Callbacks
#if WEB_SUPPORT
wsOnReceiveRegister(_buttonWebSocketOnReceive);
#endif

// Register loop
espurnaRegisterLoop(buttonLoop);

Expand Down
3 changes: 3 additions & 0 deletions code/espurna/config/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ 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);

// -----------------------------------------------------------------------------
// WIFI
// -----------------------------------------------------------------------------
Expand Down
Binary file modified code/espurna/data/index.html.gz
Binary file not shown.
5 changes: 5 additions & 0 deletions code/espurna/domoticz.ino
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload)

#if WEB_SUPPORT

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

void _domoticzWebSocketOnSend(JsonObject& root) {

root["dczVisible"] = 1;
Expand Down Expand Up @@ -145,6 +149,7 @@ void domoticzSetup() {
#if WEB_SUPPORT
wsOnSendRegister(_domoticzWebSocketOnSend);
wsOnAfterParseRegister(_domoticzConfigure);
wsOnReceiveRegister(_domoticzWebSocketOnReceive);
#endif
mqttRegister(_domoticzMqtt);
}
Expand Down
5 changes: 5 additions & 0 deletions code/espurna/homeassistant.ino
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ void _haConfigure() {

#if WEB_SUPPORT

bool _haWebSocketOnReceive(const char * key, JsonVariant& value) {
return (strncmp(key, "ha", 2) == 0);
}

void _haWebSocketOnSend(JsonObject& root) {
root["haVisible"] = 1;
root["haPrefix"] = getSetting("haPrefix", HOMEASSISTANT_PREFIX);
Expand Down Expand Up @@ -275,6 +279,7 @@ void haSetup() {
wsOnSendRegister(_haWebSocketOnSend);
wsOnAfterParseRegister(_haConfigure);
wsOnActionRegister(_haWebSocketOnAction);
wsOnReceiveRegister(_haWebSocketOnReceive);
#endif

// On MQTT connect check if we have something to send
Expand Down
5 changes: 5 additions & 0 deletions code/espurna/influxdb.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ SyncClient _idb_client;

// -----------------------------------------------------------------------------

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

void _idbWebSocketOnSend(JsonObject& root) {
root["idbVisible"] = 1;
root["idbEnabled"] = getSetting("idbEnabled", INFLUXDB_ENABLED).toInt() == 1;
Expand Down Expand Up @@ -100,6 +104,7 @@ void idbSetup() {
#if WEB_SUPPORT
wsOnSendRegister(_idbWebSocketOnSend);
wsOnAfterParseRegister(_idbConfigure);
wsOnReceiveRegister(_idbWebSocketOnReceive);
#endif
}

Expand Down
7 changes: 7 additions & 0 deletions code/espurna/led.ino
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@ void _ledBlink(unsigned char id, unsigned long delayOff, unsigned long delayOn)
}

#if WEB_SUPPORT

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

void _ledWebSocketOnSend(JsonObject& root) {
if (_ledCount() == 0) return;
root["ledVisible"] = 1;
root["ledMode0"] = _ledMode(0);
}

#endif

#if MQTT_SUPPORT
Expand Down Expand Up @@ -163,6 +169,7 @@ void ledSetup() {
#if WEB_SUPPORT
wsOnSendRegister(_ledWebSocketOnSend);
wsOnAfterParseRegister(_ledConfigure);
wsOnReceiveRegister(_ledWebSocketOnReceive);
#endif

DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size());
Expand Down
7 changes: 7 additions & 0 deletions code/espurna/light.ino
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,12 @@ void lightBrightnessStep(int steps) {

#if WEB_SUPPORT

bool _lightWebSocketOnReceive(const char * key, JsonVariant& value) {
if (strncmp(key, "light", 5) == 0) return true;
if (strncmp(key, "use", 3) == 0) return true;
return false;
}

void _lightWebSocketOnSend(JsonObject& root) {
root["colorVisible"] = 1;
root["mqttGroupColor"] = getSetting("mqttGroupColor");
Expand Down Expand Up @@ -1056,6 +1062,7 @@ void lightSetup() {
_lightAPISetup();
wsOnSendRegister(_lightWebSocketOnSend);
wsOnActionRegister(_lightWebSocketOnAction);
wsOnReceiveRegister(_lightWebSocketOnReceive);
wsOnAfterParseRegister([]() {
#if LIGHT_SAVE_ENABLED == 0
lightSave();
Expand Down
5 changes: 5 additions & 0 deletions code/espurna/mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ unsigned long _mqttNextMessageId() {

#if WEB_SUPPORT

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

void _mqttWebSocketOnSend(JsonObject& root) {
root["mqttVisible"] = 1;
root["mqttStatus"] = mqttConnected();
Expand Down Expand Up @@ -795,6 +799,7 @@ void mqttSetup() {
#if WEB_SUPPORT
wsOnSendRegister(_mqttWebSocketOnSend);
wsOnAfterParseRegister(_mqttConfigure);
wsOnReceiveRegister(_mqttWebSocketOnReceive);
#endif

#if TERMINAL_SUPPORT
Expand Down
9 changes: 9 additions & 0 deletions code/espurna/nofuss.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ bool _nofussEnabled = false;
// NOFUSS
// -----------------------------------------------------------------------------

#if WEB_SUPPORT

bool _nofussWebSocketOnReceive(const char * key, JsonVariant& value) {
return (strncmp(key, "nofuss", 6) == 0);
}

void _nofussWebSocketOnSend(JsonObject& root) {
root["nofussVisible"] = 1;
root["nofussEnabled"] = getSetting("nofussEnabled", NOFUSS_ENABLED).toInt() == 1;
root["nofussServer"] = getSetting("nofussServer", NOFUSS_SERVER);
}

#endif

void _nofussConfigure() {

String nofussServer = getSetting("nofussServer", NOFUSS_SERVER);
Expand Down Expand Up @@ -147,6 +155,7 @@ void nofussSetup() {
#if WEB_SUPPORT
wsOnSendRegister(_nofussWebSocketOnSend);
wsOnAfterParseRegister(_nofussConfigure);
wsOnReceiveRegister(_nofussWebSocketOnReceive);
#endif

#if TERMINAL_SUPPORT
Expand Down
9 changes: 9 additions & 0 deletions code/espurna/ntp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ bool _ntp_configure = false;
// NTP
// -----------------------------------------------------------------------------

#if WEB_SUPPORT

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

void _ntpWebSocketOnSend(JsonObject& root) {
root["ntpVisible"] = 1;
root["ntpStatus"] = (timeStatus() == timeSet);
Expand All @@ -31,6 +37,8 @@ void _ntpWebSocketOnSend(JsonObject& root) {
if (ntpSynced()) root["now"] = now();
}

#endif

void _ntpStart() {

_ntp_start = 0;
Expand Down Expand Up @@ -159,6 +167,7 @@ void ntpSetup() {

#if WEB_SUPPORT
wsOnSendRegister(_ntpWebSocketOnSend);
wsOnReceiveRegister(_ntpWebSocketOnReceive);
wsOnAfterParseRegister([]() { _ntp_configure = true; });
#endif

Expand Down
5 changes: 5 additions & 0 deletions code/espurna/relay.ino
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,10 @@ void _relayConfigure() {

#if WEB_SUPPORT

bool _relayWebSocketOnReceive(const char * key, JsonVariant& value) {
return (strncmp(key, "relay", 5) == 0);
}

void _relayWebSocketUpdate(JsonObject& root) {
JsonArray& relay = root.createNestedArray("relayStatus");
for (unsigned char i=0; i<relayCount(); i++) {
Expand Down Expand Up @@ -577,6 +581,7 @@ void relaySetupWS() {
wsOnSendRegister(_relayWebSocketOnStart);
wsOnActionRegister(_relayWebSocketOnAction);
wsOnAfterParseRegister(_relayConfigure);
wsOnReceiveRegister(_relayWebSocketOnReceive);
}

#endif // WEB_SUPPORT
Expand Down
5 changes: 5 additions & 0 deletions code/espurna/scheduler.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Adapted by Xose Pérez <xose dot perez at gmail dot com>

#if WEB_SUPPORT

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

void _schWebSocketOnSend(JsonObject &root){

if (relayCount() > 0) {
Expand Down Expand Up @@ -201,6 +205,7 @@ void schSetup() {
// Update websocket clients
#if WEB_SUPPORT
wsOnSendRegister(_schWebSocketOnSend);
wsOnReceiveRegister(_schWebSocketOnReceive);
wsOnAfterParseRegister(_schConfigure);
#endif

Expand Down
17 changes: 15 additions & 2 deletions code/espurna/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ double _magnitudeProcess(unsigned char type, double value) {

#if WEB_SUPPORT

bool _sensorWebSocketOnReceive(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;
if (strncmp(key, "hum", 3) == 0) return true;
if (strncmp(key, "energy", 6) == 0) return true;
return false;
}

void _sensorWebSocketSendData(JsonObject& root) {

char buffer[10];
Expand Down Expand Up @@ -168,7 +177,7 @@ void _sensorWebSocketStart(JsonObject& root) {
if (_magnitudes.size() > 0) {
root["sensorsVisible"] = 1;
//root["apiRealTime"] = _sensor_realtime;
root["powerUnits"] = _sensor_power_units;
root["pwrUnits"] = _sensor_power_units;
root["energyUnits"] = _sensor_energy_units;
root["tmpUnits"] = _sensor_temperature_units;
root["tmpCorrection"] = _sensor_temperature_correction;
Expand Down Expand Up @@ -587,7 +596,7 @@ void _sensorConfigure() {
_sensor_read_interval = 1000 * constrain(getSetting("snsRead", SENSOR_READ_INTERVAL).toInt(), SENSOR_READ_MIN_INTERVAL, SENSOR_READ_MAX_INTERVAL);
_sensor_report_every = constrain(getSetting("snsReport", SENSOR_REPORT_EVERY).toInt(), SENSOR_REPORT_MIN_EVERY, SENSOR_REPORT_MAX_EVERY);
_sensor_realtime = getSetting("apiRealTime", API_REAL_TIME_VALUES).toInt() == 1;
_sensor_power_units = getSetting("powerUnits", SENSOR_POWER_UNITS).toInt();
_sensor_power_units = getSetting("pwrUnits", SENSOR_POWER_UNITS).toInt();
_sensor_energy_units = getSetting("energyUnits", SENSOR_ENERGY_UNITS).toInt();
_sensor_temperature_units = getSetting("tmpUnits", SENSOR_TEMPERATURE_UNITS).toInt();
_sensor_temperature_correction = getSetting("tmpCorrection", SENSOR_TEMPERATURE_CORRECTION).toFloat();
Expand Down Expand Up @@ -774,6 +783,9 @@ String magnitudeUnits(unsigned char type) {

void sensorSetup() {

// Backwards compatibility
moveSetting("powerUnits", "pwrUnits");

// Load sensors
_sensorLoad();
_sensorInit();
Expand All @@ -785,6 +797,7 @@ void sensorSetup() {

// Websockets
wsOnSendRegister(_sensorWebSocketStart);
wsOnReceiveRegister(_sensorWebSocketOnReceive);
wsOnSendRegister(_sensorWebSocketSendData);
wsOnAfterParseRegister(_sensorConfigure);

Expand Down
Loading

0 comments on commit fa20c47

Please sign in to comment.