From 31a0772e3787d7080408350b65394318367b8ad0 Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Fri, 8 Feb 2019 10:24:06 +0300 Subject: [PATCH] Print each HA config entry separately --- code/espurna/homeassistant.ino | 65 +++++++++++++++++++++++++--------- code/html/custom.js | 5 ++- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/code/espurna/homeassistant.ino b/code/espurna/homeassistant.ino index 30a4722b..e7a289fa 100644 --- a/code/espurna/homeassistant.ino +++ b/code/espurna/homeassistant.ino @@ -9,10 +9,13 @@ Copyright (C) 2017-2018 by Xose PĂ©rez #if HOMEASSISTANT_SUPPORT #include +#include bool _haEnabled = false; bool _haSendFlag = false; +std::queue _ha_send_config; + // ----------------------------------------------------------------------------- // UTILS // ----------------------------------------------------------------------------- @@ -147,9 +150,7 @@ void _haSendSwitches() { // ----------------------------------------------------------------------------- -String _haGetConfig() { - - String output; +void _haDumpConfig(std::function printer) { #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) || (defined(ITEAD_SLAMPHER)) String type = String("light"); @@ -163,8 +164,12 @@ String _haGetConfig() { JsonObject& config = jsonBuffer.createObject(); _haSendSwitch(i, config); - output += "\n" + type + ":\n"; + String output; + output.reserve(config.measureLength() + 32); + + output += "\n\n" + type + ":\n"; bool first = true; + for (auto kv : config) { if (first) { output += " - "; @@ -172,11 +177,17 @@ String _haGetConfig() { } else { output += " "; } - output += kv.key + String(": ") + kv.value.as() + String("\n"); + output += kv.key; + output += ": "; + output += kv.value.as(); + output += "\n"; } + output += " "; jsonBuffer.clear(); + printer(output); + } #if SENSOR_SUPPORT @@ -187,8 +198,12 @@ String _haGetConfig() { JsonObject& config = jsonBuffer.createObject(); _haSendMagnitude(i, config); - output += "\nsensor:\n"; + String output; + output.reserve(config.measureLength() + 32); + + output += "\n\nsensor:\n"; bool first = true; + for (auto kv : config) { if (first) { output += " - "; @@ -198,18 +213,21 @@ String _haGetConfig() { } String value = kv.value.as(); value.replace("%", "'%'"); - output += kv.key + String(": ") + value + String("\n"); + output += kv.key; + output += ": "; + output += value; + output += "\n"; } - output += "\n"; + output += " "; jsonBuffer.clear(); + printer(output); + } #endif - return output; - } void _haSend() { @@ -253,11 +271,7 @@ void _haWebSocketOnSend(JsonObject& root) { void _haWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) { if (strcmp(action, "haconfig") == 0) { - String output = _haGetConfig(); - output.replace(" ", " "); - output.replace("\n", "
"); - output = String("{\"haConfig\": \"") + output + String("\"}"); - wsSend(client_id, output.c_str()); + _ha_send_config.push(client_id); } } @@ -268,7 +282,10 @@ void _haWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& d void _haInitCommands() { terminalRegisterCommand(F("HA.CONFIG"), [](Embedis* e) { - DEBUG_MSG(_haGetConfig().c_str()); + _haDumpConfig([](String& data) { + DEBUG_MSG(data.c_str()); + }); + DEBUG_MSG("\n"); terminalOK(); }); @@ -296,6 +313,21 @@ void _haInitCommands() { // ----------------------------------------------------------------------------- +void _haLoop() { + if (_ha_send_config.empty()) return; + + uint32_t client_id = _ha_send_config.front(); + _ha_send_config.pop(); + + _haDumpConfig([client_id](String& output) { + output.replace(" ", " "); + output.replace("\n", "
"); + output = String("{\"haConfig\": \"") + output + String("\"}"); + wsSend(client_id, output.c_str()); + yield(); + }); +} + void haSetup() { _haConfigure(); @@ -317,6 +349,7 @@ void haSetup() { // Main callbacks espurnaRegisterReload(_haConfigure); + espurnaRegisterLoop(_haLoop); } diff --git a/code/html/custom.js b/code/html/custom.js index 1f2c8df7..079664bb 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -658,6 +658,7 @@ function doScan() { function doHAConfig() { $("#haConfig").html(""); + $("#haConfig").show(); sendAction("haconfig", {}); return false; } @@ -1366,7 +1367,9 @@ function processData(data) { // ----------------------------------------------------------------------------- if ("haConfig" === key) { - $("#haConfig").show(); + websock.send("{}"); + $("#haConfig").append(value); + return; } // -----------------------------------------------------------------------------