diff --git a/gateway/include/JsonHA.h b/gateway/include/JsonHA.h new file mode 100644 index 0000000..d805f13 --- /dev/null +++ b/gateway/include/JsonHA.h @@ -0,0 +1,47 @@ +#pragma once + +namespace JsonHA { + bool buildSensor(JsonDocument& jsonDoc, unsigned long value) { + jsonDoc["id"] = ID(value); + + float voltage = (float)GET_VCC(value) / 1000; + if (voltage != 0) { + JsonObject diagnostic = jsonDoc.createNestedObject("diagnostic"); + diagnostic["voltage"] = voltage; + } + + JsonObject sensor = jsonDoc.createNestedObject("sensor"); + 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 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, decoded); + rcSwitch["state"] = decoded.state; + rcSwitch["group"] = String(decoded.group, BIN); + rcSwitch["channel"] = decoded.device; + } else { + rcSwitch["value"] = value; + } + } +} diff --git a/gateway/include/Rc.h b/gateway/include/Rc.h index df71dd9..a72faa1 100644 --- a/gateway/include/Rc.h +++ b/gateway/include/Rc.h @@ -2,6 +2,7 @@ #include #include "RcDecoder.h" +#include "JsonHA.h" RCSwitch mySwitch = RCSwitch(); @@ -12,36 +13,6 @@ namespace Rc { mySwitch.setRepeatTransmit(10); } - bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) { - jsonDoc["id"] = ID(value); - - float voltage = (float)GET_VCC(value) / 1000; - if (voltage != 0) { - JsonObject diagnostic = jsonDoc.createNestedObject("diagnostic"); - diagnostic["voltage"] = voltage; - } - - JsonObject sensor = jsonDoc.createNestedObject("sensor"); - 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 readRcSwitch(JsonDocument& jsonDoc) { if (mySwitch.available()) { unsigned long value = mySwitch.getReceivedValue(); @@ -58,21 +29,24 @@ namespace Rc { motion["basement"] = value == 1879048230L ? "on" : "off"; return; } - if (buildSensorJson(jsonDoc, value)) { + if (JsonHA::buildSensor(jsonDoc, value)) { return; } } - 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; - } + JsonHA::buildRcSwitch(jsonDoc, mySwitch.getReceivedProtocol(), value); + } + } + + void runRcSwitchCommand(JsonObject 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"]); } } } diff --git a/gateway/include/SerialInput.h b/gateway/include/SerialInput.h index be02ef5..ac093dd 100644 --- a/gateway/include/SerialInput.h +++ b/gateway/include/SerialInput.h @@ -3,23 +3,6 @@ namespace SerialInput { - void runRcSwitchCommand(JsonVariant jsonDoc) { - JsonObject rcSwitch = jsonDoc["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"]); - } - serializeJson(jsonDoc, Serial); - Serial.println(); - // blink(); - } - void runJsonCommands(const char* cmd) { String origCmd = String(cmd); StaticJsonDocument<512> jsonArray; @@ -28,7 +11,9 @@ namespace SerialInput { JsonArray array = jsonArray.as(); for (JsonVariant jsonDoc : array) { if (jsonDoc.containsKey("rcSwitch")) { - runRcSwitchCommand(jsonDoc); + Rc::runRcSwitchCommand(jsonDoc["rcSwitch"]); + serializeJson(jsonDoc, Serial); + Serial.println(); } } } else {