From c053cc93105522a2ffbc69c2d307778d9dfcf967 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Wed, 26 Oct 2022 08:44:33 +0200 Subject: [PATCH] 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(); }