From 7865ddc51f393e68f318054c8ee6cf4b1d432070 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sat, 3 Jun 2023 16:36:53 +0200 Subject: [PATCH 01/18] use espMqttClient, qos2 fixed --- lib/espMqttClient/LICENSE | 21 + lib/espMqttClient/README.md | 54 ++ lib/espMqttClient/src/Config.h | 58 ++ lib/espMqttClient/src/Helpers.h | 49 ++ lib/espMqttClient/src/Logging.h | 43 + lib/espMqttClient/src/MqttClient.cpp | 754 ++++++++++++++++++ lib/espMqttClient/src/MqttClient.h | 190 +++++ lib/espMqttClient/src/MqttClientSetup.h | 116 +++ lib/espMqttClient/src/Outbox.h | 207 +++++ lib/espMqttClient/src/Packets/Constants.h | 77 ++ lib/espMqttClient/src/Packets/Packet.cpp | 438 ++++++++++ lib/espMqttClient/src/Packets/Packet.h | 155 ++++ lib/espMqttClient/src/Packets/Parser.cpp | 316 ++++++++ lib/espMqttClient/src/Packets/Parser.h | 100 +++ .../src/Packets/RemainingLength.cpp | 57 ++ .../src/Packets/RemainingLength.h | 32 + lib/espMqttClient/src/Packets/String.cpp | 26 + lib/espMqttClient/src/Packets/String.h | 22 + .../src/Transport/ClientAsync.cpp | 58 ++ lib/espMqttClient/src/Transport/ClientAsync.h | 44 + .../src/Transport/ClientPosix.cpp | 92 +++ lib/espMqttClient/src/Transport/ClientPosix.h | 51 ++ .../src/Transport/ClientSecureSync.cpp | 71 ++ .../src/Transport/ClientSecureSync.h | 34 + .../src/Transport/ClientSync.cpp | 71 ++ lib/espMqttClient/src/Transport/ClientSync.h | 34 + lib/espMqttClient/src/Transport/IPAddress.cpp | 32 + lib/espMqttClient/src/Transport/IPAddress.h | 28 + lib/espMqttClient/src/Transport/Transport.h | 28 + lib/espMqttClient/src/TypeDefs.cpp | 51 ++ lib/espMqttClient/src/TypeDefs.h | 73 ++ lib/espMqttClient/src/espMqttClient.cpp | 113 +++ lib/espMqttClient/src/espMqttClient.h | 80 ++ lib/espMqttClient/src/espMqttClientAsync.cpp | 61 ++ lib/espMqttClient/src/espMqttClientAsync.h | 36 + lib/framework/ESP8266React.h | 2 +- lib/framework/MqttPubSub.h | 163 ---- lib/framework/MqttSettingsService.cpp | 32 +- lib/framework/MqttSettingsService.h | 26 +- src/emsdevice.cpp | 10 +- src/emsesp.cpp | 3 + src/mqtt.cpp | 62 +- src/mqtt.h | 9 +- 43 files changed, 3749 insertions(+), 230 deletions(-) create mode 100644 lib/espMqttClient/LICENSE create mode 100644 lib/espMqttClient/README.md create mode 100644 lib/espMqttClient/src/Config.h create mode 100644 lib/espMqttClient/src/Helpers.h create mode 100644 lib/espMqttClient/src/Logging.h create mode 100644 lib/espMqttClient/src/MqttClient.cpp create mode 100644 lib/espMqttClient/src/MqttClient.h create mode 100644 lib/espMqttClient/src/MqttClientSetup.h create mode 100644 lib/espMqttClient/src/Outbox.h create mode 100644 lib/espMqttClient/src/Packets/Constants.h create mode 100644 lib/espMqttClient/src/Packets/Packet.cpp create mode 100644 lib/espMqttClient/src/Packets/Packet.h create mode 100644 lib/espMqttClient/src/Packets/Parser.cpp create mode 100644 lib/espMqttClient/src/Packets/Parser.h create mode 100644 lib/espMqttClient/src/Packets/RemainingLength.cpp create mode 100644 lib/espMqttClient/src/Packets/RemainingLength.h create mode 100644 lib/espMqttClient/src/Packets/String.cpp create mode 100644 lib/espMqttClient/src/Packets/String.h create mode 100644 lib/espMqttClient/src/Transport/ClientAsync.cpp create mode 100644 lib/espMqttClient/src/Transport/ClientAsync.h create mode 100644 lib/espMqttClient/src/Transport/ClientPosix.cpp create mode 100644 lib/espMqttClient/src/Transport/ClientPosix.h create mode 100644 lib/espMqttClient/src/Transport/ClientSecureSync.cpp create mode 100644 lib/espMqttClient/src/Transport/ClientSecureSync.h create mode 100644 lib/espMqttClient/src/Transport/ClientSync.cpp create mode 100644 lib/espMqttClient/src/Transport/ClientSync.h create mode 100644 lib/espMqttClient/src/Transport/IPAddress.cpp create mode 100644 lib/espMqttClient/src/Transport/IPAddress.h create mode 100644 lib/espMqttClient/src/Transport/Transport.h create mode 100644 lib/espMqttClient/src/TypeDefs.cpp create mode 100644 lib/espMqttClient/src/TypeDefs.h create mode 100644 lib/espMqttClient/src/espMqttClient.cpp create mode 100644 lib/espMqttClient/src/espMqttClient.h create mode 100644 lib/espMqttClient/src/espMqttClientAsync.cpp create mode 100644 lib/espMqttClient/src/espMqttClientAsync.h delete mode 100644 lib/framework/MqttPubSub.h diff --git a/lib/espMqttClient/LICENSE b/lib/espMqttClient/LICENSE new file mode 100644 index 000000000..1cc5546f9 --- /dev/null +++ b/lib/espMqttClient/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Bert Melis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/lib/espMqttClient/README.md b/lib/espMqttClient/README.md new file mode 100644 index 000000000..586d3339e --- /dev/null +++ b/lib/espMqttClient/README.md @@ -0,0 +1,54 @@ +# espMqttClient + +MQTT client library for the Espressif devices ESP8266 and ESP32 on the Arduino framework. +Aims to be a non-blocking, fully compliant MQTT 3.1.1 client. + +![platformio](https://github.com/bertmelis/espMqttClient/actions/workflows/build_platformio.yml/badge.svg) +![cpplint](https://github.com/bertmelis/espMqttClient/actions/workflows/cpplint.yml/badge.svg) +![cppcheck](https://github.com/bertmelis/espMqttClient/actions/workflows/cppcheck.yml/badge.svg) +[![PlatformIO Registry](https://badges.registry.platformio.org/packages/bertmelis/library/espMqttClient.svg)](https://registry.platformio.org/libraries/bertmelis/espMqttClient) + +# Features + +- MQTT 3.1.1 compliant library +- Sending and receiving at all QoS levels +- TCP and TCP/TLS using standard WiFiClient and WiFiClientSecure connections +- Virtually unlimited incoming and outgoing payload sizes +- Readable and understandable code +- Fully async clients available via [AsyncTCP](https://github.com/me-no-dev/AsyncTCP) or [ESPAsnycTCP](https://github.com/me-no-dev/ESPAsyncTCP) (no TLS supported) +- Supported platforms: + - Espressif ESP8266 and ESP32 using the Arduino framework +- Basic Linux compatibility*. This includes WSL on Windows + + > Linux compatibility is mainly for automatic testing. It relies on a quick and dirty Arduino-style `Client` with a POSIX TCP client underneath and Arduino-style `IPAddress` class. These are lacking many features needed for proper Linux support. + +# Documentation + +See [documentation](https://www.emelis.net/espMqttClient/) and the [examples](examples/). + +## Limitations + +### MQTT 3.1.1 Compliancy + +Outgoing messages and session data are not stored in non-volatile memory. Any events like loss of power or sudden resets result in loss of data. Despite this limitation, one could still consider this library as fully complaint based on the non normative remark in point 4.1.1 of the specification. + +### Non-blocking + +This library aims to be fully non-blocking. It is however limited by the underlying `WiFiClient` library which is part of the Arduino framework and has a blocking `connect` method. This is not an issue on ESP32 because the call is offloaded to a separate task. On ESP8266 however, connecting will block until succesful or until the connection timeouts. + +If you need a fully asynchronous MQTT client, you can use `espMqttClientAsync` which uses AsyncTCP/ESPAsyncTCP under the hood. These underlying libraries do not support TLS (anymore). I will not provide support TLS for the async client. + +# Bugs and feature requests + +Please use Github's facilities to get in touch. + +# About this library + +This client wouldn't exist without [Async-mqtt-client](https://github.com/marvinroger/async-mqtt-client). It has been my go-to MQTT client for many years. It was fast, reliable and had features that were non-existing in alternative libraries. However, the underlying async TCP libraries are lacking updates, especially updates related to secure connections. Adapting this library to use up-to-date TCP clients would not be trivial. I eventually decided to write my own MQTT library, from scratch. + +The result is an almost non-blocking library with no external dependencies. The library is almost a drop-in replacement for the async-mqtt-client except a few parameter type changes (eg. `uint8_t*` instead of `char*` for payloads). + +# License + +This library is released under the MIT Licence. A copy is included in the repo. +Parts of this library, most notably the API, are based on [Async MQTT client for ESP8266 and ESP32](https://github.com/marvinroger/async-mqtt-client). diff --git a/lib/espMqttClient/src/Config.h b/lib/espMqttClient/src/Config.h new file mode 100644 index 000000000..aba779565 --- /dev/null +++ b/lib/espMqttClient/src/Config.h @@ -0,0 +1,58 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#ifndef EMC_TX_TIMEOUT +#define EMC_TX_TIMEOUT 5000 +#endif + +#ifndef EMC_RX_BUFFER_SIZE +#define EMC_RX_BUFFER_SIZE 1440 +#endif + +#ifndef EMC_TX_BUFFER_SIZE +#define EMC_TX_BUFFER_SIZE 1440 +#endif + +#ifndef EMC_MAX_TOPIC_LENGTH +#define EMC_MAX_TOPIC_LENGTH 128 +#endif + +#ifndef EMC_PAYLOAD_BUFFER_SIZE +#define EMC_PAYLOAD_BUFFER_SIZE 32 +#endif + +#ifndef EMC_MIN_FREE_MEMORY +#define EMC_MIN_FREE_MEMORY 4096 +#endif + +#ifndef EMC_ESP8266_MULTITHREADING +#define EMC_ESP8266_MULTITHREADING 0 +#endif + +#ifndef EMC_ALLOW_NOT_CONNECTED_PUBLISH +#define EMC_ALLOW_NOT_CONNECTED_PUBLISH 1 +#endif + +#ifndef EMC_WAIT_FOR_CONNACK +#define EMC_WAIT_FOR_CONNACK 1 +#endif + +#ifndef EMC_CLIENTID_LENGTH +// esp8266abc123 and esp32abcdef123456 +#define EMC_CLIENTID_LENGTH 23 + 1 +#endif + +#ifndef EMC_TASK_STACK_SIZE +#define EMC_TASK_STACK_SIZE 5120 +#endif + +#ifndef EMC_USE_WATCHDOG +#define EMC_USE_WATCHDOG 0 +#endif diff --git a/lib/espMqttClient/src/Helpers.h b/lib/espMqttClient/src/Helpers.h new file mode 100644 index 000000000..4a19224ba --- /dev/null +++ b/lib/espMqttClient/src/Helpers.h @@ -0,0 +1,49 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO_ARCH_ESP32) + #include // millis(), ESP.getFreeHeap(); + #include "freertos/FreeRTOS.h" + #include "freertos/task.h" + #include "esp_task_wdt.h" + #define EMC_SEMAPHORE_TAKE() xSemaphoreTake(_xSemaphore, portMAX_DELAY) + #define EMC_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore) + #define EMC_GET_FREE_MEMORY() std::max(ESP.getMaxAllocHeap(), ESP.getMaxAllocPsram()) + #define EMC_YIELD() taskYIELD() + #define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "esp32%06llx", ESP.getEfuseMac()); +#elif defined(ARDUINO_ARCH_ESP8266) + #include // millis(), ESP.getFreeHeap(); + #if EMC_ESP8266_MULTITHREADING + // This lib doesn't run use multithreading on ESP8266 + // _xSemaphore defined as std::atomic + #define EMC_SEMAPHORE_TAKE() while (_xSemaphore) { /*ESP.wdtFeed();*/ } _xSemaphore = true + #define EMC_SEMAPHORE_GIVE() _xSemaphore = false + #else + #define EMC_SEMAPHORE_TAKE() + #define EMC_SEMAPHORE_GIVE() + #endif + #define EMC_GET_FREE_MEMORY() ESP.getMaxFreeBlockSize() + // no need to yield for ESP8266, the Arduino framework does this internally + // yielding in async is forbidden (will crash) + #define EMC_YIELD() + #define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "esp8266%06x", ESP.getChipId()); +#elif defined(__linux__) + #include // NOLINT [build/c++11] + #include // NOLINT [build/c++11] for yield() + #define millis() std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count() + #define EMC_GET_FREE_MEMORY() 1000000000 + #define EMC_YIELD() std::this_thread::yield() + #define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "Client%04d%04d%04d", rand()%10000, rand()%10000, rand()%10000) + #include // NOLINT [build/c++11] + #define EMC_SEMAPHORE_TAKE() mtx.lock(); + #define EMC_SEMAPHORE_GIVE() mtx.unlock(); +#else + #error Target platform not supported +#endif diff --git a/lib/espMqttClient/src/Logging.h b/lib/espMqttClient/src/Logging.h new file mode 100644 index 000000000..3ba096a8d --- /dev/null +++ b/lib/espMqttClient/src/Logging.h @@ -0,0 +1,43 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO_ARCH_ESP32) + #include + #include "freertos/FreeRTOS.h" + #include "freertos/task.h" + #if defined(DEBUG_ESP_MQTT_CLIENT) + // Logging is en/disabled by Arduino framework macros + #define emc_log_i(...) log_i(__VA_ARGS__) + #define emc_log_e(...) log_e(__VA_ARGS__) + #define emc_log_w(...) log_w(__VA_ARGS__) + #else + // Logging is disabled + #define emc_log_i(...) + #define emc_log_e(...) + #define emc_log_w(...) + #endif +#elif defined(ARDUINO_ARCH_ESP8266) + #if defined(DEBUG_ESP_PORT) && defined(DEBUG_ESP_MQTT_CLIENT) + #include + #define emc_log_i(...) DEBUG_ESP_PORT.printf(__VA_ARGS__); DEBUG_ESP_PORT.print("\n") + #define emc_log_e(...) DEBUG_ESP_PORT.printf(__VA_ARGS__); DEBUG_ESP_PORT.print("\n") + #define emc_log_w(...) DEBUG_ESP_PORT.printf(__VA_ARGS__); DEBUG_ESP_PORT.print("\n") + #else + #define emc_log_i(...) + #define emc_log_e(...) + #define emc_log_w(...) + #endif +#else + // when building for PC, always show debug statements as part of testing suite + #include + #define emc_log_i(...) std::cout << "[I] " << __FILE__ ":" << __LINE__ << ": "; printf(__VA_ARGS__); std::cout << std::endl + #define emc_log_e(...) std::cout << "[E] " << __FILE__ ":" << __LINE__ << ": "; printf(__VA_ARGS__); std::cout << std::endl + #define emc_log_w(...) std::cout << "[W] " << __FILE__ ":" << __LINE__ << ": "; printf(__VA_ARGS__); std::cout << std::endl +#endif diff --git a/lib/espMqttClient/src/MqttClient.cpp b/lib/espMqttClient/src/MqttClient.cpp new file mode 100644 index 000000000..314a3c385 --- /dev/null +++ b/lib/espMqttClient/src/MqttClient.cpp @@ -0,0 +1,754 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "MqttClient.h" + +using espMqttClientInternals::Packet; +using espMqttClientInternals::PacketType; +using espMqttClientTypes::DisconnectReason; +using espMqttClientTypes::Error; + +MqttClient::MqttClient(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority, uint8_t core) +#if defined(ARDUINO_ARCH_ESP32) + : _useInternalTask(useInternalTask) + , _transport(nullptr) +#else + : _transport(nullptr) +#endif + , _onConnectCallback(nullptr) + , _onDisconnectCallback(nullptr) + , _onSubscribeCallback(nullptr) + , _onUnsubscribeCallback(nullptr) + , _onMessageCallback(nullptr) + , _onPublishCallback(nullptr) + , _onErrorCallback(nullptr) + , _clientId(nullptr) + , _ip() + , _host(nullptr) + , _port(1883) + , _useIp(false) + , _keepAlive(15000) + , _cleanSession(true) + , _username(nullptr) + , _password(nullptr) + , _willTopic(nullptr) + , _willPayload(nullptr) + , _willPayloadLength(0) + , _willQos(0) + , _willRetain(false) + , _timeout(EMC_TX_TIMEOUT) + , _state(State::disconnected) + , _generatedClientId{0} + , _packetId(0) +#if defined(ARDUINO_ARCH_ESP32) + , _xSemaphore(nullptr) + , _taskHandle(nullptr) +#endif + , _rxBuffer{0} + , _outbox() + , _bytesSent(0) + , _parser() + , _lastClientActivity(0) + , _lastServerActivity(0) + , _pingSent(false) + , _disconnectReason(DisconnectReason::TCP_DISCONNECTED) +#if defined(ARDUINO_ARCH_ESP32) && ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO + , _highWaterMark(4294967295) +#endif +{ + EMC_GENERATE_CLIENTID(_generatedClientId); +#if defined(ARDUINO_ARCH_ESP32) + _xSemaphore = xSemaphoreCreateMutex(); + EMC_SEMAPHORE_GIVE(); // release before first use + if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { + xTaskCreatePinnedToCore((TaskFunction_t)_loop, "mqttclient", EMC_TASK_STACK_SIZE, this, priority, &_taskHandle, core); + } +#else + (void)useInternalTask; + (void)priority; + (void)core; +#endif + _clientId = _generatedClientId; +} + +MqttClient::~MqttClient() { + disconnect(true); + _clearQueue(2); +#if defined(ARDUINO_ARCH_ESP32) + vSemaphoreDelete(_xSemaphore); + if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { +#if EMC_USE_WATCHDOG + esp_task_wdt_delete(_taskHandle); // not sure if this is really needed +#endif + vTaskDelete(_taskHandle); + } +#endif +} + +bool MqttClient::connected() const { + if (_state == State::connected) + return true; + return false; +} + +bool MqttClient::disconnected() const { + if (_state == State::disconnected) + return true; + return false; +} + +bool MqttClient::connect() { + bool result = true; + if (_state == State::disconnected) { + EMC_SEMAPHORE_TAKE(); + if (_addPacketFront(_cleanSession, + _username, + _password, + _willTopic, + _willRetain, + _willQos, + _willPayload, + _willPayloadLength, + (uint16_t)(_keepAlive / 1000), // 32b to 16b doesn't overflow because it comes from 16b orignally + _clientId)) { +#if defined(ARDUINO_ARCH_ESP32) + if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { + vTaskResume(_taskHandle); + } +#endif + _state = State::connectingTcp1; + } else { + EMC_SEMAPHORE_GIVE(); + emc_log_e("Could not create CONNECT packet"); + _onError(0, Error::OUT_OF_MEMORY); + result = false; + } + EMC_SEMAPHORE_GIVE(); + } + return result; +} + +bool MqttClient::disconnect(bool force) { + if (force && _state != State::disconnected && _state != State::disconnectingTcp1 && _state != State::disconnectingTcp2) { + _state = State::disconnectingTcp1; + return true; + } + if (!force && _state == State::connected) { + _state = State::disconnectingMqtt1; + return true; + } + return false; +} + +uint16_t MqttClient::publish(const char * topic, uint8_t qos, bool retain, const uint8_t * payload, size_t length) { +#if !EMC_ALLOW_NOT_CONNECTED_PUBLISH + if (_state != State::connected) { +#else + if (_state > State::connected) { +#endif + return 0; + } + uint16_t packetId = (qos > 0) ? _getNextPacketId() : 1; + EMC_SEMAPHORE_TAKE(); + if (!_addPacket(packetId, topic, payload, length, qos, retain)) { + emc_log_e("Could not create PUBLISH packet"); + _onError(packetId, Error::OUT_OF_MEMORY); + packetId = 0; + } + EMC_SEMAPHORE_GIVE(); + return packetId; +} + +uint16_t MqttClient::publish(const char * topic, uint8_t qos, bool retain, const char * payload) { + size_t len = strlen(payload); + return publish(topic, qos, retain, reinterpret_cast(payload), len); +} + +uint16_t MqttClient::publish(const char * topic, uint8_t qos, bool retain, espMqttClientTypes::PayloadCallback callback, size_t length) { +#if !EMC_ALLOW_NOT_CONNECTED_PUBLISH + if (_state != State::connected) { +#else + if (_state > State::connected) { +#endif + return 0; + } + uint16_t packetId = (qos > 0) ? _getNextPacketId() : 1; + EMC_SEMAPHORE_TAKE(); + if (!_addPacket(packetId, topic, callback, length, qos, retain)) { + emc_log_e("Could not create PUBLISH packet"); + _onError(packetId, Error::OUT_OF_MEMORY); + packetId = 0; + } + EMC_SEMAPHORE_GIVE(); + return packetId; +} + +void MqttClient::clearQueue(bool deleteSessionData) { + _clearQueue(deleteSessionData ? 2 : 0); +} + +const char * MqttClient::getClientId() const { + return _clientId; +} + +void MqttClient::loop() { + switch (_state) { + case State::disconnected: +#if defined(ARDUINO_ARCH_ESP32) + if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { + vTaskSuspend(_taskHandle); + } +#endif + break; + case State::connectingTcp1: + if (_useIp ? _transport->connect(_ip, _port) : _transport->connect(_host, _port)) { + _state = State::connectingTcp2; + } else { + _state = State::disconnectingTcp1; + _disconnectReason = DisconnectReason::TCP_DISCONNECTED; + break; + } + // Falling through to speed up connecting on blocking transport 'connect' implementations + [[fallthrough]]; + case State::connectingTcp2: + if (_transport->connected()) { + _parser.reset(); + _lastClientActivity = _lastServerActivity = millis(); + _state = State::connectingMqtt; + } + break; + case State::connectingMqtt: +#if EMC_WAIT_FOR_CONNACK + if (_transport->connected()) { + _sendPacket(); + _checkIncoming(); + _checkPing(); + } else { + _state = State::disconnectingTcp1; + _disconnectReason = DisconnectReason::TCP_DISCONNECTED; + } + break; +#else + // receipt of CONNACK packet will set state to CONNECTED + // client however is allowed to send packets before CONNACK is received + // so we fall through to 'connected' + [[fallthrough]]; +#endif + case State::connected: + [[fallthrough]]; + case State::disconnectingMqtt2: + if (_transport->connected()) { + // CONNECT packet is first in the queue + _checkOutbox(); + _checkIncoming(); + _checkPing(); + _checkTimeout(); + } else { + _state = State::disconnectingTcp1; + _disconnectReason = DisconnectReason::TCP_DISCONNECTED; + } + break; + case State::disconnectingMqtt1: + EMC_SEMAPHORE_TAKE(); + if (_outbox.empty()) { + if (!_addPacket(PacketType.DISCONNECT)) { + EMC_SEMAPHORE_GIVE(); + emc_log_e("Could not create DISCONNECT packet"); + _onError(0, Error::OUT_OF_MEMORY); + } else { + _state = State::disconnectingMqtt2; + } + } + EMC_SEMAPHORE_GIVE(); + _checkOutbox(); + _checkIncoming(); + _checkPing(); + _checkTimeout(); + break; + case State::disconnectingTcp1: + _transport->stop(); + _state = State::disconnectingTcp2; + break; // keep break to accomodate async clients + case State::disconnectingTcp2: + if (_transport->disconnected()) { + _clearQueue(0); + _bytesSent = 0; + _state = State::disconnected; + if (_onDisconnectCallback) + _onDisconnectCallback(_disconnectReason); + } + break; + // all cases covered, no default case + } + EMC_YIELD(); +#if defined(ARDUINO_ARCH_ESP32) && ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO + size_t waterMark = uxTaskGetStackHighWaterMark(NULL); + if (waterMark < _highWaterMark) { + _highWaterMark = waterMark; + emc_log_i("Stack usage: %zu/%i", EMC_TASK_STACK_SIZE - _highWaterMark, EMC_TASK_STACK_SIZE); + } +#endif +} + +#if defined(ARDUINO_ARCH_ESP32) +void MqttClient::_loop(MqttClient * c) { +#if EMC_USE_WATCHDOG + if (esp_task_wdt_add(NULL) != ESP_OK) { + emc_log_e("Failed to add async task to WDT"); + } +#endif + for (;;) { + c->loop(); +#if EMC_USE_WATCHDOG + esp_task_wdt_reset(); +#endif + } +} +#endif + +uint16_t MqttClient::_getNextPacketId() { + uint16_t packetId = 0; + EMC_SEMAPHORE_TAKE(); + // cppcheck-suppress knownConditionTrueFalse + packetId = (++_packetId == 0) ? ++_packetId : _packetId; + EMC_SEMAPHORE_GIVE(); + return packetId; +} + +void MqttClient::_checkOutbox() { + while (_sendPacket() > 0) { + if (!_advanceOutbox()) { + break; + } + } +} + +int MqttClient::_sendPacket() { + EMC_SEMAPHORE_TAKE(); + OutgoingPacket * packet = _outbox.getCurrent(); + + int32_t wantToWrite = 0; + int32_t written = 0; + if (packet && (wantToWrite == written)) { + // mixing signed with unsigned here but safe because of MQTT packet size limits + wantToWrite = packet->packet.available(_bytesSent); + if (wantToWrite == 0) { + EMC_SEMAPHORE_GIVE(); + return 0; + } + written = _transport->write(packet->packet.data(_bytesSent), wantToWrite); + if (written < 0) { + emc_log_w("Write error, check connection"); + EMC_SEMAPHORE_GIVE(); + return -1; + } + packet->timeSent = millis(); + _lastClientActivity = millis(); + _bytesSent += written; + emc_log_i("tx %zu/%zu (%02x)", _bytesSent, packet->packet.size(), packet->packet.packetType()); + } + EMC_SEMAPHORE_GIVE(); + return written; +} + +bool MqttClient::_advanceOutbox() { + EMC_SEMAPHORE_TAKE(); + OutgoingPacket * packet = _outbox.getCurrent(); + if (packet && _bytesSent == packet->packet.size()) { + if ((packet->packet.packetType()) == PacketType.DISCONNECT) { + _state = State::disconnectingTcp1; + _disconnectReason = DisconnectReason::USER_OK; + } + if (packet->packet.removable()) { + _outbox.removeCurrent(); + } else { + // we already set 'dup' here, in case we have to retry + if ((packet->packet.packetType()) == PacketType.PUBLISH) + packet->packet.setDup(); + _outbox.next(); + } + packet = _outbox.getCurrent(); + _bytesSent = 0; + } + EMC_SEMAPHORE_GIVE(); + return packet; +} + +void MqttClient::_checkIncoming() { + int32_t remainingBufferLength = _transport->read(_rxBuffer, EMC_RX_BUFFER_SIZE); + if (remainingBufferLength > 0) { + _lastServerActivity = millis(); + emc_log_i("rx len %i", remainingBufferLength); + size_t bytesParsed = 0; + size_t index = 0; + while (remainingBufferLength > 0) { + espMqttClientInternals::ParserResult result = _parser.parse(&_rxBuffer[index], remainingBufferLength, &bytesParsed); + if (result == espMqttClientInternals::ParserResult::packet) { + espMqttClientInternals::MQTTPacketType packetType = _parser.getPacket().fixedHeader.packetType & 0xF0; + if (_state == State::connectingMqtt && packetType != PacketType.CONNACK) { + emc_log_w("Disconnecting, expected CONNACK - protocol error"); + _state = State::disconnectingTcp1; + return; + } + switch (packetType & 0xF0) { + case PacketType.CONNACK: + _onConnack(); + if (_state != State::connected) { + return; + } + break; + case PacketType.PUBLISH: + if (_state >= State::disconnectingMqtt1) + break; // stop processing incoming once user has called disconnect + _onPublish(); + break; + case PacketType.PUBACK: + _onPuback(); + break; + case PacketType.PUBREC: + _onPubrec(); + break; + case PacketType.PUBREL: + _onPubrel(); + break; + case PacketType.PUBCOMP: + _onPubcomp(); + break; + case PacketType.SUBACK: + _onSuback(); + break; + case PacketType.UNSUBACK: + _onUnsuback(); + break; + case PacketType.PINGRESP: + _pingSent = false; + break; + } + } else if (result == espMqttClientInternals::ParserResult::protocolError) { + emc_log_w("Disconnecting, protocol error"); + _state = State::disconnectingTcp1; + _disconnectReason = DisconnectReason::TCP_DISCONNECTED; + return; + } + remainingBufferLength -= bytesParsed; + index += bytesParsed; + emc_log_i("Parsed %zu - remaining %i", bytesParsed, remainingBufferLength); + bytesParsed = 0; + } + } +} + +void MqttClient::_checkPing() { + if (_keepAlive == 0) + return; // keepalive is disabled + + uint32_t currentMillis = millis(); + + // disconnect when server was inactive for twice the keepalive time + if (currentMillis - _lastServerActivity > 2 * _keepAlive) { + emc_log_w("Disconnecting, server exceeded keepalive"); + _state = State::disconnectingTcp1; + _disconnectReason = DisconnectReason::TCP_DISCONNECTED; + return; + } + + // send ping when client was inactive during the keepalive time + // or when server hasn't responded within keepalive time (typically due to QOS 0) + if (!_pingSent && ((currentMillis - _lastClientActivity > _keepAlive) || (currentMillis - _lastServerActivity > _keepAlive))) { + EMC_SEMAPHORE_TAKE(); + if (!_addPacket(PacketType.PINGREQ)) { + EMC_SEMAPHORE_GIVE(); + emc_log_e("Could not create PING packet"); + return; + } + EMC_SEMAPHORE_GIVE(); + _pingSent = true; + } +} + +void MqttClient::_checkTimeout() { + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + // check that we're not busy sending + // don't check when first item hasn't been sent yet + if (it && _bytesSent == 0 && it.get() != _outbox.getCurrent()) { + if (millis() - it.get()->timeSent > _timeout) { + emc_log_w("Packet ack timeout, retrying"); + _outbox.resetCurrent(); + } + } + EMC_SEMAPHORE_GIVE(); +} + +void MqttClient::_onConnack() { + if (_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode == 0x00) { + _pingSent = false; // reset after keepalive timeout disconnect + _state = State::connected; + _advanceOutbox(); + if (_parser.getPacket().variableHeader.fixed.connackVarHeader.sessionPresent == 0) { + _clearQueue(1); + } + if (_onConnectCallback) { + _onConnectCallback(_parser.getPacket().variableHeader.fixed.connackVarHeader.sessionPresent); + } + } else { + _state = State::disconnectingTcp1; + // cast is safe because the parser already checked for a valid return code + _disconnectReason = static_cast(_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode); + } +} + +void MqttClient::_onPublish() { + espMqttClientInternals::IncomingPacket p = _parser.getPacket(); + uint8_t qos = p.qos(); + bool retain = p.retain(); + bool dup = p.dup(); + uint16_t packetId = p.variableHeader.fixed.packetId; + bool callback = true; + if (qos == 1) { + if (p.payload.index + p.payload.length == p.payload.total) { + EMC_SEMAPHORE_TAKE(); + if (!_addPacket(PacketType.PUBACK, packetId)) { + emc_log_e("Could not create PUBACK packet"); + } + EMC_SEMAPHORE_GIVE(); + } + } else if (qos == 2) { + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + while (it) { + if ((it.get()->packet.packetType()) == PacketType.PUBREC && it.get()->packet.packetId() == packetId) { + callback = false; + _outbox.remove(it); + emc_log_e("QoS2 packet previously delivered"); + break; + } + ++it; + } + if (p.payload.index + p.payload.length == p.payload.total) { + if (!_addPacket(PacketType.PUBREC, packetId)) { + emc_log_e("Could not create PUBREC packet"); + } + } + EMC_SEMAPHORE_GIVE(); + } + if (callback && _onMessageCallback) + _onMessageCallback({qos, dup, retain, packetId}, p.variableHeader.topic, p.payload.data, p.payload.length, p.payload.index, p.payload.total); +} + +void MqttClient::_onPuback() { + bool callback = false; + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + while (it) { + // PUBACKs come in the order PUBs are sent. So we only check the first PUB packet in outbox + // if it doesn't match the ID, return + if ((it.get()->packet.packetType()) == PacketType.PUBLISH) { + if (it.get()->packet.packetId() == idToMatch) { + callback = true; + _outbox.remove(it); + break; + } + emc_log_w("Received out of order PUBACK"); + break; + } + ++it; + } + EMC_SEMAPHORE_GIVE(); + if (callback) { + if (_onPublishCallback) + _onPublishCallback(idToMatch); + } else { + emc_log_w("No matching PUBLISH packet found"); + } +} + +void MqttClient::_onPubrec() { + bool success = false; + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + while (it) { + // PUBRECs come in the order PUBs are sent. So we only check the first PUB packet in outbox + // if it doesn't match the ID, return + if ((it.get()->packet.packetType()) == PacketType.PUBLISH) { + if (it.get()->packet.packetId() == idToMatch) { + if (!_addPacket(PacketType.PUBREL, idToMatch)) { + emc_log_e("Could not create PUBREL packet"); + } + _outbox.remove(it); + success = true; + break; + } + emc_log_w("Received out of order PUBREC"); + break; + } + ++it; + } + if (!success) { + emc_log_w("No matching PUBLISH packet found"); + } + EMC_SEMAPHORE_GIVE(); +} + +void MqttClient::_onPubrel() { + bool success = false; + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + while (it) { + // PUBRELs come in the order PUBRECs are sent. So we only check the first PUBREC packet in outbox + // if it doesn't match the ID, return + if ((it.get()->packet.packetType()) == PacketType.PUBREC) { + if (it.get()->packet.packetId() == idToMatch) { + if (!_addPacket(PacketType.PUBCOMP, idToMatch)) { + emc_log_e("Could not create PUBCOMP packet"); + } + _outbox.remove(it); + success = true; + break; + } + emc_log_w("Received out of order PUBREL"); + break; + } + ++it; + } + if (!success) { + emc_log_w("No matching PUBREC packet found"); + } + EMC_SEMAPHORE_GIVE(); +} + +void MqttClient::_onPubcomp() { + bool callback = false; + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + while (it) { + // PUBCOMPs come in the order PUBRELs are sent. So we only check the first PUBREL packet in outbox + // if it doesn't match the ID, return + if ((it.get()->packet.packetType()) == PacketType.PUBREL) { + if (it.get()->packet.packetId() == idToMatch) { + // if (!_addPacket(PacketType.PUBCOMP, idToMatch)) { + // emc_log_e("Could not create PUBCOMP packet"); + // } + callback = true; + _outbox.remove(it); + break; + } + emc_log_w("Received out of order PUBCOMP"); + break; + } + ++it; + } + EMC_SEMAPHORE_GIVE(); + if (callback) { + if (_onPublishCallback) + _onPublishCallback(idToMatch); + } else { + emc_log_w("No matching PUBREL packet found"); + } +} + +void MqttClient::_onSuback() { + bool callback = false; + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + while (it) { + if (((it.get()->packet.packetType()) == PacketType.SUBSCRIBE) && it.get()->packet.packetId() == idToMatch) { + callback = true; + _outbox.remove(it); + break; + } + ++it; + } + EMC_SEMAPHORE_GIVE(); + if (callback) { + if (_onSubscribeCallback) + _onSubscribeCallback(idToMatch, + reinterpret_cast(_parser.getPacket().payload.data), + _parser.getPacket().payload.total); + } else { + emc_log_w("received SUBACK without SUB"); + } +} + +void MqttClient::_onUnsuback() { + bool callback = false; + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + while (it) { + if (it.get()->packet.packetId() == idToMatch) { + callback = true; + _outbox.remove(it); + break; + } + ++it; + } + EMC_SEMAPHORE_GIVE(); + if (callback) { + if (_onUnsubscribeCallback) + _onUnsubscribeCallback(idToMatch); + } else { + emc_log_w("received UNSUBACK without UNSUB"); + } +} + +uint16_t MqttClient::getQueue() const { + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + uint16_t count = 0; + while (it) { + ++count; + ++it; + } + EMC_SEMAPHORE_GIVE(); + return count; +} + +void MqttClient::_clearQueue(int clearData) { + emc_log_i("clearing queue (clear session: %d)", clearData); + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + if (clearData == 0) { + // keep PUB (qos > 0, aka packetID != 0), PUBREC and PUBREL + // Spec only mentions PUB and PUBREL but this lib implements method B from point 4.3.3 (Fig. 4.3) + // and stores the packet id in the PUBREC packet. So we also must keep PUBREC. + while (it) { + espMqttClientInternals::MQTTPacketType type = it.get()->packet.packetType(); + if (type == PacketType.PUBREC || type == PacketType.PUBREL || (type == PacketType.PUBLISH && it.get()->packet.packetId() != 0)) { + ++it; + } else { + _outbox.remove(it); + } + } + } else if (clearData == 1) { + // keep PUB + while (it) { + if (it.get()->packet.packetType() == PacketType.PUBLISH) { + ++it; + } else { + _outbox.remove(it); + } + } + } else { // clearData == 2 + while (it) { + _outbox.remove(it); + } + } + EMC_SEMAPHORE_GIVE(); +} + +void MqttClient::_onError(uint16_t packetId, espMqttClientTypes::Error error) { + if (_onErrorCallback) { + _onErrorCallback(packetId, error); + } +} diff --git a/lib/espMqttClient/src/MqttClient.h b/lib/espMqttClient/src/MqttClient.h new file mode 100644 index 000000000..6d36e0af5 --- /dev/null +++ b/lib/espMqttClient/src/MqttClient.h @@ -0,0 +1,190 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +API is based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include +#include + +#include "Helpers.h" +#include "Config.h" +#include "TypeDefs.h" +#include "Logging.h" +#include "Outbox.h" +#include "Packets/Packet.h" +#include "Packets/Parser.h" +#include "Transport/Transport.h" + +class MqttClient { + public: + virtual ~MqttClient(); + bool connected() const; + bool disconnected() const; + bool connect(); + bool disconnect(bool force = false); + template + uint16_t subscribe(const char* topic, uint8_t qos, Args&&... args) { + uint16_t packetId = _getNextPacketId(); + if (_state != State::connected) { + packetId = 0; + } else { + EMC_SEMAPHORE_TAKE(); + if (!_addPacket(packetId, topic, qos, std::forward(args) ...)) { + emc_log_e("Could not create SUBSCRIBE packet"); + packetId = 0; + } + EMC_SEMAPHORE_GIVE(); + } + return packetId; + } + template + uint16_t unsubscribe(const char* topic, Args&&... args) { + uint16_t packetId = _getNextPacketId(); + if (_state != State::connected) { + packetId = 0; + } else { + EMC_SEMAPHORE_TAKE(); + if (!_addPacket(packetId, topic, std::forward(args) ...)) { + emc_log_e("Could not create UNSUBSCRIBE packet"); + packetId = 0; + } + EMC_SEMAPHORE_GIVE(); + } + return packetId; + } + uint16_t publish(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length); + uint16_t publish(const char* topic, uint8_t qos, bool retain, const char* payload); + uint16_t publish(const char* topic, uint8_t qos, bool retain, espMqttClientTypes::PayloadCallback callback, size_t length); + void clearQueue(bool deleteSessionData = false); // Not MQTT compliant and may cause unpredictable results when `deleteSessionData` = true! + const char* getClientId() const; + uint16_t getQueue() const; + void loop(); + + protected: + explicit MqttClient(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority = 1, uint8_t core = 1); + espMqttClientTypes::UseInternalTask _useInternalTask; + espMqttClientInternals::Transport* _transport; + + espMqttClientTypes::OnConnectCallback _onConnectCallback; + espMqttClientTypes::OnDisconnectCallback _onDisconnectCallback; + espMqttClientTypes::OnSubscribeCallback _onSubscribeCallback; + espMqttClientTypes::OnUnsubscribeCallback _onUnsubscribeCallback; + espMqttClientTypes::OnMessageCallback _onMessageCallback; + espMqttClientTypes::OnPublishCallback _onPublishCallback; + espMqttClientTypes::OnErrorCallback _onErrorCallback; + typedef void(*mqttClientHook)(void*); + const char* _clientId; + IPAddress _ip; + const char* _host; + uint16_t _port; + bool _useIp; + uint32_t _keepAlive; + bool _cleanSession; + const char* _username; + const char* _password; + const char* _willTopic; + const uint8_t* _willPayload; + uint16_t _willPayloadLength; + uint8_t _willQos; + bool _willRetain; + uint32_t _timeout; + + // state is protected to allow state changes by the transport system, defined in child classes + // eg. to allow AsyncTCP + enum class State { + disconnected = 0, + connectingTcp1 = 1, + connectingTcp2 = 2, + connectingMqtt = 3, + connected = 4, + disconnectingMqtt1 = 5, + disconnectingMqtt2 = 6, + disconnectingTcp1 = 7, + disconnectingTcp2 = 8 + }; + std::atomic _state; + + private: + char _generatedClientId[EMC_CLIENTID_LENGTH]; + uint16_t _packetId; + +#if defined(ARDUINO_ARCH_ESP32) + SemaphoreHandle_t _xSemaphore; + TaskHandle_t _taskHandle; + static void _loop(MqttClient* c); +#elif defined(ARDUINO_ARCH_ESP8266) && EMC_ESP8266_MULTITHREADING + std::atomic _xSemaphore = false; +#elif defined(__linux__) + std::mutex mtx; +#endif + + uint8_t _rxBuffer[EMC_RX_BUFFER_SIZE]; + struct OutgoingPacket { + uint32_t timeSent; + espMqttClientInternals::Packet packet; + template + OutgoingPacket(uint32_t t, espMqttClientTypes::Error error, Args&&... args) : + timeSent(t), + packet(error, std::forward(args) ...) {} + }; + espMqttClientInternals::Outbox _outbox; + size_t _bytesSent; + espMqttClientInternals::Parser _parser; + uint32_t _lastClientActivity; + uint32_t _lastServerActivity; + bool _pingSent; + espMqttClientTypes::DisconnectReason _disconnectReason; + + uint16_t _getNextPacketId(); + + template + bool _addPacket(Args&&... args) { + espMqttClientTypes::Error error(espMqttClientTypes::Error::SUCCESS); + espMqttClientInternals::Outbox::Iterator it = _outbox.emplace(0, error, std::forward(args) ...); + if (it && error == espMqttClientTypes::Error::SUCCESS) return true; + return false; + } + + template + bool _addPacketFront(Args&&... args) { + espMqttClientTypes::Error error(espMqttClientTypes::Error::SUCCESS); + espMqttClientInternals::Outbox::Iterator it = _outbox.emplaceFront(0, error, std::forward(args) ...); + if (it && error == espMqttClientTypes::Error::SUCCESS) return true; + return false; + } + + void _checkOutbox(); + int _sendPacket(); + bool _advanceOutbox(); + void _checkIncoming(); + void _checkPing(); + void _checkTimeout(); + + void _onConnack(); + void _onPublish(); + void _onPuback(); + void _onPubrec(); + void _onPubrel(); + void _onPubcomp(); + void _onSuback(); + void _onUnsuback(); + + void _clearQueue(int clearData); // 0: keep session, + // 1: keep only PUBLISH qos > 0 + // 2: delete all + void _onError(uint16_t packetId, espMqttClientTypes::Error error); + + #if defined(ARDUINO_ARCH_ESP32) + #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO + size_t _highWaterMark; + #endif + #endif +}; diff --git a/lib/espMqttClient/src/MqttClientSetup.h b/lib/espMqttClient/src/MqttClientSetup.h new file mode 100644 index 000000000..73458d477 --- /dev/null +++ b/lib/espMqttClient/src/MqttClientSetup.h @@ -0,0 +1,116 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +API is based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include "MqttClient.h" + +template +class MqttClientSetup : public MqttClient { + public: + T& setKeepAlive(uint16_t keepAlive) { + _keepAlive = keepAlive * 1000; // s to ms conversion, will also do 16 to 32 bit conversion + return static_cast(*this); + } + + T& setClientId(const char* clientId) { + _clientId = clientId; + return static_cast(*this); + } + + T& setCleanSession(bool cleanSession) { + _cleanSession = cleanSession; + return static_cast(*this); + } + + T& setCredentials(const char* username, const char* password) { + _username = username; + _password = password; + return static_cast(*this); + } + + T& setWill(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length) { + _willTopic = topic; + _willQos = qos; + _willRetain = retain; + _willPayload = payload; + if (!_willPayload) { + _willPayloadLength = 0; + } else { + _willPayloadLength = length; + } + return static_cast(*this); + } + + T& setWill(const char* topic, uint8_t qos, bool retain, const char* payload) { + return setWill(topic, qos, retain, reinterpret_cast(payload), strlen(payload)); + } + + T& setServer(IPAddress ip, uint16_t port) { + _ip = ip; + _port = port; + _useIp = true; + return static_cast(*this); + } + + T& setServer(const char* host, uint16_t port) { + _host = host; + _port = port; + _useIp = false; + return static_cast(*this); + } + + T& setTimeout(uint16_t timeout) { + _timeout = timeout * 1000; // s to ms conversion, will also do 16 to 32 bit conversion + return static_cast(*this); + } + + T& onConnect(espMqttClientTypes::OnConnectCallback callback) { + _onConnectCallback = callback; + return static_cast(*this); + } + + T& onDisconnect(espMqttClientTypes::OnDisconnectCallback callback) { + _onDisconnectCallback = callback; + return static_cast(*this); + } + + T& onSubscribe(espMqttClientTypes::OnSubscribeCallback callback) { + _onSubscribeCallback = callback; + return static_cast(*this); + } + + T& onUnsubscribe(espMqttClientTypes::OnUnsubscribeCallback callback) { + _onUnsubscribeCallback = callback; + return static_cast(*this); + } + + T& onMessage(espMqttClientTypes::OnMessageCallback callback) { + _onMessageCallback = callback; + return static_cast(*this); + } + + T& onPublish(espMqttClientTypes::OnPublishCallback callback) { + _onPublishCallback = callback; + return static_cast(*this); + } + + /* + T& onError(espMqttClientTypes::OnErrorCallback callback) { + _onErrorCallback = callback; + return static_cast(*this); + } + */ + + protected: + explicit MqttClientSetup(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority = 1, uint8_t core = 1) + : MqttClient(useInternalTask, priority, core) {} +}; diff --git a/lib/espMqttClient/src/Outbox.h b/lib/espMqttClient/src/Outbox.h new file mode 100644 index 000000000..dfbbd13c0 --- /dev/null +++ b/lib/espMqttClient/src/Outbox.h @@ -0,0 +1,207 @@ + +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include // new (std::nothrow) +#include // std::forward + +namespace espMqttClientInternals { + +/** + * @brief Singly linked queue with builtin non-invalidating forward iterator + * + * Queue items can only be emplaced, at front and back of the queue. + * Remove items using an iterator or the builtin iterator. + */ + +template +class Outbox { + public: + Outbox() + : _first(nullptr) + , _last(nullptr) + , _current(nullptr) + , _prev(nullptr) {} + ~Outbox() { + while (_first) { + Node* n = _first->next; + delete _first; + _first = n; + } + } + + struct Node { + public: + template + explicit Node(Args&&... args) + : data(std::forward(args) ...) + , next(nullptr) { + // empty + } + + T data; + Node* next; + }; + + class Iterator { + friend class Outbox; + public: + void operator++() { + if (_node) { + _prev = _node; + _node = _node->next; + } + } + + explicit operator bool() const { + if (_node) return true; + return false; + } + + T* get() const { + if (_node) return &(_node->data); + return nullptr; + } + + private: + Node* _node = nullptr; + Node* _prev = nullptr; + }; + + // add node to back, advance current to new if applicable + template + Iterator emplace(Args&&... args) { + Iterator it; + Node* node = new (std::nothrow) Node(std::forward(args) ...); + if (node != nullptr) { + if (!_first) { + // queue is empty + _first = _current = node; + } else { + // queue has at least one item + _last->next = node; + it._prev = _last; + } + _last = node; + it._node = node; + // point current to newly created if applicable + if (!_current) { + _current = _last; + } + } + return it; + } + + // add item to front, current points to newly created front. + template + Iterator emplaceFront(Args&&... args) { + Iterator it; + Node* node = new (std::nothrow) Node(std::forward(args) ...); + if (node != nullptr) { + if (!_first) { + // queue is empty + _last = node; + } else { + // queue has at least one item + node->next = _first; + } + _current = _first = node; + _prev = nullptr; + it._node = node; + } + return it; + } + + // remove node at iterator, iterator points to next + void remove(Iterator& it) { // NOLINT(runtime/references) + if (!it) return; + Node* node = it._node; + Node* prev = it._prev; + ++it; + _remove(prev, node); + } + + // remove current node, current points to next + void removeCurrent() { + _remove(_prev, _current); + } + + // Get current item or return nullptr + T* getCurrent() const { + if (_current) return &(_current->data); + return nullptr; + } + + void resetCurrent() { + _current = _first; + } + + Iterator front() const { + Iterator it; + it._node = _first; + return it; + } + + // Advance current item + void next() { + if (_current) { + _prev = _current; + _current = _current->next; + } + } + + // Outbox is empty + bool empty() { + if (!_first) return true; + return false; + } + + private: + Node* _first; + Node* _last; + Node* _current; + Node* _prev; // element just before _current + + void _remove(Node* prev, Node* node) { + if (!node) return; + + // set current to next, node->next may be nullptr + if (_current == node) { + _current = node->next; + } + + if (_prev == node) { + _prev = prev; + } + + // only one element in outbox + if (_first == _last) { + _first = _last = nullptr; + + // delete first el in longer outbox + } else if (_first == node) { + _first = node->next; + + // delete last in longer outbox + } else if (_last == node) { + _last = prev; + _last->next = nullptr; + + // delete somewhere in the middle + } else { + prev->next = node->next; + } + + // finally, delete the node + delete node; + } +}; + +} // end namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/Constants.h b/lib/espMqttClient/src/Packets/Constants.h new file mode 100644 index 000000000..ee92e3114 --- /dev/null +++ b/lib/espMqttClient/src/Packets/Constants.h @@ -0,0 +1,77 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +Parts are based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include + +namespace espMqttClientInternals { + +constexpr const char PROTOCOL[] = "MQTT"; +constexpr const uint8_t PROTOCOL_LEVEL = 0b00000100; + +typedef uint8_t MQTTPacketType; + +constexpr struct { + const uint8_t RESERVED1 = 0; + const uint8_t CONNECT = 1 << 4; + const uint8_t CONNACK = 2 << 4; + const uint8_t PUBLISH = 3 << 4; + const uint8_t PUBACK = 4 << 4; + const uint8_t PUBREC = 5 << 4; + const uint8_t PUBREL = 6 << 4; + const uint8_t PUBCOMP = 7 << 4; + const uint8_t SUBSCRIBE = 8 << 4; + const uint8_t SUBACK = 9 << 4; + const uint8_t UNSUBSCRIBE = 10 << 4; + const uint8_t UNSUBACK = 11 << 4; + const uint8_t PINGREQ = 12 << 4; + const uint8_t PINGRESP = 13 << 4; + const uint8_t DISCONNECT = 14 << 4; + const uint8_t RESERVED2 = 1 << 4; +} PacketType; + +constexpr struct { + const uint8_t CONNECT_RESERVED = 0x00; + const uint8_t CONNACK_RESERVED = 0x00; + const uint8_t PUBLISH_DUP = 0x08; + const uint8_t PUBLISH_QOS0 = 0x00; + const uint8_t PUBLISH_QOS1 = 0x02; + const uint8_t PUBLISH_QOS2 = 0x04; + const uint8_t PUBLISH_QOSRESERVED = 0x06; + const uint8_t PUBLISH_RETAIN = 0x01; + const uint8_t PUBACK_RESERVED = 0x00; + const uint8_t PUBREC_RESERVED = 0x00; + const uint8_t PUBREL_RESERVED = 0x02; + const uint8_t PUBCOMP_RESERVED = 0x00; + const uint8_t SUBSCRIBE_RESERVED = 0x02; + const uint8_t SUBACK_RESERVED = 0x00; + const uint8_t UNSUBSCRIBE_RESERVED = 0x02; + const uint8_t UNSUBACK_RESERVED = 0x00; + const uint8_t PINGREQ_RESERVED = 0x00; + const uint8_t PINGRESP_RESERVED = 0x00; + const uint8_t DISCONNECT_RESERVED = 0x00; + const uint8_t RESERVED2_RESERVED = 0x00; +} HeaderFlag; + +constexpr struct { + const uint8_t USERNAME = 0x80; + const uint8_t PASSWORD = 0x40; + const uint8_t WILL_RETAIN = 0x20; + const uint8_t WILL_QOS0 = 0x00; + const uint8_t WILL_QOS1 = 0x08; + const uint8_t WILL_QOS2 = 0x10; + const uint8_t WILL = 0x04; + const uint8_t CLEAN_SESSION = 0x02; + const uint8_t RESERVED = 0x00; +} ConnectFlag; + +} // end namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/Packet.cpp b/lib/espMqttClient/src/Packets/Packet.cpp new file mode 100644 index 000000000..df463ef7b --- /dev/null +++ b/lib/espMqttClient/src/Packets/Packet.cpp @@ -0,0 +1,438 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "Packet.h" + +namespace espMqttClientInternals { + +Packet::~Packet() { + free(_data); +} + +size_t Packet::available(size_t index) { + if (index >= _size) return 0; + if (!_getPayload) return _size - index; + return _chunkedAvailable(index); +} + +const uint8_t* Packet::data(size_t index) const { + if (!_getPayload) { + if (!_data) return nullptr; + if (index >= _size) return nullptr; + return &_data[index]; + } + return _chunkedData(index); +} + +size_t Packet::size() const { + return _size; +} + +void Packet::setDup() { + if (!_data) return; + if (packetType() != PacketType.PUBLISH) return; + if (_packetId == 0) return; + _data[0] |= 0x08; +} + +uint16_t Packet::packetId() const { + return _packetId; +} + +MQTTPacketType Packet::packetType() const { + if (_data) return static_cast(_data[0] & 0xF0); + return static_cast(0); +} + +bool Packet::removable() const { + if (_packetId == 0) return true; + if ((packetType() == PacketType.PUBACK) || (packetType() == PacketType.PUBCOMP)) return true; + return false; +} + +Packet::Packet(espMqttClientTypes::Error& error, + bool cleanSession, + const char* username, + const char* password, + const char* willTopic, + bool willRetain, + uint8_t willQos, + const uint8_t* willPayload, + uint16_t willPayloadLength, + uint16_t keepAlive, + const char* clientId) +: _packetId(0) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + if (willPayload && willPayloadLength == 0) { + size_t length = strlen(reinterpret_cast(willPayload)); + if (length > UINT16_MAX) { + emc_log_w("Payload length truncated (l:%zu)", length); + willPayloadLength = UINT16_MAX; + } else { + willPayloadLength = length; + } + } + if (!clientId || strlen(clientId) == 0) { + emc_log_w("clientId not set error"); + error = espMqttClientTypes::Error::MALFORMED_PARAMETER; + return; + } + + // Calculate size + size_t remainingLength = + 6 + // protocol + 1 + // protocol level + 1 + // connect flags + 2 + // keepalive + 2 + strlen(clientId) + + (willTopic ? 2 + strlen(willTopic) + 2 + willPayloadLength : 0) + + (username ? 2 + strlen(username) : 0) + + (password ? 2 + strlen(password) : 0); + + // allocate memory + if (!_allocate(remainingLength)) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + + // serialize + size_t pos = 0; + + // FIXED HEADER + _data[pos++] = PacketType.CONNECT | HeaderFlag.CONNECT_RESERVED; + pos += encodeRemainingLength(remainingLength, &_data[pos]); + pos += encodeString(PROTOCOL, &_data[pos]); + _data[pos++] = PROTOCOL_LEVEL; + uint8_t connectFlags = 0; + if (cleanSession) connectFlags |= espMqttClientInternals::ConnectFlag.CLEAN_SESSION; + if (username != nullptr) connectFlags |= espMqttClientInternals::ConnectFlag.USERNAME; + if (password != nullptr) connectFlags |= espMqttClientInternals::ConnectFlag.PASSWORD; + if (willTopic != nullptr) { + connectFlags |= espMqttClientInternals::ConnectFlag.WILL; + if (willRetain) connectFlags |= espMqttClientInternals::ConnectFlag.WILL_RETAIN; + switch (willQos) { + case 0: + connectFlags |= espMqttClientInternals::ConnectFlag.WILL_QOS0; + break; + case 1: + connectFlags |= espMqttClientInternals::ConnectFlag.WILL_QOS1; + break; + case 2: + connectFlags |= espMqttClientInternals::ConnectFlag.WILL_QOS2; + break; + } + } + _data[pos++] = connectFlags; + _data[pos++] = keepAlive >> 8; + _data[pos++] = keepAlive & 0xFF; + + // PAYLOAD + // client ID + pos += encodeString(clientId, &_data[pos]); + // will + if (willTopic != nullptr && willPayload != nullptr) { + pos += encodeString(willTopic, &_data[pos]); + _data[pos++] = willPayloadLength >> 8; + _data[pos++] = willPayloadLength & 0xFF; + memcpy(&_data[pos], willPayload, willPayloadLength); + pos += willPayloadLength; + } + // credentials + if (username != nullptr) pos += encodeString(username, &_data[pos]); + if (password != nullptr) encodeString(password, &_data[pos]); + + error = espMqttClientTypes::Error::SUCCESS; +} + +Packet::Packet(espMqttClientTypes::Error& error, + uint16_t packetId, + const char* topic, + const uint8_t* payload, + size_t payloadLength, + uint8_t qos, + bool retain) +: _packetId(packetId) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + size_t remainingLength = + 2 + strlen(topic) + // topic length + topic + 2 + // packet ID + payloadLength; + + if (qos == 0) { + remainingLength -= 2; + _packetId = 0; + } + + if (!_allocate(remainingLength)) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + + size_t pos = _fillPublishHeader(packetId, topic, remainingLength, qos, retain); + + // PAYLOAD + memcpy(&_data[pos], payload, payloadLength); + + error = espMqttClientTypes::Error::SUCCESS; +} + +Packet::Packet(espMqttClientTypes::Error& error, + uint16_t packetId, + const char* topic, + espMqttClientTypes::PayloadCallback payloadCallback, + size_t payloadLength, + uint8_t qos, + bool retain) +: _packetId(packetId) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(payloadCallback) { + size_t remainingLength = + 2 + strlen(topic) + // topic length + topic + 2 + // packet ID + payloadLength; + + if (qos == 0) { + remainingLength -= 2; + _packetId = 0; + } + + if (!_allocate(remainingLength - payloadLength + std::min(payloadLength, static_cast(EMC_RX_BUFFER_SIZE)))) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + + size_t pos = _fillPublishHeader(packetId, topic, remainingLength, qos, retain); + + // payload will be added by 'Packet::available' + _size = pos + payloadLength; + _payloadIndex = pos; + _payloadStartIndex = _payloadIndex; + _payloadEndIndex = _payloadIndex; + + error = espMqttClientTypes::Error::SUCCESS; +} + +Packet::Packet(espMqttClientTypes::Error& error, uint16_t packetId, const char* topic, uint8_t qos) +: _packetId(packetId) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + SubscribeItem list[1] = {topic, qos}; + _createSubscribe(error, list, 1); +} + +Packet::Packet(espMqttClientTypes::Error& error, MQTTPacketType type, uint16_t packetId) +: _packetId(packetId) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + if (!_allocate(2)) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + + size_t pos = 0; + _data[pos] = type; + if (type == PacketType.PUBREL) { + _data[pos++] |= HeaderFlag.PUBREL_RESERVED; + } else { + pos++; + } + pos += encodeRemainingLength(2, &_data[pos]); + _data[pos++] = packetId >> 8; + _data[pos] = packetId & 0xFF; + + error = espMqttClientTypes::Error::SUCCESS; +} + +Packet::Packet(espMqttClientTypes::Error& error, uint16_t packetId, const char* topic) +: _packetId(packetId) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + const char* list[1] = {topic}; + _createUnsubscribe(error, list, 1); +} + +Packet::Packet(espMqttClientTypes::Error& error, MQTTPacketType type) +: _packetId(0) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + if (!_allocate(0)) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + _data[0] |= type; + + error = espMqttClientTypes::Error::SUCCESS; +} + + +bool Packet::_allocate(size_t remainingLength) { + if (EMC_GET_FREE_MEMORY() < EMC_MIN_FREE_MEMORY) { + emc_log_w("Packet buffer not allocated: low memory"); + return false; + } + _size = 1 + remainingLengthLength(remainingLength) + remainingLength; + _data = reinterpret_cast(malloc(_size)); + if (!_data) { + _size = 0; + emc_log_w("Alloc failed (l:%zu)", _size); + return false; + } + emc_log_i("Alloc (l:%zu)", _size); + memset(_data, 0, _size); + return true; +} + +size_t Packet::_fillPublishHeader(uint16_t packetId, + const char* topic, + size_t remainingLength, + uint8_t qos, + bool retain) { + size_t index = 0; + + // FIXED HEADER + _data[index] = PacketType.PUBLISH; + if (retain) _data[index] |= HeaderFlag.PUBLISH_RETAIN; + if (qos == 0) { + _data[index++] |= HeaderFlag.PUBLISH_QOS0; + } else if (qos == 1) { + _data[index++] |= HeaderFlag.PUBLISH_QOS1; + } else if (qos == 2) { + _data[index++] |= HeaderFlag.PUBLISH_QOS2; + } + index += encodeRemainingLength(remainingLength, &_data[index]); + + // VARIABLE HEADER + index += encodeString(topic, &_data[index]); + if (qos > 0) { + _data[index++] = packetId >> 8; + _data[index++] = packetId & 0xFF; + } + + return index; +} + +void Packet::_createSubscribe(espMqttClientTypes::Error& error, + SubscribeItem* list, + size_t numberTopics) { + // Calculate size + size_t payload = 0; + for (size_t i = 0; i < numberTopics; ++i) { + payload += 2 + strlen(list[i].topic) + 1; // length bytes, string, qos + } + size_t remainingLength = 2 + payload; // packetId + payload + + // allocate memory + if (!_allocate(remainingLength)) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + + // serialize + size_t pos = 0; + _data[pos++] = PacketType.SUBSCRIBE | HeaderFlag.SUBSCRIBE_RESERVED; + pos += encodeRemainingLength(remainingLength, &_data[pos]); + _data[pos++] = _packetId >> 8; + _data[pos++] = _packetId & 0xFF; + for (size_t i = 0; i < numberTopics; ++i) { + pos += encodeString(list[i].topic, &_data[pos]); + _data[pos++] = list[i].qos; + } + + error = espMqttClientTypes::Error::SUCCESS; +} + +void Packet::_createUnsubscribe(espMqttClientTypes::Error& error, + const char** list, + size_t numberTopics) { + // Calculate size + size_t payload = 0; + for (size_t i = 0; i < numberTopics; ++i) { + payload += 2 + strlen(list[i]); // length bytes, string + } + size_t remainingLength = 2 + payload; // packetId + payload + + // allocate memory + if (!_allocate(remainingLength)) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + + // serialize + size_t pos = 0; + _data[pos++] = PacketType.UNSUBSCRIBE | HeaderFlag.UNSUBSCRIBE_RESERVED; + pos += encodeRemainingLength(remainingLength, &_data[pos]); + _data[pos++] = _packetId >> 8; + _data[pos++] = _packetId & 0xFF; + for (size_t i = 0; i < numberTopics; ++i) { + pos += encodeString(list[i], &_data[pos]); + } + + error = espMqttClientTypes::Error::SUCCESS; +} + +size_t Packet::_chunkedAvailable(size_t index) { + // index vs size check done in 'available(index)' + + // index points to header or first payload byte + if (index < _payloadIndex) { + if (_size > _payloadIndex && _payloadEndIndex != 0) { + size_t copied = _getPayload(&_data[_payloadIndex], std::min(static_cast(EMC_TX_BUFFER_SIZE), _size - _payloadStartIndex), index); + _payloadStartIndex = _payloadIndex; + _payloadEndIndex = _payloadStartIndex + copied - 1; + } + + // index points to payload unavailable + } else if (index > _payloadEndIndex || _payloadStartIndex > index) { + _payloadStartIndex = index; + size_t copied = _getPayload(&_data[_payloadIndex], std::min(static_cast(EMC_TX_BUFFER_SIZE), _size - _payloadStartIndex), index); + _payloadEndIndex = _payloadStartIndex + copied - 1; + } + + // now index points to header or payload available + return _payloadEndIndex - index + 1; +} + +const uint8_t* Packet::_chunkedData(size_t index) const { + // CAUTION!! available(index) has to be called first to check available data and possibly fill payloadbuffer + if (index < _payloadIndex) { + return &_data[index]; + } + return &_data[index - _payloadStartIndex + _payloadIndex]; +} + +} // end namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/Packet.h b/lib/espMqttClient/src/Packets/Packet.h new file mode 100644 index 000000000..1af2f06af --- /dev/null +++ b/lib/espMqttClient/src/Packets/Packet.h @@ -0,0 +1,155 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include +#include + +#include "Constants.h" +#include "../Config.h" +#include "../TypeDefs.h" +#include "../Helpers.h" +#include "../Logging.h" +#include "RemainingLength.h" +#include "String.h" + +namespace espMqttClientInternals { + +class Packet { + public: + ~Packet(); + size_t available(size_t index); + const uint8_t* data(size_t index) const; + + size_t size() const; + void setDup(); + uint16_t packetId() const; + MQTTPacketType packetType() const; + bool removable() const; + + protected: + uint16_t _packetId; // save as separate variable: will be accessed frequently + uint8_t* _data; + size_t _size; + + // variables for chunked payload handling + size_t _payloadIndex; + size_t _payloadStartIndex; + size_t _payloadEndIndex; + espMqttClientTypes::PayloadCallback _getPayload; + + struct SubscribeItem { + const char* topic; + uint8_t qos; + }; + + public: + // CONNECT + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + bool cleanSession, + const char* username, + const char* password, + const char* willTopic, + bool willRetain, + uint8_t willQos, + const uint8_t* willPayload, + uint16_t willPayloadLength, + uint16_t keepAlive, + const char* clientId); + // PUBLISH + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + uint16_t packetId, + const char* topic, + const uint8_t* payload, + size_t payloadLength, + uint8_t qos, + bool retain); + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + uint16_t packetId, + const char* topic, + espMqttClientTypes::PayloadCallback payloadCallback, + size_t payloadLength, + uint8_t qos, + bool retain); + // SUBSCRIBE + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + uint16_t packetId, + const char* topic, + uint8_t qos); + template + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + uint16_t packetId, + const char* topic1, + uint8_t qos1, + const char* topic2, + uint8_t qos2, + Args&& ... args) + : _packetId(packetId) + , _data(nullptr) + , _size(0) + , _payloadIndex(0) + , _payloadStartIndex(0) + , _payloadEndIndex(0) + , _getPayload(nullptr) { + static_assert(sizeof...(Args) % 2 == 0, "Subscribe should be in topic/qos pairs"); + size_t numberTopics = 2 + (sizeof...(Args) / 2); + SubscribeItem list[numberTopics] = {topic1, qos1, topic2, qos2, args...}; + _createSubscribe(error, list, numberTopics); + } + // UNSUBSCRIBE + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + uint16_t packetId, + const char* topic); + template + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + uint16_t packetId, + const char* topic1, + const char* topic2, + Args&& ... args) + : _packetId(packetId) + , _data(nullptr) + , _size(0) + , _payloadIndex(0) + , _payloadStartIndex(0) + , _payloadEndIndex(0) + , _getPayload(nullptr) { + size_t numberTopics = 2 + sizeof...(Args); + const char* list[numberTopics] = {topic1, topic2, args...}; + _createUnsubscribe(error, list, numberTopics); + } + // PUBACK, PUBREC, PUBREL, PUBCOMP + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + MQTTPacketType type, + uint16_t packetId); + // PING, DISCONN + explicit Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + MQTTPacketType type); + + private: + // pass remainingLength = total size - header - remainingLengthLength! + bool _allocate(size_t remainingLength); + + // fills header and returns index of next available byte in buffer + size_t _fillPublishHeader(uint16_t packetId, + const char* topic, + size_t remainingLength, + uint8_t qos, + bool retain); + void _createSubscribe(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + SubscribeItem* list, + size_t numberTopics); + void _createUnsubscribe(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + const char** list, + size_t numberTopics); + + size_t _chunkedAvailable(size_t index); + const uint8_t* _chunkedData(size_t index) const; +}; + +} // end namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/Parser.cpp b/lib/espMqttClient/src/Packets/Parser.cpp new file mode 100644 index 000000000..07998a3da --- /dev/null +++ b/lib/espMqttClient/src/Packets/Parser.cpp @@ -0,0 +1,316 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "Parser.h" + +namespace espMqttClientInternals { + +uint8_t IncomingPacket::qos() const { + if ((fixedHeader.packetType & 0xF0) != PacketType.PUBLISH) return 0; + return (fixedHeader.packetType & 0x06) >> 1; // mask 0x00000110 +} + +bool IncomingPacket::retain() const { + if ((fixedHeader.packetType & 0xF0) != PacketType.PUBLISH) return 0; + return fixedHeader.packetType & 0x01; // mask 0x00000001 +} + +bool IncomingPacket::dup() const { + if ((fixedHeader.packetType & 0xF0) != PacketType.PUBLISH) return 0; + return fixedHeader.packetType & 0x08; // mask 0x00001000 +} + +void IncomingPacket::reset() { + fixedHeader.packetType = 0; + variableHeader.topicLength = 0; + variableHeader.fixed.packetId = 0; + payload.index = 0; + payload.length = 0; +} + +Parser::Parser() +: _data(nullptr) +, _len(0) +, _bytesRead(0) +, _bytePos(0) +, _parse(_fixedHeader) +, _packet() +, _payloadBuffer{0} { + // empty +} + +ParserResult Parser::parse(const uint8_t* data, size_t len, size_t* bytesRead) { + _data = data; + _len = len; + _bytesRead = 0; + ParserResult result = ParserResult::awaitData; + while (result == ParserResult::awaitData && _bytesRead < _len) { + result = _parse(this); + ++_bytesRead; + } + (*bytesRead) += _bytesRead; + return result; +} + +const IncomingPacket& Parser::getPacket() const { + return _packet; +} + +void Parser::reset() { + _parse = _fixedHeader; + _bytesRead = 0; + _bytePos = 0; + _packet.reset(); +} + +ParserResult Parser::_fixedHeader(Parser* p) { + p->_packet.reset(); + p->_packet.fixedHeader.packetType = p->_data[p->_bytesRead]; + + // keep PUBLISH out of the switch and handle in separate if/else + if ((p->_packet.fixedHeader.packetType & 0xF0) == PacketType.PUBLISH) { + uint8_t headerFlags = p->_packet.fixedHeader.packetType & 0x0F; + /* flags can be: 0b0000 --> no dup, qos 0, no retain + 0x0001 --> no dup, qos 0, retain + 0x0010 --> no dup, qos 1, no retain + 0x0011 --> no dup, qos 1, retain + 0x0100 --> no dup, qos 2, no retain + 0x0101 --> no dup, qos 2, retain + 0x1010 --> dup, qos 1, no retain + 0x1011 --> dup, qos 1, retain + 0x1100 --> dup, qos 2, no retain + 0x1101 --> dup, qos 2, retain + */ + if (headerFlags <= 0x05 || headerFlags >= 0x0A) { + p->_parse = _remainingLengthVariable; + p->_bytePos = 0; + } else { + emc_log_w("Invalid packet header: 0x%02x", p->_packet.fixedHeader.packetType); + return ParserResult::protocolError; + } + } else { + switch (p->_packet.fixedHeader.packetType) { + case PacketType.CONNACK | HeaderFlag.CONNACK_RESERVED: + case PacketType.PUBACK | HeaderFlag.PUBACK_RESERVED: + case PacketType.PUBREC | HeaderFlag.PUBREC_RESERVED: + case PacketType.PUBREL | HeaderFlag.PUBREL_RESERVED: + case PacketType.PUBCOMP | HeaderFlag.PUBCOMP_RESERVED: + case PacketType.UNSUBACK | HeaderFlag.UNSUBACK_RESERVED: + p->_parse = _remainingLengthFixed; + break; + case PacketType.SUBACK | HeaderFlag.SUBACK_RESERVED: + p->_parse = _remainingLengthVariable; + p->_bytePos = 0; + break; + case PacketType.PINGRESP | HeaderFlag.PINGRESP_RESERVED: + p->_parse = _remainingLengthNone; + break; + default: + emc_log_w("Invalid packet header: 0x%02x", p->_packet.fixedHeader.packetType); + return ParserResult::protocolError; + } + } + emc_log_i("Packet type: 0x%02x", p->_packet.fixedHeader.packetType); + return ParserResult::awaitData; +} + +ParserResult Parser::_remainingLengthFixed(Parser* p) { + p->_packet.fixedHeader.remainingLength.remainingLength = p->_data[p->_bytesRead]; + + if (p->_packet.fixedHeader.remainingLength.remainingLength == 2) { // variable header is 2 bytes long + if ((p->_packet.fixedHeader.packetType & 0xF0) != PacketType.CONNACK) { + p->_parse = _varHeaderPacketId1; + } else { + p->_parse = _varHeaderConnack1; + } + emc_log_i("Remaining length: %zu", p->_packet.fixedHeader.remainingLength.remainingLength); + return ParserResult::awaitData; + } + p->_parse = _fixedHeader; + emc_log_w("Invalid remaining length (fixed): %zu", p->_packet.fixedHeader.remainingLength.remainingLength); + return ParserResult::protocolError; +} + +ParserResult Parser::_remainingLengthVariable(Parser* p) { + p->_packet.fixedHeader.remainingLength.remainingLengthRaw[p->_bytePos] = p->_data[p->_bytesRead]; + if (p->_packet.fixedHeader.remainingLength.remainingLengthRaw[p->_bytePos] & 0x80) { + p->_bytePos++; + if (p->_bytePos == 4) { + emc_log_w("Invalid remaining length (variable)"); + return ParserResult::protocolError; + } else { + return ParserResult::awaitData; + } + } + + // no need to check for negative decoded length, check is already done + p->_packet.fixedHeader.remainingLength.remainingLength = decodeRemainingLength(p->_packet.fixedHeader.remainingLength.remainingLengthRaw); + + if ((p->_packet.fixedHeader.packetType & 0xF0) == PacketType.PUBLISH) { + p->_parse = _varHeaderTopicLength1; + emc_log_i("Remaining length: %zu", p->_packet.fixedHeader.remainingLength.remainingLength); + return ParserResult::awaitData; + } else { + int32_t payloadSize = p->_packet.fixedHeader.remainingLength.remainingLength - 2; // total - packet ID + if (0 < payloadSize && payloadSize < EMC_PAYLOAD_BUFFER_SIZE) { + p->_bytePos = 0; + p->_packet.payload.data = p->_payloadBuffer; + p->_packet.payload.index = 0; + p->_packet.payload.length = payloadSize; + p->_packet.payload.total = payloadSize; + p->_parse = _varHeaderPacketId1; + emc_log_i("Remaining length: %zu", p->_packet.fixedHeader.remainingLength.remainingLength); + return ParserResult::awaitData; + } else { + emc_log_w("Invalid payload length"); + } + } + p->_parse = _fixedHeader; + return ParserResult::protocolError; +} + +ParserResult Parser::_remainingLengthNone(Parser* p) { + p->_packet.fixedHeader.remainingLength.remainingLength = p->_data[p->_bytesRead]; + p->_parse = _fixedHeader; + if (p->_packet.fixedHeader.remainingLength.remainingLength == 0) { + emc_log_i("Remaining length: %zu", p->_packet.fixedHeader.remainingLength.remainingLength); + return ParserResult::packet; + } + emc_log_w("Invalid remaining length (none)"); + return ParserResult::protocolError; +} + +ParserResult Parser::_varHeaderConnack1(Parser* p) { + uint8_t data = p->_data[p->_bytesRead]; + if (data < 2) { // session present flag: equal to 0 or 1 + p->_packet.variableHeader.fixed.connackVarHeader.sessionPresent = data; + p->_parse = _varHeaderConnack2; + return ParserResult::awaitData; + } + p->_parse = _fixedHeader; + emc_log_w("Invalid session flags"); + return ParserResult::protocolError; +} + +ParserResult Parser::_varHeaderConnack2(Parser* p) { + uint8_t data = p->_data[p->_bytesRead]; + p->_parse = _fixedHeader; + if (data <= 5) { // connect return code max is 5 + p->_packet.variableHeader.fixed.connackVarHeader.returnCode = data; + emc_log_i("Packet complete"); + return ParserResult::packet; + } + emc_log_w("Invalid connack return code"); + return ParserResult::protocolError; +} + +ParserResult Parser::_varHeaderPacketId1(Parser* p) { + p->_packet.variableHeader.fixed.packetId |= p->_data[p->_bytesRead] << 8; + p->_parse = _varHeaderPacketId2; + return ParserResult::awaitData; +} + +ParserResult Parser::_varHeaderPacketId2(Parser* p) { + p->_packet.variableHeader.fixed.packetId |= p->_data[p->_bytesRead]; + p->_parse = _fixedHeader; + if (p->_packet.variableHeader.fixed.packetId != 0) { + emc_log_i("Packet variable header complete"); + if ((p->_packet.fixedHeader.packetType & 0xF0) == PacketType.SUBACK) { + p->_parse = _payloadSuback; + return ParserResult::awaitData; + } else if ((p->_packet.fixedHeader.packetType & 0xF0) == PacketType.PUBLISH) { + p->_packet.payload.total -= 2; // substract packet id length from payload + if (p->_packet.payload.total == 0) { + p->_parse = _fixedHeader; + return ParserResult::packet; + } else { + p->_parse = _payloadPublish; + } + return ParserResult::awaitData; + } else { + return ParserResult::packet; + } + } else { + emc_log_w("Invalid packet id"); + return ParserResult::protocolError; + } +} + +ParserResult Parser::_varHeaderTopicLength1(Parser* p) { + p->_packet.variableHeader.topicLength = p->_data[p->_bytesRead] << 8; + p->_parse = _varHeaderTopicLength2; + return ParserResult::awaitData; +} + +ParserResult Parser::_varHeaderTopicLength2(Parser* p) { + p->_packet.variableHeader.topicLength |= p->_data[p->_bytesRead]; + size_t maxTopicLength = + p->_packet.fixedHeader.remainingLength.remainingLength + - 2 // topic length bytes + - ((p->_packet.fixedHeader.packetType & (HeaderFlag.PUBLISH_QOS1 | HeaderFlag.PUBLISH_QOS2)) ? 2 : 0); + if (p->_packet.variableHeader.topicLength <= maxTopicLength) { + p->_parse = _varHeaderTopic; + p->_bytePos = 0; + p->_packet.payload.total = p->_packet.fixedHeader.remainingLength.remainingLength - 2 - p->_packet.variableHeader.topicLength; + return ParserResult::awaitData; + } + emc_log_w("Invalid topic length: %u > %zu", p->_packet.variableHeader.topicLength, maxTopicLength); + p->_parse = _fixedHeader; + return ParserResult::protocolError; +} + +ParserResult Parser::_varHeaderTopic(Parser* p) { + // no checking for character [MQTT-3.3.2-1] [MQTT-3.3.2-2] + p->_packet.variableHeader.topic[p->_bytePos] = static_cast(p->_data[p->_bytesRead]); + p->_bytePos++; + if (p->_bytePos == p->_packet.variableHeader.topicLength || p->_bytePos == EMC_MAX_TOPIC_LENGTH) { + p->_packet.variableHeader.topic[p->_bytePos] = 0x00; // add c-string delimiter + emc_log_i("Packet variable header topic complete"); + if (p->_packet.fixedHeader.packetType & (HeaderFlag.PUBLISH_QOS1 | HeaderFlag.PUBLISH_QOS2)) { + p->_parse = _varHeaderPacketId1; + } else if (p->_packet.payload.total == 0) { + p->_parse = _fixedHeader; + return ParserResult::packet; + } else { + p->_parse = _payloadPublish; + } + } + return ParserResult::awaitData; +} + +ParserResult Parser::_payloadSuback(Parser* p) { + uint8_t data = p->_data[p->_bytesRead]; + if (data < 0x03 || data == 0x80) { + p->_payloadBuffer[p->_bytePos] = data; + p->_bytePos++; + } else { + p->_parse = _fixedHeader; + emc_log_w("Invalid suback return code"); + return ParserResult::protocolError; + } + if (p->_bytePos == p->_packet.payload.total) { + p->_parse = _fixedHeader; + emc_log_i("Packet complete"); + return ParserResult::packet; + } + return ParserResult::awaitData; +} + +ParserResult Parser::_payloadPublish(Parser* p) { + p->_packet.payload.index += p->_packet.payload.length; + p->_packet.payload.data = &p->_data[p->_bytesRead]; + emc_log_i("payload: index %zu, total %zu, avail %zu/%zu", p->_packet.payload.index, p->_packet.payload.total, p->_len - p->_bytesRead, p->_len); + p->_packet.payload.length = std::min(p->_len - p->_bytesRead, p->_packet.payload.total - p->_packet.payload.index); + p->_bytesRead += p->_packet.payload.length - 1; // compensate for increment in _parse-loop + if (p->_packet.payload.index + p->_packet.payload.length == p->_packet.payload.total) { + p->_parse = _fixedHeader; + } + return ParserResult::packet; +} + +} // end namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/Parser.h b/lib/espMqttClient/src/Packets/Parser.h new file mode 100644 index 000000000..2f6334e42 --- /dev/null +++ b/lib/espMqttClient/src/Packets/Parser.h @@ -0,0 +1,100 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include +#include +#include + +#include "../Config.h" +#include "Constants.h" +#include "../Logging.h" +#include "RemainingLength.h" + +namespace espMqttClientInternals { + +struct IncomingPacket { + struct __attribute__((__packed__)) { + MQTTPacketType packetType; + union { + size_t remainingLength; + uint8_t remainingLengthRaw[4]; + } remainingLength; + } fixedHeader; + struct __attribute__((__packed__)) { + uint16_t topicLength; + char topic[EMC_MAX_TOPIC_LENGTH + 1]; // + 1 for c-string delimiter + union { + struct { + uint8_t sessionPresent; + uint8_t returnCode; + } connackVarHeader; + uint16_t packetId; + } fixed; + } variableHeader; + struct { + const uint8_t* data; + size_t length; + size_t index; + size_t total; + } payload; + + uint8_t qos() const; + bool retain() const; + bool dup() const; + void reset(); +}; + +enum class ParserResult : uint8_t { + awaitData, + packet, + protocolError +}; + +class Parser; +typedef ParserResult(*ParserFunc)(Parser*); + +class Parser { + public: + Parser(); + ParserResult parse(const uint8_t* data, size_t len, size_t* bytesRead); + const IncomingPacket& getPacket() const; + void reset(); + + private: + // keep data variables in class to avoid copying on every iteration of the parser + const uint8_t* _data; + size_t _len; + size_t _bytesRead; + size_t _bytePos; + ParserFunc _parse; + IncomingPacket _packet; + uint8_t _payloadBuffer[EMC_PAYLOAD_BUFFER_SIZE]; + + static ParserResult _fixedHeader(Parser* p); + static ParserResult _remainingLengthFixed(Parser* p); + static ParserResult _remainingLengthNone(Parser* p); + static ParserResult _remainingLengthVariable(Parser* p); + + + static ParserResult _varHeaderConnack1(Parser* p); + static ParserResult _varHeaderConnack2(Parser* p); + + static ParserResult _varHeaderPacketId1(Parser* p); + static ParserResult _varHeaderPacketId2(Parser* p); + + static ParserResult _varHeaderTopicLength1(Parser* p); + static ParserResult _varHeaderTopicLength2(Parser* p); + static ParserResult _varHeaderTopic(Parser* p); + + static ParserResult _payloadSuback(Parser* p); + static ParserResult _payloadPublish(Parser* p); +}; + +} // end namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/RemainingLength.cpp b/lib/espMqttClient/src/Packets/RemainingLength.cpp new file mode 100644 index 000000000..d8644a33a --- /dev/null +++ b/lib/espMqttClient/src/Packets/RemainingLength.cpp @@ -0,0 +1,57 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "RemainingLength.h" + +namespace espMqttClientInternals { + +int32_t decodeRemainingLength(const uint8_t* stream) { + uint32_t multiplier = 1; + int32_t remainingLength = 0; + uint8_t currentByte = 0; + uint8_t encodedByte; + + do { + encodedByte = stream[currentByte++]; + remainingLength += (encodedByte & 127) * multiplier; + if (multiplier > 128 * 128 * 128) { + emc_log_e("Malformed Remaining Length"); + return -1; + } + multiplier *= 128; + } while ((encodedByte & 128) != 0); + + return remainingLength; +} + +uint8_t remainingLengthLength(uint32_t remainingLength) { + if (remainingLength < 128) return 1; + if (remainingLength < 16384) return 2; + if (remainingLength < 2097152) return 3; + if (remainingLength > 268435455) return 0; + return 4; +} + +uint8_t encodeRemainingLength(uint32_t remainingLength, uint8_t* destination) { + uint8_t currentByte = 0; + uint8_t bytesNeeded = 0; + + do { + uint8_t encodedByte = remainingLength % 128; + remainingLength /= 128; + if (remainingLength > 0) { + encodedByte = encodedByte | 128; + } + destination[currentByte++] = encodedByte; + bytesNeeded++; + } while (remainingLength > 0); + + return bytesNeeded; +} + +} // namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/RemainingLength.h b/lib/espMqttClient/src/Packets/RemainingLength.h new file mode 100644 index 000000000..0b84e23e5 --- /dev/null +++ b/lib/espMqttClient/src/Packets/RemainingLength.h @@ -0,0 +1,32 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include + +#include "../Logging.h" + +namespace espMqttClientInternals { + +// Calculations are based on non normative comment in section 2.2.3 Remaining Length of the MQTT specification + +// returns decoded length based on input stream +// stream is expected to contain full encoded remaining length +// return -1 on error. +int32_t decodeRemainingLength(const uint8_t* stream); + + +// returns the number of bytes needed to encode the remaining length +uint8_t remainingLengthLength(uint32_t remainingLength); + +// encodes the given remaining length to destination and returns number of bytes used +// destination is expected to be large enough to hold the number of bytes needed +uint8_t encodeRemainingLength(uint32_t remainingLength, uint8_t* destination); + +} // namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/String.cpp b/lib/espMqttClient/src/Packets/String.cpp new file mode 100644 index 000000000..c3fe23fdc --- /dev/null +++ b/lib/espMqttClient/src/Packets/String.cpp @@ -0,0 +1,26 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "String.h" + +namespace espMqttClientInternals { + +size_t encodeString(const char* source, uint8_t* dest) { + size_t length = strlen(source); + if (length > 65535) { + emc_log_e("String length error"); + return 0; + } + + dest[0] = static_cast(length) >> 8; + dest[1] = static_cast(length) & 0xFF; + memcpy(&dest[2], source, length); + return 2 + length; +} + +} // namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/String.h b/lib/espMqttClient/src/Packets/String.h new file mode 100644 index 000000000..7f1e1e848 --- /dev/null +++ b/lib/espMqttClient/src/Packets/String.h @@ -0,0 +1,22 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include +#include // memcpy + +#include "../Logging.h" + +namespace espMqttClientInternals { + +// encodes the given source string into destination and returns number of bytes used +// destination is expected to be large enough to hold the number of bytes needed +size_t encodeString(const char* source, uint8_t* dest); + +} // namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Transport/ClientAsync.cpp b/lib/espMqttClient/src/Transport/ClientAsync.cpp new file mode 100644 index 000000000..4f8d69eeb --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientAsync.cpp @@ -0,0 +1,58 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include "ClientAsync.h" + +namespace espMqttClientInternals { + +ClientAsync::ClientAsync() +: client() +, availableData(0) +, bufData(nullptr) { + // empty +} + +bool ClientAsync::connect(IPAddress ip, uint16_t port) { + return client.connect(ip, port); +} + +bool ClientAsync::connect(const char* host, uint16_t port) { + return client.connect(host, port); +} + +size_t ClientAsync::write(const uint8_t* buf, size_t size) { + return client.write(reinterpret_cast(buf), size); +} + +int ClientAsync::read(uint8_t* buf, size_t size) { + size_t willRead = std::min(size, availableData); + memcpy(buf, bufData, std::min(size, availableData)); + if (availableData > size) { + emc_log_w("Buffer is smaller than available data: %zu - %zu", size, availableData); + } + availableData = 0; + return willRead; +} + +void ClientAsync::stop() { + client.close(false); +} + +bool ClientAsync::connected() { + return client.connected(); +} + +bool ClientAsync::disconnected() { + return client.disconnected(); +} + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientAsync.h b/lib/espMqttClient/src/Transport/ClientAsync.h new file mode 100644 index 000000000..c3ddd0388 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientAsync.h @@ -0,0 +1,44 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#pragma once + +#if defined(ARDUINO_ARCH_ESP32) + #include "freertos/FreeRTOS.h" + #include +#elif defined(ARDUINO_ARCH_ESP8266) + #include +#endif + +#include "Transport.h" +#include "../Config.h" +#include "../Logging.h" + +namespace espMqttClientInternals { + +class ClientAsync : public Transport { + public: + ClientAsync(); + bool connect(IPAddress ip, uint16_t port) override; + bool connect(const char* host, uint16_t port) override; + size_t write(const uint8_t* buf, size_t size) override; + int read(uint8_t* buf, size_t size) override; + void stop() override; + bool connected() override; + bool disconnected() override; + + AsyncClient client; + size_t availableData; + uint8_t* bufData; +}; + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientPosix.cpp b/lib/espMqttClient/src/Transport/ClientPosix.cpp new file mode 100644 index 000000000..83b555d1b --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientPosix.cpp @@ -0,0 +1,92 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "ClientPosix.h" + +#if defined(__linux__) + +namespace espMqttClientInternals { + +ClientPosix::ClientPosix() +: _sockfd(-1) +, _host() { + // empty +} + +ClientPosix::~ClientPosix() { + ClientPosix::stop(); +} + +bool ClientPosix::connect(IPAddress ip, uint16_t port) { + if (connected()) stop(); + + _sockfd = ::socket(AF_INET, SOCK_STREAM, 0); + if (_sockfd < 0) { + emc_log_e("Error %d opening socket", errno); + } + + int flag = 1; + if (setsockopt(_sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)) < 0) { + emc_log_e("Error %d disabling nagle", errno); + } + + memset(&_host, 0, sizeof(_host)); + _host.sin_family = AF_INET; + _host.sin_addr.s_addr = htonl(uint32_t(ip)); + _host.sin_port = ::htons(port); + + int ret = ::connect(_sockfd, (struct sockaddr *)&_host, sizeof(_host)); + + if (ret < 0) { + emc_log_e("Error connecting: %d - (%d) %s", ret, errno, strerror(errno)); + return false; + } + + emc_log_i("Connected"); + return true; +} + +bool ClientPosix::connect(const char* host, uint16_t port) { + // tbi + (void) host; + (void) port; + return false; +} + +size_t ClientPosix::write(const uint8_t* buf, size_t size) { + return ::send(_sockfd, buf, size, 0); +} + +int ClientPosix::read(uint8_t* buf, size_t size) { + int ret = ::recv(_sockfd, buf, size, MSG_DONTWAIT); + /* + if (ret < 0) { + emc_log_e("Error reading: %s", strerror(errno)); + } + */ + return ret; +} + +void ClientPosix::stop() { + if (_sockfd >= 0) { + ::close(_sockfd); + _sockfd = -1; + } +} + +bool ClientPosix::connected() { + return _sockfd >= 0; +} + +bool ClientPosix::disconnected() { + return _sockfd < 0; +} + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientPosix.h b/lib/espMqttClient/src/Transport/ClientPosix.h new file mode 100644 index 000000000..a2f9c9fae --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientPosix.h @@ -0,0 +1,51 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(__linux__) + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Transport.h" // includes IPAddress +#include "../Logging.h" + +#ifndef EMC_POSIX_PEEK_SIZE +#define EMC_POSIX_PEEK_SIZE 1500 +#endif + +namespace espMqttClientInternals { + +class ClientPosix : public Transport { + public: + ClientPosix(); + ~ClientPosix(); + bool connect(IPAddress ip, uint16_t port) override; + bool connect(const char* host, uint16_t port) override; + size_t write(const uint8_t* buf, size_t size) override; + int read(uint8_t* buf, size_t size) override; + void stop() override; + bool connected() override; + bool disconnected() override; + + protected: + int _sockfd; + struct sockaddr_in _host; +}; + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientSecureSync.cpp b/lib/espMqttClient/src/Transport/ClientSecureSync.cpp new file mode 100644 index 000000000..36288c6a5 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSecureSync.cpp @@ -0,0 +1,71 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include "ClientSecureSync.h" +#include // socket options + +namespace espMqttClientInternals { + +ClientSecureSync::ClientSecureSync() +: client() { + // empty +} + +bool ClientSecureSync::connect(IPAddress ip, uint16_t port) { + bool ret = client.connect(ip, port); // implicit conversion of return code int --> bool + if (ret) { + #if defined(ARDUINO_ARCH_ESP8266) + client.setNoDelay(true); + #elif defined(ARDUINO_ARCH_ESP32) + // Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure + int val = true; + client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int)); + #endif + } + return ret; +} + +bool ClientSecureSync::connect(const char* host, uint16_t port) { + bool ret = client.connect(host, port); // implicit conversion of return code int --> bool + if (ret) { + #if defined(ARDUINO_ARCH_ESP8266) + client.setNoDelay(true); + #elif defined(ARDUINO_ARCH_ESP32) + // Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure + int val = true; + client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int)); + #endif + } + return ret; +} + +size_t ClientSecureSync::write(const uint8_t* buf, size_t size) { + return client.write(buf, size); +} + +int ClientSecureSync::read(uint8_t* buf, size_t size) { + return client.read(buf, size); +} + +void ClientSecureSync::stop() { + client.stop(); +} + +bool ClientSecureSync::connected() { + return client.connected(); +} + +bool ClientSecureSync::disconnected() { + return !client.connected(); +} + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientSecureSync.h b/lib/espMqttClient/src/Transport/ClientSecureSync.h new file mode 100644 index 000000000..b81681e36 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSecureSync.h @@ -0,0 +1,34 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include // includes IPAddress + +#include "Transport.h" + +namespace espMqttClientInternals { + +class ClientSecureSync : public Transport { + public: + ClientSecureSync(); + bool connect(IPAddress ip, uint16_t port) override; + bool connect(const char* host, uint16_t port) override; + size_t write(const uint8_t* buf, size_t size) override; + int read(uint8_t* buf, size_t size) override; + void stop() override; + bool connected() override; + bool disconnected() override; + WiFiClientSecure client; +}; + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientSync.cpp b/lib/espMqttClient/src/Transport/ClientSync.cpp new file mode 100644 index 000000000..b2c4045f1 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSync.cpp @@ -0,0 +1,71 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include "ClientSync.h" +#include // socket options + +namespace espMqttClientInternals { + +ClientSync::ClientSync() +: client() { + // empty +} + +bool ClientSync::connect(IPAddress ip, uint16_t port) { + bool ret = client.connect(ip, port); // implicit conversion of return code int --> bool + if (ret) { + #if defined(ARDUINO_ARCH_ESP8266) + client.setNoDelay(true); + #elif defined(ARDUINO_ARCH_ESP32) + // Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here) + int val = true; + client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int)); + #endif + } + return ret; +} + +bool ClientSync::connect(const char* host, uint16_t port) { + bool ret = client.connect(host, port); // implicit conversion of return code int --> bool + if (ret) { + #if defined(ARDUINO_ARCH_ESP8266) + client.setNoDelay(true); + #elif defined(ARDUINO_ARCH_ESP32) + // Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here) + int val = true; + client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int)); + #endif + } + return ret; +} + +size_t ClientSync::write(const uint8_t* buf, size_t size) { + return client.write(buf, size); +} + +int ClientSync::read(uint8_t* buf, size_t size) { + return client.read(buf, size); +} + +void ClientSync::stop() { + client.stop(); +} + +bool ClientSync::connected() { + return client.connected(); +} + +bool ClientSync::disconnected() { + return !client.connected(); +} + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientSync.h b/lib/espMqttClient/src/Transport/ClientSync.h new file mode 100644 index 000000000..ccfbdbae5 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSync.h @@ -0,0 +1,34 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include // includes IPAddress + +#include "Transport.h" + +namespace espMqttClientInternals { + +class ClientSync : public Transport { + public: + ClientSync(); + bool connect(IPAddress ip, uint16_t port) override; + bool connect(const char* host, uint16_t port) override; + size_t write(const uint8_t* buf, size_t size) override; + int read(uint8_t* buf, size_t size) override; + void stop() override; + bool connected() override; + bool disconnected() override; + WiFiClient client; +}; + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/IPAddress.cpp b/lib/espMqttClient/src/Transport/IPAddress.cpp new file mode 100644 index 000000000..b198429dc --- /dev/null +++ b/lib/espMqttClient/src/Transport/IPAddress.cpp @@ -0,0 +1,32 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#if defined(__linux__) + +#include "IPAddress.h" + +IPAddress::IPAddress() +: _address(0) { + // empty +} + +IPAddress::IPAddress(uint8_t p0, uint8_t p1, uint8_t p2, uint8_t p3) +: _address(0) { + _address = (uint32_t)p0 << 24 | (uint32_t)p1 << 16 | (uint32_t)p2 << 8 | p3; +} + +IPAddress::IPAddress(uint32_t address) +: _address(address) { + // empty +} + +IPAddress::operator uint32_t() { + return _address; +} + +#endif diff --git a/lib/espMqttClient/src/Transport/IPAddress.h b/lib/espMqttClient/src/Transport/IPAddress.h new file mode 100644 index 000000000..279a1957d --- /dev/null +++ b/lib/espMqttClient/src/Transport/IPAddress.h @@ -0,0 +1,28 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO) + #include +#else + +#include + +class IPAddress { + public: + IPAddress(); + IPAddress(uint8_t p0, uint8_t p1, uint8_t p2, uint8_t p3); + explicit IPAddress(uint32_t address); + operator uint32_t(); + + protected: + uint32_t _address; +}; + +#endif diff --git a/lib/espMqttClient/src/Transport/Transport.h b/lib/espMqttClient/src/Transport/Transport.h new file mode 100644 index 000000000..6720c024f --- /dev/null +++ b/lib/espMqttClient/src/Transport/Transport.h @@ -0,0 +1,28 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include // size_t + +#include "IPAddress.h" + +namespace espMqttClientInternals { + +class Transport { + public: + virtual bool connect(IPAddress ip, uint16_t port) = 0; + virtual bool connect(const char* host, uint16_t port) = 0; + virtual size_t write(const uint8_t* buf, size_t size) = 0; + virtual int read(uint8_t* buf, size_t size) = 0; + virtual void stop() = 0; + virtual bool connected() = 0; + virtual bool disconnected() = 0; +}; + +} // namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/TypeDefs.cpp b/lib/espMqttClient/src/TypeDefs.cpp new file mode 100644 index 000000000..4f92c1f64 --- /dev/null +++ b/lib/espMqttClient/src/TypeDefs.cpp @@ -0,0 +1,51 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +Parts are based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "TypeDefs.h" + +namespace espMqttClientTypes { + +const char* disconnectReasonToString(DisconnectReason reason) { + switch (reason) { + case DisconnectReason::USER_OK: return "No error"; + case DisconnectReason::MQTT_UNACCEPTABLE_PROTOCOL_VERSION: return "Unacceptable protocol version"; + case DisconnectReason::MQTT_IDENTIFIER_REJECTED: return "Identified rejected"; + case DisconnectReason::MQTT_SERVER_UNAVAILABLE: return "Server unavailable"; + case DisconnectReason::MQTT_MALFORMED_CREDENTIALS: return "Malformed credentials"; + case DisconnectReason::MQTT_NOT_AUTHORIZED: return "Not authorized"; + case DisconnectReason::TLS_BAD_FINGERPRINT: return "Bad fingerprint"; + case DisconnectReason::TCP_DISCONNECTED: return "TCP disconnected"; + default: return ""; + } +} + +const char* subscribeReturncodeToString(SubscribeReturncode returnCode) { + switch (returnCode) { + case SubscribeReturncode::QOS0: return "QoS 0"; + case SubscribeReturncode::QOS1: return "QoS 1"; + case SubscribeReturncode::QOS2: return "QoS 2"; + case SubscribeReturncode::FAIL: return "Failed"; + default: return ""; + } +} + +const char* errorToString(Error error) { + switch (error) { + case Error::SUCCESS: return "Success"; + case Error::OUT_OF_MEMORY: return "Out of memory"; + case Error::MAX_RETRIES: return "Maximum retries exceeded"; + case Error::MALFORMED_PARAMETER: return "Malformed parameters"; + case Error::MISC_ERROR: return "Misc error"; + default: return ""; + } +} + +} // end namespace espMqttClientTypes diff --git a/lib/espMqttClient/src/TypeDefs.h b/lib/espMqttClient/src/TypeDefs.h new file mode 100644 index 000000000..0f1536069 --- /dev/null +++ b/lib/espMqttClient/src/TypeDefs.h @@ -0,0 +1,73 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +Parts are based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include +#include +#include + +namespace espMqttClientTypes { + +enum class DisconnectReason : uint8_t { + USER_OK = 0, + MQTT_UNACCEPTABLE_PROTOCOL_VERSION = 1, + MQTT_IDENTIFIER_REJECTED = 2, + MQTT_SERVER_UNAVAILABLE = 3, + MQTT_MALFORMED_CREDENTIALS = 4, + MQTT_NOT_AUTHORIZED = 5, + TLS_BAD_FINGERPRINT = 6, + TCP_DISCONNECTED = 7 +}; + +const char* disconnectReasonToString(DisconnectReason reason); + +enum class SubscribeReturncode : uint8_t { + QOS0 = 0x00, + QOS1 = 0x01, + QOS2 = 0x02, + FAIL = 0X80 +}; + +const char* subscribeReturncodeToString(SubscribeReturncode returnCode); + +enum class Error : uint8_t { + SUCCESS = 0, + OUT_OF_MEMORY = 1, + MAX_RETRIES = 2, + MALFORMED_PARAMETER = 3, + MISC_ERROR = 4 +}; + +const char* errorToString(Error error); + +struct MessageProperties { + uint8_t qos; + bool dup; + bool retain; + uint16_t packetId; +}; + +typedef std::function OnConnectCallback; +typedef std::function OnDisconnectCallback; +typedef std::function OnSubscribeCallback; +typedef std::function OnUnsubscribeCallback; +typedef std::function OnMessageCallback; +typedef std::function OnPublishCallback; +typedef std::function PayloadCallback; +typedef std::function OnErrorCallback; + +enum class UseInternalTask { + NO = 0, + YES = 1, +}; + +} // end namespace espMqttClientTypes diff --git a/lib/espMqttClient/src/espMqttClient.cpp b/lib/espMqttClient/src/espMqttClient.cpp new file mode 100644 index 000000000..833ece10b --- /dev/null +++ b/lib/espMqttClient/src/espMqttClient.cpp @@ -0,0 +1,113 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "espMqttClient.h" + +#if defined(ARDUINO_ARCH_ESP8266) +espMqttClient::espMqttClient() +: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO) +, _client() { + _transport = &_client; +} + +espMqttClientSecure::espMqttClientSecure() +: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO) +, _client() { + _transport = &_client; +} + +espMqttClientSecure& espMqttClientSecure::setInsecure() { + _client.client.setInsecure(); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setFingerprint(const uint8_t fingerprint[20]) { + _client.client.setFingerprint(fingerprint); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setTrustAnchors(const X509List *ta) { + _client.client.setTrustAnchors(ta); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setClientRSACert(const X509List *cert, const PrivateKey *sk) { + _client.client.setClientRSACert(cert, sk); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type) { + _client.client.setClientECCert(cert, sk, allowed_usages, cert_issuer_key_type); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setCertStore(CertStoreBase *certStore) { + _client.client.setCertStore(certStore); + return *this; +} +#endif + +#if defined(ARDUINO_ARCH_ESP32) +espMqttClient::espMqttClient(espMqttClientTypes::UseInternalTask useInternalTask) +: MqttClientSetup(useInternalTask) +, _client() { + _transport = &_client; +} + +espMqttClient::espMqttClient(uint8_t priority, uint8_t core) +: MqttClientSetup(espMqttClientTypes::UseInternalTask::YES, priority, core) +, _client() { + _transport = &_client; +} + +espMqttClientSecure::espMqttClientSecure(espMqttClientTypes::UseInternalTask useInternalTask) +: MqttClientSetup(useInternalTask) +, _client() { + _transport = &_client; +} + +espMqttClientSecure::espMqttClientSecure(uint8_t priority, uint8_t core) +: MqttClientSetup(espMqttClientTypes::UseInternalTask::YES, priority, core) +, _client() { + _transport = &_client; +} + +espMqttClientSecure& espMqttClientSecure::setInsecure() { + _client.client.setInsecure(); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setCACert(const char* rootCA) { + _client.client.setCACert(rootCA); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setCertificate(const char* clientCa) { + _client.client.setCertificate(clientCa); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setPrivateKey(const char* privateKey) { + _client.client.setPrivateKey(privateKey); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setPreSharedKey(const char* pskIdent, const char* psKey) { + _client.client.setPreSharedKey(pskIdent, psKey); + return *this; +} + +#endif + +#if defined(__linux__) +espMqttClient::espMqttClient() +: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO) +, _client() { + _transport = &_client; +} +#endif diff --git a/lib/espMqttClient/src/espMqttClient.h b/lib/espMqttClient/src/espMqttClient.h new file mode 100644 index 000000000..4e448011d --- /dev/null +++ b/lib/espMqttClient/src/espMqttClient.h @@ -0,0 +1,80 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +API is based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) +#include "Transport/ClientSync.h" +#include "Transport/ClientSecureSync.h" +#elif defined(__linux__) +#include "Transport/ClientPosix.h" +#endif + +#include "MqttClientSetup.h" + +#if defined(ARDUINO_ARCH_ESP8266) +class espMqttClient : public MqttClientSetup { + public: + espMqttClient(); + + protected: + espMqttClientInternals::ClientSync _client; +}; + +class espMqttClientSecure : public MqttClientSetup { + public: + espMqttClientSecure(); + espMqttClientSecure& setInsecure(); + espMqttClientSecure& setFingerprint(const uint8_t fingerprint[20]); + espMqttClientSecure& setTrustAnchors(const X509List *ta); + espMqttClientSecure& setClientRSACert(const X509List *cert, const PrivateKey *sk); + espMqttClientSecure& setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type); + espMqttClientSecure& setCertStore(CertStoreBase *certStore); + + protected: + espMqttClientInternals::ClientSecureSync _client; +}; +#endif + +#if defined(ARDUINO_ARCH_ESP32) +class espMqttClient : public MqttClientSetup { + public: + explicit espMqttClient(espMqttClientTypes::UseInternalTask useInternalTask); + explicit espMqttClient(uint8_t priority = 1, uint8_t core = 1); + + protected: + espMqttClientInternals::ClientSync _client; +}; + +class espMqttClientSecure : public MqttClientSetup { + public: + explicit espMqttClientSecure(espMqttClientTypes::UseInternalTask useInternalTask); + explicit espMqttClientSecure(uint8_t priority = 1, uint8_t core = 1); + espMqttClientSecure& setInsecure(); + espMqttClientSecure& setCACert(const char* rootCA); + espMqttClientSecure& setCertificate(const char* clientCa); + espMqttClientSecure& setPrivateKey(const char* privateKey); + espMqttClientSecure& setPreSharedKey(const char* pskIdent, const char* psKey); + + protected: + espMqttClientInternals::ClientSecureSync _client; +}; +#endif + +#if defined(__linux__) +class espMqttClient : public MqttClientSetup { + public: + espMqttClient(); + + protected: + espMqttClientInternals::ClientPosix _client; +}; +#endif diff --git a/lib/espMqttClient/src/espMqttClientAsync.cpp b/lib/espMqttClient/src/espMqttClientAsync.cpp new file mode 100644 index 000000000..98b7f1519 --- /dev/null +++ b/lib/espMqttClient/src/espMqttClientAsync.cpp @@ -0,0 +1,61 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include "espMqttClientAsync.h" + +espMqttClientAsync::espMqttClientAsync() +: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO) +, _clientAsync() { + _transport = &_clientAsync; + _clientAsync.client.onConnect(onConnectCb, this); + _clientAsync.client.onDisconnect(onDisconnectCb, this); + _clientAsync.client.onData(onDataCb, this); + _clientAsync.client.onPoll(onPollCb, this); +} + +bool espMqttClientAsync::connect() { + bool ret = MqttClient::connect(); + loop(); + return ret; +} + +void espMqttClientAsync::_setupClient(espMqttClientAsync* c) { + (void)c; +} + +void espMqttClientAsync::onConnectCb(void* a, AsyncClient* c) { + c->setNoDelay(true); + espMqttClientAsync* client = reinterpret_cast(a); + client->_state = MqttClient::State::connectingTcp2; + client->loop(); +} + +void espMqttClientAsync::onDataCb(void* a, AsyncClient* c, void* data, size_t len) { + (void)c; + espMqttClientAsync* client = reinterpret_cast(a); + client->_clientAsync.bufData = reinterpret_cast(data); + client->_clientAsync.availableData = len; + client->loop(); +} + +void espMqttClientAsync::onDisconnectCb(void* a, AsyncClient* c) { + (void)c; + espMqttClientAsync* client = reinterpret_cast(a); + client->_state = MqttClient::State::disconnectingTcp2; + client->loop(); +} + +void espMqttClientAsync::onPollCb(void* a, AsyncClient* c) { + (void)c; + espMqttClientAsync* client = reinterpret_cast(a); + client->loop(); +} + +#endif diff --git a/lib/espMqttClient/src/espMqttClientAsync.h b/lib/espMqttClient/src/espMqttClientAsync.h new file mode 100644 index 000000000..1b9ed8beb --- /dev/null +++ b/lib/espMqttClient/src/espMqttClientAsync.h @@ -0,0 +1,36 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +API is based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include "Transport/ClientAsync.h" + +#include "MqttClientSetup.h" + +class espMqttClientAsync : public MqttClientSetup { + public: + espMqttClientAsync(); + bool connect(); + + protected: + espMqttClientInternals::ClientAsync _clientAsync; + static void _setupClient(espMqttClientAsync* c); + static void _disconnectClient(espMqttClientAsync* c); + + static void onConnectCb(void* a, AsyncClient* c); + static void onDataCb(void* a, AsyncClient* c, void* data, size_t len); + static void onDisconnectCb(void* a, AsyncClient* c); + static void onPollCb(void* a, AsyncClient* c); +}; + +#endif diff --git a/lib/framework/ESP8266React.h b/lib/framework/ESP8266React.h index 953232f78..32ca0f55c 100644 --- a/lib/framework/ESP8266React.h +++ b/lib/framework/ESP8266React.h @@ -59,7 +59,7 @@ class ESP8266React { return &_mqttSettingsService; } - AsyncMqttClient * getMqttClient() { + espMqttClient * getMqttClient() { return _mqttSettingsService.getMqttClient(); } diff --git a/lib/framework/MqttPubSub.h b/lib/framework/MqttPubSub.h deleted file mode 100644 index fe8e56434..000000000 --- a/lib/framework/MqttPubSub.h +++ /dev/null @@ -1,163 +0,0 @@ -#ifndef MqttPubSub_h -#define MqttPubSub_h - -#include -#include - -using namespace std::placeholders; // for `_1` etc - -#define MQTT_ORIGIN_ID "mqtt" - -template -class MqttConnector { - protected: - StatefulService * _statefulService; - AsyncMqttClient * _mqttClient; - size_t _bufferSize; - - MqttConnector(StatefulService * statefulService, AsyncMqttClient * mqttClient, size_t bufferSize) - : _statefulService(statefulService) - , _mqttClient(mqttClient) - , _bufferSize(bufferSize) { - _mqttClient->onConnect(std::bind(&MqttConnector::onConnect, this)); - } - - virtual void onConnect() = 0; - - public: - inline AsyncMqttClient * getMqttClient() const { - return _mqttClient; - } -}; - -template -class MqttPub : virtual public MqttConnector { - public: - MqttPub(JsonStateReader stateReader, - StatefulService * statefulService, - AsyncMqttClient * mqttClient, - const String & pubTopic = "", - size_t bufferSize = DEFAULT_BUFFER_SIZE) - : MqttConnector(statefulService, mqttClient, bufferSize) - , _stateReader(stateReader) - , _pubTopic(pubTopic) { - MqttConnector::_statefulService->addUpdateHandler([&](const String & originId) { publish(); }, false); - } - - void setPubTopic(const String & pubTopic) { - _pubTopic = pubTopic; - publish(); - } - - protected: - virtual void onConnect() { - publish(); - } - - private: - JsonStateReader _stateReader; - String _pubTopic; - - void publish() { - if (_pubTopic.length() > 0 && MqttConnector::_mqttClient->connected()) { - // serialize to json doc - DynamicJsonDocument json(MqttConnector::_bufferSize); - JsonObject jsonObject = json.to(); - MqttConnector::_statefulService->read(jsonObject, _stateReader); - - // serialize to string - String payload; - serializeJson(json, payload); - - // publish the payload - MqttConnector::_mqttClient->publish(_pubTopic.c_str(), 0, false, payload.c_str()); - } - } -}; - -template -class MqttSub : virtual public MqttConnector { - public: - MqttSub(JsonStateUpdater stateUpdater, - StatefulService * statefulService, - AsyncMqttClient * mqttClient, - const String & subTopic = "", - size_t bufferSize = DEFAULT_BUFFER_SIZE) - : MqttConnector(statefulService, mqttClient, bufferSize) - , _stateUpdater(stateUpdater) - , _subTopic(subTopic) { - MqttConnector::_mqttClient->onMessage(std::bind(&MqttSub::onMqttMessage, this, _1, _2, _3, _4, _5, _6)); - } - - void setSubTopic(const String & subTopic) { - if (!_subTopic.equals(subTopic)) { - // unsubscribe from the existing topic if one was set - if (_subTopic.length() > 0) { - MqttConnector::_mqttClient->unsubscribe(_subTopic.c_str()); - } - // set the new topic and re-configure the subscription - _subTopic = subTopic; - subscribe(); - } - } - - protected: - virtual void onConnect() { - subscribe(); - } - - private: - JsonStateUpdater _stateUpdater; - String _subTopic; - - void subscribe() { - if (_subTopic.length() > 0) { - MqttConnector::_mqttClient->subscribe(_subTopic.c_str(), 2); - } - } - - void onMqttMessage(char * topic, char * payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { - // we only care about the topic we are watching in this class - if (strcmp(_subTopic.c_str(), topic)) { - return; - } - - // deserialize from string - DynamicJsonDocument json(MqttConnector::_bufferSize); - DeserializationError error = deserializeJson(json, payload, len); - if (!error && json.is()) { - JsonObject jsonObject = json.as(); - MqttConnector::_statefulService->update(jsonObject, _stateUpdater, MQTT_ORIGIN_ID); - } - } -}; - -template -class MqttPubSub : public MqttPub, public MqttSub { - public: - MqttPubSub(JsonStateReader stateReader, - JsonStateUpdater stateUpdater, - StatefulService * statefulService, - AsyncMqttClient * mqttClient, - const String & pubTopic = "", - const String & subTopic = "", - size_t bufferSize = DEFAULT_BUFFER_SIZE) - : MqttConnector(statefulService, mqttClient, bufferSize) - , MqttPub(stateReader, statefulService, mqttClient, pubTopic, bufferSize) - , MqttSub(stateUpdater, statefulService, mqttClient, subTopic, bufferSize) { - } - - public: - void configureTopics(const String & pubTopic, const String & subTopic) { - MqttSub::setSubTopic(subTopic); - MqttPub::setPubTopic(pubTopic); - } - - protected: - void onConnect() { - MqttSub::onConnect(); - MqttPub::onConnect(); - } -}; - -#endif diff --git a/lib/framework/MqttSettingsService.cpp b/lib/framework/MqttSettingsService.cpp index 13e9d243d..4658250cf 100644 --- a/lib/framework/MqttSettingsService.cpp +++ b/lib/framework/MqttSettingsService.cpp @@ -33,8 +33,8 @@ MqttSettingsService::MqttSettingsService(AsyncWebServer * server, FS * fs, Secur , _retainedPassword(nullptr) , _reconfigureMqtt(false) , _disconnectedAt(0) - , _disconnectReason(AsyncMqttClientDisconnectReason::TCP_DISCONNECTED) - , _mqttClient() { + , _disconnectReason(espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED) + , _mqttClient(espMqttClientTypes::UseInternalTask::NO) { WiFi.onEvent(std::bind(&MqttSettingsService::WiFiEvent, this, _1, _2)); _mqttClient.onConnect(std::bind(&MqttSettingsService::onMqttConnect, this, _1)); _mqttClient.onDisconnect(std::bind(&MqttSettingsService::onMqttDisconnect, this, _1)); @@ -51,12 +51,13 @@ void MqttSettingsService::begin() { void MqttSettingsService::loop() { if (_reconfigureMqtt || (_disconnectedAt && (uint32_t)(uuid::get_uptime() - _disconnectedAt) >= MQTT_RECONNECTION_DELAY)) { // reconfigure MQTT client - configureMqtt(); - - // clear the reconnection flags + _disconnectedAt = uuid::get_uptime(); + if (configureMqtt()) { + _disconnectedAt = 0; + } _reconfigureMqtt = false; - _disconnectedAt = 0; } + _mqttClient.loop(); } bool MqttSettingsService::isEnabled() { @@ -71,22 +72,27 @@ const char * MqttSettingsService::getClientId() { return _mqttClient.getClientId(); } -AsyncMqttClientDisconnectReason MqttSettingsService::getDisconnectReason() { +espMqttClientTypes::DisconnectReason MqttSettingsService::getDisconnectReason() { return _disconnectReason; } -AsyncMqttClient * MqttSettingsService::getMqttClient() { +espMqttClient * MqttSettingsService::getMqttClient() { return &_mqttClient; } void MqttSettingsService::onMqttConnect(bool sessionPresent) { + // _disconnectedAt = 0; + emsesp::EMSESP::mqtt_.on_connect(); // emsesp::EMSESP::logger().info("Connected to MQTT, %s", (sessionPresent) ? ("with persistent session") : ("without persistent session")); } -void MqttSettingsService::onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { +void MqttSettingsService::onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { // emsesp::EMSESP::logger().info("Disconnected from MQTT reason: %d", (uint8_t)reason); _disconnectReason = reason; - _disconnectedAt = uuid::get_uptime(); + if (!_disconnectedAt) { + _disconnectedAt = uuid::get_uptime(); + } + emsesp::EMSESP::mqtt_.on_disconnect(reason); } void MqttSettingsService::onConfigUpdated() { @@ -121,7 +127,7 @@ void MqttSettingsService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { } } -void MqttSettingsService::configureMqtt() { +bool MqttSettingsService::configureMqtt() { // disconnect if connected _mqttClient.disconnect(); // only connect if WiFi is connected and MQTT is enabled @@ -137,11 +143,11 @@ void MqttSettingsService::configureMqtt() { _mqttClient.setClientId(retainCstr(_state.clientId.c_str(), &_retainedClientId)); _mqttClient.setKeepAlive(_state.keepAlive); _mqttClient.setCleanSession(_state.cleanSession); - _mqttClient.setMaxTopicLength(FACTORY_MQTT_MAX_TOPIC_LENGTH); // hardcode. We don't take this from the settings anymore. - _mqttClient.connect(); + return _mqttClient.connect(); // } else { // emsesp::EMSESP::logger().info("Error configuring MQTT client"); } + return false; } void MqttSettings::read(MqttSettings & settings, JsonObject & root) { diff --git a/lib/framework/MqttSettingsService.h b/lib/framework/MqttSettingsService.h index d013853b5..1db25c0d4 100644 --- a/lib/framework/MqttSettingsService.h +++ b/lib/framework/MqttSettingsService.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include @@ -104,13 +104,13 @@ class MqttSettingsService : public StatefulService { MqttSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager); ~MqttSettingsService(); - void begin(); - void loop(); - bool isEnabled(); - bool isConnected(); - const char * getClientId(); - AsyncMqttClientDisconnectReason getDisconnectReason(); - AsyncMqttClient * getMqttClient(); + void begin(); + void loop(); + bool isEnabled(); + bool isConnected(); + const char * getClientId(); + espMqttClientTypes::DisconnectReason getDisconnectReason(); + espMqttClient * getMqttClient(); protected: void onConfigUpdated(); @@ -120,7 +120,7 @@ class MqttSettingsService : public StatefulService { FSPersistence _fsPersistence; // Pointers to hold retained copies of the mqtt client connection strings. - // This is required as AsyncMqttClient holds references to the supplied connection strings. + // This is required as espMqttClient holds references to the supplied connection strings. char * _retainedHost; char * _retainedClientId; char * _retainedUsername; @@ -131,15 +131,15 @@ class MqttSettingsService : public StatefulService { unsigned long _disconnectedAt; // connection status - AsyncMqttClientDisconnectReason _disconnectReason; + espMqttClientTypes::DisconnectReason _disconnectReason; // the MQTT client instance - AsyncMqttClient _mqttClient; + espMqttClient _mqttClient; void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info); void onMqttConnect(bool sessionPresent); - void onMqttDisconnect(AsyncMqttClientDisconnectReason reason); - void configureMqtt(); + void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason); + bool configureMqtt(); }; #endif diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index 97d74f6dc..1b04ce9f7 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -1706,13 +1706,13 @@ void EMSdevice::mqtt_ha_entity_config_create() { Mqtt::publish_ha_sensor_config(dv, name(), brand_to_char(), false, create_device_config); dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED); create_device_config = false; // only create the main config once - } - #ifndef EMSESP_STANDALONE - if (ESP.getFreeHeap() < (65 * 1024)) { - break; - } + // always create minimum one config + if (ESP.getMaxAllocHeap() < (6 * 1024) || (!emsesp::EMSESP::system_.PSram() && ESP.getFreeHeap() < (65 * 1024))) { + break; + } #endif + } } ha_config_done(!create_device_config); diff --git a/src/emsesp.cpp b/src/emsesp.cpp index b269eb749..8d8436637 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -1297,6 +1297,9 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) { LOG_ERROR("Last Tx write rejected by host"); txservice_.send_poll(); // close the bus } + } else if (tx_state == Telegram::Operation::TX_READ && length == 1) { + EMSbus::tx_state(Telegram::Operation::TX_READ); // reset Tx wait state + return; } else if (tx_state == Telegram::Operation::TX_READ) { // got a telegram with data in it. See if the src/dest matches that from the last one we sent and continue to process it uint8_t src = data[0]; diff --git a/src/mqtt.cpp b/src/mqtt.cpp index eaba3391f..3461403c5 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -23,7 +23,7 @@ namespace emsesp { -AsyncMqttClient * Mqtt::mqttClient_; +espMqttClient * Mqtt::mqttClient_; // static parameters we make global std::string Mqtt::mqtt_base_; @@ -461,30 +461,6 @@ void Mqtt::start() { // add the 'publish' command ('call system publish' in console or via API) Command::add(EMSdevice::DeviceType::SYSTEM, F_(publish), System::command_publish, FL_(publish_cmd)); - mqttClient_->onConnect([this](bool sessionPresent) { on_connect(); }); - - mqttClient_->onDisconnect([this](AsyncMqttClientDisconnectReason reason) { - // only show the error once, not every 2 seconds - if (!connecting_ && first_connect_attempted_) { - return; - } - first_connect_attempted_ = true; - connecting_ = false; - if (reason == AsyncMqttClientDisconnectReason::TCP_DISCONNECTED) { - LOG_WARNING("MQTT disconnected: TCP"); - } else if (reason == AsyncMqttClientDisconnectReason::MQTT_IDENTIFIER_REJECTED) { - LOG_WARNING("MQTT disconnected: Identifier Rejected"); - } else if (reason == AsyncMqttClientDisconnectReason::MQTT_SERVER_UNAVAILABLE) { - LOG_WARNING("MQTT disconnected: Server unavailable"); - } else if (reason == AsyncMqttClientDisconnectReason::MQTT_MALFORMED_CREDENTIALS) { - LOG_WARNING("MQTT disconnected: Malformed credentials"); - } else if (reason == AsyncMqttClientDisconnectReason::MQTT_NOT_AUTHORIZED) { - LOG_WARNING("MQTT disconnected: Not authorized"); - } else { - LOG_WARNING("MQTT disconnected: code %d", reason); - } - }); - // create last will topic with the base prefixed. It has to be static because asyncmqttclient destroys the reference static char will_topic[MQTT_TOPIC_MAX_SIZE]; if (!mqtt_base_.empty()) { @@ -493,9 +469,10 @@ void Mqtt::start() { mqttClient_->setWill(will_topic, 1, true, "offline"); // with qos 1, retain true - mqttClient_->onMessage([this](const char * topic, const char * payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { - on_message(topic, payload, len); // receiving mqtt - }); + mqttClient_->onMessage( + [this](const espMqttClientTypes::MessageProperties & properties, const char * topic, const uint8_t * payload, size_t len, size_t index, size_t total) { + on_message(topic, (const char *)payload, len); // receiving mqtt + }); mqttClient_->onPublish([this](uint16_t packetId) { on_publish(packetId); // publish @@ -555,6 +532,30 @@ bool Mqtt::get_publish_onchange(uint8_t device_type) { } return false; } +void Mqtt::on_disconnect(espMqttClientTypes::DisconnectReason reason) { + // only show the error once, not every 2 seconds + if (!connecting_) { + return; + } + connecting_ = false; + if (reason == espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED) { + LOG_WARNING("MQTT disconnected: TCP"); + } else if (reason == espMqttClientTypes::DisconnectReason::MQTT_UNACCEPTABLE_PROTOCOL_VERSION) { + LOG_WARNING("MQTT disconnected: Unacceptable protocol version"); + } else if (reason == espMqttClientTypes::DisconnectReason::MQTT_IDENTIFIER_REJECTED) { + LOG_WARNING("MQTT disconnected: Identifier Rejected"); + } else if (reason == espMqttClientTypes::DisconnectReason::MQTT_SERVER_UNAVAILABLE) { + LOG_WARNING("MQTT disconnected: Server unavailable"); + } else if (reason == espMqttClientTypes::DisconnectReason::MQTT_MALFORMED_CREDENTIALS) { + LOG_WARNING("MQTT disconnected: Malformed credentials"); + } else if (reason == espMqttClientTypes::DisconnectReason::MQTT_NOT_AUTHORIZED) { + LOG_WARNING("MQTT disconnected: Not authorized"); + } else if (reason == espMqttClientTypes::DisconnectReason::TLS_BAD_FINGERPRINT) { + LOG_WARNING("MQTT disconnected: Server fingerprint invalid"); + } else { + LOG_WARNING("MQTT disconnected: code %d", reason); + } +} // MQTT onConnect - when an MQTT connect is established void Mqtt::on_connect() { @@ -680,7 +681,7 @@ void Mqtt::queue_message(const uint8_t operation, const std::string & topic, con #ifndef EMSESP_STANDALONE // anything below 60MB available free heap is dangerously low, so we stop adding to prevent a crash // instead of doing a mqtt_messages_.pop_front() - if (mqtt_messages_.size() >= MAX_MQTT_MESSAGES || ESP.getFreeHeap() < (60 * 1024)) { + if (mqtt_messages_.size() >= MAX_MQTT_MESSAGES || (!emsesp::EMSESP::system_.PSram() && ESP.getFreeHeap() < (60 * 1024))) { LOG_WARNING("Queue overflow (queue count=%d, topic=%s)", mqtt_messages_.size(), topic.c_str()); mqtt_publish_fails_++; return; // don't add to top of queue @@ -857,7 +858,8 @@ void Mqtt::process_queue() { // else try and publish it // this is where the *real* publish happens - uint16_t packet_id = mqttClient_->publish(topic, mqtt_qos_, message->retain, message->payload.c_str(), message->payload.size(), false, mqtt_message.id_); + // uint16_t packet_id = mqttClient_->publish(topic, mqtt_qos_, message->retain, message->payload.c_str(), message->payload.size(), false, mqtt_message.id_); + uint16_t packet_id = mqttClient_->publish(topic, mqtt_qos_, message->retain, message->payload.c_str()); lasttopic_ = topic; lastpayload_ = message->payload; diff --git a/src/mqtt.h b/src/mqtt.h index c1be14df7..ad77fb53b 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -19,7 +19,7 @@ #ifndef EMSESP_MQTT_H_ #define EMSESP_MQTT_H_ -#include +#include #include "helpers.h" #include "system.h" @@ -76,6 +76,7 @@ class Mqtt { static constexpr uint8_t MQTT_TOPIC_MAX_SIZE = 128; // fixed, not a user setting anymore static void on_connect(); + static void on_disconnect(espMqttClientTypes::DisconnectReason reason); static void subscribe(const uint8_t device_type, const std::string & topic, mqtt_sub_function_p cb); static void subscribe(const std::string & topic); @@ -129,7 +130,7 @@ class Mqtt { #endif } - static AsyncMqttClient * client() { + static espMqttClient * client() { return mqttClient_; } @@ -266,7 +267,7 @@ class Mqtt { private: static uuid::log::Logger logger_; - static AsyncMqttClient * mqttClient_; + static espMqttClient * mqttClient_; static uint32_t mqtt_message_id_; static constexpr uint32_t MQTT_PUBLISH_WAIT = 100; // delay in ms between sending publishes, to account for large payloads @@ -306,8 +307,6 @@ class Mqtt { uint32_t last_publish_heartbeat_ = 0; uint32_t last_publish_queue_ = 0; - bool first_connect_attempted_ = false; - static bool connecting_; static bool initialized_; static uint32_t mqtt_publish_fails_; From cbc9607b26b434c0c51452efe24aff660a1e142d Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sat, 3 Jun 2023 17:46:26 +0200 Subject: [PATCH 02/18] apply changes/blocker for manage users --- .../framework/security/ManageUsersForm.tsx | 51 ++++++++++++++----- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/interface/src/framework/security/ManageUsersForm.tsx b/interface/src/framework/security/ManageUsersForm.tsx index e4aa4862a..72b9303ea 100644 --- a/interface/src/framework/security/ManageUsersForm.tsx +++ b/interface/src/framework/security/ManageUsersForm.tsx @@ -1,22 +1,24 @@ +import CancelIcon from '@mui/icons-material/Cancel'; import CheckIcon from '@mui/icons-material/Check'; import CloseIcon from '@mui/icons-material/Close'; import DeleteIcon from '@mui/icons-material/Delete'; import EditIcon from '@mui/icons-material/Edit'; import PersonAddIcon from '@mui/icons-material/PersonAdd'; -import SaveIcon from '@mui/icons-material/Save'; import VpnKeyIcon from '@mui/icons-material/VpnKey'; +import WarningIcon from '@mui/icons-material/Warning'; import { Button, IconButton, Box } from '@mui/material'; import { Table, Header, HeaderRow, HeaderCell, Body, Row, Cell } from '@table-library/react-table-library/table'; import { useTheme } from '@table-library/react-table-library/theme'; import { useContext, useState } from 'react'; +import { unstable_useBlocker as useBlocker } from 'react-router-dom'; import GenerateToken from './GenerateToken'; import UserForm from './UserForm'; import type { FC } from 'react'; import type { SecuritySettings, User } from 'types'; import * as SecurityApi from 'api/security'; -import { ButtonRow, FormLoader, MessageBox, SectionContent } from 'components'; +import { ButtonRow, FormLoader, MessageBox, SectionContent, BlockNavigation } from 'components'; import { AuthenticatedContext } from 'contexts/authentication'; import { useI18nContext } from 'i18n/i18n-react'; import { useRest } from 'utils'; @@ -30,9 +32,11 @@ const ManageUsersForm: FC = () => { const [user, setUser] = useState(); const [creating, setCreating] = useState(false); + const [changed, setChanged] = useState(0); const [generatingToken, setGeneratingToken] = useState(); const authenticatedContext = useContext(AuthenticatedContext); + const blocker = useBlocker(changed !== 0); const { LL } = useI18nContext(); const table_theme = useTheme({ @@ -85,6 +89,7 @@ const ManageUsersForm: FC = () => { const removeUser = (toRemove: User) => { const users = data.users.filter((u) => u.username !== toRemove.username); setData({ ...data, users }); + setChanged(changed + 1); }; const createUser = () => { @@ -110,6 +115,7 @@ const ManageUsersForm: FC = () => { const users = [...data.users.filter((u) => u.username !== user.username), user]; setData({ ...data, users }); setUser(undefined); + setChanged(changed + 1); } }; @@ -124,6 +130,12 @@ const ManageUsersForm: FC = () => { const onSubmit = async () => { await saveData(); await authenticatedContext.refresh(); + setChanged(0); + }; + + const onCancelSubmit = () => { + loadData(); + setChanged(0); }; const user_table = data.users.map((u) => ({ ...u, id: u.username })); @@ -172,16 +184,30 @@ const ManageUsersForm: FC = () => { - + {changed !== 0 && ( + + + + + )} @@ -208,6 +234,7 @@ const ManageUsersForm: FC = () => { return ( + {blocker ? : null} {content()} ); From 181b672c1a22c02765f92bb5830e300c73ea0854 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sun, 4 Jun 2023 15:32:43 +0200 Subject: [PATCH 03/18] add ventilation bypass --- interface/package.json | 2 +- interface/yarn.lock | 100 +++++++++++++-------------- lib/espMqttClient/src/MqttClient.cpp | 6 +- src/devices/ventilation.cpp | 18 ++++- src/devices/ventilation.h | 4 +- src/emsesp.cpp | 1 + 6 files changed, 76 insertions(+), 55 deletions(-) diff --git a/interface/package.json b/interface/package.json index 0898b2c16..067bb18f1 100644 --- a/interface/package.json +++ b/interface/package.json @@ -47,7 +47,7 @@ "devDependencies": { "@typescript-eslint/eslint-plugin": "^5.59.8", "@typescript-eslint/parser": "^5.59.8", - "@vitejs/plugin-react-swc": "^3.3.1", + "@vitejs/plugin-react-swc": "^3.3.2", "eslint": "^8.42.0", "eslint-config-airbnb": "^19.0.4", "eslint-config-airbnb-typescript": "^17.0.0", diff --git a/interface/yarn.lock b/interface/yarn.lock index 2ecb333b0..835d157aa 100644 --- a/interface/yarn.lock +++ b/interface/yarn.lock @@ -1101,90 +1101,90 @@ __metadata: languageName: node linkType: hard -"@swc/core-darwin-arm64@npm:1.3.56": - version: 1.3.56 - resolution: "@swc/core-darwin-arm64@npm:1.3.56" +"@swc/core-darwin-arm64@npm:1.3.62": + version: 1.3.62 + resolution: "@swc/core-darwin-arm64@npm:1.3.62" conditions: os=darwin & cpu=arm64 languageName: node linkType: hard -"@swc/core-darwin-x64@npm:1.3.56": - version: 1.3.56 - resolution: "@swc/core-darwin-x64@npm:1.3.56" +"@swc/core-darwin-x64@npm:1.3.62": + version: 1.3.62 + resolution: "@swc/core-darwin-x64@npm:1.3.62" conditions: os=darwin & cpu=x64 languageName: node linkType: hard -"@swc/core-linux-arm-gnueabihf@npm:1.3.56": - version: 1.3.56 - resolution: "@swc/core-linux-arm-gnueabihf@npm:1.3.56" +"@swc/core-linux-arm-gnueabihf@npm:1.3.62": + version: 1.3.62 + resolution: "@swc/core-linux-arm-gnueabihf@npm:1.3.62" conditions: os=linux & cpu=arm languageName: node linkType: hard -"@swc/core-linux-arm64-gnu@npm:1.3.56": - version: 1.3.56 - resolution: "@swc/core-linux-arm64-gnu@npm:1.3.56" +"@swc/core-linux-arm64-gnu@npm:1.3.62": + version: 1.3.62 + resolution: "@swc/core-linux-arm64-gnu@npm:1.3.62" conditions: os=linux & cpu=arm64 & libc=glibc languageName: node linkType: hard -"@swc/core-linux-arm64-musl@npm:1.3.56": - version: 1.3.56 - resolution: "@swc/core-linux-arm64-musl@npm:1.3.56" +"@swc/core-linux-arm64-musl@npm:1.3.62": + version: 1.3.62 + resolution: "@swc/core-linux-arm64-musl@npm:1.3.62" conditions: os=linux & cpu=arm64 & libc=musl languageName: node linkType: hard -"@swc/core-linux-x64-gnu@npm:1.3.56": - version: 1.3.56 - resolution: "@swc/core-linux-x64-gnu@npm:1.3.56" +"@swc/core-linux-x64-gnu@npm:1.3.62": + version: 1.3.62 + resolution: "@swc/core-linux-x64-gnu@npm:1.3.62" conditions: os=linux & cpu=x64 & libc=glibc languageName: node linkType: hard -"@swc/core-linux-x64-musl@npm:1.3.56": - version: 1.3.56 - resolution: "@swc/core-linux-x64-musl@npm:1.3.56" +"@swc/core-linux-x64-musl@npm:1.3.62": + version: 1.3.62 + resolution: "@swc/core-linux-x64-musl@npm:1.3.62" conditions: os=linux & cpu=x64 & libc=musl languageName: node linkType: hard -"@swc/core-win32-arm64-msvc@npm:1.3.56": - version: 1.3.56 - resolution: "@swc/core-win32-arm64-msvc@npm:1.3.56" +"@swc/core-win32-arm64-msvc@npm:1.3.62": + version: 1.3.62 + resolution: "@swc/core-win32-arm64-msvc@npm:1.3.62" conditions: os=win32 & cpu=arm64 languageName: node linkType: hard -"@swc/core-win32-ia32-msvc@npm:1.3.56": - version: 1.3.56 - resolution: "@swc/core-win32-ia32-msvc@npm:1.3.56" +"@swc/core-win32-ia32-msvc@npm:1.3.62": + version: 1.3.62 + resolution: "@swc/core-win32-ia32-msvc@npm:1.3.62" conditions: os=win32 & cpu=ia32 languageName: node linkType: hard -"@swc/core-win32-x64-msvc@npm:1.3.56": - version: 1.3.56 - resolution: "@swc/core-win32-x64-msvc@npm:1.3.56" +"@swc/core-win32-x64-msvc@npm:1.3.62": + version: 1.3.62 + resolution: "@swc/core-win32-x64-msvc@npm:1.3.62" conditions: os=win32 & cpu=x64 languageName: node linkType: hard -"@swc/core@npm:^1.3.56": - version: 1.3.56 - resolution: "@swc/core@npm:1.3.56" +"@swc/core@npm:^1.3.61": + version: 1.3.62 + resolution: "@swc/core@npm:1.3.62" dependencies: - "@swc/core-darwin-arm64": 1.3.56 - "@swc/core-darwin-x64": 1.3.56 - "@swc/core-linux-arm-gnueabihf": 1.3.56 - "@swc/core-linux-arm64-gnu": 1.3.56 - "@swc/core-linux-arm64-musl": 1.3.56 - "@swc/core-linux-x64-gnu": 1.3.56 - "@swc/core-linux-x64-musl": 1.3.56 - "@swc/core-win32-arm64-msvc": 1.3.56 - "@swc/core-win32-ia32-msvc": 1.3.56 - "@swc/core-win32-x64-msvc": 1.3.56 + "@swc/core-darwin-arm64": 1.3.62 + "@swc/core-darwin-x64": 1.3.62 + "@swc/core-linux-arm-gnueabihf": 1.3.62 + "@swc/core-linux-arm64-gnu": 1.3.62 + "@swc/core-linux-arm64-musl": 1.3.62 + "@swc/core-linux-x64-gnu": 1.3.62 + "@swc/core-linux-x64-musl": 1.3.62 + "@swc/core-win32-arm64-msvc": 1.3.62 + "@swc/core-win32-ia32-msvc": 1.3.62 + "@swc/core-win32-x64-msvc": 1.3.62 peerDependencies: "@swc/helpers": ^0.5.0 dependenciesMeta: @@ -1211,7 +1211,7 @@ __metadata: peerDependenciesMeta: "@swc/helpers": optional: true - checksum: c468e281f0249742bc0ba4b7cd4076cdbf87bfc82b8bd5ad1ca8940d36372ca22754df80ab54b22613121680718eab26b92c48c8c9f5f3abb24434f05e5e1ea0 + checksum: aaa0827960f656c762733836938d31b2d596495b8430eb6feb0d1f6b1416b3444e7b59c326ae37ee410d8d3d25fff20ac8ff0f66ebe8a87e7fae1ca651aff915 languageName: node linkType: hard @@ -1507,14 +1507,14 @@ __metadata: languageName: node linkType: hard -"@vitejs/plugin-react-swc@npm:^3.3.1": - version: 3.3.1 - resolution: "@vitejs/plugin-react-swc@npm:3.3.1" +"@vitejs/plugin-react-swc@npm:^3.3.2": + version: 3.3.2 + resolution: "@vitejs/plugin-react-swc@npm:3.3.2" dependencies: - "@swc/core": ^1.3.56 + "@swc/core": ^1.3.61 peerDependencies: vite: ^4 - checksum: 72ab0a72d41c949009a2f71836894fb0003939329a2d1bb59b1181b03d21fda5002ccd20b40b48ddc8f12511cc8717122141f49ac51e97263df3c3f3142ae937 + checksum: 8544023de3dc605d00f66db10de085b2a90887111657db87dd192e02ffc1e4a191b09d58180b4ba498858f01e8861d15e216d744e0f6dfad2550065999890fb2 languageName: node linkType: hard @@ -1534,7 +1534,7 @@ __metadata: "@types/react-router-dom": ^5.3.3 "@typescript-eslint/eslint-plugin": ^5.59.8 "@typescript-eslint/parser": ^5.59.8 - "@vitejs/plugin-react-swc": ^3.3.1 + "@vitejs/plugin-react-swc": ^3.3.2 async-validator: ^4.2.5 axios: ^1.4.0 eslint: ^8.42.0 diff --git a/lib/espMqttClient/src/MqttClient.cpp b/lib/espMqttClient/src/MqttClient.cpp index 314a3c385..c371c9f93 100644 --- a/lib/espMqttClient/src/MqttClient.cpp +++ b/lib/espMqttClient/src/MqttClient.cpp @@ -577,7 +577,7 @@ void MqttClient::_onPubrec() { while (it) { // PUBRECs come in the order PUBs are sent. So we only check the first PUB packet in outbox // if it doesn't match the ID, return - if ((it.get()->packet.packetType()) == PacketType.PUBLISH) { + if ((it.get()->packet.packetType()) == PacketType.PUBLISH || (it.get()->packet.packetType()) == PacketType.PUBREL) { if (it.get()->packet.packetId() == idToMatch) { if (!_addPacket(PacketType.PUBREL, idToMatch)) { emc_log_e("Could not create PUBREL packet"); @@ -707,7 +707,9 @@ uint16_t MqttClient::getQueue() const { espMqttClientInternals::Outbox::Iterator it = _outbox.front(); uint16_t count = 0; while (it) { - ++count; + if (it.get()->packet.packetType() == PacketType.PUBLISH) { + ++count; + } ++it; } EMC_SEMAPHORE_GIVE(); diff --git a/src/devices/ventilation.cpp b/src/devices/ventilation.cpp index 18d2d1579..f68059200 100644 --- a/src/devices/ventilation.cpp +++ b/src/devices/ventilation.cpp @@ -29,6 +29,7 @@ Ventilation::Ventilation(uint8_t device_type, uint8_t device_id, uint8_t product register_telegram_type(0x585, "Blowerspeed", false, MAKE_PF_CB(process_BlowerMessage)); register_telegram_type(0x583, "VentilationMonitor", false, MAKE_PF_CB(process_MonitorMessage)); register_telegram_type(0x5D9, "Airquality", false, MAKE_PF_CB(process_VOCMessage)); + register_telegram_type(0x587, "Bypass", false, MAKE_PF_CB(process_BypassMessage)); // register_telegram_type(0x5, "VentilationSet", true, MAKE_PF_CB(process_SetMessage)); register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, @@ -44,7 +45,8 @@ Ventilation::Ventilation(uint8_t device_type, uint8_t device_id, uint8_t product register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, &ventOutSpeed_, DeviceValueType::UINT, FL_(ventOutSpeed), DeviceValueUOM::PERCENT); register_device_value( DeviceValueTAG::TAG_DEVICE_DATA, &mode_, DeviceValueType::ENUM, FL_(enum_ventMode), FL_(ventInSpeed), DeviceValueUOM::NONE, MAKE_CF_CB(set_ventMode)); - register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, &voc_, DeviceValueType::USHORT, DeviceValueNumOp::DV_NUMOP_DIV10, FL_(airquality), DeviceValueUOM::NONE); + register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, &voc_, DeviceValueType::USHORT, FL_(airquality), DeviceValueUOM::NONE); + register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, &bypass_, DeviceValueType::BOOL, FL_(bypass), DeviceValueUOM::NONE, MAKE_CF_CB(set_bypass)); } // message @@ -81,6 +83,11 @@ void Ventilation::process_ModeMessage(std::shared_ptr telegram) has_enumupdate(telegram, mode_, 0, -1); } +// message 0x0587, data: 01 00 +void Ventilation::process_BypassMessage(std::shared_ptr telegram) { + has_update(telegram, bypass_, 1); +} + bool Ventilation::set_ventMode(const char * value, const int8_t id) { uint8_t v; if (!Helpers::value2enum(value, v, FL_(enum_ventMode))) { @@ -99,4 +106,13 @@ bool Ventilation::set_filter(const char * value, const int8_t id) { return true; } +bool Ventilation::set_bypass(const char * value, const int8_t id) { + bool b; + if (!Helpers::value2bool(value, b)) { + return false; + } + write_command(0x55C, 1, b ? 1 : 0, 0x587); + return true; +} + } // namespace emsesp diff --git a/src/devices/ventilation.h b/src/devices/ventilation.h index a48285374..88b24d7ff 100644 --- a/src/devices/ventilation.h +++ b/src/devices/ventilation.h @@ -34,7 +34,7 @@ class Ventilation : public EMSdevice { int16_t inEx_; int16_t outEx_; uint16_t voc_; - // uint8_t bypass_; + uint8_t bypass_; // uint16_t filterRemain_; uint8_t ventInSpeed_; uint8_t ventOutSpeed_; @@ -45,8 +45,10 @@ class Ventilation : public EMSdevice { void process_ModeMessage(std::shared_ptr telegram); // 0x56B void process_BlowerMessage(std::shared_ptr telegram); // 0x56B void process_VOCMessage(std::shared_ptr telegram); // 0x56B + void process_BypassMessage(std::shared_ptr telegram); // 0x56B bool set_ventMode(const char * value, const int8_t id); + bool set_bypass(const char * value, const int8_t id); bool set_filter(const char * value, const int8_t id); diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 8d8436637..f9ec33684 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -597,6 +597,7 @@ void EMSESP::publish_other_values() { publish_device_values(EMSdevice::DeviceType::SWITCH); publish_device_values(EMSdevice::DeviceType::HEATPUMP); publish_device_values(EMSdevice::DeviceType::HEATSOURCE); + publish_device_values(EMSdevice::DeviceType::VENTILATION); // other devices without values yet // publish_device_values(EMSdevice::DeviceType::GATEWAY); // publish_device_values(EMSdevice::DeviceType::CONNECT); From 759fe699093dd0e7aac1fa19068cb5c4f0cd8c4c Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sun, 4 Jun 2023 15:39:57 +0200 Subject: [PATCH 04/18] fix prefix of customizations --- src/emsdevice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index 1b04ce9f7..a5df59e21 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -1130,7 +1130,7 @@ void EMSdevice::getCustomEntities(std::vector & entity_ids) { for (const auto & dv : devicevalues_) { char name[100]; name[0] = '\0'; - if (dv.has_tag()) { + if (dv.tag < DeviceValueTAG::TAG_HC1) { // prefix tag strcpy(name, tag_to_mqtt(dv.tag)); strcat(name, "/"); From 9a34b2dd81ecb0a8b48a5f51fe7926e128c4ebc5 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sun, 4 Jun 2023 17:24:03 +0200 Subject: [PATCH 05/18] force mqtt disconnect on wifi disconnect --- lib/framework/MqttSettingsService.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/framework/MqttSettingsService.cpp b/lib/framework/MqttSettingsService.cpp index 4658250cf..59b5c0f43 100644 --- a/lib/framework/MqttSettingsService.cpp +++ b/lib/framework/MqttSettingsService.cpp @@ -118,7 +118,7 @@ void MqttSettingsService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { case ARDUINO_EVENT_ETH_DISCONNECTED: if (_state.enabled) { // emsesp::EMSESP::logger().info("Network connection dropped, stopping MQTT client"); - _mqttClient.disconnect(); + _mqttClient.disconnect(true); } break; @@ -129,7 +129,7 @@ void MqttSettingsService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { bool MqttSettingsService::configureMqtt() { // disconnect if connected - _mqttClient.disconnect(); + _mqttClient.disconnect(true); // only connect if WiFi is connected and MQTT is enabled if (_state.enabled && emsesp::EMSESP::system_.network_connected() && !_state.host.isEmpty()) { // emsesp::EMSESP::logger().info("Configuring MQTT client"); From b28865a2839909556ab5db9779a58de1f83b2aee Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Mon, 5 Jun 2023 10:06:19 +0200 Subject: [PATCH 06/18] use only espMqttClient queue --- lib/espMqttClient/src/MqttClient.cpp | 4 +- src/emsdevice.cpp | 21 +- src/emsesp.cpp | 2 +- src/mqtt.cpp | 380 +++++++-------------------- src/mqtt.h | 79 ++---- 5 files changed, 124 insertions(+), 362 deletions(-) diff --git a/lib/espMqttClient/src/MqttClient.cpp b/lib/espMqttClient/src/MqttClient.cpp index c371c9f93..2f1d0d643 100644 --- a/lib/espMqttClient/src/MqttClient.cpp +++ b/lib/espMqttClient/src/MqttClient.cpp @@ -707,9 +707,7 @@ uint16_t MqttClient::getQueue() const { espMqttClientInternals::Outbox::Iterator it = _outbox.front(); uint16_t count = 0; while (it) { - if (it.get()->packet.packetType() == PacketType.PUBLISH) { - ++count; - } + ++count; ++it; } EMC_SEMAPHORE_GIVE(); diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index aa22b4b14..91de94609 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -1689,23 +1689,26 @@ void EMSdevice::mqtt_ha_entity_config_create() { for (auto & dv : devicevalues_) { if (!strcmp(dv.short_name, FL_(haclimate)[0]) && !dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE) && dv.has_state(DeviceValueState::DV_ACTIVE)) { if (*(int8_t *)(dv.value_p) == 1 && (!dv.has_state(DeviceValueState::DV_HA_CONFIG_CREATED) || dv.has_state(DeviceValueState::DV_HA_CLIMATE_NO_RT))) { - dv.remove_state(DeviceValueState::DV_HA_CLIMATE_NO_RT); - dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED); - Mqtt::publish_ha_climate_config(dv.tag, true, false, dv.min, dv.max); // roomTemp + if (Mqtt::publish_ha_climate_config(dv.tag, true, false, dv.min, dv.max)) { // roomTemp + dv.remove_state(DeviceValueState::DV_HA_CLIMATE_NO_RT); + dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED); + } } else if (*(int8_t *)(dv.value_p) == 0 && (!dv.has_state(DeviceValueState::DV_HA_CONFIG_CREATED) || !dv.has_state(DeviceValueState::DV_HA_CLIMATE_NO_RT))) { - dv.add_state(DeviceValueState::DV_HA_CLIMATE_NO_RT); - dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED); - Mqtt::publish_ha_climate_config(dv.tag, false, false, dv.min, dv.max); // no roomTemp + if (Mqtt::publish_ha_climate_config(dv.tag, false, false, dv.min, dv.max)) { // no roomTemp + dv.add_state(DeviceValueState::DV_HA_CLIMATE_NO_RT); + dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED); + } } } if (!dv.has_state(DeviceValueState::DV_HA_CONFIG_CREATED) && (dv.type != DeviceValueType::CMD) && dv.has_state(DeviceValueState::DV_ACTIVE) && !dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE)) { // create_device_config is only done once for the EMS device. It can added to any entity, so we take the first - Mqtt::publish_ha_sensor_config(dv, name(), brand_to_char(), false, create_device_config); - dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED); - create_device_config = false; // only create the main config once + if (Mqtt::publish_ha_sensor_config(dv, name(), brand_to_char(), false, create_device_config)) { + dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED); + create_device_config = false; // only create the main config once + } #ifndef EMSESP_STANDALONE // always create minimum one config if (ESP.getMaxAllocHeap() < (6 * 1024) || (!emsesp::EMSESP::system_.PSram() && ESP.getFreeHeap() < (65 * 1024))) { diff --git a/src/emsesp.cpp b/src/emsesp.cpp index f9ec33684..ceeb25c6f 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -493,7 +493,7 @@ void EMSESP::publish_all_loop() { } // wait for free queue before sending next message, HA-messages are also queued - if (!Mqtt::is_empty()) { + if (Mqtt::publish_queued() > 0) { return; } diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 3461403c5..974e60740 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -47,13 +47,13 @@ bool Mqtt::send_response_; bool Mqtt::publish_single_; bool Mqtt::publish_single2cmd_; -std::deque Mqtt::mqtt_messages_; -std::vector Mqtt::mqtt_subfunctions_; +std::vector Mqtt::mqtt_subfunctions_; uint32_t Mqtt::mqtt_publish_fails_ = 0; bool Mqtt::connecting_ = false; bool Mqtt::initialized_ = false; bool Mqtt::ha_climate_reset_ = false; +uint16_t Mqtt::queuecount_ = 0; uint8_t Mqtt::connectcount_ = 0; uint32_t Mqtt::mqtt_message_id_ = 0; char will_topic_[Mqtt::MQTT_TOPIC_MAX_SIZE]; // because MQTT library keeps only char pointer @@ -95,7 +95,7 @@ void Mqtt::subscribe(const uint8_t device_type, const std::string & topic, mqtt_ // We store the original topic string without base mqtt_subfunctions_.emplace_back(device_type, std::move(topic), std::move(cb)); - if (!enabled()) { + if (!enabled() || !connected()) { return; } @@ -117,14 +117,7 @@ void Mqtt::resubscribe() { } for (const auto & mqtt_subfunction : mqtt_subfunctions_) { - bool found = false; - for (const auto & message : mqtt_messages_) { - found |= ((message.content_->operation == Operation::SUBSCRIBE) && (mqtt_subfunction.topic_ == message.content_->topic)); - } - - if (!found) { - queue_subscribe_message(mqtt_subfunction.topic_); - } + queue_subscribe_message(mqtt_subfunction.topic_); } } @@ -137,12 +130,6 @@ void Mqtt::loop() { uint32_t currentMillis = uuid::get_uptime(); - // publish MQTT queue, but timed to avoid overloading the TCP pipe - if ((uint32_t)(currentMillis - last_mqtt_poll_) > MQTT_PUBLISH_WAIT) { - last_mqtt_poll_ = currentMillis; - process_queue(); - } - // send heartbeat if ((currentMillis - last_publish_heartbeat_ > publish_time_heartbeat_)) { last_publish_heartbeat_ = currentMillis; @@ -154,7 +141,8 @@ void Mqtt::loop() { EMSESP::publish_sensor_values(false); } - if (!mqtt_messages_.empty()) { + queuecount_ = mqttClient_->getQueue(); + if (queuecount_ > 0) { return; } @@ -197,6 +185,7 @@ void Mqtt::show_mqtt(uuid::console::Shell & shell) { shell.printfln("MQTT Entity ID format is %d", entity_format_); shell.printfln("MQTT publish errors: %lu", mqtt_publish_fails_); + shell.printfln("MQTT queue: %d", queuecount_); shell.println(); // show subscriptions @@ -206,47 +195,6 @@ void Mqtt::show_mqtt(uuid::console::Shell & shell) { } shell.println(); - // show queues - if (mqtt_messages_.empty()) { - shell.printfln("MQTT queue is empty"); - shell.println(); - return; - } - - shell.printfln("MQTT queue (%d):", mqtt_messages_.size()); - - for (const auto & message : mqtt_messages_) { - auto content = message.content_; - char topic[MQTT_TOPIC_MAX_SIZE]; - - // prefix base, only if it's not a discovery topic - if (content->topic.compare(0, discovery_prefix().size(), discovery_prefix()) == 0) { - snprintf(topic, sizeof(topic), "%s/%s", mqtt_base_.c_str(), content->topic.c_str()); - } else { - snprintf(topic, sizeof(topic), "%s", content->topic.c_str()); - } - - if (content->operation == Operation::PUBLISH) { - // Publish messages - if (message.retry_count_ == 0) { - if (message.packet_id_ == 0) { - shell.printfln(" [%02d] (Pub) topic=%s payload=%s", message.id_, topic, content->payload.c_str()); - } else { - shell.printfln(" [%02d] (Pub) topic=%s payload=%s (pid %d)", message.id_, topic, content->payload.c_str(), message.packet_id_); - } - } else { - shell.printfln(" [%02d] (Pub) topic=%s payload=%s (pid %d, retry #%d)", - message.id_, - topic, - content->payload.c_str(), - message.packet_id_, - message.retry_count_); - } - } else { - // Subscribe messages - shell.printfln(" [%02d] (Sub) topic=%s", message.id_, topic); - } - } shell.println(); } @@ -265,7 +213,7 @@ void Mqtt::incoming(const char * topic, const char * payload) { void Mqtt::on_message(const char * topic, const char * payload, size_t len) const { // sometimes the payload is not terminated correctly, so make a copy // convert payload to a null-terminated char string - char message[len + 2]; + char message[len + 2] = {'\0'}; if (payload != nullptr) { strlcpy(message, payload, len + 1); } @@ -366,42 +314,12 @@ void Mqtt::show_topic_handlers(uuid::console::Shell & shell, const uint8_t devic } // called when an MQTT Publish ACK is received -// its a poor man's QOS we assume the ACK represents the last Publish sent -// check if ACK matches the last Publish we sent, if not report an error. Only if qos is 1 or 2 -// and always remove from queue void Mqtt::on_publish(uint16_t packetId) const { - // find the MQTT message in the queue and remove it - if (mqtt_messages_.empty()) { - LOG_DEBUG("No message stored for ACK pid %d", packetId); - return; - } - - auto mqtt_message = mqtt_messages_.front(); - - // if the last published failed, don't bother checking it. wait for the next retry - if (mqtt_message.packet_id_ == 0) { -#if defined(EMSESP_DEBUG) - LOG_DEBUG("ACK for failed message pid 0"); -#endif - return; - } - - if (mqtt_message.packet_id_ != packetId) { - LOG_ERROR("Mismatch, expecting PID %d, got %d", mqtt_message.packet_id_, packetId); - mqtt_publish_fails_++; // increment error count - } - - LOG_DEBUG("ACK pid %d", packetId); - - mqtt_messages_.pop_front(); // always remove from queue, regardless if there was a successful ACK + LOG_DEBUG("Packet %d sent successfull", packetId); } // called when MQTT settings have changed via the Web forms void Mqtt::reset_mqtt() { - if (!mqtt_enabled_) { - mqtt_messages_.clear(); - } - if (!mqttClient_) { return; } @@ -465,6 +383,8 @@ void Mqtt::start() { static char will_topic[MQTT_TOPIC_MAX_SIZE]; if (!mqtt_base_.empty()) { snprintf(will_topic, MQTT_TOPIC_MAX_SIZE, "%s/status", mqtt_base_.c_str()); + } else { + snprintf(will_topic, MQTT_TOPIC_MAX_SIZE, "status"); } mqttClient_->setWill(will_topic, 1, true, "offline"); // with qos 1, retain true @@ -474,9 +394,11 @@ void Mqtt::start() { on_message(topic, (const char *)payload, len); // receiving mqtt }); + /* mqttClient_->onPublish([this](uint16_t packetId) { on_publish(packetId); // publish }); + */ } void Mqtt::set_publish_time_boiler(uint16_t publish_time) { @@ -567,6 +489,7 @@ void Mqtt::on_connect() { connecting_ = true; connectcount_++; // count # reconnects. not currently used. + queuecount_ = 0; load_settings(); // reload MQTT settings - in case they have changes @@ -595,15 +518,6 @@ void Mqtt::on_connect() { queue_publish_retain("status", "online", true); // with retain on mqtt_publish_fails_ = 0; // reset fail count to 0 - - /* - // for debugging only - LOG_INFO("Queue size: %d", mqtt_messages_.size()); - for (const auto & message : mqtt_messages_) { - auto content = message.content_; - LOG_INFO(" [%02d] (%d) topic=%s payload=%s", message.id_, content->operation, content->topic.c_str(), content->payload.c_str()); - } - */ } // Home Assistant Discovery - the main master Device called EMS-ESP @@ -673,45 +587,44 @@ void Mqtt::ha_status() { // add sub or pub task to the queue. // the base is not included in the topic -void Mqtt::queue_message(const uint8_t operation, const std::string & topic, const std::string & payload, bool retain) { +bool Mqtt::queue_message(const uint8_t operation, const std::string & topic, const std::string & payload, const bool retain) { if (topic.empty()) { - return; + return false; } + uint16_t packet_id = 0; + char fulltopic[MQTT_TOPIC_MAX_SIZE]; -#ifndef EMSESP_STANDALONE - // anything below 60MB available free heap is dangerously low, so we stop adding to prevent a crash - // instead of doing a mqtt_messages_.pop_front() - if (mqtt_messages_.size() >= MAX_MQTT_MESSAGES || (!emsesp::EMSESP::system_.PSram() && ESP.getFreeHeap() < (60 * 1024))) { - LOG_WARNING("Queue overflow (queue count=%d, topic=%s)", mqtt_messages_.size(), topic.c_str()); - mqtt_publish_fails_++; - return; // don't add to top of queue + if (topic.find(discovery_prefix_) == 0) { + strlcpy(fulltopic, topic.c_str(), sizeof(fulltopic)); // leave topic as it is + } else { + // it's a discovery topic, added the mqtt base to the topic path + snprintf(fulltopic, sizeof(fulltopic), "%s/%s", mqtt_base_.c_str(), topic.c_str()); // uses base } -#endif - - // take the topic and prefix the base, unless its for HA - std::shared_ptr message = std::make_shared(operation, topic, payload, retain); if (operation == Operation::PUBLISH) { - if (message->payload.empty()) { - LOG_DEBUG("Adding to queue: (remove) topic='%s'", message->topic.c_str()); - } else { - LOG_DEBUG("Adding to queue: (publish) topic='%s' payload=%s", message->topic.c_str(), message->payload.c_str()); - } - } else { - LOG_DEBUG("Adding to queue: (subscribe) topic='%s'", message->topic.c_str()); + packet_id = mqttClient_->publish(fulltopic, mqtt_qos_, retain, payload.c_str()); + mqtt_message_id_++; + LOG_DEBUG("Publishing topic '%s', pid %d", fulltopic, packet_id); + } else if (operation == Operation::SUBSCRIBE) { + packet_id = mqttClient_->subscribe(fulltopic, mqtt_qos_); + LOG_DEBUG("Subscribing to topic '%s', pid %d", fulltopic, packet_id); + } else if (operation == Operation::UNSUBSCRIBE) { + packet_id = mqttClient_->unsubscribe(fulltopic); + LOG_DEBUG("Unsubscribing to topic '%s', pid %d", fulltopic, packet_id); } - - mqtt_messages_.emplace_back(mqtt_message_id_++, std::move(message)); - - return; + if (packet_id == 0) { + LOG_WARNING("%s failed: %s", operation == Operation::PUBLISH ? "Publish" : operation == Operation::SUBSCRIBE ? "Subscribe" : "Unsubscribe", fulltopic); + mqtt_publish_fails_++; + } + return (packet_id != 0); } // add MQTT message to queue, payload is a string -void Mqtt::queue_publish_message(const std::string & topic, const std::string & payload, bool retain) { +bool Mqtt::queue_publish_message(const std::string & topic, const std::string & payload, const bool retain) { if (!enabled()) { - return; + return false; }; - queue_message(Operation::PUBLISH, topic, payload, retain); + return queue_message(Operation::PUBLISH, topic, payload, retain); } // add MQTT subscribe message to queue @@ -725,187 +638,77 @@ void Mqtt::queue_unsubscribe_message(const std::string & topic) { } // MQTT Publish, using a user's retain flag -void Mqtt::queue_publish(const std::string & topic, const std::string & payload) { - queue_publish_message(topic, payload, mqtt_retain_); +bool Mqtt::queue_publish(const std::string & topic, const std::string & payload) { + return queue_publish_message(topic, payload, mqtt_retain_); } // MQTT Publish, using a user's retain flag - except for char * strings -void Mqtt::queue_publish(const char * topic, const char * payload) { - queue_publish_message((topic), payload, mqtt_retain_); +bool Mqtt::queue_publish(const char * topic, const char * payload) { + return queue_publish_message((topic), payload, mqtt_retain_); } // MQTT Publish, using a specific retain flag, topic is a flash string -void Mqtt::queue_publish(const char * topic, const std::string & payload) { - queue_publish_message((topic), payload, mqtt_retain_); +bool Mqtt::queue_publish(const char * topic, const std::string & payload) { + return queue_publish_message((topic), payload, mqtt_retain_); } -void Mqtt::queue_publish(const char * topic, const JsonObject & payload) { - queue_publish_retain(topic, payload, mqtt_retain_); +bool Mqtt::queue_publish(const char * topic, const JsonObject & payload) { + return queue_publish_retain(topic, payload, mqtt_retain_); } // publish json doc, only if its not empty -void Mqtt::queue_publish(const std::string & topic, const JsonObject & payload) { - queue_publish_retain(topic, payload, mqtt_retain_); +bool Mqtt::queue_publish(const std::string & topic, const JsonObject & payload) { + return queue_publish_retain(topic, payload, mqtt_retain_); } // MQTT Publish, using a specific retain flag, topic is a flash string, forcing retain flag -void Mqtt::queue_publish_retain(const char * topic, const std::string & payload, bool retain) { - queue_publish_message((topic), payload, retain); +bool Mqtt::queue_publish_retain(const char * topic, const std::string & payload, const bool retain) { + return queue_publish_message((topic), payload, retain); } // publish json doc, only if its not empty, using the retain flag -void Mqtt::queue_publish_retain(const std::string & topic, const JsonObject & payload, bool retain) { - queue_publish_retain(topic.c_str(), payload, retain); +bool Mqtt::queue_publish_retain(const std::string & topic, const JsonObject & payload, const bool retain) { + return queue_publish_retain(topic.c_str(), payload, retain); } -void Mqtt::queue_publish_retain(const char * topic, const JsonObject & payload, bool retain) { +bool Mqtt::queue_publish_retain(const char * topic, const JsonObject & payload, const bool retain) { if (enabled() && payload.size()) { std::string payload_text; serializeJson(payload, payload_text); // convert json to string - queue_publish_message(topic, payload_text, retain); + return queue_publish_message(topic, payload_text, retain); } + return false; } // publish empty payload to remove the topic -void Mqtt::queue_remove_topic(const char * topic) { +bool Mqtt::queue_remove_topic(const char * topic) { if (!enabled()) { - return; + return false; } if (ha_enabled_) { - queue_publish_message(Mqtt::discovery_prefix() + topic, "", true); // publish with retain to remove from broker + return queue_publish_message(Mqtt::discovery_prefix() + topic, "", true); // publish with retain to remove from broker } else { - queue_publish_message(topic, "", true); // publish with retain to remove from broker + return queue_publish_message(topic, "", true); // publish with retain to remove from broker } } // queue a Home Assistant config topic and payload, with retain flag off. -void Mqtt::queue_ha(const char * topic, const JsonObject & payload) { +bool Mqtt::queue_ha(const char * topic, const JsonObject & payload) { if (!enabled()) { - return; + return false; } std::string payload_text; payload_text.reserve(measureJson(payload) + 1); - serializeJson(payload, payload_text); // convert json to string + serializeJson(payload, payload_text); // convert json to string - queue_publish_message(Mqtt::discovery_prefix() + topic, payload_text, true); // with retain true -} - -// take top from queue and perform the publish or subscribe action -// assumes there is an MQTT connection -void Mqtt::process_queue() { - if (mqtt_messages_.empty()) { - return; - } - - // fetch first from queue and create the full topic name - auto mqtt_message = mqtt_messages_.front(); - auto message = mqtt_message.content_; - char topic[MQTT_TOPIC_MAX_SIZE]; - - if (message->topic.find(discovery_prefix_) == 0) { - strlcpy(topic, message->topic.c_str(), sizeof(topic)); // leave topic as it is - } else { - // it's a discovery topic, added the mqtt base to the topic path - snprintf(topic, MQTT_TOPIC_MAX_SIZE, "%s/%s", mqtt_base_.c_str(), message->topic.c_str()); // uses base - } - - // if this has already been published and we're waiting for an ACK, don't publish again - // it will have a real packet ID - if (mqtt_message.packet_id_ > 0) { - LOG_DEBUG("Waiting for QOS-ACK"); - // if we don't get the ack within 10 seconds, republish with new packet_id - if (uuid::get_uptime_sec() - last_publish_queue_ < 10) { - return; - } - } - last_publish_queue_ = uuid::get_uptime_sec(); - - // if we're subscribing... - if (message->operation == Operation::SUBSCRIBE) { - LOG_DEBUG("Subscribing to topic '%s'", topic); - uint16_t packet_id = mqttClient_->subscribe(topic, mqtt_qos_); - if (!packet_id) { - if (++mqtt_messages_.front().retry_count_ < MQTT_PUBLISH_MAX_RETRY) { - return; - } - LOG_ERROR("Error subscribing to topic '%s'", topic); - mqtt_publish_fails_++; // increment failure counter - } - - mqtt_messages_.pop_front(); // remove the message from the queue - - return; - } - - // if we're unsubscribing... - if (message->operation == Operation::UNSUBSCRIBE) { - LOG_DEBUG("Subscribing to topic '%s'", topic); - uint16_t packet_id = mqttClient_->unsubscribe(topic); - if (!packet_id) { - if (++mqtt_messages_.front().retry_count_ < MQTT_PUBLISH_MAX_RETRY) { - return; - } - LOG_ERROR("Error unsubscribing to topic '%s'", topic); - mqtt_publish_fails_++; // increment failure counter - } - - mqtt_messages_.pop_front(); // remove the message from the queue - - return; - } - - // else try and publish it - // this is where the *real* publish happens - // uint16_t packet_id = mqttClient_->publish(topic, mqtt_qos_, message->retain, message->payload.c_str(), message->payload.size(), false, mqtt_message.id_); - uint16_t packet_id = mqttClient_->publish(topic, mqtt_qos_, message->retain, message->payload.c_str()); - lasttopic_ = topic; - lastpayload_ = message->payload; - - LOG_DEBUG("Published topic %s (#%02d, retain=%d, retry=%d, size=%d, pid=%d)", - topic, - mqtt_message.id_, - message->retain, - mqtt_message.retry_count_ + 1, - message->payload.size(), - packet_id); - - /* - if (!message->payload.empty()) { - LOG_DEBUG("Payload: %s", message->payload.c_str()); // extra message for #784 - } - */ - - if (packet_id == 0) { - // it failed. if we retried n times, give up. remove from queue - if (mqtt_message.retry_count_ == (MQTT_PUBLISH_MAX_RETRY - 1)) { - LOG_ERROR("Failed to publish to %s after %d attempts", topic, mqtt_message.retry_count_ + 1); - mqtt_publish_fails_++; // increment failure counter - mqtt_messages_.pop_front(); // delete - return; - } else { - // update the record - mqtt_messages_.front().retry_count_++; - LOG_DEBUG("Failed to publish to %s. Trying again, #%d", topic, mqtt_message.retry_count_ + 1); - return; // leave on queue for next time so it gets republished - } - } - - // if we have ACK set with QOS 1 or 2, leave on queue and let the ACK process remove it - // but add the packet_id so we can check it later - if (mqtt_qos_ != 0) { - mqtt_messages_.front().packet_id_ = packet_id; - LOG_DEBUG("Setting packetID for ACK to %d", packet_id); - return; - } - - mqtt_messages_.pop_front(); // remove the message from the queue + return queue_publish_message(Mqtt::discovery_prefix() + topic, payload_text, true); // with retain true } // create's a ha sensor config topic from a device value object // and also takes a flag (create_device_config) used to also create the main HA device config. This is only needed for one entity -void Mqtt::publish_ha_sensor_config(DeviceValue & dv, const char * model, const char * brand, const bool remove, const bool create_device_config) { +bool Mqtt::publish_ha_sensor_config(DeviceValue & dv, const char * model, const char * brand, const bool remove, const bool create_device_config) { StaticJsonDocument dev_json; // always create the ids @@ -934,38 +737,39 @@ void Mqtt::publish_ha_sensor_config(DeviceValue & dv, const char * model, const // unless the entity has been marked as read-only and so it'll default to using the sensor/ type bool has_cmd = dv.has_cmd && !dv.has_state(DeviceValueState::DV_READONLY); - publish_ha_sensor_config(dv.type, - dv.tag, - dv.get_fullname().c_str(), - (dv.fullname ? dv.fullname[0] : nullptr), // EN name - dv.device_type, - dv.short_name, - dv.uom, - remove, - has_cmd, - dv.options, - dv.options_size, - dv_set_min, - dv_set_max, - dv.numeric_operator, - dev_json.as()); + return publish_ha_sensor_config(dv.type, + dv.tag, + dv.get_fullname().c_str(), + (dv.fullname ? dv.fullname[0] : nullptr), // EN name + dv.device_type, + dv.short_name, + dv.uom, + remove, + has_cmd, + dv.options, + dv.options_size, + dv_set_min, + dv_set_max, + dv.numeric_operator, + dev_json.as()); } // publish HA sensor for System using the heartbeat tag -void Mqtt::publish_system_ha_sensor_config(uint8_t type, const char * name, const char * entity, const uint8_t uom) { +bool Mqtt::publish_system_ha_sensor_config(uint8_t type, const char * name, const char * entity, const uint8_t uom) { StaticJsonDocument doc; JsonObject dev_json = doc.createNestedObject("dev"); JsonArray ids = dev_json.createNestedArray("ids"); ids.add("ems-esp"); - publish_ha_sensor_config(type, DeviceValueTAG::TAG_HEARTBEAT, name, name, EMSdevice::DeviceType::SYSTEM, entity, uom, false, false, nullptr, 0, 0, 0, 0, dev_json); + return publish_ha_sensor_config( + type, DeviceValueTAG::TAG_HEARTBEAT, name, name, EMSdevice::DeviceType::SYSTEM, entity, uom, false, false, nullptr, 0, 0, 0, 0, dev_json); } // MQTT discovery configs // entity must match the key/value pair in the *_data topic // note: some extra string copying done here, it looks messy but does help with heap fragmentation issues -void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdevice::DeviceValueType +bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdevice::DeviceValueType uint8_t tag, // EMSdevice::DeviceValueTAG const char * const fullname, // fullname, already translated const char * const en_name, // original name in english @@ -982,7 +786,7 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev const JsonObject & dev_json) { // ignore if name (fullname) is empty if (!fullname || !en_name) { - return; + return false; } // create the device name @@ -1074,8 +878,7 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev // https://github.com/emsesp/EMS-ESP32/issues/196 if (remove) { LOG_DEBUG("Queuing removing topic %s", topic); - queue_remove_topic(topic); - return; + return queue_remove_topic(topic); } // build the payload @@ -1305,10 +1108,10 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev add_avty_to_doc(stat_t, doc.as(), val_cond); // TODO queue it or send it directly via publish? - queue_ha(topic, doc.as()); + return queue_ha(topic, doc.as()); } -void Mqtt::publish_ha_climate_config(const uint8_t tag, const bool has_roomtemp, const bool remove, const int16_t min, const uint16_t max) { +bool Mqtt::publish_ha_climate_config(const uint8_t tag, const bool has_roomtemp, const bool remove, const int16_t min, const uint16_t max) { uint8_t hc_num = tag - DeviceValueTAG::TAG_HC1 + 1; char topic[Mqtt::MQTT_TOPIC_MAX_SIZE]; @@ -1329,8 +1132,7 @@ void Mqtt::publish_ha_climate_config(const uint8_t tag, const bool has_roomtemp, snprintf(topic, sizeof(topic), "climate/%s/thermostat_hc%d/config", mqtt_basename_.c_str(), hc_num); if (remove) { - queue_remove_topic(topic); // publish empty payload with retain flag - return; + return queue_remove_topic(topic); // publish empty payload with retain flag } if (Mqtt::is_nested()) { @@ -1415,7 +1217,7 @@ void Mqtt::publish_ha_climate_config(const uint8_t tag, const bool has_roomtemp, // add "availability" section add_avty_to_doc(topic_t, doc.as(), seltemp_cond, has_roomtemp ? currtemp_cond : nullptr, hc_mode_cond); - queue_ha(topic, doc.as()); // publish the config payload with retain flag + return queue_ha(topic, doc.as()); // publish the config payload with retain flag } // based on the device and tag, create the MQTT topic name (without the basename) diff --git a/src/mqtt.h b/src/mqtt.h index ad77fb53b..a6f54ee8d 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -31,26 +31,8 @@ using uuid::console::Shell; namespace emsesp { -// size of queue -static constexpr uint16_t MAX_MQTT_MESSAGES = 300; - using mqtt_sub_function_p = std::function; -struct MqttMessage { - const uint8_t operation; - const std::string topic; - const std::string payload; - const bool retain; - - MqttMessage(const uint8_t operation, const std::string & topic, const std::string & payload, bool retain) - : operation(operation) - , topic(topic) - , payload(payload) - , retain(retain) { - } - ~MqttMessage() = default; -}; - class Mqtt { public: enum discoveryType : uint8_t { HOMEASSISTANT, DOMOTICZ }; @@ -77,24 +59,23 @@ class Mqtt { static void on_connect(); static void on_disconnect(espMqttClientTypes::DisconnectReason reason); - static void subscribe(const uint8_t device_type, const std::string & topic, mqtt_sub_function_p cb); static void subscribe(const std::string & topic); static void resubscribe(); - static void queue_publish(const std::string & topic, const std::string & payload); - static void queue_publish(const char * topic, const char * payload); - static void queue_publish(const std::string & topic, const JsonObject & payload); - static void queue_publish(const char * topic, const JsonObject & payload); - static void queue_publish(const char * topic, const std::string & payload); - static void queue_publish_retain(const std::string & topic, const JsonObject & payload, bool retain); - static void queue_publish_retain(const char * topic, const std::string & payload, bool retain); - static void queue_publish_retain(const char * topic, const JsonObject & payload, bool retain); - static void queue_ha(const char * topic, const JsonObject & payload); - static void queue_remove_topic(const char * topic); + static bool queue_publish(const std::string & topic, const std::string & payload); + static bool queue_publish(const char * topic, const char * payload); + static bool queue_publish(const std::string & topic, const JsonObject & payload); + static bool queue_publish(const char * topic, const JsonObject & payload); + static bool queue_publish(const char * topic, const std::string & payload); + static bool queue_publish_retain(const std::string & topic, const JsonObject & payload, const bool retain); + static bool queue_publish_retain(const char * topic, const std::string & payload, const bool retain); + static bool queue_publish_retain(const char * topic, const JsonObject & payload, const bool retain); + static bool queue_ha(const char * topic, const JsonObject & payload); + static bool queue_remove_topic(const char * topic); - static void publish_ha_sensor_config(DeviceValue & dv, const char * model, const char * brand, const bool remove, const bool create_device_config = false); - static void publish_ha_sensor_config(uint8_t type, + static bool publish_ha_sensor_config(DeviceValue & dv, const char * model, const char * brand, const bool remove, const bool create_device_config = false); + static bool publish_ha_sensor_config(uint8_t type, uint8_t tag, const char * const fullname, const char * const en_name, @@ -110,8 +91,8 @@ class Mqtt { const int8_t num_op, const JsonObject & dev_json); - static void publish_system_ha_sensor_config(uint8_t type, const char * name, const char * entity, const uint8_t uom); - static void publish_ha_climate_config(const uint8_t tag, const bool has_roomtemp, const bool remove = false, const int16_t min = 5, const uint16_t max = 30); + static bool publish_system_ha_sensor_config(uint8_t type, const char * name, const char * entity, const uint8_t uom); + static bool publish_ha_climate_config(const uint8_t tag, const bool has_roomtemp, const bool remove = false, const int16_t min = 5, const uint16_t max = 30); static void show_topic_handlers(uuid::console::Shell & shell, const uint8_t device_type); static void show_mqtt(uuid::console::Shell & shell); @@ -171,7 +152,7 @@ class Mqtt { } static uint32_t publish_queued() { - return mqtt_messages_.size(); + return queuecount_; } static uint8_t connect_count() { @@ -240,47 +221,24 @@ class Mqtt { mqtt_retain_ = mqtt_retain; } - static bool is_empty() { - return mqtt_messages_.empty(); - } - static std::string tag_to_topic(uint8_t device_type, uint8_t tag); static void add_avty_to_doc(const char * state_t, const JsonObject & doc, const char * cond1 = nullptr, const char * cond2 = nullptr, const char * negcond = nullptr); - struct QueuedMqttMessage { - const uint32_t id_; - const std::shared_ptr content_; - uint8_t retry_count_ = 0; - uint16_t packet_id_ = 0; - - ~QueuedMqttMessage() = default; - QueuedMqttMessage(uint32_t id, std::shared_ptr && content) - : id_(id) - , content_(std::move(content)) { - } - }; - static std::deque mqtt_messages_; - - private: static uuid::log::Logger logger_; static espMqttClient * mqttClient_; - static uint32_t mqtt_message_id_; + static uint32_t mqtt_message_id_; - static constexpr uint32_t MQTT_PUBLISH_WAIT = 100; // delay in ms between sending publishes, to account for large payloads - static constexpr uint8_t MQTT_PUBLISH_MAX_RETRY = 3; // max retries for giving up on publishing - - static void queue_message(const uint8_t operation, const std::string & topic, const std::string & payload, bool retain); - static void queue_publish_message(const std::string & topic, const std::string & payload, bool retain); + static bool queue_message(const uint8_t operation, const std::string & topic, const std::string & payload, const bool retain); + static bool queue_publish_message(const std::string & topic, const std::string & payload, const bool retain); static void queue_subscribe_message(const std::string & topic); static void queue_unsubscribe_message(const std::string & topic); void on_publish(uint16_t packetId) const; void on_message(const char * topic, const char * payload, size_t len) const; - void process_queue(); // function handlers for MQTT subscriptions struct MQTTSubFunction { @@ -310,6 +268,7 @@ class Mqtt { static bool connecting_; static bool initialized_; static uint32_t mqtt_publish_fails_; + static uint16_t queuecount_; static uint8_t connectcount_; static bool ha_climate_reset_; From 81e5ee836434f8f50dc22382b9ee7e6c4bf67482 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Mon, 5 Jun 2023 19:16:43 +0200 Subject: [PATCH 07/18] fix typo ventMode --- src/devices/ventilation.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/devices/ventilation.cpp b/src/devices/ventilation.cpp index f68059200..0c81519ef 100644 --- a/src/devices/ventilation.cpp +++ b/src/devices/ventilation.cpp @@ -44,7 +44,7 @@ Ventilation::Ventilation(uint8_t device_type, uint8_t device_id, uint8_t product register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, &ventInSpeed_, DeviceValueType::UINT, FL_(ventInSpeed), DeviceValueUOM::PERCENT); register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, &ventOutSpeed_, DeviceValueType::UINT, FL_(ventOutSpeed), DeviceValueUOM::PERCENT); register_device_value( - DeviceValueTAG::TAG_DEVICE_DATA, &mode_, DeviceValueType::ENUM, FL_(enum_ventMode), FL_(ventInSpeed), DeviceValueUOM::NONE, MAKE_CF_CB(set_ventMode)); + DeviceValueTAG::TAG_DEVICE_DATA, &mode_, DeviceValueType::ENUM, FL_(enum_ventMode), FL_(ventMode), DeviceValueUOM::NONE, MAKE_CF_CB(set_ventMode)); register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, &voc_, DeviceValueType::USHORT, FL_(airquality), DeviceValueUOM::NONE); register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, &bypass_, DeviceValueType::BOOL, FL_(bypass), DeviceValueUOM::NONE, MAKE_CF_CB(set_bypass)); } From d17705db6cf81cd326c7ee78e998e505c85c2860 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Fri, 23 Jun 2023 07:04:59 +0200 Subject: [PATCH 08/18] thermostat hpmodes --- src/devices/thermostat.cpp | 108 +++++++++++++++++++++++++++++++++++++ src/devices/thermostat.h | 14 +++++ src/locale_common.h | 1 + src/locale_translations.h | 7 ++- 4 files changed, 129 insertions(+), 1 deletion(-) diff --git a/src/devices/thermostat.cpp b/src/devices/thermostat.cpp index 6025ded43..cf68370e6 100644 --- a/src/devices/thermostat.cpp +++ b/src/devices/thermostat.cpp @@ -136,6 +136,8 @@ Thermostat::Thermostat(uint8_t device_type, uint8_t device_id, uint8_t product_i summer_typeids = {0x02AF, 0x02B0, 0x02B1, 0x02B2, 0x02B3, 0x02B4, 0x02B5, 0x02B6}; curve_typeids = {0x029B, 0x029C, 0x029D, 0x029E, 0x029F, 0x02A0, 0x02A1, 0x02A2}; summer2_typeids = {0x0471, 0x0472, 0x0473, 0x0474, 0x0475, 0x0476, 0x0477, 0x0478}; + hp_typeids = {0x0467, 0x0468, 0x0469, 0x046A}; + hpmode_typeids = {0x0291, 0x0292, 0x0293, 0x0294}; for (uint8_t i = 0; i < monitor_typeids.size(); i++) { register_telegram_type(monitor_typeids[i], "RC300Monitor", false, MAKE_PF_CB(process_RC300Monitor)); register_telegram_type(set_typeids[i], "RC300Set", false, MAKE_PF_CB(process_RC300Set)); @@ -145,6 +147,8 @@ Thermostat::Thermostat(uint8_t device_type, uint8_t device_id, uint8_t product_i } for (uint8_t i = 0; i < set2_typeids.size(); i++) { register_telegram_type(set2_typeids[i], "RC300Set2", false, MAKE_PF_CB(process_RC300Set2)); + register_telegram_type(hp_typeids[i], "HPSet", false, MAKE_PF_CB(process_HPSet)); + register_telegram_type(hpmode_typeids[i], "HPMode", true, MAKE_PF_CB(process_HPMode)); } register_telegram_type(0x2F5, "RC300WWmode", true, MAKE_PF_CB(process_RC300WWmode)); register_telegram_type(0x31B, "RC300WWtemp", true, MAKE_PF_CB(process_RC300WWtemp)); @@ -306,6 +310,25 @@ std::shared_ptr Thermostat::heating_circuit(std::sha } } + // not found, search heatpump message types + if (hc_num == 0) { + for (uint8_t i = 0; i < hp_typeids.size(); i++) { + if (hp_typeids[i] == telegram->type_id) { + hc_num = i + 1; + break; + } + } + } + + if (hc_num == 0) { + for (uint8_t i = 0; i < hpmode_typeids.size(); i++) { + if (hpmode_typeids[i] == telegram->type_id) { + hc_num = i + 1; + break; + } + } + } + // not found, search device-id types for remote thermostats if (hc_num == 0 && telegram->src >= 0x18 && telegram->src <= 0x1F) { hc_num = telegram->src - 0x17; @@ -383,6 +406,9 @@ std::shared_ptr Thermostat::heating_circuit(std::sha if (summer2_typeids.size()) { toggle_fetch(summer2_typeids[hc_num - 1], toggle_); } + if (hp_typeids.size()) { + toggle_fetch(hp_typeids[hc_num - 1], toggle_); + } return new_hc; // return back point to new HC object } @@ -1128,6 +1154,26 @@ void Thermostat::process_RC300Floordry(std::shared_ptr telegram) has_update(telegram, floordrytemp_, 1); } +// 0x291 ff. HP mode +void Thermostat::process_HPMode(std::shared_ptr telegram) { + std::shared_ptr hc = heating_circuit(telegram); + if (hc == nullptr) { + return; + } + has_update(telegram, hc->hpmode, 0); +} + +// 0x467 ff HP settings +void Thermostat::process_HPSet(std::shared_ptr telegram) { + std::shared_ptr hc = heating_circuit(telegram); + if (hc == nullptr) { + return; + } + has_update(telegram, hc->dewoffset, 0); // 7-35°C + has_update(telegram, hc->roomtempdiff, 3); // 1-10K + has_update(telegram, hc->hpminflowtemp, 4); // 2-10K +} + // type 0x41 - data from the RC30 thermostat(0x10) - 14 bytes long // RC30Monitor(0x41), data: 80 20 00 AC 00 00 00 02 00 05 09 00 AC 00 void Thermostat::process_RC30Monitor(std::shared_ptr telegram) { @@ -1447,6 +1493,62 @@ void Thermostat::process_RCErrorMessage(std::shared_ptr telegram * */ +// hp mode RC300 +bool Thermostat::set_roomtempdiff(const char * value, const int8_t id) { + uint8_t hc_num = (id == -1) ? AUTO_HEATING_CIRCUIT : id; + std::shared_ptr hc = heating_circuit(hc_num); + if (hc == nullptr) { + return false; + } + int v; + if (Helpers::value2number(value, v)) { + write_command(hp_typeids[hc->hc()], 3, v, hp_typeids[hc->hc()]); + return true; + } + return false; +} +bool Thermostat::set_dewoffset(const char * value, const int8_t id) { + uint8_t hc_num = (id == -1) ? AUTO_HEATING_CIRCUIT : id; + std::shared_ptr hc = heating_circuit(hc_num); + if (hc == nullptr) { + return false; + } + int v; + if (Helpers::value2number(value, v)) { + write_command(hp_typeids[hc->hc()], 4, v, hp_typeids[hc->hc()]); + return true; + } + return false; +} + +bool Thermostat::set_hpminflowtemp(const char * value, const int8_t id) { + uint8_t hc_num = (id == -1) ? AUTO_HEATING_CIRCUIT : id; + std::shared_ptr hc = heating_circuit(hc_num); + if (hc == nullptr) { + return false; + } + int v; + if (Helpers::value2temperature(value, v)) { + write_command(hp_typeids[hc->hc()], 0, v, hp_typeids[hc->hc()]); + return true; + } + return false; +} + +bool Thermostat::set_hpmode(const char * value, const int8_t id) { + uint8_t hc_num = (id == -1) ? AUTO_HEATING_CIRCUIT : id; + std::shared_ptr hc = heating_circuit(hc_num); + if (hc == nullptr) { + return false; + } + uint8_t v; + if (!Helpers::value2enum(value, v, FL_(enum_hpmode))) { + return false; + } + write_command(hpmode_typeids[hc->hc()], 0, v, hpmode_typeids[hc->hc()]); + return true; +} + // 0xBB Hybrid pump bool Thermostat::set_hybridStrategy(const char * value, const int8_t id) { uint8_t v; @@ -4076,6 +4178,12 @@ void Thermostat::register_device_values_hc(std::shared_ptrnoreducetemp, DeviceValueType::INT, FL_(noreducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_noreducetemp)); register_device_value(tag, &hc->reducetemp, DeviceValueType::INT, FL_(reducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_reducetemp)); register_device_value(tag, &hc->wwprio, DeviceValueType::BOOL, FL_(wwprio), DeviceValueUOM::NONE, MAKE_CF_CB(set_wwprio)); + + register_device_value(tag, &hc->hpmode, DeviceValueType::ENUM, FL_(enum_hpmode), FL_(hpmode), DeviceValueUOM::NONE, MAKE_CF_CB(set_hpmode)); + register_device_value(tag, &hc->dewoffset, DeviceValueType::UINT, FL_(dewoffset), DeviceValueUOM::K, MAKE_CF_CB(set_dewoffset)); + register_device_value(tag, &hc->roomtempdiff, DeviceValueType::UINT, FL_(roomtempdiff), DeviceValueUOM::K, MAKE_CF_CB(set_roomtempdiff)); + register_device_value(tag, &hc->hpminflowtemp, DeviceValueType::UINT, FL_(hpminflowtemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_hpminflowtemp)); + break; case EMS_DEVICE_FLAG_CRF: register_device_value(tag, &hc->mode, DeviceValueType::ENUM, FL_(enum_mode5), FL_(mode), DeviceValueUOM::NONE); diff --git a/src/devices/thermostat.h b/src/devices/thermostat.h index a73abd26b..b347ac411 100644 --- a/src/devices/thermostat.h +++ b/src/devices/thermostat.h @@ -89,6 +89,11 @@ class Thermostat : public EMSdevice { uint16_t reduceminutes; // remaining minutes to night->day // FW100 temperature uint8_t roomsensor; // 1-intern, 2-extern, 3-autoselect the lower value + // hp + uint8_t dewoffset; + uint8_t roomtempdiff; + uint8_t hpminflowtemp; + uint8_t hpmode; uint8_t hc_num() const { return hc_num_; @@ -170,6 +175,8 @@ class Thermostat : public EMSdevice { std::vector summer_typeids; std::vector summer2_typeids; std::vector curve_typeids; + std::vector hp_typeids; + std::vector hpmode_typeids; // standard for all thermostats char status_[20]; // online or offline @@ -392,6 +399,8 @@ class Thermostat : public EMSdevice { void process_RemoteTemp(std::shared_ptr telegram); void process_RemoteHumidity(std::shared_ptr telegram); void process_RemoteCorrection(std::shared_ptr telegram); + void process_HPSet(std::shared_ptr telegram); + void process_HPMode(std::shared_ptr telegram); // internal helper functions bool set_mode_n(const uint8_t mode, const uint8_t hc_num); @@ -551,6 +560,11 @@ class Thermostat : public EMSdevice { bool set_pvEnableWw(const char * value, const int8_t id); bool set_pvRaiseHeat(const char * value, const int8_t id); bool set_pvLowerCool(const char * value, const int8_t id); + + bool set_roomtempdiff(const char * value, const int8_t id); + bool set_dewoffset(const char * value, const int8_t id); + bool set_hpminflowtemp(const char * value, const int8_t id); + bool set_hpmode(const char * value, const int8_t id); }; } // namespace emsesp diff --git a/src/locale_common.h b/src/locale_common.h index afab14406..3e373dab1 100644 --- a/src/locale_common.h +++ b/src/locale_common.h @@ -292,6 +292,7 @@ MAKE_ENUM(enum_summermode, FL_(summer), FL_(auto), FL_(winter)) MAKE_ENUM(enum_hpoperatingmode, FL_(off), FL_(auto), FL_(heating), FL_(cooling)) MAKE_ENUM(enum_summer, FL_(winter), FL_(summer)) MAKE_ENUM(enum_operatingstate, FL_(heating), FL_(off), FL_(cooling)) +MAKE_ENUM(enum_hpmode, FL_(heating), FL_(cooling), FL_(heatandcool)) MAKE_ENUM(enum_mode, FL_(manual), FL_(auto)) // RC100, RC300, RC310 MAKE_ENUM(enum_mode2, FL_(off), FL_(manual), FL_(auto)) // RC20, RC30 diff --git a/src/locale_translations.h b/src/locale_translations.h index baa571cdd..c8b8e0c48 100644 --- a/src/locale_translations.h +++ b/src/locale_translations.h @@ -178,6 +178,7 @@ MAKE_WORD_TRANSLATION(layeredbuffer, "layered buffer", "Schichtspeicher", "Gelaa MAKE_WORD_TRANSLATION(maintenance, "maintenance", "Wartung", "Onderhoud", "UnderhÃ¥ll", "przeglÄ…d", "vedlikehold", "maintenance", "bakım") MAKE_WORD_TRANSLATION(heating, "heating", "Heizen", "Verwarmen", "Uppvärmning", "ogrzewanie", "oppvarming", "chauffage", "ısıtma") MAKE_WORD_TRANSLATION(cooling, "cooling", "Kühlen", "Koelen", "Kyler", "chÅ‚odzenie", "kjøling", "refroidissement", "soÄŸuma") +MAKE_WORD_TRANSLATION(heatandcool, "heating&cooling", "Heizen&Kühlen", "Verwarmen&Koelen", "Uppvärmning&Kyler") // TODO translate MAKE_WORD_TRANSLATION(disinfecting, "disinfecting", "Desinfizieren", "Desinfecteren", "Desinficerar", "dezynfekcja termiczna", "desinfisering", "désinfection", "dezenfeksiyon") MAKE_WORD_TRANSLATION(no_heat, "no heat", "keine Wärme", "Geen warmte", "Ingen värme", "brak ciepÅ‚a", "ingen varme", "pas de chauffage", "ısınma yok") MAKE_WORD_TRANSLATION(heatrequest, "heat request", "Wärmeanforderung", "Verwarmignsverzoek", "VärmeförfrÃ¥gan", "zapotrzebowanie na ciepÅ‚o", "varmeforespørsel", "demande de chauffage", "ısınma ihtiyacı") @@ -574,7 +575,6 @@ MAKE_TRANSLATION(mixingvalves, "mixingvalves", "mixing valves", "Mischventile", MAKE_TRANSLATION(pvEnableWw, "pvenableww", "enable raise dhw", "aktiviere Anhebung WW", "", "", "podwyższenie c.w.u. z PV", "aktivere hevet temperatur bereder", "", "") // TODO translate MAKE_TRANSLATION(pvRaiseHeat, "pvraiseheat", "raise heating with PV", "Anhebung Heizen mit PV", "", "", "podwyższenie grzania z PV", "heve varmen med solpanel", "", "") // TODO translate MAKE_TRANSLATION(pvLowerCool, "pvlowercool", "lower cooling with PV", "Kühlabsenkung mit PV", "", "", "obniżenie chÅ‚odzenia z PV", "nedre kjøling solpanel", "", "") // TODO translate - // thermostat ww MAKE_TRANSLATION(wwMode, "wwmode", "mode", "Modus", "Modus", "Läge", "tryb pracy", "modus", "mode", "") MAKE_TRANSLATION(wwSetTempLow, "wwsettemplow", "set low temperature", "untere Solltemperatur", "Onderste streeftemperatuur", "Nedre Börvärde", "zadana temperatura obniżona", "nedre settverdi", "réglage température basse", "") @@ -649,6 +649,11 @@ MAKE_TRANSLATION(reducehours, "reducehours", "duration for nighttemp", "Dauer Na MAKE_TRANSLATION(reduceminutes, "reduceminutes", "remaining time for nightmode", "Restzeit Nachttemp.", "Resterende tijd nachtverlaging", "Ã…terstÃ¥ende Tid Nattläge", "czas do koÅ„ca trybu nocnego", "gjenværende tid i nattstilling", "temps restant mode nuit", "") MAKE_TRANSLATION(switchonoptimization, "switchonoptimization", "switch-on optimization", "Einschaltoptimierung", "Inschakeloptimalisering", "Växlingsoptimering", "optymalizacja załączania", "slÃ¥ pÃ¥ optimalisering", "optimisation mise en marche", "") +MAKE_TRANSLATION(hpmode, "hpmode", "HP Mode", "WP Modus") +MAKE_TRANSLATION(dewoffset, "dewoffset", "dew point offset", "Taupunkt Differenz") +MAKE_TRANSLATION(roomtempdiff, "roomtempdiff", "room temp difference", "Raumtemperatur Differenz") +MAKE_TRANSLATION(hpminflowtemp, "hpminflowtemp", "HP min. flow temp.", "WP minimale Vorlauftemperatur") + // heatpump MAKE_TRANSLATION(airHumidity, "airhumidity", "relative air humidity", "relative Luftfeuchte", "Relatieve luchtvochtigheid", "Relativ Luftfuktighet", "wilgotność wzglÄ™dna w pomieszczeniu", "luftfuktighet", "humidité relative air", "") MAKE_TRANSLATION(dewTemperature, "dewtemperature", "dew point temperature", "Taupunkttemperatur", "Dauwpunttemperatuur", "Daggpunkt", "punkt rosy w pomieszczeniu", "duggtemperatur", "température point rosée", "") From f4cae5027e63fb91173aa2333ee1a834fe9733d4 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sun, 25 Jun 2023 08:53:20 +0200 Subject: [PATCH 09/18] update packages --- interface/package.json | 24 ++-- interface/yarn.lock | 308 ++++++++++++++++++++++------------------- 2 files changed, 178 insertions(+), 154 deletions(-) diff --git a/interface/package.json b/interface/package.json index 067bb18f1..68f823f93 100644 --- a/interface/package.json +++ b/interface/package.json @@ -19,15 +19,15 @@ "lint": "eslint . --cache --fix" }, "dependencies": { - "@emotion/react": "^11.11.0", + "@emotion/react": "^11.11.1", "@emotion/styled": "^11.11.0", "@mui/icons-material": "^5.11.16", - "@mui/material": "^5.13.3", + "@mui/material": "^5.13.6", "@table-library/react-table-library": "4.1.4", "@types/lodash-es": "^4.17.7", - "@types/node": "^20.2.5", - "@types/react": "^18.2.8", - "@types/react-dom": "^18.2.4", + "@types/node": "^20.3.1", + "@types/react": "^18.2.14", + "@types/react-dom": "^18.2.6", "@types/react-router-dom": "^5.3.3", "async-validator": "^4.2.5", "axios": "^1.4.0", @@ -37,18 +37,18 @@ "react": "latest", "react-dom": "latest", "react-dropzone": "^14.2.3", - "react-icons": "^4.9.0", - "react-router-dom": "^6.11.2", + "react-icons": "^4.10.1", + "react-router-dom": "^6.14.0", "react-toastify": "^9.1.3", "sockette": "^2.0.6", "typesafe-i18n": "^5.24.3", "typescript": "^5.1.3" }, "devDependencies": { - "@typescript-eslint/eslint-plugin": "^5.59.8", - "@typescript-eslint/parser": "^5.59.8", + "@typescript-eslint/eslint-plugin": "^5.60.0", + "@typescript-eslint/parser": "^5.60.0", "@vitejs/plugin-react-swc": "^3.3.2", - "eslint": "^8.42.0", + "eslint": "^8.43.0", "eslint-config-airbnb": "^19.0.4", "eslint-config-airbnb-typescript": "^17.0.0", "eslint-config-prettier": "^8.8.0", @@ -62,8 +62,8 @@ "nodemon": "^2.0.22", "npm-run-all": "^4.1.5", "prettier": "^2.8.8", - "rollup-plugin-visualizer": "^5.9.0", - "terser": "^5.17.7", + "rollup-plugin-visualizer": "^5.9.2", + "terser": "^5.18.1", "vite": "^4.3.9", "vite-plugin-svgr": "^3.2.0", "vite-tsconfig-paths": "^4.2.0" diff --git a/interface/yarn.lock b/interface/yarn.lock index 835d157aa..8e5fee1d2 100644 --- a/interface/yarn.lock +++ b/interface/yarn.lock @@ -227,6 +227,15 @@ __metadata: languageName: node linkType: hard +"@babel/runtime@npm:^7.22.5": + version: 7.22.5 + resolution: "@babel/runtime@npm:7.22.5" + dependencies: + regenerator-runtime: ^0.13.11 + checksum: 11dcaeecd2246857ccf22f939fcae28a58d29e410607bfa28b95d9b03e298a3e3df8a530e22637d5bfccfc1661fb39cc50c06b404b5d53454bd93889c7dd3eb8 + languageName: node + linkType: hard + "@babel/template@npm:^7.20.7": version: 7.20.7 resolution: "@babel/template@npm:7.20.7" @@ -333,9 +342,9 @@ __metadata: languageName: node linkType: hard -"@emotion/react@npm:^11.11.0": - version: 11.11.0 - resolution: "@emotion/react@npm:11.11.0" +"@emotion/react@npm:^11.11.1": + version: 11.11.1 + resolution: "@emotion/react@npm:11.11.1" dependencies: "@babel/runtime": ^7.18.3 "@emotion/babel-plugin": ^11.11.0 @@ -350,7 +359,7 @@ __metadata: peerDependenciesMeta: "@types/react": optional: true - checksum: c287fdef680c6cc95c021d2ccd48891052cd97edfe371ef0c0a9aa78f1cb764587c80a50e9f22eb943f522258dc4d7b80c4778c45331720e330e338db32f8a95 + checksum: 1aea4d735b537fbfbeda828bbf929488a7e1b5b7d131f14aeede8737e92bb3b611e15fec353e97f85aed7a65a1c86a695a04ba6e9be905231beef6bd624cb705 languageName: node linkType: hard @@ -613,10 +622,10 @@ __metadata: languageName: node linkType: hard -"@eslint/js@npm:8.42.0": - version: 8.42.0 - resolution: "@eslint/js@npm:8.42.0" - checksum: 4ae46df1f32095cf9527d1f6a8a30512151f8eb66dd883a226face17c9e7cfdd2dcb3d4e3124fb67ac5801e0a776b1d7bba368276cfb1e1e4eefb047e38b41d6 +"@eslint/js@npm:8.43.0": + version: 8.43.0 + resolution: "@eslint/js@npm:8.43.0" + checksum: ff1a1587e8f28c21dda36a331cf70ca16b76e5897cecf10f6b4c326abddf18db565ee5f71feb89cbb0d3d20ff321a2536357562c0233868eed70784640b73cf4 languageName: node linkType: hard @@ -714,15 +723,15 @@ __metadata: languageName: node linkType: hard -"@mui/base@npm:5.0.0-beta.3": - version: 5.0.0-beta.3 - resolution: "@mui/base@npm:5.0.0-beta.3" +"@mui/base@npm:5.0.0-beta.5": + version: 5.0.0-beta.5 + resolution: "@mui/base@npm:5.0.0-beta.5" dependencies: - "@babel/runtime": ^7.21.0 + "@babel/runtime": ^7.22.5 "@emotion/is-prop-valid": ^1.2.1 "@mui/types": ^7.2.4 - "@mui/utils": ^5.13.1 - "@popperjs/core": ^2.11.7 + "@mui/utils": ^5.13.6 + "@popperjs/core": ^2.11.8 clsx: ^1.2.1 prop-types: ^15.8.1 react-is: ^18.2.0 @@ -733,14 +742,14 @@ __metadata: peerDependenciesMeta: "@types/react": optional: true - checksum: d1b0aa82efcca8b22f301082fa4157e6b5e6c233f3b65b6295ff0cf5bd8e60c1b289e6340cf68110b01c76c6da9467a6ad1dfc91f26401ff79ed9a81c9104ae9 + checksum: 3d36edb5648e2e1dfd65f5c832650c0d394f96d5cee6ef638632a0cd3a6540eb654c453c4a168226bef33dc7616c7a7ac67c4a707403b1b4584b7c0f89d4db4f languageName: node linkType: hard -"@mui/core-downloads-tracker@npm:^5.13.3": - version: 5.13.3 - resolution: "@mui/core-downloads-tracker@npm:5.13.3" - checksum: edf92ed962e3e7ab74279db52b8f75393dced39db393a0f7162fc493f3fd9b428353472621839d94ab7c5813b1d4bc71889eb20473ca196106c3654cbd78ad28 +"@mui/core-downloads-tracker@npm:^5.13.4": + version: 5.13.4 + resolution: "@mui/core-downloads-tracker@npm:5.13.4" + checksum: f00f3d086df55531da600b2b58197c4c3d9b586129b00a72a14320492934c098fb1cb1c50a23ea394367c92cac869eab9588bb83885c163c3b0ba0e28c3815ec languageName: node linkType: hard @@ -760,16 +769,16 @@ __metadata: languageName: node linkType: hard -"@mui/material@npm:^5.13.3": - version: 5.13.3 - resolution: "@mui/material@npm:5.13.3" +"@mui/material@npm:^5.13.6": + version: 5.13.6 + resolution: "@mui/material@npm:5.13.6" dependencies: - "@babel/runtime": ^7.21.0 - "@mui/base": 5.0.0-beta.3 - "@mui/core-downloads-tracker": ^5.13.3 - "@mui/system": ^5.13.2 + "@babel/runtime": ^7.22.5 + "@mui/base": 5.0.0-beta.5 + "@mui/core-downloads-tracker": ^5.13.4 + "@mui/system": ^5.13.6 "@mui/types": ^7.2.4 - "@mui/utils": ^5.13.1 + "@mui/utils": ^5.13.6 "@types/react-transition-group": ^4.4.6 clsx: ^1.2.1 csstype: ^3.1.2 @@ -789,7 +798,7 @@ __metadata: optional: true "@types/react": optional: true - checksum: 28d782d9366b5e8209fc1ef7a8eacc46c30366a437994927a029debaf44a99582136d56a734633b833512de888d658deebffae584a52427ecdacdba65c28147f + checksum: 208dc3f77471d823ba83fac62c5752a5e5aadef983419b4dbd5756ac16c2ce91b3405e771dce33fcbbd51459a7ff65e8d02b08c2a46c812f0012dce3dbf93550 languageName: node linkType: hard @@ -831,15 +840,15 @@ __metadata: languageName: node linkType: hard -"@mui/system@npm:^5.13.2": - version: 5.13.2 - resolution: "@mui/system@npm:5.13.2" +"@mui/system@npm:^5.13.6": + version: 5.13.6 + resolution: "@mui/system@npm:5.13.6" dependencies: - "@babel/runtime": ^7.21.0 + "@babel/runtime": ^7.22.5 "@mui/private-theming": ^5.13.1 "@mui/styled-engine": ^5.13.2 "@mui/types": ^7.2.4 - "@mui/utils": ^5.13.1 + "@mui/utils": ^5.13.6 clsx: ^1.2.1 csstype: ^3.1.2 prop-types: ^15.8.1 @@ -855,7 +864,7 @@ __metadata: optional: true "@types/react": optional: true - checksum: 34ebb580e5dd83123cc397c3fd54c3430f66ab715eb1538cf2510821d88249814294f79ea046081b61249643383fd9c23552d9791322855fa2099bf8f1c4e51b + checksum: 10d2e668a09429d55572aa62e033107ead4bb6edf9addb48c9f2aa1c937989f0d7c307f868c4a947408b43fac8ce4f0b3307396b4011a0c31d6c0b844ec0fbf4 languageName: node linkType: hard @@ -886,6 +895,21 @@ __metadata: languageName: node linkType: hard +"@mui/utils@npm:^5.13.6": + version: 5.13.6 + resolution: "@mui/utils@npm:5.13.6" + dependencies: + "@babel/runtime": ^7.22.5 + "@types/prop-types": ^15.7.5 + "@types/react-is": ^18.2.0 + prop-types: ^15.8.1 + react-is: ^18.2.0 + peerDependencies: + react: ^17.0.0 || ^18.0.0 + checksum: 9e82c91ed3ae47705f44ba5b31d50b7c8a9591c855d05764389d0c3cbb10687628e1ffd6af1e04e490d979c4d32c669ddd24bc72643f040550674919ab94a3ec + languageName: node + linkType: hard + "@nodelib/fs.scandir@npm:2.1.5": version: 2.1.5 resolution: "@nodelib/fs.scandir@npm:2.1.5" @@ -947,17 +971,17 @@ __metadata: languageName: node linkType: hard -"@popperjs/core@npm:^2.11.7": - version: 2.11.7 - resolution: "@popperjs/core@npm:2.11.7" - checksum: e3238267a1c010f7e46adf0d1bf2a6530ccba603cce320ed8f8208217b919f0d9b12c45d4aa535d4945ee791a42b6ef3d016ba108e2d962d29cfb3f7bf18bd28 +"@popperjs/core@npm:^2.11.8": + version: 2.11.8 + resolution: "@popperjs/core@npm:2.11.8" + checksum: 4681e682abc006d25eb380d0cf3efc7557043f53b6aea7a5057d0d1e7df849a00e281cd8ea79c902a35a414d7919621fc2ba293ecec05f413598e0b23d5a1e63 languageName: node linkType: hard -"@remix-run/router@npm:1.6.2": - version: 1.6.2 - resolution: "@remix-run/router@npm:1.6.2" - checksum: 73da6884e53873e4290abb3978373cafc3f351994273b0663eda5e12c81cb427fc6fe4df1924569d9a214f701d0106cf37122455951e0239d7e6fa35071df558 +"@remix-run/router@npm:1.7.0": + version: 1.7.0 + resolution: "@remix-run/router@npm:1.7.0" + checksum: 05ac3b300eb6676f359c18280dc3870b3f0dde5553b58d914c2ef9d1c2feae71ad4b81ad8dd3b20182cc8fba40a6f6e5236f68d1fe02989f71cd5b55ae25a75c languageName: node linkType: hard @@ -1281,10 +1305,10 @@ __metadata: languageName: node linkType: hard -"@types/node@npm:^20.2.5": - version: 20.2.5 - resolution: "@types/node@npm:20.2.5" - checksum: 1c3db8a4ceb5e5d12e7cb140e37c14a16ce013084c6d65579b91cefbe0ecaca57d85093d968172b11c3d1d95bcbc5d972b08aa3dc3935206fb39bc6c10751102 +"@types/node@npm:^20.3.1": + version: 20.3.1 + resolution: "@types/node@npm:20.3.1" + checksum: 7e8a6f5d6fc1ad3778f038f5f8df570741459984280fd2e9539af32620d93438c955fd1b90d00f9cc438cd132ec04d7669ada9e32502336e78713a3ad9b51d10 languageName: node linkType: hard @@ -1302,12 +1326,12 @@ __metadata: languageName: node linkType: hard -"@types/react-dom@npm:^18.2.4": - version: 18.2.4 - resolution: "@types/react-dom@npm:18.2.4" +"@types/react-dom@npm:^18.2.6": + version: 18.2.6 + resolution: "@types/react-dom@npm:18.2.6" dependencies: "@types/react": "*" - checksum: dfeaabb4268d39bdd5addc6c0b7099d5c57a364e70f1087b7c3ee189374312dc65201abfd3d87fee0de11d27c225678ce39c22d14b3035cde5792678704c27b5 + checksum: bd734ca04c52b3c96891a7f9c1139486807dac7a2449fb72e8f8e23018bc6eeeb87a490a105cb39d05ccb7ddf80ed7a441e5bd3e5866c6f6ae8870cd723599e8 languageName: node linkType: hard @@ -1361,14 +1385,14 @@ __metadata: languageName: node linkType: hard -"@types/react@npm:^18.2.8": - version: 18.2.8 - resolution: "@types/react@npm:18.2.8" +"@types/react@npm:^18.2.14": + version: 18.2.14 + resolution: "@types/react@npm:18.2.14" dependencies: "@types/prop-types": "*" "@types/scheduler": "*" csstype: ^3.0.2 - checksum: 4ed72e0cb9974f7002ebdd27bdb55beb7e715f18e1f537748f3a64ddd5b11ac25f61287d146feb4e2dd876eb615c49eead8cae65d75d5a4584572c00ab2093c5 + checksum: a728a90e242fb41c233729fa46885cc47aca7df2035ed803f83bf0b582dde81143d465ecbf04a056bc6404f0f746f219d7043245ebd99baf83a178bbbb856c76 languageName: node linkType: hard @@ -1386,14 +1410,14 @@ __metadata: languageName: node linkType: hard -"@typescript-eslint/eslint-plugin@npm:^5.59.8": - version: 5.59.8 - resolution: "@typescript-eslint/eslint-plugin@npm:5.59.8" +"@typescript-eslint/eslint-plugin@npm:^5.60.0": + version: 5.60.0 + resolution: "@typescript-eslint/eslint-plugin@npm:5.60.0" dependencies: "@eslint-community/regexpp": ^4.4.0 - "@typescript-eslint/scope-manager": 5.59.8 - "@typescript-eslint/type-utils": 5.59.8 - "@typescript-eslint/utils": 5.59.8 + "@typescript-eslint/scope-manager": 5.60.0 + "@typescript-eslint/type-utils": 5.60.0 + "@typescript-eslint/utils": 5.60.0 debug: ^4.3.4 grapheme-splitter: ^1.0.4 ignore: ^5.2.0 @@ -1406,43 +1430,43 @@ __metadata: peerDependenciesMeta: typescript: optional: true - checksum: 491f88984dd032f309d84b41af6a227a9da944ad6c806b868e71122bd55ad355d8738e7925019f54929784ba631ae9b186a028b194bdb9bad72d122229c029e4 + checksum: e41d1a45f330e766afb594429fad535f4db06efc458e74cc05109c4555550efdad57141aa088d5cb836aeb90b822e08e7690e5b650fd7b2419da1d64113d5360 languageName: node linkType: hard -"@typescript-eslint/parser@npm:^5.59.8": - version: 5.59.8 - resolution: "@typescript-eslint/parser@npm:5.59.8" +"@typescript-eslint/parser@npm:^5.60.0": + version: 5.60.0 + resolution: "@typescript-eslint/parser@npm:5.60.0" dependencies: - "@typescript-eslint/scope-manager": 5.59.8 - "@typescript-eslint/types": 5.59.8 - "@typescript-eslint/typescript-estree": 5.59.8 + "@typescript-eslint/scope-manager": 5.60.0 + "@typescript-eslint/types": 5.60.0 + "@typescript-eslint/typescript-estree": 5.60.0 debug: ^4.3.4 peerDependencies: eslint: ^6.0.0 || ^7.0.0 || ^8.0.0 peerDependenciesMeta: typescript: optional: true - checksum: 710daf64331d5bc198c22c66c1fdd06db2300487f655d161f0e59971fcd0c70661a7059ff7e3cf2ed66fc72d6674a3f4a317d5d5778ce6605d18e831b0a7039e + checksum: a9b4875a3ed37cfe8205173caf85b21f8025cf21bc295036c6265010ff622054b137fa7f3251476104086804bf55b420431efa887935b67c506800e3adcc8910 languageName: node linkType: hard -"@typescript-eslint/scope-manager@npm:5.59.8": - version: 5.59.8 - resolution: "@typescript-eslint/scope-manager@npm:5.59.8" +"@typescript-eslint/scope-manager@npm:5.60.0": + version: 5.60.0 + resolution: "@typescript-eslint/scope-manager@npm:5.60.0" dependencies: - "@typescript-eslint/types": 5.59.8 - "@typescript-eslint/visitor-keys": 5.59.8 - checksum: 164ea98d0d7dd4dd0c462eb7238266b2260af63fd29b96746dd978322114c7ebf31ba697c424397e3fb36027704e1c5d788cb6049a6ccb52fec8c6c134d7503b + "@typescript-eslint/types": 5.60.0 + "@typescript-eslint/visitor-keys": 5.60.0 + checksum: 87c742ea716359206244e1c7a8d2805b9e1caf04bce127f84b790046ae994849f25bf38af05de7a283eec58b34ecc701f441f23dfcccb59b9185260667bfe6e7 languageName: node linkType: hard -"@typescript-eslint/type-utils@npm:5.59.8": - version: 5.59.8 - resolution: "@typescript-eslint/type-utils@npm:5.59.8" +"@typescript-eslint/type-utils@npm:5.60.0": + version: 5.60.0 + resolution: "@typescript-eslint/type-utils@npm:5.60.0" dependencies: - "@typescript-eslint/typescript-estree": 5.59.8 - "@typescript-eslint/utils": 5.59.8 + "@typescript-eslint/typescript-estree": 5.60.0 + "@typescript-eslint/utils": 5.60.0 debug: ^4.3.4 tsutils: ^3.21.0 peerDependencies: @@ -1450,23 +1474,23 @@ __metadata: peerDependenciesMeta: typescript: optional: true - checksum: 70c64edb564d4c5270cb8cea226544eebcf0f0a394c185a3c5b5bfd9df2e97e0396fa6324ba58da3136d99d12adcfe5f21243aa6c997734e6daa154a6708ae60 + checksum: 0baa4baa9c059e3a0d4da19cb62b821ababce781208cf18965e54916ea718a993d969f8f42b4356409ac1ce74228532e9d1cd0f2e9d3e0815c405467775b4015 languageName: node linkType: hard -"@typescript-eslint/types@npm:5.59.8": - version: 5.59.8 - resolution: "@typescript-eslint/types@npm:5.59.8" - checksum: 3f5000f556b4fe45c16e00b24d18c0f8930a214e61a0302daf3ef952a7a45342d9e63119626bd0556b252a6345e1fa423e34908eaf08560756f6c747dcffb56a +"@typescript-eslint/types@npm:5.60.0": + version: 5.60.0 + resolution: "@typescript-eslint/types@npm:5.60.0" + checksum: 008aedc2322120b9b760204ae26b5ecf5a1a61da84e77427048d076074cef703914a7a2db0286f891bbd045c1246238823671ec97192e03eabec35e9f75288e2 languageName: node linkType: hard -"@typescript-eslint/typescript-estree@npm:5.59.8": - version: 5.59.8 - resolution: "@typescript-eslint/typescript-estree@npm:5.59.8" +"@typescript-eslint/typescript-estree@npm:5.60.0": + version: 5.60.0 + resolution: "@typescript-eslint/typescript-estree@npm:5.60.0" dependencies: - "@typescript-eslint/types": 5.59.8 - "@typescript-eslint/visitor-keys": 5.59.8 + "@typescript-eslint/types": 5.60.0 + "@typescript-eslint/visitor-keys": 5.60.0 debug: ^4.3.4 globby: ^11.1.0 is-glob: ^4.0.3 @@ -1475,35 +1499,35 @@ __metadata: peerDependenciesMeta: typescript: optional: true - checksum: a6ec2654c5c8c0bead99e1a43936a56e9bd0e1422b993fcd9d603ff31fe4701e9562fbc6d0f2b4d50a7d113d27d5fb68c428f78b938bd110b0112ed4df81ed72 + checksum: 83352afbd5b32a2c0d939ba17dc3420c0e72b5d920146b96af863acda675d4f307bb5b8cff25637761dfcba0cbe71c624307f45e2b87810798967b5af0798d43 languageName: node linkType: hard -"@typescript-eslint/utils@npm:5.59.8": - version: 5.59.8 - resolution: "@typescript-eslint/utils@npm:5.59.8" +"@typescript-eslint/utils@npm:5.60.0": + version: 5.60.0 + resolution: "@typescript-eslint/utils@npm:5.60.0" dependencies: "@eslint-community/eslint-utils": ^4.2.0 "@types/json-schema": ^7.0.9 "@types/semver": ^7.3.12 - "@typescript-eslint/scope-manager": 5.59.8 - "@typescript-eslint/types": 5.59.8 - "@typescript-eslint/typescript-estree": 5.59.8 + "@typescript-eslint/scope-manager": 5.60.0 + "@typescript-eslint/types": 5.60.0 + "@typescript-eslint/typescript-estree": 5.60.0 eslint-scope: ^5.1.1 semver: ^7.3.7 peerDependencies: eslint: ^6.0.0 || ^7.0.0 || ^8.0.0 - checksum: 20b859508942b79111ddbea8e777864fa76a5597b217bff921ba0e9ad245f71cff7ed598d18f384f441d4b433cfae0645654455fa38f313e24869fa062582964 + checksum: 00556a31fc288d2d59e85c139077c111ad2218ce817e24d02d9a50fb29c62293be7ab5200ae2a0cecce9c193a43519b690e9fd263bdc8bcef940eec005dc2bef languageName: node linkType: hard -"@typescript-eslint/visitor-keys@npm:5.59.8": - version: 5.59.8 - resolution: "@typescript-eslint/visitor-keys@npm:5.59.8" +"@typescript-eslint/visitor-keys@npm:5.60.0": + version: 5.60.0 + resolution: "@typescript-eslint/visitor-keys@npm:5.60.0" dependencies: - "@typescript-eslint/types": 5.59.8 + "@typescript-eslint/types": 5.60.0 eslint-visitor-keys: ^3.3.0 - checksum: 0e7cdb5c0e9106bbb77bb599d9a6464306d7cfa1b35435810c5d59b951f3b65ac3a1a829894e328e43d411da189247568eb24d28dd9ceca95dfc3f19011b889f + checksum: 797888d1e9cfd42b92382443956f0a46d093c49bca2789699f638d79387f26c91c55b8545bfaba7b9b6d846efc6b1134df640c3975d51a8c3b57d8e6a837ab5b languageName: node linkType: hard @@ -1522,22 +1546,22 @@ __metadata: version: 0.0.0-use.local resolution: "EMS-ESP@workspace:." dependencies: - "@emotion/react": ^11.11.0 + "@emotion/react": ^11.11.1 "@emotion/styled": ^11.11.0 "@mui/icons-material": ^5.11.16 - "@mui/material": ^5.13.3 + "@mui/material": ^5.13.6 "@table-library/react-table-library": 4.1.4 "@types/lodash-es": ^4.17.7 - "@types/node": ^20.2.5 - "@types/react": ^18.2.8 - "@types/react-dom": ^18.2.4 + "@types/node": ^20.3.1 + "@types/react": ^18.2.14 + "@types/react-dom": ^18.2.6 "@types/react-router-dom": ^5.3.3 - "@typescript-eslint/eslint-plugin": ^5.59.8 - "@typescript-eslint/parser": ^5.59.8 + "@typescript-eslint/eslint-plugin": ^5.60.0 + "@typescript-eslint/parser": ^5.60.0 "@vitejs/plugin-react-swc": ^3.3.2 async-validator: ^4.2.5 axios: ^1.4.0 - eslint: ^8.42.0 + eslint: ^8.43.0 eslint-config-airbnb: ^19.0.4 eslint-config-airbnb-typescript: ^17.0.0 eslint-config-prettier: ^8.8.0 @@ -1557,12 +1581,12 @@ __metadata: react: latest react-dom: latest react-dropzone: ^14.2.3 - react-icons: ^4.9.0 - react-router-dom: ^6.11.2 + react-icons: ^4.10.1 + react-router-dom: ^6.14.0 react-toastify: ^9.1.3 - rollup-plugin-visualizer: ^5.9.0 + rollup-plugin-visualizer: ^5.9.2 sockette: ^2.0.6 - terser: ^5.17.7 + terser: ^5.18.1 typesafe-i18n: ^5.24.3 typescript: ^5.1.3 vite: ^4.3.9 @@ -2834,14 +2858,14 @@ __metadata: languageName: node linkType: hard -"eslint@npm:^8.42.0": - version: 8.42.0 - resolution: "eslint@npm:8.42.0" +"eslint@npm:^8.43.0": + version: 8.43.0 + resolution: "eslint@npm:8.43.0" dependencies: "@eslint-community/eslint-utils": ^4.2.0 "@eslint-community/regexpp": ^4.4.0 "@eslint/eslintrc": ^2.0.3 - "@eslint/js": 8.42.0 + "@eslint/js": 8.43.0 "@humanwhocodes/config-array": ^0.11.10 "@humanwhocodes/module-importer": ^1.0.1 "@nodelib/fs.walk": ^1.2.8 @@ -2879,7 +2903,7 @@ __metadata: text-table: ^0.2.0 bin: eslint: bin/eslint.js - checksum: 8ab5a3c1619008c946497a16b88a811b1f6c49a750a9bd0f81085dff4166418b9da4e46108b09d920877ab2f5981e3613332653b7f5e3917d8088bc4b8d40b5a + checksum: 1f9ff2c774e852c179ba569a3b672cbc4cf91aa59843ee32f7da363c10b5aad842672005ac04c760f6077b3471da428562274e0fcb0a78c2056866b3d36be948 languageName: node linkType: hard @@ -4768,12 +4792,12 @@ __metadata: languageName: node linkType: hard -"react-icons@npm:^4.9.0": - version: 4.9.0 - resolution: "react-icons@npm:4.9.0" +"react-icons@npm:^4.10.1": + version: 4.10.1 + resolution: "react-icons@npm:4.10.1" peerDependencies: react: "*" - checksum: 66eb6d170643d2a35a92418e9fa4416f728ebb537d99d3d0d875faa3e46952667da31777f9ca10b7351515785b3d92dd052fd67b7863471d332cd3b68beec7d1 + checksum: aa4ecd390751bf61fc7967c2256eace1351c4af9a941ea4459464ac5cae012fe99970430ab659ad38a633ca55b0392d9ea5624ab3953c0bc86e1809199d6e2e2 languageName: node linkType: hard @@ -4791,27 +4815,27 @@ __metadata: languageName: node linkType: hard -"react-router-dom@npm:^6.11.2": - version: 6.11.2 - resolution: "react-router-dom@npm:6.11.2" +"react-router-dom@npm:^6.14.0": + version: 6.14.0 + resolution: "react-router-dom@npm:6.14.0" dependencies: - "@remix-run/router": 1.6.2 - react-router: 6.11.2 + "@remix-run/router": 1.7.0 + react-router: 6.14.0 peerDependencies: react: ">=16.8" react-dom: ">=16.8" - checksum: be7433bc290e56c0dd3e1008d53a76cc9866bf460980658501880876420086f11810ec3355a3abcd79ac537d6a1351eda009fade841c266456d0e8df60967b76 + checksum: 104a09aa48b00bdf7bf021bc8c915d36e2045ff0a41b3b23b92b5d3ba5c16cbb0dd4eb00e01188a419e143af2212cbcd3c983ebde26e52c1ed33916746edba31 languageName: node linkType: hard -"react-router@npm:6.11.2": - version: 6.11.2 - resolution: "react-router@npm:6.11.2" +"react-router@npm:6.14.0": + version: 6.14.0 + resolution: "react-router@npm:6.14.0" dependencies: - "@remix-run/router": 1.6.2 + "@remix-run/router": 1.7.0 peerDependencies: react: ">=16.8" - checksum: a437606078d6096a6dfa322adf80d00ce153f20cd470ad888088c8da99f44477b963425c53f5461a540b909fc274154292ed80d636482dcdc58a423915ca1433 + checksum: 60a87b4e1bc684ddced3418f4cd91983ac5f7ea4aa1ac07dd9d336c67eb2511b22eda866a7d7bacd45abb81322ecc9a93afea98fb9daaef6b939a14eebbe4f73 languageName: node linkType: hard @@ -5014,9 +5038,9 @@ __metadata: languageName: node linkType: hard -"rollup-plugin-visualizer@npm:^5.9.0": - version: 5.9.0 - resolution: "rollup-plugin-visualizer@npm:5.9.0" +"rollup-plugin-visualizer@npm:^5.9.2": + version: 5.9.2 + resolution: "rollup-plugin-visualizer@npm:5.9.2" dependencies: open: ^8.4.0 picomatch: ^2.3.1 @@ -5029,7 +5053,7 @@ __metadata: optional: true bin: rollup-plugin-visualizer: dist/bin/cli.js - checksum: dc706e09c78124b2e05b58779c757e488c76e679b92795f8538f8efffa61845339d75a7f767d5a9eeb5d7e4fd76a835dd93ebf17315bf8a0a0e5fc85a4f59ab5 + checksum: e6280bed797084e9f9ee06726e46706e32854fc7647c5c5bcec68a9be8ca8e6fbf7f8a0b2f76255e9df72e84135a7d57eb2662b6cfd68496a1ef60fa33597eaf languageName: node linkType: hard @@ -5520,9 +5544,9 @@ __metadata: languageName: node linkType: hard -"terser@npm:^5.17.7": - version: 5.17.7 - resolution: "terser@npm:5.17.7" +"terser@npm:^5.18.1": + version: 5.18.1 + resolution: "terser@npm:5.18.1" dependencies: "@jridgewell/source-map": ^0.3.3 acorn: ^8.8.2 @@ -5530,7 +5554,7 @@ __metadata: source-map-support: ~0.5.20 bin: terser: bin/terser - checksum: 864154a1750daf516012e5add4f0749bfc71e8f4f918973ec3d504db6a148be976adf46ae490e795173eeff59ec579d7d464bb6354c1bb71f8e14ff398409aed + checksum: f3ab58c6193f05cf4a4c06999dd95f23151542701782a3e91348828b184b7f54efebcbad3cc462b39b96b788a38936a4f6388edb022e9c696acf73af93692fdb languageName: node linkType: hard From 31220b3fded73ed85fb61ba1b98cd3bedb1b84de Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sun, 25 Jun 2023 08:54:11 +0200 Subject: [PATCH 10/18] fix webEntity commands --- .../src/project/DashboardDevicesDialog.tsx | 12 ++-- src/command.cpp | 13 ++++ src/web/WebDataService.cpp | 10 +-- src/web/WebEntityService.cpp | 66 +++++++++++++++---- src/web/WebEntityService.h | 1 + 5 files changed, 80 insertions(+), 22 deletions(-) diff --git a/interface/src/project/DashboardDevicesDialog.tsx b/interface/src/project/DashboardDevicesDialog.tsx index f2ff2bc33..19103959a 100644 --- a/interface/src/project/DashboardDevicesDialog.tsx +++ b/interface/src/project/DashboardDevicesDialog.tsx @@ -94,12 +94,12 @@ const DashboarDevicesDialog = ({ } let helperText = '<'; - if (dv.u !== DeviceValueUOM.NONE) { + if (dv.s) { helperText += 'n'; - if (dv.m && dv.x) { + if (dv.m !== undefined && dv.x !== undefined) { + helperText += ' between ' + dv.m + ' and ' + dv.x; - } - if (dv.s) { + } else { helperText += ' , step ' + dv.s; } } else { @@ -144,7 +144,7 @@ const DashboarDevicesDialog = ({ ))} - ) : editItem.u !== DeviceValueUOM.NONE ? ( + ) : editItem.s || editItem.u !== DeviceValueUOM.NONE ? ( {setUom(editItem.u)} }} diff --git a/src/command.cpp b/src/command.cpp index 1f5cb1fc6..3bb89fd69 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -602,6 +602,19 @@ void Command::show_all(uuid::console::Shell & shell) { shell.print(COLOR_RESET); show(shell, EMSdevice::DeviceType::SYSTEM, true); + // show Custom + if (EMSESP::webEntityService.has_commands()) { + shell.print(COLOR_BOLD_ON); + shell.print(COLOR_YELLOW); + shell.printf(" %s: ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::CUSTOM)); + shell.println(COLOR_RESET); + shell.printf(" info: %slists all values %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_RED); + shell.println(COLOR_RESET); + shell.printf(" commands: %slists all commands %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_RED); + shell.print(COLOR_RESET); + show(shell, EMSdevice::DeviceType::CUSTOM, true); + } + // show scheduler if (EMSESP::webSchedulerService.has_commands()) { shell.print(COLOR_BOLD_ON); diff --git a/src/web/WebDataService.cpp b/src/web/WebDataService.cpp index 054082011..472f94d10 100644 --- a/src/web/WebDataService.cpp +++ b/src/web/WebDataService.cpp @@ -242,7 +242,7 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar // the data could be in any format, but we need string // authenticated is always true JsonVariant data = dv["v"]; // the value in any format - uint8_t return_code = CommandRet::OK; + uint8_t return_code = CommandRet::NOT_FOUND; uint8_t device_type = emsdevice->device_type(); if (data.is()) { return_code = Command::call(device_type, cmd, data.as(), true, id, output); @@ -279,16 +279,16 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar auto * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL); JsonObject output = response->getRoot(); JsonVariant data = dv["v"]; // the value in any format - uint8_t return_code = CommandRet::OK; + uint8_t return_code = CommandRet::NOT_FOUND; uint8_t device_type = EMSdevice::DeviceType::CUSTOM; - if (data.is()) { + if (data.is()) { + return_code = Command::call(device_type, cmd, data.as(), true, id, output); + } else if (data.is()) { char s[10]; return_code = Command::call(device_type, cmd, Helpers::render_value(s, data.as(), 0), true, id, output); } else if (data.is()) { char s[10]; return_code = Command::call(device_type, cmd, Helpers::render_value(s, data.as(), 1), true, id, output); - } else if (data.is()) { - return_code = Command::call(device_type, cmd, data.as() ? "true" : "false", true, id, output); } if (return_code != CommandRet::OK) { EMSESP::logger().err("Write command failed %s (%s)", (const char *)output["message"], Command::return_code_string(return_code).c_str()); diff --git a/src/web/WebEntityService.cpp b/src/web/WebEntityService.cpp index a52d8fc1b..7fbc20630 100644 --- a/src/web/WebEntityService.cpp +++ b/src/web/WebEntityService.cpp @@ -83,10 +83,12 @@ StateUpdateResult WebEntity::update(JsonObject & root, WebEntity & webEntity) { entityItem.value = EMS_VALUE_DEFAULT_SHORT; } else if (entityItem.value_type == DeviceValueType::USHORT) { entityItem.value = EMS_VALUE_DEFAULT_USHORT; - } else { // if (entityItem.value_type == DeviceValueType::ULONG || entityItem.valuetype == DeviceValueType::TIME) { + } else { // if (entityItem.value_type == DeviceValueType::ULONG || entityItem.value_type == DeviceValueType::TIME) { entityItem.value = EMS_VALUE_DEFAULT_ULONG; } - + if (entityItem.factor == 0) { + entityItem.factor = 1; + } webEntity.entityItems.push_back(entityItem); // add to list if (entityItem.writeable) { @@ -108,13 +110,13 @@ StateUpdateResult WebEntity::update(JsonObject & root, WebEntity & webEntity) { bool WebEntityService::command_setvalue(const char * value, const std::string name) { EMSESP::webEntityService.read([&](WebEntity & webEntity) { entityItems = &webEntity.entityItems; }); for (EntityItem & entityItem : *entityItems) { - if (entityItem.name == name) { + if (Helpers::toLower(entityItem.name) == Helpers::toLower(name)) { if (entityItem.value_type == DeviceValueType::BOOL) { bool v; if (!Helpers::value2bool(value, v)) { return false; } - EMSESP::send_write_request(entityItem.type_id, entityItem.device_id, entityItem.offset, v ? entityItem.type_id > 0xFF ? 1 : 0xFF : 0, 0); + EMSESP::send_write_request(entityItem.type_id, entityItem.device_id, entityItem.offset, v ? 0xFF : 0, 0); } else { float f; if (!Helpers::value2float(value, f)) { @@ -226,6 +228,7 @@ bool WebEntityService::get_value_info(JsonObject & output, const char * cmd) { if (Helpers::toLower(entity.name) == Helpers::toLower(command_s)) { output["name"] = entity.name; output["uom"] = EMSdevice::uom_to_string(entity.uom); + output["type"] = entity.value_type == DeviceValueType::BOOL ? "boolean" : F_(number); output["readable"] = true; output["writeable"] = entity.writeable; output["visible"] = true; @@ -311,19 +314,47 @@ void WebEntityService::publish(const bool force) { config["name"] = entityItem.name.c_str(); char topic[Mqtt::MQTT_TOPIC_MAX_SIZE]; - snprintf(topic, sizeof(topic), "sensor/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str()); - //char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE]; - // snprintf(command_topic, sizeof(command_topic), "%s/custom/%s", Mqtt::basename().c_str(), entityItem.name.c_str()); - // config["cmd_t"] = command_topic; - + if (entityItem.writeable) { + if (entityItem.value_type == DeviceValueType::BOOL) { + snprintf(topic, sizeof(topic), "switch/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str()); + } else if (Mqtt::discovery_type() == Mqtt::discoveryType::HOMEASSISTANT) { + snprintf(topic, sizeof(topic), "number/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str()); + } else { + snprintf(topic, sizeof(topic), "sensor/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str()); + } + char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE]; + snprintf(command_topic, sizeof(command_topic), "%s/custom/%s", Mqtt::basename().c_str(), entityItem.name.c_str()); + config["cmd_t"] = command_topic; + } else { + if (entityItem.value_type == DeviceValueType::BOOL) { + snprintf(topic, sizeof(topic), "binary_sensor/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str()); + } else { + snprintf(topic, sizeof(topic), "sensor/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str()); + } + } + if (entityItem.value_type == DeviceValueType::BOOL) { + // applies to both Binary Sensor (read only) and a Switch (for a command) + if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) { + config["pl_on"] = true; + config["pl_off"] = false; + } else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) { + config["pl_on"] = 1; + config["pl_off"] = 0; + } else { + char result[12]; + config["pl_on"] = Helpers::render_boolean(result, true); + config["pl_off"] = Helpers::render_boolean(result, false); + } + } JsonObject dev = config.createNestedObject("dev"); JsonArray ids = dev.createNestedArray("ids"); ids.add("ems-esp"); // add "availability" section Mqtt::add_avty_to_doc(stat_t, config.as(), val_cond); - Mqtt::queue_ha(topic, config.as()); - ha_registered_ = true; + if (Mqtt::queue_ha(topic, config.as())) { + ha_registered_ = true; + } } } if (output.size() > 0) { @@ -349,6 +380,15 @@ uint8_t WebEntityService::count_entities() { return count; } +uint8_t WebEntityService::has_commands() { + EMSESP::webEntityService.read([&](WebEntity & webEntity) { entityItems = &webEntity.entityItems; }); + uint8_t count = 0; + for (const EntityItem & entity : *entityItems) { + count += entity.writeable ? 1 : 0; + } + return count; +} + // send to dashboard, msgpack don't like serialized, use number void WebEntityService::generate_value_web(JsonObject & output) { EMSESP::webEntityService.read([&](WebEntity & webEntity) { entityItems = &webEntity.entityItems; }); @@ -362,6 +402,10 @@ void WebEntityService::generate_value_web(JsonObject & output) { obj["u"] = entity.uom; if (entity.writeable) { obj["c"] = entity.name; + if (entity.value_type != DeviceValueType::BOOL) { + char s[10]; + obj["s"] = Helpers::render_value(s, entity.factor, 1); + } } switch (entity.value_type) { diff --git a/src/web/WebEntityService.h b/src/web/WebEntityService.h index 9010cacf1..05160ee1a 100644 --- a/src/web/WebEntityService.h +++ b/src/web/WebEntityService.h @@ -60,6 +60,7 @@ class WebEntityService : public StatefulService { void fetch(); void render_value(JsonObject & output, EntityItem entity, const bool useVal = false, const bool web = false); uint8_t count_entities(); + uint8_t has_commands(); void generate_value_web(JsonObject & output); From 8b800ded21ab347949f8f44067cc62e9ed9c34a3 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Fri, 30 Jun 2023 09:57:49 +0200 Subject: [PATCH 11/18] update packages --- interface/package.json | 10 +-- interface/yarn.lock | 138 ++++++++++++++++++++--------------------- 2 files changed, 74 insertions(+), 74 deletions(-) diff --git a/interface/package.json b/interface/package.json index 68f823f93..cfc140991 100644 --- a/interface/package.json +++ b/interface/package.json @@ -25,7 +25,7 @@ "@mui/material": "^5.13.6", "@table-library/react-table-library": "4.1.4", "@types/lodash-es": "^4.17.7", - "@types/node": "^20.3.1", + "@types/node": "^20.3.2", "@types/react": "^18.2.14", "@types/react-dom": "^18.2.6", "@types/react-router-dom": "^5.3.3", @@ -42,11 +42,11 @@ "react-toastify": "^9.1.3", "sockette": "^2.0.6", "typesafe-i18n": "^5.24.3", - "typescript": "^5.1.3" + "typescript": "^5.1.6" }, "devDependencies": { - "@typescript-eslint/eslint-plugin": "^5.60.0", - "@typescript-eslint/parser": "^5.60.0", + "@typescript-eslint/eslint-plugin": "^5.60.1", + "@typescript-eslint/parser": "^5.60.1", "@vitejs/plugin-react-swc": "^3.3.2", "eslint": "^8.43.0", "eslint-config-airbnb": "^19.0.4", @@ -63,7 +63,7 @@ "npm-run-all": "^4.1.5", "prettier": "^2.8.8", "rollup-plugin-visualizer": "^5.9.2", - "terser": "^5.18.1", + "terser": "^5.18.2", "vite": "^4.3.9", "vite-plugin-svgr": "^3.2.0", "vite-tsconfig-paths": "^4.2.0" diff --git a/interface/yarn.lock b/interface/yarn.lock index 8e5fee1d2..a34bc31ec 100644 --- a/interface/yarn.lock +++ b/interface/yarn.lock @@ -1305,10 +1305,10 @@ __metadata: languageName: node linkType: hard -"@types/node@npm:^20.3.1": - version: 20.3.1 - resolution: "@types/node@npm:20.3.1" - checksum: 7e8a6f5d6fc1ad3778f038f5f8df570741459984280fd2e9539af32620d93438c955fd1b90d00f9cc438cd132ec04d7669ada9e32502336e78713a3ad9b51d10 +"@types/node@npm:^20.3.2": + version: 20.3.2 + resolution: "@types/node@npm:20.3.2" + checksum: d857cbe388d11fefd6c598144db42a32e1c15c09624b9e0669ec65e9d72e080093db3ec6b536037e6575574e33413479d4b3762140c2544ff30eb0c2111b5596 languageName: node linkType: hard @@ -1410,14 +1410,14 @@ __metadata: languageName: node linkType: hard -"@typescript-eslint/eslint-plugin@npm:^5.60.0": - version: 5.60.0 - resolution: "@typescript-eslint/eslint-plugin@npm:5.60.0" +"@typescript-eslint/eslint-plugin@npm:^5.60.1": + version: 5.60.1 + resolution: "@typescript-eslint/eslint-plugin@npm:5.60.1" dependencies: "@eslint-community/regexpp": ^4.4.0 - "@typescript-eslint/scope-manager": 5.60.0 - "@typescript-eslint/type-utils": 5.60.0 - "@typescript-eslint/utils": 5.60.0 + "@typescript-eslint/scope-manager": 5.60.1 + "@typescript-eslint/type-utils": 5.60.1 + "@typescript-eslint/utils": 5.60.1 debug: ^4.3.4 grapheme-splitter: ^1.0.4 ignore: ^5.2.0 @@ -1430,43 +1430,43 @@ __metadata: peerDependenciesMeta: typescript: optional: true - checksum: e41d1a45f330e766afb594429fad535f4db06efc458e74cc05109c4555550efdad57141aa088d5cb836aeb90b822e08e7690e5b650fd7b2419da1d64113d5360 + checksum: 1861e7fde48019ecae9acbc654d24f746e6a375f11f6f924e2ff3c9aa52528744a003da14592fafdbc407cf58b8cf7d0e42c624f3de06cb7bee5d8a31879ed79 languageName: node linkType: hard -"@typescript-eslint/parser@npm:^5.60.0": - version: 5.60.0 - resolution: "@typescript-eslint/parser@npm:5.60.0" +"@typescript-eslint/parser@npm:^5.60.1": + version: 5.60.1 + resolution: "@typescript-eslint/parser@npm:5.60.1" dependencies: - "@typescript-eslint/scope-manager": 5.60.0 - "@typescript-eslint/types": 5.60.0 - "@typescript-eslint/typescript-estree": 5.60.0 + "@typescript-eslint/scope-manager": 5.60.1 + "@typescript-eslint/types": 5.60.1 + "@typescript-eslint/typescript-estree": 5.60.1 debug: ^4.3.4 peerDependencies: eslint: ^6.0.0 || ^7.0.0 || ^8.0.0 peerDependenciesMeta: typescript: optional: true - checksum: a9b4875a3ed37cfe8205173caf85b21f8025cf21bc295036c6265010ff622054b137fa7f3251476104086804bf55b420431efa887935b67c506800e3adcc8910 + checksum: b44d041bb46908078df898ab1d8a0da0a58901caced0bb657af9d48c6bb54c090e565cc31d7526a27f25faeb25315fdec648873b2527feed043532a053914dd2 languageName: node linkType: hard -"@typescript-eslint/scope-manager@npm:5.60.0": - version: 5.60.0 - resolution: "@typescript-eslint/scope-manager@npm:5.60.0" +"@typescript-eslint/scope-manager@npm:5.60.1": + version: 5.60.1 + resolution: "@typescript-eslint/scope-manager@npm:5.60.1" dependencies: - "@typescript-eslint/types": 5.60.0 - "@typescript-eslint/visitor-keys": 5.60.0 - checksum: 87c742ea716359206244e1c7a8d2805b9e1caf04bce127f84b790046ae994849f25bf38af05de7a283eec58b34ecc701f441f23dfcccb59b9185260667bfe6e7 + "@typescript-eslint/types": 5.60.1 + "@typescript-eslint/visitor-keys": 5.60.1 + checksum: 50164675adb4850354a8e0297d498b59283eee961e10e0c00f9d664e03b464a9de4dc7a9d84c534c84df6ac3ea6f32238e2df9528374104bd66678b1ec9fbfec languageName: node linkType: hard -"@typescript-eslint/type-utils@npm:5.60.0": - version: 5.60.0 - resolution: "@typescript-eslint/type-utils@npm:5.60.0" +"@typescript-eslint/type-utils@npm:5.60.1": + version: 5.60.1 + resolution: "@typescript-eslint/type-utils@npm:5.60.1" dependencies: - "@typescript-eslint/typescript-estree": 5.60.0 - "@typescript-eslint/utils": 5.60.0 + "@typescript-eslint/typescript-estree": 5.60.1 + "@typescript-eslint/utils": 5.60.1 debug: ^4.3.4 tsutils: ^3.21.0 peerDependencies: @@ -1474,23 +1474,23 @@ __metadata: peerDependenciesMeta: typescript: optional: true - checksum: 0baa4baa9c059e3a0d4da19cb62b821ababce781208cf18965e54916ea718a993d969f8f42b4356409ac1ce74228532e9d1cd0f2e9d3e0815c405467775b4015 + checksum: ef07f5c84636b8a9ff68dbc093ba6bce32d0e0f56831b214c2df3ff189502ae103c73bda7eb5e9f9ca21d3492dc851b7ac4b2cb83bdd6fbf5a9fe21068b404f4 languageName: node linkType: hard -"@typescript-eslint/types@npm:5.60.0": - version: 5.60.0 - resolution: "@typescript-eslint/types@npm:5.60.0" - checksum: 008aedc2322120b9b760204ae26b5ecf5a1a61da84e77427048d076074cef703914a7a2db0286f891bbd045c1246238823671ec97192e03eabec35e9f75288e2 +"@typescript-eslint/types@npm:5.60.1": + version: 5.60.1 + resolution: "@typescript-eslint/types@npm:5.60.1" + checksum: 4be7654356f9b79fb942ae22196b518f30e20b07588355df22c85dfdcdaafe02f312b473de67af34fbb2283182d0d337aa65312f1117c6f8bf635cc427944c23 languageName: node linkType: hard -"@typescript-eslint/typescript-estree@npm:5.60.0": - version: 5.60.0 - resolution: "@typescript-eslint/typescript-estree@npm:5.60.0" +"@typescript-eslint/typescript-estree@npm:5.60.1": + version: 5.60.1 + resolution: "@typescript-eslint/typescript-estree@npm:5.60.1" dependencies: - "@typescript-eslint/types": 5.60.0 - "@typescript-eslint/visitor-keys": 5.60.0 + "@typescript-eslint/types": 5.60.1 + "@typescript-eslint/visitor-keys": 5.60.1 debug: ^4.3.4 globby: ^11.1.0 is-glob: ^4.0.3 @@ -1499,35 +1499,35 @@ __metadata: peerDependenciesMeta: typescript: optional: true - checksum: 83352afbd5b32a2c0d939ba17dc3420c0e72b5d920146b96af863acda675d4f307bb5b8cff25637761dfcba0cbe71c624307f45e2b87810798967b5af0798d43 + checksum: 09933d3c1b1bacf7e043d33a7e134489650fca7b7b965b633a1c526453f20478b38c4aa9b7c6da269d2a43f26608110d4c129eec917c4561431149dae82c3b9d languageName: node linkType: hard -"@typescript-eslint/utils@npm:5.60.0": - version: 5.60.0 - resolution: "@typescript-eslint/utils@npm:5.60.0" +"@typescript-eslint/utils@npm:5.60.1": + version: 5.60.1 + resolution: "@typescript-eslint/utils@npm:5.60.1" dependencies: "@eslint-community/eslint-utils": ^4.2.0 "@types/json-schema": ^7.0.9 "@types/semver": ^7.3.12 - "@typescript-eslint/scope-manager": 5.60.0 - "@typescript-eslint/types": 5.60.0 - "@typescript-eslint/typescript-estree": 5.60.0 + "@typescript-eslint/scope-manager": 5.60.1 + "@typescript-eslint/types": 5.60.1 + "@typescript-eslint/typescript-estree": 5.60.1 eslint-scope: ^5.1.1 semver: ^7.3.7 peerDependencies: eslint: ^6.0.0 || ^7.0.0 || ^8.0.0 - checksum: 00556a31fc288d2d59e85c139077c111ad2218ce817e24d02d9a50fb29c62293be7ab5200ae2a0cecce9c193a43519b690e9fd263bdc8bcef940eec005dc2bef + checksum: cb408bd67dd5be3a3585b67ac19f60e9feb309a56782924b314077f7dc77cb10e690fdb4a7ac643784e8fab30bc04d6f23935c1aed6d08233f8dd8604782ac27 languageName: node linkType: hard -"@typescript-eslint/visitor-keys@npm:5.60.0": - version: 5.60.0 - resolution: "@typescript-eslint/visitor-keys@npm:5.60.0" +"@typescript-eslint/visitor-keys@npm:5.60.1": + version: 5.60.1 + resolution: "@typescript-eslint/visitor-keys@npm:5.60.1" dependencies: - "@typescript-eslint/types": 5.60.0 + "@typescript-eslint/types": 5.60.1 eslint-visitor-keys: ^3.3.0 - checksum: 797888d1e9cfd42b92382443956f0a46d093c49bca2789699f638d79387f26c91c55b8545bfaba7b9b6d846efc6b1134df640c3975d51a8c3b57d8e6a837ab5b + checksum: e70bd584ff0eef1c8739e3457e7402485acc06aba468ff8f191f4546aaed93ec91f309bb567a3d8bbd9a4aec030f3b73c8e8df085bb82cbb8ea9a0209c7355f8 languageName: node linkType: hard @@ -1552,12 +1552,12 @@ __metadata: "@mui/material": ^5.13.6 "@table-library/react-table-library": 4.1.4 "@types/lodash-es": ^4.17.7 - "@types/node": ^20.3.1 + "@types/node": ^20.3.2 "@types/react": ^18.2.14 "@types/react-dom": ^18.2.6 "@types/react-router-dom": ^5.3.3 - "@typescript-eslint/eslint-plugin": ^5.60.0 - "@typescript-eslint/parser": ^5.60.0 + "@typescript-eslint/eslint-plugin": ^5.60.1 + "@typescript-eslint/parser": ^5.60.1 "@vitejs/plugin-react-swc": ^3.3.2 async-validator: ^4.2.5 axios: ^1.4.0 @@ -1586,9 +1586,9 @@ __metadata: react-toastify: ^9.1.3 rollup-plugin-visualizer: ^5.9.2 sockette: ^2.0.6 - terser: ^5.18.1 + terser: ^5.18.2 typesafe-i18n: ^5.24.3 - typescript: ^5.1.3 + typescript: ^5.1.6 vite: ^4.3.9 vite-plugin-svgr: ^3.2.0 vite-tsconfig-paths: ^4.2.0 @@ -5544,9 +5544,9 @@ __metadata: languageName: node linkType: hard -"terser@npm:^5.18.1": - version: 5.18.1 - resolution: "terser@npm:5.18.1" +"terser@npm:^5.18.2": + version: 5.18.2 + resolution: "terser@npm:5.18.2" dependencies: "@jridgewell/source-map": ^0.3.3 acorn: ^8.8.2 @@ -5554,7 +5554,7 @@ __metadata: source-map-support: ~0.5.20 bin: terser: bin/terser - checksum: f3ab58c6193f05cf4a4c06999dd95f23151542701782a3e91348828b184b7f54efebcbad3cc462b39b96b788a38936a4f6388edb022e9c696acf73af93692fdb + checksum: 7a7203eceef379c6381f5b43aaed509d12381c7453baee28b320fcd968523347f1bf4ba297cd3155ec860e9604279a1c9bc7060b35d9c34fae94c80cfa2738c2 languageName: node linkType: hard @@ -5691,23 +5691,23 @@ __metadata: languageName: node linkType: hard -"typescript@npm:^5.1.3": - version: 5.1.3 - resolution: "typescript@npm:5.1.3" +"typescript@npm:^5.1.6": + version: 5.1.6 + resolution: "typescript@npm:5.1.6" bin: tsc: bin/tsc tsserver: bin/tsserver - checksum: 1faba8d5ffd4717864ddce767613c5ab77c1c8510c1ce21dc9b112a4c662357b9338dc0a6121615266d3a44ebec699f115ef2dabf18d9d7341ea1675692b9b24 + checksum: 45ac28e2df8365fd28dac42f5d62edfe69a7203d5ec646732cadc04065331f34f9078f81f150fde42ed9754eed6fa3b06a8f3523c40b821e557b727f1992e025 languageName: node linkType: hard -"typescript@patch:typescript@^5.1.3#~builtin": - version: 5.1.3 - resolution: "typescript@patch:typescript@npm%3A5.1.3#~builtin::version=5.1.3&hash=1f5320" +"typescript@patch:typescript@^5.1.6#~builtin": + version: 5.1.6 + resolution: "typescript@patch:typescript@npm%3A5.1.6#~builtin::version=5.1.6&hash=1f5320" bin: tsc: bin/tsc tsserver: bin/tsserver - checksum: 6219383250b585b201c9ea10576164c9d5760c7a167bc761b118692c9fb8e88610f37730c0a1169d96ac19b29ed80418048763d0c1ff00ce48e051abbc213a9b + checksum: c134abcd9fc5c393db30498db18d8e89453efae14e11a39c1adb6238138d4c152472e497499b8a67b0c87ca6eafcb26b0eb02f962f240c233c2e0ad3785f8742 languageName: node linkType: hard From 3efe16c840cff21287e9913eee4da8f27eda011b Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Fri, 30 Jun 2023 09:58:24 +0200 Subject: [PATCH 12/18] fix min/max adaption --- src/emsdevice.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index 91de94609..efb1bd7a6 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -1657,18 +1657,16 @@ bool EMSdevice::generate_values(JsonObject & output, const uint8_t tag_filter, c // check for value outside min/max range and adapt the limits to avoid HA complains // Should this also check for api output? if ((output_target == OUTPUT_TARGET::MQTT) && (dv.min != 0 || dv.max != 0)) { - if (json[name].is() || json[name].is()) { - int v = json[name]; - if (fahrenheit) { - v = (v - (32 * (fahrenheit - 1))) / 1.8; // reset to °C - } - if (v < dv.min) { - dv.min = v; - dv.remove_state(DeviceValueState::DV_HA_CONFIG_CREATED); - } else if (v > dv.max) { - dv.max = v; - dv.remove_state(DeviceValueState::DV_HA_CONFIG_CREATED); - } + int v = Helpers::atoint(json[name]); + if (fahrenheit) { + v = (v - (32 * (fahrenheit - 1))) / 1.8; // reset to °C + } + if (v < dv.min) { + dv.min = v; + dv.remove_state(DeviceValueState::DV_HA_CONFIG_CREATED); + } else if (v > dv.max) { + dv.max = v; + dv.remove_state(DeviceValueState::DV_HA_CONFIG_CREATED); } } } From cbb7d46ede18c1f449ca15a23ecc846e13fe12aa Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Fri, 30 Jun 2023 10:00:04 +0200 Subject: [PATCH 13/18] add cooling #1198, fix min limit of noreducetemp --- src/devices/thermostat.cpp | 18 +++++++++++++++++- src/devices/thermostat.h | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/devices/thermostat.cpp b/src/devices/thermostat.cpp index cf68370e6..6d3a56efa 100644 --- a/src/devices/thermostat.cpp +++ b/src/devices/thermostat.cpp @@ -1019,6 +1019,7 @@ void Thermostat::process_RC300Set(std::shared_ptr telegram) { has_update(telegram, hc->reducetemp, 9); has_update(telegram, hc->noreducetemp, 12); has_update(telegram, hc->remoteseltemp, 17); // see https://github.com/emsesp/EMS-ESP32/issues/590 + has_update(telegram, hc->cooling, 28); } // types 0x2AF ff @@ -1970,6 +1971,20 @@ bool Thermostat::set_wwprio(const char * value, const int8_t id) { return true; } +// set cooling +bool Thermostat::set_cooling(const char * value, const int8_t id) { + std::shared_ptr hc = heating_circuit((id == -1) ? AUTO_HEATING_CIRCUIT : id); + if (hc == nullptr) { + return false; + } + + bool b; + if (!Helpers::value2bool(value, b)) { + return false; + } + write_command(set_typeids[hc->hc()], 28, b ? 0x01 : 0x00, set_typeids[hc->hc()]); + return true; +} // sets the thermostat ww circulation working mode, where mode is a string bool Thermostat::set_wwcircmode(const char * value, const int8_t id) { @@ -4178,6 +4193,7 @@ void Thermostat::register_device_values_hc(std::shared_ptrnoreducetemp, DeviceValueType::INT, FL_(noreducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_noreducetemp)); register_device_value(tag, &hc->reducetemp, DeviceValueType::INT, FL_(reducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_reducetemp)); register_device_value(tag, &hc->wwprio, DeviceValueType::BOOL, FL_(wwprio), DeviceValueUOM::NONE, MAKE_CF_CB(set_wwprio)); + register_device_value(tag, &hc->cooling, DeviceValueType::BOOL, FL_(cooling), DeviceValueUOM::NONE, MAKE_CF_CB(set_cooling)); register_device_value(tag, &hc->hpmode, DeviceValueType::ENUM, FL_(enum_hpmode), FL_(hpmode), DeviceValueUOM::NONE, MAKE_CF_CB(set_hpmode)); register_device_value(tag, &hc->dewoffset, DeviceValueType::UINT, FL_(dewoffset), DeviceValueUOM::K, MAKE_CF_CB(set_dewoffset)); @@ -4350,7 +4366,7 @@ void Thermostat::register_device_values_hc(std::shared_ptrnoreducetemp, DeviceValueType::INT, FL_(noreducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_noreducetemp), -30, 10); + register_device_value(tag, &hc->noreducetemp, DeviceValueType::INT, FL_(noreducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_noreducetemp), -31, 10); register_device_value(tag, &hc->reducetemp, DeviceValueType::INT, FL_(reducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_reducetemp), -20, 10); register_device_value(tag, &hc->vacreducetemp, DeviceValueType::INT, FL_(vacreducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_vacreducetemp), -20, 10); register_device_value( diff --git a/src/devices/thermostat.h b/src/devices/thermostat.h index b347ac411..6196b41b9 100644 --- a/src/devices/thermostat.h +++ b/src/devices/thermostat.h @@ -94,6 +94,7 @@ class Thermostat : public EMSdevice { uint8_t roomtempdiff; uint8_t hpminflowtemp; uint8_t hpmode; + uint8_t cooling; uint8_t hc_num() const { return hc_num_; @@ -565,6 +566,7 @@ class Thermostat : public EMSdevice { bool set_dewoffset(const char * value, const int8_t id); bool set_hpminflowtemp(const char * value, const int8_t id); bool set_hpmode(const char * value, const int8_t id); + bool set_cooling(const char * value, const int8_t id); }; } // namespace emsesp From d27ef2530d765eda12ba624e2121ecf0641b1f38 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sat, 1 Jul 2023 09:08:27 +0200 Subject: [PATCH 14/18] fix crash on min/max check --- src/emsdevice.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index efb1bd7a6..30b8580d8 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -1616,7 +1616,7 @@ bool EMSdevice::generate_values(JsonObject & output, const uint8_t tag_filter, c : (dv.uom == DeviceValueUOM::DEGREES) ? 2 : (dv.uom == DeviceValueUOM::DEGREES_R) ? 1 : 0; - char val[10]; + char val[10] = {'\0'}; if (dv.type == DeviceValueType::INT) { json[name] = serialized(Helpers::render_value(val, *(int8_t *)(dv.value_p), dv.numeric_operator, fahrenheit)); } else if (dv.type == DeviceValueType::UINT) { @@ -1645,7 +1645,7 @@ bool EMSdevice::generate_values(JsonObject & output, const uint8_t tag_filter, c Helpers::translated_word(FL_(minutes))); json[name] = time_s; } else { - json[name] = time_value; + json[name] = serialized(Helpers::render_value(val, time_value, 1)); } } @@ -1657,7 +1657,7 @@ bool EMSdevice::generate_values(JsonObject & output, const uint8_t tag_filter, c // check for value outside min/max range and adapt the limits to avoid HA complains // Should this also check for api output? if ((output_target == OUTPUT_TARGET::MQTT) && (dv.min != 0 || dv.max != 0)) { - int v = Helpers::atoint(json[name]); + int v = Helpers::atoint(val); if (fahrenheit) { v = (v - (32 * (fahrenheit - 1))) / 1.8; // reset to °C } From 32f3c646f8923bf9f838d650ca7af9c307528f3c Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sat, 1 Jul 2023 15:51:26 +0200 Subject: [PATCH 15/18] fix translation cooling --- src/devices/thermostat.cpp | 2 +- src/locale_translations.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/devices/thermostat.cpp b/src/devices/thermostat.cpp index 6d3a56efa..1b00ba870 100644 --- a/src/devices/thermostat.cpp +++ b/src/devices/thermostat.cpp @@ -4193,7 +4193,7 @@ void Thermostat::register_device_values_hc(std::shared_ptrnoreducetemp, DeviceValueType::INT, FL_(noreducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_noreducetemp)); register_device_value(tag, &hc->reducetemp, DeviceValueType::INT, FL_(reducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_reducetemp)); register_device_value(tag, &hc->wwprio, DeviceValueType::BOOL, FL_(wwprio), DeviceValueUOM::NONE, MAKE_CF_CB(set_wwprio)); - register_device_value(tag, &hc->cooling, DeviceValueType::BOOL, FL_(cooling), DeviceValueUOM::NONE, MAKE_CF_CB(set_cooling)); + register_device_value(tag, &hc->cooling, DeviceValueType::BOOL, FL_(hpcooling), DeviceValueUOM::NONE, MAKE_CF_CB(set_cooling)); register_device_value(tag, &hc->hpmode, DeviceValueType::ENUM, FL_(enum_hpmode), FL_(hpmode), DeviceValueUOM::NONE, MAKE_CF_CB(set_hpmode)); register_device_value(tag, &hc->dewoffset, DeviceValueType::UINT, FL_(dewoffset), DeviceValueUOM::K, MAKE_CF_CB(set_dewoffset)); diff --git a/src/locale_translations.h b/src/locale_translations.h index c8b8e0c48..e2f5eb2ab 100644 --- a/src/locale_translations.h +++ b/src/locale_translations.h @@ -653,6 +653,7 @@ MAKE_TRANSLATION(hpmode, "hpmode", "HP Mode", "WP Modus") MAKE_TRANSLATION(dewoffset, "dewoffset", "dew point offset", "Taupunkt Differenz") MAKE_TRANSLATION(roomtempdiff, "roomtempdiff", "room temp difference", "Raumtemperatur Differenz") MAKE_TRANSLATION(hpminflowtemp, "hpminflowtemp", "HP min. flow temp.", "WP minimale Vorlauftemperatur") +MAKE_TRANSLATION(hpcooling, "cooling", "cooling", "Kühlen", "Koelen", "Kyler", "chÅ‚odzenie", "kjøling", "refroidissement", "soÄŸuma") // heatpump MAKE_TRANSLATION(airHumidity, "airhumidity", "relative air humidity", "relative Luftfeuchte", "Relatieve luchtvochtigheid", "Relativ Luftfuktighet", "wilgotność wzglÄ™dna w pomieszczeniu", "luftfuktighet", "humidité relative air", "") From 09aea280b51367f8dd907eb74c0ae92c580b7b1d Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sun, 2 Jul 2023 12:09:14 +0200 Subject: [PATCH 16/18] add it-translations --- interface/package.json | 6 +- interface/src/SignIn.tsx | 5 + .../src/components/layout/LayoutAuthMenu.tsx | 5 + interface/src/i18n/IT.svg | 1 + interface/src/i18n/it/index.ts | 331 +++++ interface/src/project/SettingsApplication.tsx | 1 + interface/yarn.lock | 121 +- src/locale_translations.h | 1307 +++++++++-------- src/system.cpp | 2 +- 9 files changed, 1066 insertions(+), 713 deletions(-) create mode 100644 interface/src/i18n/IT.svg create mode 100644 interface/src/i18n/it/index.ts diff --git a/interface/package.json b/interface/package.json index cfc140991..d7dff3ee4 100644 --- a/interface/package.json +++ b/interface/package.json @@ -25,7 +25,7 @@ "@mui/material": "^5.13.6", "@table-library/react-table-library": "4.1.4", "@types/lodash-es": "^4.17.7", - "@types/node": "^20.3.2", + "@types/node": "^20.3.3", "@types/react": "^18.2.14", "@types/react-dom": "^18.2.6", "@types/react-router-dom": "^5.3.3", @@ -38,7 +38,7 @@ "react-dom": "latest", "react-dropzone": "^14.2.3", "react-icons": "^4.10.1", - "react-router-dom": "^6.14.0", + "react-router-dom": "^6.14.1", "react-toastify": "^9.1.3", "sockette": "^2.0.6", "typesafe-i18n": "^5.24.3", @@ -48,7 +48,7 @@ "@typescript-eslint/eslint-plugin": "^5.60.1", "@typescript-eslint/parser": "^5.60.1", "@vitejs/plugin-react-swc": "^3.3.2", - "eslint": "^8.43.0", + "eslint": "^8.44.0", "eslint-config-airbnb": "^19.0.4", "eslint-config-airbnb-typescript": "^17.0.0", "eslint-config-prettier": "^8.8.0", diff --git a/interface/src/SignIn.tsx b/interface/src/SignIn.tsx index 0cca183c9..63f453bdc 100644 --- a/interface/src/SignIn.tsx +++ b/interface/src/SignIn.tsx @@ -16,6 +16,7 @@ import { AuthenticationContext } from 'contexts/authentication'; import { ReactComponent as DEflag } from 'i18n/DE.svg'; import { ReactComponent as FRflag } from 'i18n/FR.svg'; import { ReactComponent as GBflag } from 'i18n/GB.svg'; +import { ReactComponent as ITflag } from 'i18n/IT.svg'; import { ReactComponent as NLflag } from 'i18n/NL.svg'; import { ReactComponent as NOflag } from 'i18n/NO.svg'; import { ReactComponent as PLflag } from 'i18n/PL.svg'; @@ -122,6 +123,10 @@ const SignIn: FC = () => {  FR +