refactor 2
This commit is contained in:
parent
e355a77a16
commit
ad2e8afc9f
47
gateway/include/JsonHA.h
Normal file
47
gateway/include/JsonHA.h
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
#include <RCSwitch.h>
|
||||
#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"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<JsonArray>();
|
||||
for (JsonVariant jsonDoc : array) {
|
||||
if (jsonDoc.containsKey("rcSwitch")) {
|
||||
runRcSwitchCommand(jsonDoc);
|
||||
Rc::runRcSwitchCommand(jsonDoc["rcSwitch"]);
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user