diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index ecb86391..d9b5d435 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -184,6 +184,8 @@ void webRequestRegister(web_request_callback_f callback); #if WEB_SUPPORT typedef std::function ws_on_send_callback_f; void wsOnSendRegister(ws_on_send_callback_f callback); + void wsSend(uint32_t, JsonObject& root); + void wsSend(JsonObject& root); void wsSend(ws_on_send_callback_f sender); typedef std::function ws_on_action_callback_f; @@ -194,6 +196,7 @@ void webRequestRegister(web_request_callback_f callback); bool wsConnected(); bool wsConnected(uint32_t); + bool wsDebugSend(const char*, const char*); #else #define ws_on_send_callback_f void * #define ws_on_action_callback_f void * diff --git a/code/espurna/debug.ino b/code/espurna/debug.ino index b4cd80a2..2969d0ee 100644 --- a/code/espurna/debug.ino +++ b/code/espurna/debug.ino @@ -16,22 +16,43 @@ char _udp_syslog_header[40] = {0}; #endif #endif -void _debugSend(char * message) { +#if DEBUG_SERIAL_SUPPORT + void _debugSendSerial(const char* prefix, const char* data) { + if (prefix && (prefix[0] != '\0')) { + Serial.print(prefix); + } + Serial.print(data); + + } +#endif + +#if DEBUG_TELNET_SUPPORT + void _debugSendTelnet(const char* prefix, const char* data) { + if (prefix && (prefix[0] != '\0')) { + _telnetWrite(prefix); + } + _telnetWrite(data); + + } +#endif + +void _debugSend(const char * message) { + + const size_t msg_len = strlen(message); bool pause = false; + char timestamp[10] = {0}; #if DEBUG_ADD_TIMESTAMP static bool add_timestamp = true; - char timestamp[10] = {0}; - if (add_timestamp) snprintf_P(timestamp, sizeof(timestamp), PSTR("[%06lu] "), millis() % 1000000); - add_timestamp = (message[strlen(message)-1] == 10) || (message[strlen(message)-1] == 13); + if (add_timestamp) { + snprintf(timestamp, sizeof(timestamp), "[%06lu] ", millis() % 1000000); + } + add_timestamp = (message[msg_len - 1] == 10) || (message[msg_len - 1] == 13); #endif #if DEBUG_SERIAL_SUPPORT - #if DEBUG_ADD_TIMESTAMP - DEBUG_PORT.printf(timestamp); - #endif - DEBUG_PORT.printf(message); + _debugSendSerial(timestamp, message); #endif #if DEBUG_UDP_SUPPORT @@ -51,31 +72,13 @@ void _debugSend(char * message) { #endif #if DEBUG_TELNET_SUPPORT - #if DEBUG_ADD_TIMESTAMP - _telnetWrite(timestamp, strlen(timestamp)); - #endif - _telnetWrite(message, strlen(message)); + _debugSendTelnet(timestamp, message); pause = true; #endif #if DEBUG_WEB_SUPPORT - if (wsConnected() && (getFreeHeap() > 10000)) { - DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(1) + strlen(message) + 17); - JsonObject &root = jsonBuffer.createObject(); - #if DEBUG_ADD_TIMESTAMP - char buffer[strlen(timestamp) + strlen(message) + 1]; - snprintf_P(buffer, sizeof(buffer), "%s%s", timestamp, message); - root.set("weblog", buffer); - #else - root.set("weblog", message); - #endif - String out; - root.printTo(out); - jsonBuffer.clear(); - - wsSend(out.c_str()); - pause = true; - } + wsDebugSend(timestamp, message); + pause = true; #endif if (pause) optimistic_yield(100); diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index b6e65c8a..6482f4c5 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -183,7 +183,7 @@ bool _domoticzWebSocketOnReceive(const char * key, JsonVariant& value) { void _domoticzWebSocketOnSend(JsonObject& root) { - root["dczVisible"] = 1; + unsigned char visible = 0; root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1; root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC); root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC); @@ -192,18 +192,15 @@ void _domoticzWebSocketOnSend(JsonObject& root) { for (unsigned char i=0; i 0); #if SENSOR_SUPPORT - JsonArray& list = root.createNestedArray("dczMagnitudes"); - for (byte i=0; i 0); #endif + root["dczVisible"] = visible; + } #endif // WEB_SUPPORT diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 9ed554d2..16b72d33 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -669,7 +669,7 @@ void _lightComms(unsigned char mask) { // Report color to WS clients (using current brightness setting) #if WEB_SUPPORT - wsSend(_lightWebSocketOnSend); + wsSend(_lightWebSocketStatus); #endif // Report channels to local broker @@ -824,23 +824,13 @@ bool _lightWebSocketOnReceive(const char * key, JsonVariant& value) { return false; } -void _lightWebSocketOnSend(JsonObject& root) { - root["colorVisible"] = 1; - root["mqttGroupColor"] = getSetting("mqttGroupColor"); - root["useColor"] = _light_has_color; - root["useWhite"] = _light_use_white; - root["useGamma"] = _light_use_gamma; - root["useTransitions"] = _light_use_transitions; - root["lightTime"] = _light_transition_time; - root["useCSS"] = getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1; - bool useRGB = getSetting("useRGB", LIGHT_USE_RGB).toInt() == 1; - root["useRGB"] = useRGB; +void _lightWebSocketStatus(JsonObject& root) { if (_light_has_color) { if (_light_use_cct) { root["useCCT"] = _light_use_cct; root["mireds"] = _light_mireds; } - if (useRGB) { + if (getSetting("useRGB", LIGHT_USE_RGB).toInt() == 1) { root["rgb"] = lightColor(true); } else { root["hsv"] = lightColor(false); @@ -850,7 +840,20 @@ void _lightWebSocketOnSend(JsonObject& root) { for (unsigned char id=0; id < _light_channel.size(); id++) { channels.add(lightChannel(id)); } - root["brightness"] = lightBrightness(); +} + +void _lightWebSocketOnSend(JsonObject& root) { + root["colorVisible"] = 1; + root["mqttGroupColor"] = getSetting("mqttGroupColor"); + root["useColor"] = _light_has_color; + root["useWhite"] = _light_use_white; + root["useGamma"] = _light_use_gamma; + root["useTransitions"] = _light_use_transitions; + root["useCSS"] = getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1; + root["useRGB"] = getSetting("useRGB", LIGHT_USE_RGB).toInt() == 1; + root["lightTime"] = _light_transition_time; + + _lightWebSocketStatus(root); } void _lightWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) { diff --git a/code/espurna/ntp.ino b/code/espurna/ntp.ino index 555017c5..2ffd41d9 100644 --- a/code/espurna/ntp.ino +++ b/code/espurna/ntp.ino @@ -34,7 +34,6 @@ void _ntpWebSocketOnSend(JsonObject& root) { root["ntpOffset"] = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt(); root["ntpDST"] = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1; root["ntpRegion"] = getSetting("ntpRegion", NTP_DST_REGION).toInt(); - if (ntpSynced()) root["now"] = now(); } #endif diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index fa9fe407..b8b985f6 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -586,65 +586,78 @@ bool _relayWebSocketOnReceive(const char * key, JsonVariant& value) { void _relayWebSocketUpdate(JsonObject& root) { JsonArray& relay = root.createNestedArray("relayStatus"); for (unsigned char i=0; i(_relays[i].target_status); } } -void _relayWebSocketSendRelay(unsigned char i) { +String _relayFriendlyName(unsigned char i) { + String res = String("GPIO") + String(_relays[i].pin); - DynamicJsonBuffer jsonBuffer; - JsonObject& root = jsonBuffer.createObject(); - JsonArray& config = root.createNestedArray("relayConfig"); - JsonObject& line = config.createNestedObject(); - - line["id"] = i; if (GPIO_NONE == _relays[i].pin) { #if (RELAY_PROVIDER == RELAY_PROVIDER_LIGHT) uint8_t physical = _relays.size() - DUMMY_RELAY_COUNT; if (i >= physical) { if (DUMMY_RELAY_COUNT == lightChannels()) { - line["gpio"] = String("CH") + String(i-physical); + res = String("CH") + String(i-physical); } else if (DUMMY_RELAY_COUNT == (lightChannels() + 1u)) { if (physical == i) { - line["gpio"] = String("Light"); + res = String("Light"); } else { - line["gpio"] = String("CH") + String(i-1-physical); + res = String("CH") + String(i-1-physical); } } else { - line["gpio"] = String("Light"); + res = String("Light"); } } else { - line["gpio"] = String("?"); + res = String("?"); } #else - line["gpio"] = String("SW") + String(i); + res = String("SW") + String(i); #endif - } else { - line["gpio"] = String("GPIO") + String(_relays[i].pin); } - - line["type"] = _relays[i].type; - line["reset"] = _relays[i].reset_pin; - line["boot"] = getSetting("relayBoot", i, RELAY_BOOT_MODE).toInt(); - line["pulse"] = _relays[i].pulse; - line["pulse_ms"] = _relays[i].pulse_ms / 1000.0; - #if MQTT_SUPPORT - line["group"] = getSetting("mqttGroup", i, ""); - line["group_inv"] = getSetting("mqttGroupInv", i, 0).toInt(); - line["on_disc"] = getSetting("relayOnDisc", i, 0).toInt(); - #endif - - String output; - root.printTo(output); - jsonBuffer.clear(); - wsSend((char *) output.c_str()); + return res; } void _relayWebSocketSendRelays() { + DynamicJsonBuffer jsonBuffer; + JsonObject& root = jsonBuffer.createObject(); + JsonObject& relays = root.createNestedObject("relayConfig"); + + relays["size"] = relayCount(); + relays["start"] = 0; + + JsonArray& gpio = relays.createNestedArray("gpio"); + JsonArray& type = relays.createNestedArray("type"); + JsonArray& reset = relays.createNestedArray("reset"); + JsonArray& boot = relays.createNestedArray("boot"); + JsonArray& pulse = relays.createNestedArray("pulse"); + JsonArray& pulse_time = relays.createNestedArray("pulse_time"); + + #if MQTT_SUPPORT + JsonArray& group = relays.createNestedArray("group"); + JsonArray& group_inverse = relays.createNestedArray("group_inv"); + JsonArray& on_disconnect = relays.createNestedArray("on_disc"); + #endif + for (unsigned char i=0; i 0) { - - root["schVisible"] = 1; - root["maxSchedules"] = SCHEDULER_MAX_SCHEDULES; - JsonArray &sch = root.createNestedArray("schedule"); - for (byte i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) { - if (!hasSetting("schSwitch", i)) break; - JsonObject &scheduler = sch.createNestedObject(); - scheduler["schEnabled"] = getSetting("schEnabled", i, 1).toInt() == 1; - scheduler["schSwitch"] = getSetting("schSwitch", i, 0).toInt(); - scheduler["schAction"] = getSetting("schAction", i, 0).toInt(); - scheduler["schType"] = getSetting("schType", i, 0).toInt(); - scheduler["schHour"] = getSetting("schHour", i, 0).toInt(); - scheduler["schMinute"] = getSetting("schMinute", i, 0).toInt(); - scheduler["schUTC"] = getSetting("schUTC", i, 0).toInt() == 1; - scheduler["schWDs"] = getSetting("schWDs", i, ""); - } - + if (!relayCount()) return; + + root["schVisible"] = 1; + root["maxSchedules"] = SCHEDULER_MAX_SCHEDULES; + + JsonObject &schedules = root.createNestedObject("schedules"); + uint8_t size = 0; + + JsonArray& enabled = schedules.createNestedArray("schEnabled"); + JsonArray& switch_ = schedules.createNestedArray("schSwitch"); + JsonArray& action = schedules.createNestedArray("schAction"); + JsonArray& type = schedules.createNestedArray("schType"); + JsonArray& hour = schedules.createNestedArray("schHour"); + JsonArray& minute = schedules.createNestedArray("schMinute"); + JsonArray& utc = schedules.createNestedArray("schUTC"); + JsonArray& weekdays = schedules.createNestedArray("schWDs"); + + for (byte i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) { + if (!hasSetting("schSwitch", i)) break; + ++size; + + enabled.add(getSetting("schEnabled", i, 1).toInt() == 1); + utc.add(getSetting("schUTC", i, 0).toInt() == 1); + + switch_.add(getSetting("schSwitch", i, 0).toInt()); + action.add(getSetting("schAction", i, 0).toInt()); + type.add(getSetting("schType", i, 0).toInt()); + hour.add(getSetting("schHour", i, 0).toInt()); + minute.add(getSetting("schMinute", i, 0).toInt()); + weekdays.add(getSetting("schWDs", i, "")); } + schedules["size"] = size; + schedules["start"] = 0; + } #endif // WEB_SUPPORT diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 4dc84e7b..a67d9c62 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -102,6 +102,32 @@ double _magnitudeProcess(unsigned char type, double value) { #if WEB_SUPPORT +template +void _sensorWebSocketMagnitudes(JsonObject& root, T prefix) { + + // ws produces flat list Magnitudes + String ws_name = String(prefix); + ws_name.concat("Magnitudes"); + + // config uses Magnitude (cut 's') + String conf_name = ws_name.substring(0, ws_name.length() - 1); + + JsonObject& list = root.createNestedObject(ws_name); + list["size"] = magnitudeCount(); + + JsonArray& name = list.createNestedArray("name"); + JsonArray& type = list.createNestedArray("type"); + JsonArray& index = list.createNestedArray("index"); + JsonArray& idx = list.createNestedArray("idx"); + + for (unsigned char i=0; ierror(); + index.add(magnitude.global); + type.add(magnitude.type); + value.add(buffer); + units.add(magnitudeUnits(magnitude.type)); + error.add(magnitude.sensor->error()); if (magnitude.type == MAGNITUDE_ENERGY) { if (_sensor_energy_reset_ts.length() == 0) _sensorResetTS(); - element["description"] = magnitude.sensor->slot(magnitude.local) + String(" (since ") + _sensor_energy_reset_ts + String(")"); + description.add(magnitude.sensor->slot(magnitude.local) + String(" (since ") + _sensor_energy_reset_ts + String(")")); } else { - element["description"] = magnitude.sensor->slot(magnitude.local); + description.add(magnitude.sensor->slot(magnitude.local)); } if (magnitude.type == MAGNITUDE_TEMPERATURE) hasTemperature = true; @@ -148,6 +183,8 @@ void _sensorWebSocketSendData(JsonObject& root) { #endif } + magnitudes["size"] = size; + if (hasTemperature) root["temperatureVisible"] = 1; if (hasHumidity) root["humidityVisible"] = 1; if (hasMICS) root["micsVisible"] = 1; @@ -210,7 +247,7 @@ void _sensorWebSocketStart(JsonObject& root) { } - if (_magnitudes.size() > 0) { + if (magnitudeCount()) { root["snsVisible"] = 1; //root["apiRealTime"] = _sensor_realtime; root["pwrUnits"] = _sensor_power_units; diff --git a/code/espurna/telnet.ino b/code/espurna/telnet.ino index b4b53c7c..3108089f 100644 --- a/code/espurna/telnet.ino +++ b/code/espurna/telnet.ino @@ -45,14 +45,14 @@ void _telnetDisconnect(unsigned char clientId) { DEBUG_MSG_P(PSTR("[TELNET] Client #%d disconnected\n"), clientId); } -bool _telnetWrite(unsigned char clientId, void *data, size_t len) { +bool _telnetWrite(unsigned char clientId, const char *data, size_t len) { if (_telnetClients[clientId] && _telnetClients[clientId]->connected()) { - return (_telnetClients[clientId]->write((const char*) data, len) > 0); + return (_telnetClients[clientId]->write(data, len) > 0); } return false; } -unsigned char _telnetWrite(void *data, size_t len) { +unsigned char _telnetWrite(const char *data, size_t len) { unsigned char count = 0; for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) { // Do not send broadcast messages to unauthenticated clients @@ -65,8 +65,12 @@ unsigned char _telnetWrite(void *data, size_t len) { return count; } +unsigned char _telnetWrite(const char *data) { + return _telnetWrite(data, strlen(data)); +} + bool _telnetWrite(unsigned char clientId, const char * message) { - return _telnetWrite(clientId, (void *) message, strlen(message)); + return _telnetWrite(clientId, message, strlen(message)); } void _telnetData(unsigned char clientId, void *data, size_t len) { diff --git a/code/espurna/thinkspeak.ino b/code/espurna/thinkspeak.ino index 306d7666..db789e90 100644 --- a/code/espurna/thinkspeak.ino +++ b/code/espurna/thinkspeak.ino @@ -75,15 +75,8 @@ void _tspkWebSocketOnSend(JsonObject& root) { if (relayCount() > 0) visible = 1; #if SENSOR_SUPPORT - JsonArray& list = root.createNestedArray("tspkMagnitudes"); - for (byte i=0; i 0) visible = 1; + _sensorWebSocketMagnitudes(root, "tspk"); + visible = visible || (magnitudeCount() > 0); #endif root["tspkVisible"] = visible; diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index b8edb129..f5ff11e4 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -73,6 +73,27 @@ bool _wsAuth(AsyncWebSocketClient * client) { } +#if DEBUG_WEB_SUPPORT + +bool wsDebugSend(const char* prefix, const char* message) { + if (!wsConnected()) return false; + if (getFreeHeap() < (strlen(message) * 3)) return false; + + DynamicJsonBuffer jsonBuffer; + JsonObject &root = jsonBuffer.createObject(); + JsonObject &weblog = root.createNestedObject("weblog"); + + weblog.set("message", message); + if (prefix && (prefix[0] != '\0')) { + weblog.set("prefix", prefix); + } + + wsSend(root); + + return true; +} +#endif + // ----------------------------------------------------------------------------- #if MQTT_SUPPORT @@ -289,6 +310,20 @@ void _wsUpdate(JsonObject& root) { #endif } +void _wsDoUpdate(bool reset = false) { + static unsigned long last = 0; + if (reset) { + last = 0; + return; + } + + if (millis() - last > WS_UPDATE_INTERVAL) { + last = millis(); + wsSend(_wsUpdate); + } +} + + bool _wsOnReceive(const char * key, JsonVariant& value) { if (strncmp(key, "ws", 2) == 0) return true; if (strncmp(key, "admin", 5) == 0) return true; @@ -299,70 +334,95 @@ bool _wsOnReceive(const char * key, JsonVariant& value) { } void _wsOnStart(JsonObject& root) { + char chipid[7]; + snprintf_P(chipid, sizeof(chipid), PSTR("%06X"), ESP.getChipId()); + uint8_t * bssid = WiFi.BSSID(); + char bssid_str[20]; + snprintf_P(bssid_str, sizeof(bssid_str), + PSTR("%02X:%02X:%02X:%02X:%02X:%02X"), + bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5] + ); + + root["webMode"] = WEB_MODE_NORMAL; + + root["app_name"] = APP_NAME; + root["app_version"] = APP_VERSION; + root["app_build"] = buildTime(); + #if defined(APP_REVISION) + root["app_revision"] = APP_REVISION; + #endif + root["manufacturer"] = MANUFACTURER; + root["chipid"] = String(chipid); + root["mac"] = WiFi.macAddress(); + root["bssid"] = String(bssid_str); + root["channel"] = WiFi.channel(); + root["device"] = DEVICE; + root["hostname"] = getSetting("hostname"); + root["desc"] = getSetting("desc"); + root["network"] = getNetwork(); + root["deviceip"] = getIP(); + root["sketch_size"] = ESP.getSketchSize(); + root["free_size"] = ESP.getFreeSketchSpace(); + root["sdk"] = ESP.getSdkVersion(); + root["core"] = getCoreVersion(); + + root["btnDelay"] = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt(); + root["webPort"] = getSetting("webPort", WEB_PORT).toInt(); + root["wsAuth"] = getSetting("wsAuth", WS_AUTHENTICATION).toInt() == 1; + #if TERMINAL_SUPPORT + root["cmdVisible"] = 1; + #endif + root["hbMode"] = getSetting("hbMode", HEARTBEAT_MODE).toInt(); + root["hbInterval"] = getSetting("hbInterval", HEARTBEAT_INTERVAL).toInt(); + + _wsDoUpdate(true); + +} + +void wsSend(JsonObject& root) { + size_t len = root.measureLength(); + AsyncWebSocketMessageBuffer* buffer = _ws.makeBuffer(len); + + if (buffer) { + root.printTo(reinterpret_cast(buffer->get()), len + 1); + _ws.textAll(buffer); + } +} +void wsSend(uint32_t client_id, JsonObject& root) { + AsyncWebSocketClient* client = _ws.client(client_id); + if (client == nullptr) return; + + size_t len = root.measureLength(); + AsyncWebSocketMessageBuffer* buffer = _ws.makeBuffer(len); + + if (buffer) { + root.printTo(reinterpret_cast(buffer->get()), len + 1); + client->text(buffer); + } +} + +void _wsStart(uint32_t client_id) { #if USE_PASSWORD && WEB_FORCE_PASS_CHANGE bool changePassword = getAdminPass().equals(ADMIN_PASS); #else bool changePassword = false; #endif - if (changePassword) { + DynamicJsonBuffer jsonBuffer; + JsonObject& root = jsonBuffer.createObject(); + if (changePassword) { root["webMode"] = WEB_MODE_PASSWORD; - - } else { - - char chipid[7]; - snprintf_P(chipid, sizeof(chipid), PSTR("%06X"), ESP.getChipId()); - uint8_t * bssid = WiFi.BSSID(); - char bssid_str[20]; - snprintf_P(bssid_str, sizeof(bssid_str), - PSTR("%02X:%02X:%02X:%02X:%02X:%02X"), - bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5] - ); - - root["webMode"] = WEB_MODE_NORMAL; - - root["app_name"] = APP_NAME; - root["app_version"] = APP_VERSION; - root["app_build"] = buildTime(); - #if defined(APP_REVISION) - root["app_revision"] = APP_REVISION; - #endif - root["manufacturer"] = MANUFACTURER; - root["chipid"] = String(chipid); - root["mac"] = WiFi.macAddress(); - root["bssid"] = String(bssid_str); - root["channel"] = WiFi.channel(); - root["device"] = DEVICE; - root["hostname"] = getSetting("hostname"); - root["desc"] = getSetting("desc"); - root["network"] = getNetwork(); - root["deviceip"] = getIP(); - root["sketch_size"] = ESP.getSketchSize(); - root["free_size"] = ESP.getFreeSketchSpace(); - root["sdk"] = ESP.getSdkVersion(); - root["core"] = getCoreVersion(); - - _wsUpdate(root); - - root["btnDelay"] = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt(); - root["webPort"] = getSetting("webPort", WEB_PORT).toInt(); - root["wsAuth"] = getSetting("wsAuth", WS_AUTHENTICATION).toInt() == 1; - #if TERMINAL_SUPPORT - root["cmdVisible"] = 1; - #endif - root["hbMode"] = getSetting("hbMode", HEARTBEAT_MODE).toInt(); - root["hbInterval"] = getSetting("hbInterval", HEARTBEAT_INTERVAL).toInt(); - + wsSend(root); + return; } -} - -void _wsStart(uint32_t client_id) { - for (unsigned char i = 0; i < _ws_on_send_callbacks.size(); i++) { - wsSend(client_id, _ws_on_send_callbacks[i]); + for (auto& callback : _ws_on_send_callbacks) { + callback(root); } + + wsSend(client_id, root); } void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ @@ -410,12 +470,8 @@ void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTy } void _wsLoop() { - static unsigned long last = 0; if (!wsConnected()) return; - if (millis() - last > WS_UPDATE_INTERVAL) { - last = millis(); - wsSend(_wsUpdate); - } + _wsDoUpdate(); } // ----------------------------------------------------------------------------- @@ -447,10 +503,8 @@ void wsSend(ws_on_send_callback_f callback) { DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); callback(root); - String output; - root.printTo(output); - jsonBuffer.clear(); - _ws.textAll((char *) output.c_str()); + + wsSend(root); } } @@ -469,13 +523,20 @@ void wsSend_P(PGM_P payload) { } void wsSend(uint32_t client_id, ws_on_send_callback_f callback) { + AsyncWebSocketClient* client = _ws.client(client_id); + if (client == nullptr) return; + DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); callback(root); - String output; - root.printTo(output); - jsonBuffer.clear(); - _ws.text(client_id, (char *) output.c_str()); + + size_t len = root.measureLength(); + AsyncWebSocketMessageBuffer* buffer = _ws.makeBuffer(len); + + if (buffer) { + root.printTo(reinterpret_cast(buffer->get()), len + 1); + client->text(buffer); + } } void wsSend(uint32_t client_id, const char * payload) { diff --git a/code/html/custom.js b/code/html/custom.js index 0c79d78a..c3699e69 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -770,12 +770,13 @@ function createMagnitudeList(data, container, template_name) { if (current > 0) { return; } var template = $("#" + template_name + " .pure-g")[0]; - for (var i in data) { - var magnitude = data[i]; + var size = data.size; + + for (var i=0; i div").length / 6; // there are 6 divs per each relay + var current = $("#relayConfig > legend").length; // there is a legend per relay + if (current > 0) { return; } + + var size = data.size; + var start = data.start; + var template = $("#relayConfigTemplate").children(); - for (var i in data) { - - var relay = data[i]; - if (current > relay.id) continue; + for (var i=start; i function initMagnitudes(data) { - // check if already initialized + // check if already initialized (each magnitude is inside div.pure-g) var done = $("#magnitudes > div").length; if (done > 0) { return; } + var size = data.size; + // add templates var template = $("#magnitudeTemplate").children(); - for (var i in data) { - var magnitude = data[i]; + + for (var i=0; i @@ -1324,15 +1344,13 @@ function processData(data) { if ("magnitudes" === key) { initMagnitudes(value); - for (i in value) { - var magnitude = value[i]; - var error = magnitude.error || 0; + for (var i=0; i