From cd1bfa6b34d4b457ace007b00a88d1e05c93dfe9 Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Sat, 9 Feb 2019 16:15:38 +0300 Subject: [PATCH] check if ws client exists, check WEB_SUPPORT --- code/espurna/config/prototypes.h | 3 +++ code/espurna/homeassistant.ino | 11 ++++++++--- code/espurna/ws.ino | 4 ++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index d029ac46..ecb86391 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -191,6 +191,9 @@ void webRequestRegister(web_request_callback_f callback); typedef std::function 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 * diff --git a/code/espurna/homeassistant.ino b/code/espurna/homeassistant.ino index e7a289fa..0e0a8cba 100644 --- a/code/espurna/homeassistant.ino +++ b/code/espurna/homeassistant.ino @@ -14,8 +14,6 @@ Copyright (C) 2017-2018 by Xose PĂ©rez bool _haEnabled = false; bool _haSendFlag = false; -std::queue _ha_send_config; - // ----------------------------------------------------------------------------- // UTILS // ----------------------------------------------------------------------------- @@ -259,6 +257,8 @@ void _haConfigure() { #if WEB_SUPPORT +std::queue _ha_send_config; + bool _haWebSocketOnReceive(const char * key, JsonVariant& value) { return (strncmp(key, "ha", 2) == 0); } @@ -313,12 +313,16 @@ 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) { output.replace(" ", " "); output.replace("\n", "
"); @@ -327,6 +331,7 @@ void _haLoop() { yield(); }); } +#endif void haSetup() { @@ -336,6 +341,7 @@ void haSetup() { wsOnSendRegister(_haWebSocketOnSend); wsOnActionRegister(_haWebSocketOnAction); wsOnReceiveRegister(_haWebSocketOnReceive); + espurnaRegisterLoop(_haLoop); #endif #if TERMINAL_SUPPORT @@ -349,7 +355,6 @@ void haSetup() { // Main callbacks espurnaRegisterReload(_haConfigure); - espurnaRegisterLoop(_haLoop); } diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index be7022e5..b8edb129 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -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); }