From 6a27b251aaaf276f71be552011abf8e8ea698122 Mon Sep 17 00:00:00 2001 From: Ton Huisman Date: Sat, 11 Jul 2026 16:44:45 +0200 Subject: [PATCH] [P020] Send events for Derived values --- src/_P020_Ser2Net.ino | 1 + src/src/PluginStructs/P020_data_struct.cpp | 48 ++++++++++++++-------- src/src/PluginStructs/P020_data_struct.h | 8 +++- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/_P020_Ser2Net.ino b/src/_P020_Ser2Net.ino index a7b73e0f9..5c302a6cf 100644 --- a/src/_P020_Ser2Net.ino +++ b/src/_P020_Ser2Net.ino @@ -8,6 +8,7 @@ /************ * Changelog: + * 2026-07-11 tonhuisman: Send also Derived values when sending the event with received data, when enabled and available * 2025-03-26 tonhuisman: Add optional receiving (relaying) of UDP data to Serial. Uses the same port as configured for the (default) TCP * connection. Available on ESP32 only. * Can be enabled for ESP8266 in a Custom build by adding #define P020_USE_PROTOCOL 1 diff --git a/src/src/PluginStructs/P020_data_struct.cpp b/src/src/PluginStructs/P020_data_struct.cpp index d30b6b708..0c130e9d0 100644 --- a/src/src/PluginStructs/P020_data_struct.cpp +++ b/src/src/PluginStructs/P020_data_struct.cpp @@ -297,7 +297,8 @@ void P020_Task::handleSerialIn(struct EventStruct *event) { bool done = false; char ch; - do { + do + { if (ser2netSerial->available()) { if ((serial_processing != P020_Events::P1WiFiGateway) // P1 handling without this check && (serial_buffer.length() > static_cast(P020_RX_BUFFER))) { @@ -357,7 +358,7 @@ void P020_Task::handleSerialIn(struct EventStruct *event) { blinkLED(); - rulesEngine(serial_buffer); + rulesEngine(serial_buffer, event); ser2netClient.PR_9453_FLUSH_TO_CLEAR(); clearBuffer(); # ifndef BUILD_NO_DEBUG @@ -375,14 +376,16 @@ void P020_Task::discardSerialIn() { } // We can also use the rules engine for local control! -void P020_Task::rulesEngine(const String& message) { +void P020_Task::rulesEngine(const String& message, struct EventStruct *event) { if (!Settings.UseRules || message.isEmpty() || (P020_Events::None == serial_processing)) { return; } - int NewLinePos = 0; - uint16_t StartPos = 0; + int NewLinePos = 0; + uint16_t StartPos = 0; + bool eventSent = false; NewLinePos = handleMultiLine ? message.indexOf('\n', StartPos) : message.length(); - do { + do + { if (NewLinePos < 0) { NewLinePos = message.length(); } @@ -394,11 +397,15 @@ void P020_Task::rulesEngine(const String& message) { NewLinePos--; } - switch (serial_processing) { - case P020_Events::None: { break; } - case P020_Events::Generic: { // Generic + switch (serial_processing) + { + case P020_Events::None: + { break; + } + case P020_Events::Generic: // Generic + { if (NewLinePos > StartPos) { - eventString = '!'; // F("!Serial"); + eventString = '!'; // F("!Serial"); if (_serialId) { eventString += ESPEasySerialPort_toString(_port, true); @@ -421,8 +428,9 @@ void P020_Task::rulesEngine(const String& message) { } break; } - case P020_Events::RFLink: { // RFLink - StartPos += 6; // RFLink, strip 20;xx; from incoming message + case P020_Events::RFLink: // RFLink + { + StartPos += 6; // RFLink, strip 20;xx; from incoming message if (((NewLinePos - StartPos) >= 8) && message.substring(StartPos, StartPos + 8) @@ -442,6 +450,7 @@ void P020_Task::rulesEngine(const String& message) { eventString += message.substring(StartPos, NewLinePos); } eventQueue.addMove(std::move(eventString)); + eventSent = true; break; } case P020_Events::P1WiFiGateway: // P1 WiFi Gateway @@ -466,6 +475,7 @@ void P020_Task::rulesEngine(const String& message) { if (!eventString.isEmpty()) { eventQueue.add(eventString); + eventSent = true; } NewLinePos = message.indexOf('\n', StartPos); @@ -473,6 +483,12 @@ void P020_Task::rulesEngine(const String& message) { NewLinePos = message.length(); } } while (handleMultiLine && NewLinePos > StartPos); + + #if FEATURE_STRING_VARIABLES + if (eventSent) { + sendData(event); // Send derived values + } + #endif // if FEATURE_STRING_VARIABLES } bool P020_Task::isInit() const { @@ -483,10 +499,7 @@ bool P020_Task::isInit() const { ) && nullptr != ser2netSerial; } -void P020_Task::sendConnectedEvent(bool connected) -{ - eventQueue.add(_taskIndex, F("Client"), (connected ? 1 : 0)); -} +void P020_Task::sendConnectedEvent(bool connected) { eventQueue.add(_taskIndex, F("Client"), (connected ? 1 : 0)); } void P020_Task::blinkLED() { if (_ledEnabled) { @@ -605,7 +618,8 @@ bool P020_Task::handleP1Char(char ch) { bool done = false; bool invalid = false; - switch (_state) { + switch (_state) + { case ParserState::WAITING: if (ch == P020_DATAGRAM_START_CHAR) { diff --git a/src/src/PluginStructs/P020_data_struct.h b/src/src/PluginStructs/P020_data_struct.h index c322f927c..8c3f2784c 100644 --- a/src/src/PluginStructs/P020_data_struct.h +++ b/src/src/PluginStructs/P020_data_struct.h @@ -79,6 +79,7 @@ enum class P020_Events : uint8_t { Generic = 1u, RFLink = 2u, P1WiFiGateway = 3u, + }; # if P020_USE_PROTOCOL @@ -86,7 +87,9 @@ enum class P020_Protocol_e : uint8_t { TCP = 0u, UDP = 1u, TCP_UDP = 2u, + }; + # endif // if P020_USE_PROTOCOL struct P020_Task : public PluginTaskData_base { @@ -94,6 +97,7 @@ struct P020_Task : public PluginTaskData_base { WAITING, READING, CHECKSUM + }; P020_Task(struct EventStruct *event); @@ -120,7 +124,8 @@ struct P020_Task : public PluginTaskData_base { void handleSerialIn(struct EventStruct *event); void handleClientIn(struct EventStruct *event); void discardSerialIn(); - void rulesEngine(const String& message); + void rulesEngine(const String & message, + struct EventStruct *event); bool isInit() const; @@ -187,6 +192,7 @@ struct P020_Task : public PluginTaskData_base { bool _eventAsHex = false; ESPEasySerialPort _port; + }; #endif // ifdef USES_P020