From 37763f1ad42b77008c0a13aa460db87ceb81345c Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Sat, 21 Dec 2019 16:13:29 +0300 Subject: [PATCH] Test: check core feature build (#2080) * Test: check core feature build * order * add missing --- code/espurna/config/prototypes.h | 64 ----------- code/espurna/espurna.ino | 1 + code/espurna/homeassistant.ino | 2 + code/espurna/light.ino | 1 + code/espurna/mqtt.ino | 1 + code/espurna/nofuss.ino | 1 + code/espurna/ntp.ino | 1 + code/espurna/ota.h | 5 +- code/espurna/ota_arduinoota.ino | 1 + code/espurna/relay.ino | 1 + code/espurna/rfbridge.ino | 5 +- code/espurna/rfm69.ino | 1 + code/espurna/rtcmem.ino | 46 ++++---- code/espurna/sensor.ino | 8 +- code/espurna/thermostat.ino | 6 +- code/espurna/utils.h | 2 + code/espurna/wifi.ino | 2 + code/espurna/ws.h | 67 ++++++++++++ code/espurna/ws.ino | 178 ++++--------------------------- code/espurna/ws_internal.h | 167 +++++++++++++++++++++++++++++ code/test/build/core.h | 16 +++ 21 files changed, 325 insertions(+), 251 deletions(-) create mode 100644 code/espurna/ws.h create mode 100644 code/espurna/ws_internal.h create mode 100644 code/test/build/core.h diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index 0a6a332d..0dc82a58 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -11,7 +11,6 @@ extern "C" { } #define UNUSED(x) (void)(x) -#define INLINE inline __attribute__((always_inline)) // ----------------------------------------------------------------------------- // System @@ -313,69 +312,6 @@ using web_request_callback_f = std::function; void webBodyRegister(web_body_callback_f); void webRequestRegister(web_request_callback_f); -// ----------------------------------------------------------------------------- -// WebSockets -// ----------------------------------------------------------------------------- -#include - -// TODO: pending configuration headers refactoring... here for now -struct ws_counter_t; -struct ws_data_t; -struct ws_debug_t; -struct ws_callbacks_t; - -using ws_on_send_callback_f = std::function; -using ws_on_action_callback_f = std::function; -using ws_on_keycheck_callback_f = std::function; - -using ws_on_send_callback_list_t = std::vector; -using ws_on_action_callback_list_t = std::vector; -using ws_on_keycheck_callback_list_t = std::vector; - -#if WEB_SUPPORT - struct ws_callbacks_t { - ws_on_send_callback_list_t on_visible; - ws_on_send_callback_list_t on_connected; - ws_on_send_callback_list_t on_data; - - ws_on_action_callback_list_t on_action; - ws_on_keycheck_callback_list_t on_keycheck; - - ws_callbacks_t& onVisible(ws_on_send_callback_f); - ws_callbacks_t& onConnected(ws_on_send_callback_f); - ws_callbacks_t& onData(ws_on_send_callback_f); - ws_callbacks_t& onAction(ws_on_action_callback_f); - ws_callbacks_t& onKeyCheck(ws_on_keycheck_callback_f); - }; - - ws_callbacks_t& wsRegister(); - - void wsSetup(); - void wsSend(uint32_t client_id, const char* data); - void wsSend(uint32_t client_id, JsonObject& root); - void wsSend(JsonObject& root); - void wsSend(ws_on_send_callback_f callback); - - void wsSend_P(PGM_P data); - void wsSend_P(uint32_t client_id, PGM_P data); - - void INLINE wsPost(const ws_on_send_callback_f& callback); - void INLINE wsPost(uint32_t client_id, const ws_on_send_callback_f& callback); - void INLINE wsPost(const ws_on_send_callback_list_t& callbacks); - void INLINE wsPost(uint32_t client_id, const ws_on_send_callback_list_t& callbacks); - - void INLINE wsPostAll(uint32_t client_id, const ws_on_send_callback_list_t& callbacks); - void INLINE wsPostAll(const ws_on_send_callback_list_t& callbacks); - - void INLINE wsPostSequence(uint32_t client_id, const ws_on_send_callback_list_t& callbacks); - void INLINE wsPostSequence(uint32_t client_id, ws_on_send_callback_list_t&& callbacks); - void INLINE wsPostSequence(const ws_on_send_callback_list_t& callbacks); - - bool INLINE wsConnected(); - bool INLINE wsConnected(uint32_t client_id); - bool wsDebugSend(const char* prefix, const char* message); -#endif - // ----------------------------------------------------------------------------- // WIFI // ----------------------------------------------------------------------------- diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 143d3e4e..03fe355f 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -27,6 +27,7 @@ along with this program. If not, see . #include "relay.h" #include "broker.h" #include "tuya.h" +#include "ws.h" #include "libs/HeapStats.h" using void_callback_f = void (*)(); diff --git a/code/espurna/homeassistant.ino b/code/espurna/homeassistant.ino index ce164184..2f02592a 100644 --- a/code/espurna/homeassistant.ino +++ b/code/espurna/homeassistant.ino @@ -12,6 +12,8 @@ Copyright (C) 2017-2019 by Xose Pérez #include #include +#include "ws.h" + bool _ha_enabled = false; bool _ha_send_flag = false; diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 23c27a8a..f11ea793 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -11,6 +11,7 @@ Copyright (C) 2016-2019 by Xose Pérez #include "tuya.h" #include "light.h" #include "broker.h" +#include "ws.h" #include #include diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index d81f765c..ac9a7e94 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -20,6 +20,7 @@ Updated secure client support by Niek van der Maas < mail at niekvandermaas dot #include "system.h" #include "libs/SecureClientHelpers.h" +#include "ws.h" #if MQTT_LIBRARY == MQTT_LIBRARY_ASYNCMQTTCLIENT diff --git a/code/espurna/nofuss.ino b/code/espurna/nofuss.ino index e6fed563..901c9601 100644 --- a/code/espurna/nofuss.ino +++ b/code/espurna/nofuss.ino @@ -9,6 +9,7 @@ Copyright (C) 2016-2019 by Xose Pérez #if NOFUSS_SUPPORT #include "NoFUSSClient.h" +#include "ws.h" unsigned long _nofussLastCheck = 0; unsigned long _nofussInterval = 0; diff --git a/code/espurna/ntp.ino b/code/espurna/ntp.ino index 318f310c..d3f6e17d 100644 --- a/code/espurna/ntp.ino +++ b/code/espurna/ntp.ino @@ -14,6 +14,7 @@ Copyright (C) 2016-2019 by Xose Pérez #include "libs/NtpClientWrap.h" #include "broker.h" +#include "ws.h" Ticker _ntp_defer; diff --git a/code/espurna/ota.h b/code/espurna/ota.h index 0c874dd4..bd469639 100644 --- a/code/espurna/ota.h +++ b/code/espurna/ota.h @@ -6,6 +6,7 @@ OTA COMMON FUNCTIONS #pragma once +#include "ws.h" #include void otaPrintError() { @@ -61,7 +62,9 @@ bool otaVerifyHeader(uint8_t* data, size_t len) { void otaProgress(size_t bytes, size_t each = 8192u) { // Removed to avoid websocket ping back during upgrade (see #1574) // TODO: implement as separate from debugging message - if (wsConnected()) return; + #if WEB_SUPPORT + if (wsConnected()) return; + #endif // Telnet and serial will still output things, but slightly throttled static size_t last = 0; diff --git a/code/espurna/ota_arduinoota.ino b/code/espurna/ota_arduinoota.ino index f4e2807e..c00f7949 100644 --- a/code/espurna/ota_arduinoota.ino +++ b/code/espurna/ota_arduinoota.ino @@ -9,6 +9,7 @@ Copyright (C) 2016-2019 by Xose Pérez #if OTA_ARDUINOOTA_SUPPORT #include "system.h" +#include "ws.h" // TODO: allocate ArduinoOTAClass on-demand, stop using global instance diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 0e2a44c0..ee4b6582 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -16,6 +16,7 @@ Copyright (C) 2016-2019 by Xose Pérez #include "relay.h" #include "broker.h" #include "tuya.h" +#include "ws.h" #include "relay_config.h" diff --git a/code/espurna/rfbridge.ino b/code/espurna/rfbridge.ino index f2eee02b..487b7ccc 100644 --- a/code/espurna/rfbridge.ino +++ b/code/espurna/rfbridge.ino @@ -8,14 +8,15 @@ Copyright (C) 2016-2019 by Xose Pérez #if RF_SUPPORT -#include "relay.h" - #include #if RFB_DIRECT #include #endif +#include "relay.h" +#include "ws.h" + // ----------------------------------------------------------------------------- // DEFINITIONS // ----------------------------------------------------------------------------- diff --git a/code/espurna/rfm69.ino b/code/espurna/rfm69.ino index 73f05cc9..bc154370 100644 --- a/code/espurna/rfm69.ino +++ b/code/espurna/rfm69.ino @@ -9,6 +9,7 @@ Copyright (C) 2016-2017 by Xose Pérez #if RFM69_SUPPORT #include "libs/RFM69Wrap.h" +#include "ws.h" #define RFM69_PACKET_SEPARATOR ':' diff --git a/code/espurna/rtcmem.ino b/code/espurna/rtcmem.ino index 634693bf..efa34295 100644 --- a/code/espurna/rtcmem.ino +++ b/code/espurna/rtcmem.ino @@ -46,39 +46,41 @@ void _rtcmemInitCommands() { _rtcmemInit(); }); - terminalRegisterCommand(F("RTCMEM.DUMP"), [](Embedis* e) { + #if DEBUG_SUPPORT + terminalRegisterCommand(F("RTCMEM.DUMP"), [](Embedis* e) { - DEBUG_MSG_P(PSTR("[RTCMEM] boot_status=%u status=%u blocks_used=%u\n"), - _rtcmem_status, _rtcmemStatus(), RtcmemSize); + DEBUG_MSG_P(PSTR("[RTCMEM] boot_status=%u status=%u blocks_used=%u\n"), + _rtcmem_status, _rtcmemStatus(), RtcmemSize); - String line; - line.reserve(96); - char buffer[16] = {0}; + String line; + line.reserve(96); + char buffer[16] = {0}; - auto addr = reinterpret_cast(RTCMEM_ADDR); + auto addr = reinterpret_cast(RTCMEM_ADDR); - uint8_t block = 1; - uint8_t offset = 0; - uint8_t start = 0; + uint8_t block = 1; + uint8_t offset = 0; + uint8_t start = 0; - do { + do { - offset = block - 1; + offset = block - 1; - snprintf(buffer, sizeof(buffer), "%08x ", *(addr + offset)); - line += buffer; + snprintf(buffer, sizeof(buffer), "%08x ", *(addr + offset)); + line += buffer; - if ((block % 8) == 0) { - DEBUG_MSG_P(PSTR("%02u %p: %s\n"), start, addr+start, line.c_str()); - start = block; - line = ""; - } + if ((block % 8) == 0) { + DEBUG_MSG_P(PSTR("%02u %p: %s\n"), start, addr+start, line.c_str()); + start = block; + line = ""; + } - ++block; + ++block; - } while (block<(RTCMEM_BLOCKS+1)); + } while (block<(RTCMEM_BLOCKS+1)); - }); + }); + #endif } #endif diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 9301cb21..eac915e6 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -8,9 +8,6 @@ Copyright (C) 2016-2019 by Xose Pérez #if SENSOR_SUPPORT -#include "relay.h" -#include "broker.h" - #include #include "filters/LastFilter.h" #include "filters/MaxFilter.h" @@ -20,6 +17,11 @@ Copyright (C) 2016-2019 by Xose Pérez #include +#include "relay.h" +#include "broker.h" +#include "ws.h" + + struct sensor_magnitude_t { BaseSensor * sensor; // Sensor object BaseFilter * filter; // Filter object diff --git a/code/espurna/thermostat.ino b/code/espurna/thermostat.ino index 82c212da..3903d3e2 100644 --- a/code/espurna/thermostat.ino +++ b/code/espurna/thermostat.ino @@ -8,11 +8,13 @@ Copyright (C) 2017 by Dmitry Blinov #if THERMOSTAT_SUPPORT -#include "relay.h" - #include #include +#include "relay.h" +#include "ws.h" + + bool _thermostat_enabled = true; bool _thermostat_mode_cooler = false; diff --git a/code/espurna/utils.h b/code/espurna/utils.h index 0a8e5e6d..9b4905ae 100644 --- a/code/espurna/utils.h +++ b/code/espurna/utils.h @@ -8,6 +8,8 @@ Copyright (C) 2017-2019 by Xose Pérez #pragma once +#define INLINE inline __attribute__((always_inline)) + extern "C" uint32_t _SPIFFS_start; extern "C" uint32_t _SPIFFS_end; diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index 8341141c..64e82e30 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -9,6 +9,8 @@ Copyright (C) 2016-2019 by Xose Pérez #include #include +#include "ws.h" + bool _wifi_wps_running = false; bool _wifi_smartconfig_running = false; bool _wifi_smartconfig_initial = false; diff --git a/code/espurna/ws.h b/code/espurna/ws.h new file mode 100644 index 00000000..9f5ef8a8 --- /dev/null +++ b/code/espurna/ws.h @@ -0,0 +1,67 @@ +/* + +WEBSOCKET MODULE + +Copyright (C) 2016-2019 by Xose Pérez + +*/ + +#pragma once + +#include + +#include +#include +#include + +#include "utils.h" + +using ws_on_send_callback_f = std::function; +using ws_on_action_callback_f = std::function; +using ws_on_keycheck_callback_f = std::function; + +using ws_on_send_callback_list_t = std::vector; +using ws_on_action_callback_list_t = std::vector; +using ws_on_keycheck_callback_list_t = std::vector; + +struct ws_callbacks_t { + ws_on_send_callback_list_t on_visible; + ws_on_send_callback_list_t on_connected; + ws_on_send_callback_list_t on_data; + + ws_on_action_callback_list_t on_action; + ws_on_keycheck_callback_list_t on_keycheck; + + ws_callbacks_t& onVisible(ws_on_send_callback_f); + ws_callbacks_t& onConnected(ws_on_send_callback_f); + ws_callbacks_t& onData(ws_on_send_callback_f); + ws_callbacks_t& onAction(ws_on_action_callback_f); + ws_callbacks_t& onKeyCheck(ws_on_keycheck_callback_f); +}; + +ws_callbacks_t& wsRegister(); + +void wsSetup(); +void wsSend(uint32_t client_id, const char* data); +void wsSend(uint32_t client_id, JsonObject& root); +void wsSend(JsonObject& root); +void wsSend(ws_on_send_callback_f callback); + +void wsSend_P(PGM_P data); +void wsSend_P(uint32_t client_id, PGM_P data); + +void INLINE wsPost(const ws_on_send_callback_f& callback); +void INLINE wsPost(uint32_t client_id, const ws_on_send_callback_f& callback); +void INLINE wsPost(const ws_on_send_callback_list_t& callbacks); +void INLINE wsPost(uint32_t client_id, const ws_on_send_callback_list_t& callbacks); + +void INLINE wsPostAll(uint32_t client_id, const ws_on_send_callback_list_t& callbacks); +void INLINE wsPostAll(const ws_on_send_callback_list_t& callbacks); + +void INLINE wsPostSequence(uint32_t client_id, const ws_on_send_callback_list_t& callbacks); +void INLINE wsPostSequence(uint32_t client_id, ws_on_send_callback_list_t&& callbacks); +void INLINE wsPostSequence(const ws_on_send_callback_list_t& callbacks); + +bool INLINE wsConnected(); +bool INLINE wsConnected(uint32_t client_id); +bool wsDebugSend(const char* prefix, const char* message); diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index 7b5c89fe..ae875caa 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -15,6 +15,8 @@ Copyright (C) 2016-2019 by Xose Pérez #include #include "system.h" +#include "ws.h" +#include "ws_internal.h" #include "libs/WebSocketIncommingBuffer.h" AsyncWebSocket _ws("/ws"); @@ -51,112 +53,12 @@ ws_callbacks_t& ws_callbacks_t::onKeyCheck(ws_on_keycheck_callback_f cb) { } ws_callbacks_t _ws_callbacks; - -struct ws_counter_t { - - ws_counter_t() : current(0), start(0), stop(0) {} - - ws_counter_t(uint32_t start, uint32_t stop) : - current(start), start(start), stop(stop) {} - - void reset() { - current = start; - } - - void next() { - if (current < stop) { - ++current; - } - } - - bool done() { - return (current >= stop); - } - - uint32_t current; - uint32_t start; - uint32_t stop; -}; - -struct ws_data_t { - - enum mode_t { - SEQUENCE, - ALL - }; - - ws_data_t(const ws_on_send_callback_f& cb) : - storage(new ws_on_send_callback_list_t {cb}), - client_id(0), - mode(ALL), - callbacks(*storage.get()), - counter(0, 1) - {} - - ws_data_t(uint32_t client_id, const ws_on_send_callback_f& cb) : - storage(new ws_on_send_callback_list_t {cb}), - client_id(client_id), - mode(ALL), - callbacks(*storage.get()), - counter(0, 1) - {} - - ws_data_t(const uint32_t client_id, ws_on_send_callback_list_t&& callbacks, mode_t mode = SEQUENCE) : - storage(new ws_on_send_callback_list_t(std::move(callbacks))), - client_id(client_id), - mode(mode), - callbacks(*storage.get()), - counter(0, (storage.get())->size()) - {} - - ws_data_t(const uint32_t client_id, const ws_on_send_callback_list_t& callbacks, mode_t mode = SEQUENCE) : - client_id(client_id), - mode(mode), - callbacks(callbacks), - counter(0, callbacks.size()) - {} - - bool done() { - return counter.done(); - } - - void sendAll(JsonObject& root) { - while (!counter.done()) counter.next(); - for (auto& callback : callbacks) { - callback(root); - } - } - - void sendCurrent(JsonObject& root) { - callbacks[counter.current](root); - counter.next(); - } - - void send(JsonObject& root) { - switch (mode) { - case SEQUENCE: sendCurrent(root); break; - case ALL: sendAll(root); break; - } - } - - std::unique_ptr storage; - - const uint32_t client_id; - const mode_t mode; - const ws_on_send_callback_list_t& callbacks; - ws_counter_t counter; -}; - std::queue _ws_client_data; // ----------------------------------------------------------------------------- // WS authentication // ----------------------------------------------------------------------------- -struct ws_ticket_t { - IPAddress ip; - unsigned long timestamp = 0; -}; ws_ticket_t _ws_tickets[WS_BUFFER_SIZE]; void _onAuth(AsyncWebServerRequest *request) { @@ -206,71 +108,33 @@ bool _wsAuth(AsyncWebSocketClient * client) { #if DEBUG_WEB_SUPPORT -using ws_debug_msg_t = std::pair; - -struct ws_debug_t { - - ws_debug_t(size_t capacity) : - flush(false), - current(0), - capacity(capacity) - { - messages.reserve(capacity); - } +ws_debug_t _ws_debug(WS_DEBUG_MSG_BUFFER); - void clear() { - messages.clear(); - current = 0; - flush = false; +void ws_debug_t::send(const bool connected) { + if (!connected && flush) { + clear(); + return; } - void add(const char* prefix, const char* message) { - if (current >= capacity) { - flush = true; - send(wsConnected()); - } - - messages.emplace(messages.begin() + current, prefix, message); - flush = true; - ++current; - } + if (!flush) return; + // ref: http://arduinojson.org/v5/assistant/ + // {"weblog": {"msg":[...],"pre":[...]}} + DynamicJsonBuffer jsonBuffer(2*JSON_ARRAY_SIZE(messages.size()) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2)); - void send(const bool connected) { - if (!connected && flush) { - clear(); - return; - } - - if (!flush) return; - // ref: http://arduinojson.org/v5/assistant/ - // {"weblog": {"msg":[...],"pre":[...]}} - DynamicJsonBuffer jsonBuffer(2*JSON_ARRAY_SIZE(messages.size()) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2)); - - JsonObject& root = jsonBuffer.createObject(); - JsonObject& weblog = root.createNestedObject("weblog"); - - JsonArray& msg = weblog.createNestedArray("msg"); - JsonArray& pre = weblog.createNestedArray("pre"); + JsonObject& root = jsonBuffer.createObject(); + JsonObject& weblog = root.createNestedObject("weblog"); - for (auto& message : messages) { - pre.add(message.first.c_str()); - msg.add(message.second.c_str()); - } + JsonArray& msg = weblog.createNestedArray("msg"); + JsonArray& pre = weblog.createNestedArray("pre"); - wsSend(root); - clear(); + for (auto& message : messages) { + pre.add(message.first.c_str()); + msg.add(message.second.c_str()); } - bool flush; - size_t current; - const size_t capacity; - std::vector messages; - -}; - -// TODO: move to the headers? -constexpr const size_t WS_DEBUG_MSG_BUFFER = 8; -ws_debug_t _ws_debug(WS_DEBUG_MSG_BUFFER); + wsSend(root); + clear(); +} bool wsDebugSend(const char* prefix, const char* message) { if (!wsConnected()) return false; diff --git a/code/espurna/ws_internal.h b/code/espurna/ws_internal.h new file mode 100644 index 00000000..bf0b068c --- /dev/null +++ b/code/espurna/ws_internal.h @@ -0,0 +1,167 @@ +/* + +WEBSOCKET MODULE + +Copyright (C) 2016-2019 by Xose Pérez + +*/ + +#pragma once + +#include + +#include +#include +#include + +constexpr const size_t WS_DEBUG_MSG_BUFFER = 8; + +// ----------------------------------------------------------------------------- +// WS authentication +// ----------------------------------------------------------------------------- + +struct ws_ticket_t { + IPAddress ip; + unsigned long timestamp = 0; +}; + +// ----------------------------------------------------------------------------- +// WS callbacks +// ----------------------------------------------------------------------------- + +struct ws_counter_t { + + ws_counter_t() : current(0), start(0), stop(0) {} + + ws_counter_t(uint32_t start, uint32_t stop) : + current(start), start(start), stop(stop) {} + + void reset() { + current = start; + } + + void next() { + if (current < stop) { + ++current; + } + } + + bool done() { + return (current >= stop); + } + + uint32_t current; + uint32_t start; + uint32_t stop; +}; + +struct ws_data_t { + + enum mode_t { + SEQUENCE, + ALL + }; + + ws_data_t(const ws_on_send_callback_f& cb) : + storage(new ws_on_send_callback_list_t {cb}), + client_id(0), + mode(ALL), + callbacks(*storage.get()), + counter(0, 1) + {} + + ws_data_t(uint32_t client_id, const ws_on_send_callback_f& cb) : + storage(new ws_on_send_callback_list_t {cb}), + client_id(client_id), + mode(ALL), + callbacks(*storage.get()), + counter(0, 1) + {} + + ws_data_t(const uint32_t client_id, ws_on_send_callback_list_t&& callbacks, mode_t mode = SEQUENCE) : + storage(new ws_on_send_callback_list_t(std::move(callbacks))), + client_id(client_id), + mode(mode), + callbacks(*storage.get()), + counter(0, (storage.get())->size()) + {} + + ws_data_t(const uint32_t client_id, const ws_on_send_callback_list_t& callbacks, mode_t mode = SEQUENCE) : + client_id(client_id), + mode(mode), + callbacks(callbacks), + counter(0, callbacks.size()) + {} + + bool done() { + return counter.done(); + } + + void sendAll(JsonObject& root) { + while (!counter.done()) counter.next(); + for (auto& callback : callbacks) { + callback(root); + } + } + + void sendCurrent(JsonObject& root) { + callbacks[counter.current](root); + counter.next(); + } + + void send(JsonObject& root) { + switch (mode) { + case SEQUENCE: sendCurrent(root); break; + case ALL: sendAll(root); break; + } + } + + std::unique_ptr storage; + + const uint32_t client_id; + const mode_t mode; + const ws_on_send_callback_list_t& callbacks; + ws_counter_t counter; +}; + +// ----------------------------------------------------------------------------- +// Debug +// ----------------------------------------------------------------------------- + +using ws_debug_msg_t = std::pair; + +struct ws_debug_t { + + ws_debug_t(size_t capacity) : + flush(false), + current(0), + capacity(capacity) + { + messages.reserve(capacity); + } + + void clear() { + messages.clear(); + current = 0; + flush = false; + } + + void add(const char* prefix, const char* message) { + if (current >= capacity) { + flush = true; + send(wsConnected()); + } + + messages.emplace(messages.begin() + current, prefix, message); + flush = true; + ++current; + } + + void send(const bool connected); + + bool flush; + size_t current; + const size_t capacity; + std::vector messages; + +}; diff --git a/code/test/build/core.h b/code/test/build/core.h new file mode 100644 index 00000000..37b631b1 --- /dev/null +++ b/code/test/build/core.h @@ -0,0 +1,16 @@ +#define ALEXA_SUPPORT 0 +#define API_SUPPORT 0 +#define BROKER_SUPPORT 0 +#define DOMOTICZ_SUPPORT 0 +#define DEBUG_SERIAL_SUPPORT 0 +#define DEBUG_TELNET_SUPPORT 0 +#define DEBUG_WEB_SUPPORT 0 +#define HOMEASSISTANT_SUPPORT 0 +#define I2C_SUPPORT 0 +#define MQTT_SUPPORT 0 +#define NTP_SUPPORT 0 +#define RPN_RULES_SUPPORT 0 +#define SCHEDULER_SUPPORT 0 +#define SENSOR_SUPPORT 0 +#define THINGSPEAK_SUPPORT 0 +#define WEB_SUPPORT 0