mirror of
https://github.com/xoseperez/espurna.git
synced 2026-03-04 15:34:19 +01:00
check if ws client exists, check WEB_SUPPORT
This commit is contained in:
@@ -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 *
|
||||
|
||||
@@ -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(" ", " ");
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user