Browse Source

check if ws client exists, check WEB_SUPPORT

sensors
Max Prokhorov 5 years ago
parent
commit
cd1bfa6b34
3 changed files with 15 additions and 3 deletions
  1. +3
    -0
      code/espurna/config/prototypes.h
  2. +8
    -3
      code/espurna/homeassistant.ino
  3. +4
    -0
      code/espurna/ws.ino

+ 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 *


+ 8
- 3
code/espurna/homeassistant.ino View File

@ -14,8 +14,6 @@ Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
bool _haEnabled = false;
bool _haSendFlag = false;
std::queue<uint32_t> _ha_send_config;
// -----------------------------------------------------------------------------
// UTILS
// -----------------------------------------------------------------------------
@ -259,6 +257,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);
}
@ -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(" ", "&nbsp;");
output.replace("\n", "<br />");
@ -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);
}


+ 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);
}


Loading…
Cancel
Save