Browse Source

Merge pull request #1535 from mcspr/hass/stream-config

Print each HA config entry separately
sensors
Xose Pérez 5 years ago
committed by GitHub
parent
commit
3b6c64c075
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 19 deletions
  1. +3
    -0
      code/espurna/config/prototypes.h
  2. +67
    -16
      code/espurna/homeassistant.ino
  3. +4
    -0
      code/espurna/ws.ino
  4. +11
    -2
      code/html/custom.js
  5. +1
    -1
      code/html/index.html

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

@ -191,6 +191,9 @@ void webRequestRegister(web_request_callback_f callback);
typedef std::function<bool(const char *, JsonVariant&)> ws_on_receive_callback_f;
void wsOnReceiveRegister(ws_on_receive_callback_f callback);
bool wsConnected();
bool wsConnected(uint32_t);
#else
#define ws_on_send_callback_f void *
#define ws_on_action_callback_f void *


+ 67
- 16
code/espurna/homeassistant.ino View File

@ -9,6 +9,7 @@ Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
#if HOMEASSISTANT_SUPPORT
#include <ArduinoJson.h>
#include <queue>
bool _haEnabled = false;
bool _haSendFlag = false;
@ -147,9 +148,7 @@ void _haSendSwitches() {
// -----------------------------------------------------------------------------
String _haGetConfig() {
String output;
void _haDumpConfig(std::function<void(String&)> printer, bool wrapJson = false) {
#if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) || (defined(ITEAD_SLAMPHER))
String type = String("light");
@ -163,8 +162,16 @@ String _haGetConfig() {
JsonObject& config = jsonBuffer.createObject();
_haSendSwitch(i, config);
output += "\n" + type + ":\n";
String output;
output.reserve(config.measureLength() + 32);
if (wrapJson) {
output += "{\"haConfig\": \"";
}
output += "\n\n" + type + ":\n";
bool first = true;
for (auto kv : config) {
if (first) {
output += " - ";
@ -172,11 +179,21 @@ String _haGetConfig() {
} else {
output += " ";
}
output += kv.key + String(": ") + kv.value.as<String>() + String("\n");
output += kv.key;
output += ": ";
output += kv.value.as<String>();
output += "\n";
}
output += " ";
if (wrapJson) {
output += "\"}";
}
jsonBuffer.clear();
printer(output);
}
#if SENSOR_SUPPORT
@ -187,8 +204,16 @@ String _haGetConfig() {
JsonObject& config = jsonBuffer.createObject();
_haSendMagnitude(i, config);
output += "\nsensor:\n";
String output;
output.reserve(config.measureLength() + 32);
if (wrapJson) {
output += "{\"haConfig\": \"";
}
output += "\n\nsensor:\n";
bool first = true;
for (auto kv : config) {
if (first) {
output += " - ";
@ -198,18 +223,25 @@ String _haGetConfig() {
}
String value = kv.value.as<String>();
value.replace("%", "'%'");
output += kv.key + String(": ") + value + String("\n");
output += kv.key;
output += ": ";
output += value;
output += "\n";
}
output += " ";
if (wrapJson) {
output += "\"}";
}
output += "\n";
jsonBuffer.clear();
printer(output);
}
#endif
return output;
}
void _haSend() {
@ -241,6 +273,8 @@ void _haConfigure() {
#if WEB_SUPPORT
std::queue<uint32_t> _ha_send_config;
bool _haWebSocketOnReceive(const char * key, JsonVariant& value) {
return (strncmp(key, "ha", 2) == 0);
}
@ -253,11 +287,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(" ", "&nbsp;");
output.replace("\n", "<br />");
output = String("{\"haConfig\": \"") + output + String("\"}");
wsSend(client_id, output.c_str());
_ha_send_config.push(client_id);
}
}
@ -268,7 +298,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 +329,23 @@ void _haInitCommands() {
// -----------------------------------------------------------------------------
#if WEB_SUPPORT
void _haLoop() {
if (_ha_send_config.empty()) return;
uint32_t client_id = _ha_send_config.front();
_ha_send_config.pop();
if (!wsConnected(client_id)) return;
// TODO check wsConnected after each "printer" call?
_haDumpConfig([client_id](String& output) {
wsSend(client_id, output.c_str());
yield();
}, true);
}
#endif
void haSetup() {
_haConfigure();
@ -304,6 +354,7 @@ void haSetup() {
wsOnSendRegister(_haWebSocketOnSend);
wsOnActionRegister(_haWebSocketOnAction);
wsOnReceiveRegister(_haWebSocketOnReceive);
espurnaRegisterLoop(_haLoop);
#endif
#if TERMINAL_SUPPORT


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

@ -426,6 +426,10 @@ bool wsConnected() {
return (_ws.count() > 0);
}
bool wsConnected(uint32_t client_id) {
return _ws.hasClient(client_id);
}
void wsOnSendRegister(ws_on_send_callback_f callback) {
_ws_on_send_callbacks.push_back(callback);
}


+ 11
- 2
code/html/custom.js View File

@ -657,7 +657,10 @@ function doScan() {
}
function doHAConfig() {
$("#haConfig").html("");
$("#haConfig")
.text("")
.height(0)
.show();
sendAction("haconfig", {});
return false;
}
@ -1366,7 +1369,11 @@ function processData(data) {
// -----------------------------------------------------------------------------
if ("haConfig" === key) {
$("#haConfig").show();
websock.send("{}");
$("#haConfig")
.append(new Text(value))
.height($("#haConfig")[0].scrollHeight);
return;
}
// -----------------------------------------------------------------------------
@ -1712,6 +1719,8 @@ $(function() {
$(document).on("change", "input", hasChanged);
$(document).on("change", "select", hasChanged);
$("textarea").on("dblclick", function() { this.select(); });
// don't autoconnect when opening from filesystem
if (window.location.protocol === "file:") { return; }


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

@ -1114,7 +1114,7 @@
</div>
</div>
<div class="pure-g">
<span class="pure-u-1 terminal" id="haConfig" name="haConfig"></span>
<textarea class="pure-u-1 terminal" id="haConfig" name="haConfig" wrap="off" readonly></textarea>
</div>


Loading…
Cancel
Save