From ab4e406b2fba0b26e0dbd38057e5d23e430bcbfd Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 25 Oct 2022 23:23:49 +0200 Subject: [PATCH 1/6] return by value split protocol handling --- gateway/include/Dht.h | 17 ++++--- gateway/include/RcDecoder.h | 5 +- gateway/src/gateway.cpp | 91 +++++++++++++++++++++---------------- 3 files changed, 66 insertions(+), 47 deletions(-) diff --git a/gateway/include/Dht.h b/gateway/include/Dht.h index 0c0d62b..6432e3e 100644 --- a/gateway/include/Dht.h +++ b/gateway/include/Dht.h @@ -9,27 +9,30 @@ DHT dht = DHT(DHT11_PIN, DHT11); unsigned long currentTime = 0; namespace Dht { - void setup() { + void setup() { dht.begin(); - } + } - void read(JsonDocument& jsonDoc) { + void read() { currentTime = millis(); static unsigned long lastReadTime = 0; if (currentTime > lastReadTime) { lastReadTime = currentTime + READ_INTERVAL(5); + StaticJsonDocument<200> jsonDoc; JsonObject dht11 = jsonDoc.createNestedObject("dht11"); dht11["temperature"] = dht.readTemperature(); dht11["humidity"] = dht.readHumidity(); + serializeJson(jsonDoc, Serial); + Serial.println(); } } } #else namespace Dht { - void setup() { - } - - void read(JsonDocument& jsonDoc) { + void setup() { } + + void read() { +} } #endif \ No newline at end of file diff --git a/gateway/include/RcDecoder.h b/gateway/include/RcDecoder.h index d29362a..ba4d53c 100644 --- a/gateway/include/RcDecoder.h +++ b/gateway/include/RcDecoder.h @@ -10,13 +10,15 @@ namespace RcDecoder { byte device; }; - void decode(unsigned long value, RcSwitch& decoded) { + RcSwitch decode(unsigned long value) { value = value >> 2; unsigned long res = 0; for (int i = 0; i < 12; i++) { res |= ((value & 1) ^ 1) << i; value = value >> 2; } + + RcSwitch decoded; decoded.state = RC_STATE(res); decoded.group = RC_GROUP(res); switch (RC_DEVICE(res)) { @@ -36,5 +38,6 @@ namespace RcDecoder { decoded.device = 5; break; } + return decoded; } } \ No newline at end of file diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index b141d9a..abdc6b2 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -13,7 +13,7 @@ RCSwitch mySwitch = RCSwitch(); -void readRcSwitch(JsonDocument& jsonDoc); +void readRcSwitch(); void readCommand(); void setup() { @@ -34,13 +34,8 @@ void setup() { void loop() { readCommand(); - StaticJsonDocument<200> jsonDoc; - readRcSwitch(jsonDoc); - Dht::read(jsonDoc); - if (!jsonDoc.isNull()) { - serializeJson(jsonDoc, Serial); - Serial.println(); - } + readRcSwitch(); + Dht::read(); } bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) { @@ -73,36 +68,56 @@ bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) { return true; } -void readRcSwitch(JsonDocument& jsonDoc) { +void handleProtocol2(JsonDocument& jsonDoc, unsigned long value) { + switch (value) { + case 637541753L: + case 771759481L: { + JsonObject motion = jsonDoc.createNestedObject("motion"); + motion["kitchen"] = value == 637541753L ? "on" : "off"; + break; + } + case 1879048230L: + case 1879048198L: { + JsonObject motion = jsonDoc.createNestedObject("motion"); + motion["basement"] = value == 1879048230L ? "on" : "off"; + break; + } + default: + buildSensorJson(jsonDoc, value); + break; + } +} + +void buildRcSwitch(JsonDocument& jsonDoc, unsigned int protocol, unsigned long value) { + JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch"); + rcSwitch["protocol"] = protocol; + if (protocol == 1) { + RcDecoder::RcSwitch decoded = RcDecoder::decode(value); + rcSwitch["state"] = decoded.state; + rcSwitch["group"] = String(decoded.group, BIN); + rcSwitch["channel"] = decoded.device; + } else { + rcSwitch["value"] = value; + } +} + +void readRcSwitch() { if (mySwitch.available()) { unsigned long value = mySwitch.getReceivedValue(); mySwitch.resetAvailable(); - if (mySwitch.getReceivedProtocol() == 2) { - if (value == 637541753L || value == 771759481L) { - JsonObject motion = jsonDoc.createNestedObject("motion"); - motion["kitchen"] = value == 637541753L ? "on" : "off"; - return; - } - if (value == 1879048230L || value == 1879048198L) { - JsonObject motion = jsonDoc.createNestedObject("motion"); - motion["basement"] = value == 1879048230L ? "on" : "off"; - return; - } - if (buildSensorJson(jsonDoc, value)) { - return; - } + StaticJsonDocument<200> jsonDoc; + switch (mySwitch.getReceivedProtocol()) { + case 1: + handleProtocol2(jsonDoc, value); + break; + default: + buildRcSwitch(jsonDoc, mySwitch.getReceivedProtocol(), value); + break; } - JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch"); - rcSwitch["protocol"] = mySwitch.getReceivedProtocol(); - if (mySwitch.getReceivedProtocol() == 1) { - RcDecoder::RcSwitch decoded; - RcDecoder::decode(value, decoded); - rcSwitch["state"] = decoded.state; - rcSwitch["group"] = String(decoded.group, BIN); - rcSwitch["channel"] = decoded.device; - } else { - rcSwitch["value"] = value; + if (!jsonDoc.isNull()) { + serializeJson(jsonDoc, Serial); + Serial.println(); } } } @@ -113,8 +128,7 @@ void blink() { digitalWrite(LED_BUILTIN, LOW); } -void runRcSwitchCommand(JsonVariant jsonDoc) { - JsonObject rcSwitch = jsonDoc["rcSwitch"]; +void runRcSwitchCommand(JsonObjectConst rcSwitch) { unsigned int protocol = rcSwitch["protocol"]; if (protocol == 1) { mySwitch.setProtocol(protocol); @@ -125,9 +139,6 @@ void runRcSwitchCommand(JsonVariant jsonDoc) { mySwitch.setProtocol(protocol); mySwitch.send(rcSwitch["value"]); } - serializeJson(jsonDoc, Serial); - Serial.println(); - // blink(); } void runJsonCommands(const char* cmd) { @@ -138,7 +149,9 @@ void runJsonCommands(const char* cmd) { JsonArray array = jsonArray.as(); for (JsonVariant jsonDoc : array) { if (jsonDoc.containsKey("rcSwitch")) { - runRcSwitchCommand(jsonDoc); + runRcSwitchCommand(jsonDoc["rcSwitch"]); + serializeJson(jsonDoc, Serial); + Serial.println(); } } } else { -- 2.47.2 From 06f2186401ce9b980eff7bc65979518850dbea57 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Wed, 26 Oct 2022 08:29:11 +0200 Subject: [PATCH 2/6] separate toJson logic for each protocol --- gateway/include/Protocol.h | 24 ++++++++++++ gateway/include/Protocol_1.h | 24 ++++++++++++ gateway/include/Protocol_2.h | 64 +++++++++++++++++++++++++++++++ gateway/src/gateway.cpp | 73 ++++-------------------------------- 4 files changed, 119 insertions(+), 66 deletions(-) create mode 100644 gateway/include/Protocol.h create mode 100644 gateway/include/Protocol_1.h create mode 100644 gateway/include/Protocol_2.h diff --git a/gateway/include/Protocol.h b/gateway/include/Protocol.h new file mode 100644 index 0000000..8a9120f --- /dev/null +++ b/gateway/include/Protocol.h @@ -0,0 +1,24 @@ +# pragma once +#include +#include + +class Protocol { +private: + unsigned int protocol; + +public: + Protocol(unsigned int protocol) { + this->protocol = protocol; + } + + virtual void fromJson(JsonDocument& jsonDoc, RCSwitch& rcDevice) { + + } + + virtual void toJson(unsigned long value, JsonDocument& jsonDoc) { + JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch"); + rcSwitch["protocol"] = protocol; + rcSwitch["value"] = value; + } + +}; \ No newline at end of file diff --git a/gateway/include/Protocol_1.h b/gateway/include/Protocol_1.h new file mode 100644 index 0000000..b3656e0 --- /dev/null +++ b/gateway/include/Protocol_1.h @@ -0,0 +1,24 @@ +# pragma once +#include "Protocol.h" +#include "RcDecoder.h" + +class Protocol_1 : public Protocol { + +public: + Protocol_1() : Protocol(1) { + } + + void fromJson(JsonDocument& jsonDoc, RCSwitch& rcDevice) { + + } + + void toJson(unsigned long value, JsonDocument& jsonDoc) override { + JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch"); + rcSwitch["protocol"] = 1; + RcDecoder::RcSwitch decoded = RcDecoder::decode(value); + rcSwitch["state"] = decoded.state; + rcSwitch["group"] = String(decoded.group, BIN); + rcSwitch["channel"] = decoded.device; + } + +} protocol_1; \ No newline at end of file diff --git a/gateway/include/Protocol_2.h b/gateway/include/Protocol_2.h new file mode 100644 index 0000000..34229b8 --- /dev/null +++ b/gateway/include/Protocol_2.h @@ -0,0 +1,64 @@ +# pragma once +#include "Protocol.h" +#include "Tiny.h" + +class Protocol_2 : public Protocol { + +public: + Protocol_2() : Protocol(2) { + } + + void toJson(unsigned long value, JsonDocument& jsonDoc) override { + switch (value) { + case 637541753L: + case 771759481L: { + JsonObject motion = jsonDoc.createNestedObject("motion"); + motion["kitchen"] = value == 637541753L ? "on" : "off"; + break; + } + case 1879048230L: + case 1879048198L: { + JsonObject motion = jsonDoc.createNestedObject("motion"); + motion["basement"] = value == 1879048230L ? "on" : "off"; + break; + } + default: + if (!buildSensorJson(jsonDoc, value)) { + Protocol::toJson(value, jsonDoc); + } + break; + } + } + +private: + bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) { + JsonObject sensor = jsonDoc.createNestedObject("sensor"); + sensor["id"] = ID(value); + + float voltage = (float)GET_VCC(value) / 1000; + if (voltage != 0) { + JsonObject diagnostic = sensor.createNestedObject("diagnostic"); + diagnostic["voltage"] = voltage; + } + + switch (GET_TYPE(value)) { + case SensorType::GENERIC: + sensor["value"] = GET_VALUE(value); + break; + case SensorType::TEMPERATURE: + sensor["temperature"] = (float)GET_TEMP(value) / 10; + break; + case SensorType::HUMIDITY: + sensor["humidity"] = (float)GET_HUMIDITY(value) / 10; + break; + case SensorType::CONTACT: + sensor["state"] = GET_STATE(value) ? "on" : "off"; + break; + default: + return false; + } + + return true; + } + +} protocol_2; \ No newline at end of file diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index abdc6b2..042f1ce 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -4,7 +4,8 @@ #include "Tiny.h" #include #include "Dht.h" -#include "RcDecoder.h" +#include "Protocol_1.h" +#include "Protocol_2.h" #define RESET_PIN 10 #define SEND_PIN 11 @@ -38,69 +39,6 @@ void loop() { Dht::read(); } -bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) { - JsonObject sensor = jsonDoc.createNestedObject("sensor"); - sensor["id"] = ID(value); - - float voltage = (float)GET_VCC(value) / 1000; - if (voltage != 0) { - JsonObject diagnostic = sensor.createNestedObject("diagnostic"); - diagnostic["voltage"] = voltage; - } - - switch (GET_TYPE(value)) { - case SensorType::GENERIC: - sensor["value"] = GET_VALUE(value); - break; - case SensorType::TEMPERATURE: - sensor["temperature"] = (float)GET_TEMP(value) / 10; - break; - case SensorType::HUMIDITY: - sensor["humidity"] = (float)GET_HUMIDITY(value) / 10; - break; - case SensorType::CONTACT: - sensor["state"] = GET_STATE(value) ? "on" : "off"; - break; - default: - return false; - } - - return true; -} - -void handleProtocol2(JsonDocument& jsonDoc, unsigned long value) { - switch (value) { - case 637541753L: - case 771759481L: { - JsonObject motion = jsonDoc.createNestedObject("motion"); - motion["kitchen"] = value == 637541753L ? "on" : "off"; - break; - } - case 1879048230L: - case 1879048198L: { - JsonObject motion = jsonDoc.createNestedObject("motion"); - motion["basement"] = value == 1879048230L ? "on" : "off"; - break; - } - default: - buildSensorJson(jsonDoc, value); - break; - } -} - -void buildRcSwitch(JsonDocument& jsonDoc, unsigned int protocol, unsigned long value) { - JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch"); - rcSwitch["protocol"] = protocol; - if (protocol == 1) { - RcDecoder::RcSwitch decoded = RcDecoder::decode(value); - rcSwitch["state"] = decoded.state; - rcSwitch["group"] = String(decoded.group, BIN); - rcSwitch["channel"] = decoded.device; - } else { - rcSwitch["value"] = value; - } -} - void readRcSwitch() { if (mySwitch.available()) { unsigned long value = mySwitch.getReceivedValue(); @@ -108,11 +46,14 @@ void readRcSwitch() { StaticJsonDocument<200> jsonDoc; switch (mySwitch.getReceivedProtocol()) { + case 2: + protocol_2.toJson(value, jsonDoc); + break; case 1: - handleProtocol2(jsonDoc, value); + protocol_1.toJson(value, jsonDoc); break; default: - buildRcSwitch(jsonDoc, mySwitch.getReceivedProtocol(), value); + Protocol(mySwitch.getReceivedProtocol()).toJson(value, jsonDoc); break; } if (!jsonDoc.isNull()) { -- 2.47.2 From 015607d2a9733f7da3858220e5e368a3d4d479c4 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Wed, 26 Oct 2022 08:44:33 +0200 Subject: [PATCH 3/6] separate fromJson logic --- gateway/include/Protocol.h | 6 ++-- gateway/include/Protocol_1.h | 60 +++++++++++++++++++++++++++++++----- gateway/include/RcDecoder.h | 43 -------------------------- gateway/src/gateway.cpp | 39 +++++++++-------------- 4 files changed, 70 insertions(+), 78 deletions(-) delete mode 100644 gateway/include/RcDecoder.h diff --git a/gateway/include/Protocol.h b/gateway/include/Protocol.h index 8a9120f..09ab9fb 100644 --- a/gateway/include/Protocol.h +++ b/gateway/include/Protocol.h @@ -11,8 +11,10 @@ public: this->protocol = protocol; } - virtual void fromJson(JsonDocument& jsonDoc, RCSwitch& rcDevice) { - + virtual void fromJson(JsonObjectConst rcSwitch, RCSwitch& rcDevice) { + unsigned int protocol = rcSwitch["protocol"]; + rcDevice.setProtocol(protocol); + rcDevice.send(rcSwitch["value"]); } virtual void toJson(unsigned long value, JsonDocument& jsonDoc) { diff --git a/gateway/include/Protocol_1.h b/gateway/include/Protocol_1.h index b3656e0..c549fc0 100644 --- a/gateway/include/Protocol_1.h +++ b/gateway/include/Protocol_1.h @@ -1,6 +1,9 @@ # pragma once #include "Protocol.h" -#include "RcDecoder.h" + +#define RC_STATE(value) value & 0x1 +#define RC_DEVICE(value) (value >> 1) & 0x1F +#define RC_GROUP(value) (value >> 6) & 0x1F class Protocol_1 : public Protocol { @@ -8,17 +11,58 @@ public: Protocol_1() : Protocol(1) { } - void fromJson(JsonDocument& jsonDoc, RCSwitch& rcDevice) { - + void fromJson(JsonObjectConst rcSwitch, RCSwitch& rcDevice) override { + unsigned int protocol = rcSwitch["protocol"]; + rcDevice.setProtocol(protocol); + char* group = rcSwitch["group"]; + int channel = rcSwitch["channel"]; + rcSwitch["state"] ? rcDevice.switchOn(group, channel) : rcDevice.switchOff(group, channel); } void toJson(unsigned long value, JsonDocument& jsonDoc) override { JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch"); rcSwitch["protocol"] = 1; - RcDecoder::RcSwitch decoded = RcDecoder::decode(value); - rcSwitch["state"] = decoded.state; - rcSwitch["group"] = String(decoded.group, BIN); - rcSwitch["channel"] = decoded.device; + Decoder decoder; + decoder.decode(value); + rcSwitch["state"] = decoder.state; + rcSwitch["group"] = String(decoder.group, BIN); + rcSwitch["channel"] = decoder.device; } -} protocol_1; \ No newline at end of file +private: + struct Decoder { + bool state; + char group; + byte device; + + void decode(unsigned long value) { + value = value >> 2; + unsigned long res = 0; + for (int i = 0; i < 12; i++) { + res |= ((value & 1) ^ 1) << i; + value = value >> 2; + } + + state = RC_STATE(res); + group = RC_GROUP(res); + switch (RC_DEVICE(res)) { + case 0b10000: + device = 1; + break; + case 0b01000: + device = 2; + break; + case 0b00100: + device = 3; + break; + case 0b00010: + device = 4; + break; + case 0b00001: + device = 5; + break; + } + } + }; +} protocol_1; + diff --git a/gateway/include/RcDecoder.h b/gateway/include/RcDecoder.h deleted file mode 100644 index ba4d53c..0000000 --- a/gateway/include/RcDecoder.h +++ /dev/null @@ -1,43 +0,0 @@ -#define RC_STATE(value) value & 0x1 -#define RC_DEVICE(value) (value >> 1) & 0x1F -#define RC_GROUP(value) (value >> 6) & 0x1F - -namespace RcDecoder { - - struct RcSwitch { - bool state; - char group; - byte device; - }; - - RcSwitch decode(unsigned long value) { - value = value >> 2; - unsigned long res = 0; - for (int i = 0; i < 12; i++) { - res |= ((value & 1) ^ 1) << i; - value = value >> 2; - } - - RcSwitch decoded; - decoded.state = RC_STATE(res); - decoded.group = RC_GROUP(res); - switch (RC_DEVICE(res)) { - case 0b10000: - decoded.device = 1; - break; - case 0b01000: - decoded.device = 2; - break; - case 0b00100: - decoded.device = 3; - break; - case 0b00010: - decoded.device = 4; - break; - case 0b00001: - decoded.device = 5; - break; - } - return decoded; - } -} \ No newline at end of file diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index 042f1ce..55a65af 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -39,23 +39,24 @@ void loop() { Dht::read(); } +Protocol findProtocol(unsigned int protocol) { + switch (mySwitch.getReceivedProtocol()) { + case 1: + return protocol_1; + case 2: + return protocol_2; + default: + return Protocol(protocol); + } +} + void readRcSwitch() { if (mySwitch.available()) { unsigned long value = mySwitch.getReceivedValue(); mySwitch.resetAvailable(); StaticJsonDocument<200> jsonDoc; - switch (mySwitch.getReceivedProtocol()) { - case 2: - protocol_2.toJson(value, jsonDoc); - break; - case 1: - protocol_1.toJson(value, jsonDoc); - break; - default: - Protocol(mySwitch.getReceivedProtocol()).toJson(value, jsonDoc); - break; - } + findProtocol(mySwitch.getReceivedProtocol()).toJson(value, jsonDoc); if (!jsonDoc.isNull()) { serializeJson(jsonDoc, Serial); Serial.println(); @@ -69,19 +70,6 @@ void blink() { digitalWrite(LED_BUILTIN, LOW); } -void runRcSwitchCommand(JsonObjectConst rcSwitch) { - unsigned int protocol = rcSwitch["protocol"]; - if (protocol == 1) { - mySwitch.setProtocol(protocol); - char* group = rcSwitch["group"]; - int channel = rcSwitch["channel"]; - rcSwitch["state"] ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel); - } else { - mySwitch.setProtocol(protocol); - mySwitch.send(rcSwitch["value"]); - } -} - void runJsonCommands(const char* cmd) { String origCmd = String(cmd); StaticJsonDocument<512> jsonArray; @@ -90,7 +78,8 @@ void runJsonCommands(const char* cmd) { JsonArray array = jsonArray.as(); for (JsonVariant jsonDoc : array) { if (jsonDoc.containsKey("rcSwitch")) { - runRcSwitchCommand(jsonDoc["rcSwitch"]); + JsonObjectConst rcSwitch = jsonDoc["rcSwitch"]; + findProtocol(rcSwitch["protocol"]).fromJson(rcSwitch, mySwitch); serializeJson(jsonDoc, Serial); Serial.println(); } -- 2.47.2 From 3e4c3f346187609aec76e0e8a85db99565bdef40 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Wed, 26 Oct 2022 09:52:37 +0200 Subject: [PATCH 4/6] cleanup --- gateway/include/Dht.h | 1 + gateway/include/Protocol.h | 2 +- gateway/src/gateway.cpp | 18 ++++++------------ 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/gateway/include/Dht.h b/gateway/include/Dht.h index 6432e3e..81aafb9 100644 --- a/gateway/include/Dht.h +++ b/gateway/include/Dht.h @@ -1,5 +1,6 @@ #if DHT_SENSOR +#include #include #define DHT11_PIN 12 diff --git a/gateway/include/Protocol.h b/gateway/include/Protocol.h index 09ab9fb..c5af972 100644 --- a/gateway/include/Protocol.h +++ b/gateway/include/Protocol.h @@ -3,7 +3,7 @@ #include class Protocol { -private: + unsigned int protocol; public: diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index 55a65af..56ba21e 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -1,8 +1,5 @@ #include #include -#include -#include "Tiny.h" -#include #include "Dht.h" #include "Protocol_1.h" #include "Protocol_2.h" @@ -14,9 +11,6 @@ RCSwitch mySwitch = RCSwitch(); -void readRcSwitch(); -void readCommand(); - void setup() { digitalWrite(RESET_PIN, HIGH); pinMode(LED_BUILTIN, OUTPUT); @@ -33,12 +27,6 @@ void setup() { delay(1000); } -void loop() { - readCommand(); - readRcSwitch(); - Dht::read(); -} - Protocol findProtocol(unsigned int protocol) { switch (mySwitch.getReceivedProtocol()) { case 1: @@ -107,3 +95,9 @@ void readCommand() { runJsonCommands(cmd.c_str()); } } + +void loop() { + readCommand(); + readRcSwitch(); + Dht::read(); +} -- 2.47.2 From 101564bb6a728257a4002ebd7a437a8d66aa7325 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Wed, 26 Oct 2022 23:30:05 +0200 Subject: [PATCH 5/6] separate sensor from generic in protocol 2 --- gateway/include/Protocol.h | 4 ++-- gateway/include/Protocol_1.h | 4 ++-- gateway/include/Protocol_2.h | 9 ++++++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/gateway/include/Protocol.h b/gateway/include/Protocol.h index c5af972..a20e0c1 100644 --- a/gateway/include/Protocol.h +++ b/gateway/include/Protocol.h @@ -1,9 +1,9 @@ -# pragma once +#pragma once #include #include class Protocol { - +protected: unsigned int protocol; public: diff --git a/gateway/include/Protocol_1.h b/gateway/include/Protocol_1.h index c549fc0..740e61b 100644 --- a/gateway/include/Protocol_1.h +++ b/gateway/include/Protocol_1.h @@ -1,4 +1,4 @@ -# pragma once +#pragma once #include "Protocol.h" #define RC_STATE(value) value & 0x1 @@ -21,7 +21,7 @@ public: void toJson(unsigned long value, JsonDocument& jsonDoc) override { JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch"); - rcSwitch["protocol"] = 1; + rcSwitch["protocol"] = protocol; Decoder decoder; decoder.decode(value); rcSwitch["state"] = decoder.state; diff --git a/gateway/include/Protocol_2.h b/gateway/include/Protocol_2.h index 34229b8..4820c09 100644 --- a/gateway/include/Protocol_2.h +++ b/gateway/include/Protocol_2.h @@ -1,4 +1,4 @@ -# pragma once +#pragma once #include "Protocol.h" #include "Tiny.h" @@ -23,7 +23,10 @@ public: break; } default: - if (!buildSensorJson(jsonDoc, value)) { + StaticJsonDocument<200> jsonSensor; + if (buildSensorJson(value, jsonSensor)) { + jsonDoc.add(jsonSensor); + } else { Protocol::toJson(value, jsonDoc); } break; @@ -31,7 +34,7 @@ public: } private: - bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) { + bool buildSensorJson(unsigned long value, JsonDocument& jsonDoc) { JsonObject sensor = jsonDoc.createNestedObject("sensor"); sensor["id"] = ID(value); -- 2.47.2 From 7df46560ee2200c766a82351395078a97ba7d610 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Thu, 27 Oct 2022 10:48:57 +0200 Subject: [PATCH 6/6] use reference to avoid copying --- gateway/include/Protocol.h | 2 +- gateway/include/Protocol_1.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gateway/include/Protocol.h b/gateway/include/Protocol.h index a20e0c1..16da3bf 100644 --- a/gateway/include/Protocol.h +++ b/gateway/include/Protocol.h @@ -11,7 +11,7 @@ public: this->protocol = protocol; } - virtual void fromJson(JsonObjectConst rcSwitch, RCSwitch& rcDevice) { + virtual void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) { unsigned int protocol = rcSwitch["protocol"]; rcDevice.setProtocol(protocol); rcDevice.send(rcSwitch["value"]); diff --git a/gateway/include/Protocol_1.h b/gateway/include/Protocol_1.h index 740e61b..1a50917 100644 --- a/gateway/include/Protocol_1.h +++ b/gateway/include/Protocol_1.h @@ -11,7 +11,7 @@ public: Protocol_1() : Protocol(1) { } - void fromJson(JsonObjectConst rcSwitch, RCSwitch& rcDevice) override { + void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) override { unsigned int protocol = rcSwitch["protocol"]; rcDevice.setProtocol(protocol); char* group = rcSwitch["group"]; -- 2.47.2