From 8f95262fd4151b358f8beee906c2fc00f1b09692 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 25 Oct 2022 23:23:49 +0200 Subject: [PATCH] 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 {