Browse Source

Check which keys should be saved and which ones shouldn't (#730)

rfm69
Xose Pérez 6 years ago
parent
commit
fa20c47f29
22 changed files with 2545 additions and 2386 deletions
  1. +6
    -0
      code/espurna/alexa.ino
  2. +5
    -0
      code/espurna/api.ino
  3. +14
    -1
      code/espurna/button.ino
  4. +3
    -0
      code/espurna/config/prototypes.h
  5. BIN
      code/espurna/data/index.html.gz
  6. +5
    -0
      code/espurna/domoticz.ino
  7. +5
    -0
      code/espurna/homeassistant.ino
  8. +5
    -0
      code/espurna/influxdb.ino
  9. +7
    -0
      code/espurna/led.ino
  10. +7
    -0
      code/espurna/light.ino
  11. +5
    -0
      code/espurna/mqtt.ino
  12. +9
    -0
      code/espurna/nofuss.ino
  13. +9
    -0
      code/espurna/ntp.ino
  14. +5
    -0
      code/espurna/relay.ino
  15. +5
    -0
      code/espurna/scheduler.ino
  16. +15
    -2
      code/espurna/sensor.ino
  17. +2382
    -2382
      code/espurna/static/index.html.gz.h
  18. +9
    -0
      code/espurna/telnet.ino
  19. +9
    -0
      code/espurna/thinkspeak.ino
  20. +12
    -0
      code/espurna/wifi.ino
  21. +27
    -0
      code/espurna/ws.ino
  22. +1
    -1
      code/html/index.html

+ 6
- 0
code/espurna/alexa.ino View File

@ -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;
@ -46,6 +51,7 @@ void alexaSetup() {
// Websockets
wsOnSendRegister(_alexaWebSocketOnSend);
wsOnAfterParseRegister(_alexaConfigure);
wsOnReceiveRegister(_alexaWebSocketOnReceive);
#endif


+ 5
- 0
code/espurna/api.ino View File

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

+ 14
- 1
code/espurna/button.ino View File

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


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

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


BIN
code/espurna/data/index.html.gz View File


+ 5
- 0
code/espurna/domoticz.ino View File

@ -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;
@ -145,6 +149,7 @@ void domoticzSetup() {
#if WEB_SUPPORT
wsOnSendRegister(_domoticzWebSocketOnSend);
wsOnAfterParseRegister(_domoticzConfigure);
wsOnReceiveRegister(_domoticzWebSocketOnReceive);
#endif
mqttRegister(_domoticzMqtt);
}


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

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


+ 5
- 0
code/espurna/influxdb.ino View File

@ -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;
@ -100,6 +104,7 @@ void idbSetup() {
#if WEB_SUPPORT
wsOnSendRegister(_idbWebSocketOnSend);
wsOnAfterParseRegister(_idbConfigure);
wsOnReceiveRegister(_idbWebSocketOnReceive);
#endif
}


+ 7
- 0
code/espurna/led.ino View File

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


+ 7
- 0
code/espurna/light.ino View File

@ -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");
@ -1056,6 +1062,7 @@ void lightSetup() {
_lightAPISetup();
wsOnSendRegister(_lightWebSocketOnSend);
wsOnActionRegister(_lightWebSocketOnAction);
wsOnReceiveRegister(_lightWebSocketOnReceive);
wsOnAfterParseRegister([]() {
#if LIGHT_SAVE_ENABLED == 0
lightSave();


+ 5
- 0
code/espurna/mqtt.ino View File

@ -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();
@ -795,6 +799,7 @@ void mqttSetup() {
#if WEB_SUPPORT
wsOnSendRegister(_mqttWebSocketOnSend);
wsOnAfterParseRegister(_mqttConfigure);
wsOnReceiveRegister(_mqttWebSocketOnReceive);
#endif
#if TERMINAL_SUPPORT


+ 9
- 0
code/espurna/nofuss.ino View File

@ -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);
@ -147,6 +155,7 @@ void nofussSetup() {
#if WEB_SUPPORT
wsOnSendRegister(_nofussWebSocketOnSend);
wsOnAfterParseRegister(_nofussConfigure);
wsOnReceiveRegister(_nofussWebSocketOnReceive);
#endif
#if TERMINAL_SUPPORT


+ 9
- 0
code/espurna/ntp.ino View File

@ -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);
@ -31,6 +37,8 @@ void _ntpWebSocketOnSend(JsonObject& root) {
if (ntpSynced()) root["now"] = now();
}
#endif
void _ntpStart() {
_ntp_start = 0;
@ -159,6 +167,7 @@ void ntpSetup() {
#if WEB_SUPPORT
wsOnSendRegister(_ntpWebSocketOnSend);
wsOnReceiveRegister(_ntpWebSocketOnReceive);
wsOnAfterParseRegister([]() { _ntp_configure = true; });
#endif


+ 5
- 0
code/espurna/relay.ino View File

@ -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++) {
@ -577,6 +581,7 @@ void relaySetupWS() {
wsOnSendRegister(_relayWebSocketOnStart);
wsOnActionRegister(_relayWebSocketOnAction);
wsOnAfterParseRegister(_relayConfigure);
wsOnReceiveRegister(_relayWebSocketOnReceive);
}
#endif // WEB_SUPPORT


+ 5
- 0
code/espurna/scheduler.ino View File

@ -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) {
@ -201,6 +205,7 @@ void schSetup() {
// Update websocket clients
#if WEB_SUPPORT
wsOnSendRegister(_schWebSocketOnSend);
wsOnReceiveRegister(_schWebSocketOnReceive);
wsOnAfterParseRegister(_schConfigure);
#endif


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

@ -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];
@ -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;
@ -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();
@ -774,6 +783,9 @@ String magnitudeUnits(unsigned char type) {
void sensorSetup() {
// Backwards compatibility
moveSetting("powerUnits", "pwrUnits");
// Load sensors
_sensorLoad();
_sensorInit();
@ -785,6 +797,7 @@ void sensorSetup() {
// Websockets
wsOnSendRegister(_sensorWebSocketStart);
wsOnReceiveRegister(_sensorWebSocketOnReceive);
wsOnSendRegister(_sensorWebSocketSendData);
wsOnAfterParseRegister(_sensorConfigure);


+ 2382
- 2382
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 9
- 0
code/espurna/telnet.ino View File

@ -20,11 +20,19 @@ bool _telnetFirst = true;
// Private methods
// -----------------------------------------------------------------------------
#if WEB_SUPPORT
bool _telnetWebSocketOnReceive(const char * key, JsonVariant& value) {
return (strncmp(key, "telnet", 6) == 0);
}
void _telnetWebSocketOnSend(JsonObject& root) {
root["telnetVisible"] = 1;
root["telnetSTA"] = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
}
#endif
void _telnetDisconnect(unsigned char clientId) {
_telnetClients[clientId]->free();
_telnetClients[clientId] = NULL;
@ -169,6 +177,7 @@ void telnetSetup() {
#if WEB_SUPPORT
wsOnSendRegister(_telnetWebSocketOnSend);
wsOnReceiveRegister(_telnetWebSocketOnReceive);
#endif
DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT);


+ 9
- 0
code/espurna/thinkspeak.ino View File

@ -32,6 +32,12 @@ unsigned long _tspk_last_flush = 0;
// -----------------------------------------------------------------------------
#if WEB_SUPPORT
bool _tspkWebSocketOnReceive(const char * key, JsonVariant& value) {
return (strncmp(key, "tspk", 4) == 0);
}
void _tspkWebSocketOnSend(JsonObject& root) {
unsigned char visible = 0;
@ -61,6 +67,8 @@ void _tspkWebSocketOnSend(JsonObject& root) {
}
#endif
void _tspkConfigure() {
_tspk_enabled = getSetting("tspkEnabled", THINGSPEAK_ENABLED).toInt() == 1;
if (_tspk_enabled && (getSetting("tspkKey").length() == 0)) {
@ -248,6 +256,7 @@ void tspkSetup() {
#if WEB_SUPPORT
wsOnSendRegister(_tspkWebSocketOnSend);
wsOnAfterParseRegister(_tspkConfigure);
wsOnReceiveRegister(_tspkWebSocketOnReceive);
#endif
DEBUG_MSG_P(PSTR("[THINGSPEAK] Async %s, SSL %s\n"),


+ 12
- 0
code/espurna/wifi.ino View File

@ -290,6 +290,17 @@ void _wifiInitCommands() {
#if WEB_SUPPORT
bool _wifiWebSocketOnReceive(const char * key, JsonVariant& value) {
if (strncmp(key, "wifi", 4) == 0) return true;
if (strncmp(key, "ssid", 4) == 0) return true;
if (strncmp(key, "pass", 4) == 0) return true;
if (strncmp(key, "ip", 2) == 0) return true;
if (strncmp(key, "gw", 2) == 0) return true;
if (strncmp(key, "mask", 4) == 0) return true;
if (strncmp(key, "dns", 3) == 0) return true;
return false;
}
void _wifiWebSocketOnSend(JsonObject& root) {
root["maxNetworks"] = WIFI_MAX_NETWORKS;
root["wifiScan"] = getSetting("wifiScan", WIFI_SCAN_NETWORKS).toInt() == 1;
@ -417,6 +428,7 @@ void wifiSetup() {
#if WEB_SUPPORT
wsOnSendRegister(_wifiWebSocketOnSend);
wsOnReceiveRegister(_wifiWebSocketOnReceive);
wsOnAfterParseRegister(_wifiConfigure);
wsOnActionRegister(_wifiWebSocketOnAction);
#endif


+ 27
- 0
code/espurna/ws.ino View File

@ -21,6 +21,7 @@ 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;
// -----------------------------------------------------------------------------
// Private methods
@ -170,6 +171,19 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) {
continue;
}
// Check if key has to be processed
bool found = false;
for (unsigned char i = 0; i < _ws_on_receive_callbacks.size(); i++) {
found |= (_ws_on_receive_callbacks[i])(key.c_str(), value);
// TODO: remove this to call all OnReceiveCallbacks with the
// current key/value
if (found) break;
}
if (!found) {
delSetting(key);
continue;
}
// Store values
if (value.is<JsonArray&>()) {
if (_wsStore(key, value.as<JsonArray&>())) changed = true;
@ -227,6 +241,14 @@ void _wsUpdate(JsonObject& root) {
#endif
}
bool _wsOnReceive(const char * key, JsonVariant& value) {
if (strncmp(key, "ws", 2) == 0) return true;
if (strncmp(key, "admin", 5) == 0) return true;
if (strncmp(key, "hostname", 8) == 0) return true;
if (strncmp(key, "webPort", 7) == 0) return true;
return false;
}
void _wsOnStart(JsonObject& root) {
#if USE_PASSWORD && WEB_FORCE_PASS_CHANGE
@ -340,6 +362,10 @@ void wsOnSendRegister(ws_on_send_callback_f callback) {
_ws_on_send_callbacks.push_back(callback);
}
void wsOnReceiveRegister(ws_on_receive_callback_f callback) {
_ws_on_receive_callbacks.push_back(callback);
}
void wsOnActionRegister(ws_on_action_callback_f callback) {
_ws_on_action_callbacks.push_back(callback);
}
@ -411,6 +437,7 @@ void wsSetup() {
mqttRegister(_wsMQTTCallback);
#endif
wsOnSendRegister(_wsOnStart);
wsOnReceiveRegister(_wsOnReceive);
wsOnAfterParseRegister(wsConfigure);
espurnaRegisterLoop(_wsLoop);
}


+ 1
- 1
code/html/index.html View File

@ -1055,7 +1055,7 @@
<div class="pure-g module module-pwr">
<label class="pure-u-1 pure-u-lg-1-4">Power units</label>
<select name="powerUnits" tabindex="16" class="pure-u-1 pure-u-lg-1-4">
<select name="pwrUnits" tabindex="16" class="pure-u-1 pure-u-lg-1-4">
<option value="0">Watts (W)</option>
<option value="1">Kilowatts (kW)</option>
</select>


Loading…
Cancel
Save