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 <RCSwitch.h>
|
||||||
#include "RcDecoder.h"
|
#include "RcDecoder.h"
|
||||||
|
#include "JsonHA.h"
|
||||||
|
|
||||||
RCSwitch mySwitch = RCSwitch();
|
RCSwitch mySwitch = RCSwitch();
|
||||||
|
|
||||||
@ -12,36 +13,6 @@ namespace Rc {
|
|||||||
mySwitch.setRepeatTransmit(10);
|
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) {
|
void readRcSwitch(JsonDocument& jsonDoc) {
|
||||||
if (mySwitch.available()) {
|
if (mySwitch.available()) {
|
||||||
unsigned long value = mySwitch.getReceivedValue();
|
unsigned long value = mySwitch.getReceivedValue();
|
||||||
@ -58,21 +29,24 @@ namespace Rc {
|
|||||||
motion["basement"] = value == 1879048230L ? "on" : "off";
|
motion["basement"] = value == 1879048230L ? "on" : "off";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (buildSensorJson(jsonDoc, value)) {
|
if (JsonHA::buildSensor(jsonDoc, value)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
|
JsonHA::buildRcSwitch(jsonDoc, mySwitch.getReceivedProtocol(), value);
|
||||||
rcSwitch["protocol"] = mySwitch.getReceivedProtocol();
|
}
|
||||||
if (mySwitch.getReceivedProtocol() == 1) {
|
}
|
||||||
RcDecoder::RcSwitch decoded;
|
|
||||||
RcDecoder::decode(value, decoded);
|
void runRcSwitchCommand(JsonObject rcSwitch) {
|
||||||
rcSwitch["state"] = decoded.state;
|
unsigned int protocol = rcSwitch["protocol"];
|
||||||
rcSwitch["group"] = String(decoded.group, BIN);
|
if (protocol == 1) {
|
||||||
rcSwitch["channel"] = decoded.device;
|
mySwitch.setProtocol(protocol);
|
||||||
|
char* group = rcSwitch["group"];
|
||||||
|
int channel = rcSwitch["channel"];
|
||||||
|
rcSwitch["state"] ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel);
|
||||||
} else {
|
} else {
|
||||||
rcSwitch["value"] = value;
|
mySwitch.setProtocol(protocol);
|
||||||
}
|
mySwitch.send(rcSwitch["value"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,23 +3,6 @@
|
|||||||
|
|
||||||
namespace SerialInput {
|
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) {
|
void runJsonCommands(const char* cmd) {
|
||||||
String origCmd = String(cmd);
|
String origCmd = String(cmd);
|
||||||
StaticJsonDocument<512> jsonArray;
|
StaticJsonDocument<512> jsonArray;
|
||||||
@ -28,7 +11,9 @@ namespace SerialInput {
|
|||||||
JsonArray array = jsonArray.as<JsonArray>();
|
JsonArray array = jsonArray.as<JsonArray>();
|
||||||
for (JsonVariant jsonDoc : array) {
|
for (JsonVariant jsonDoc : array) {
|
||||||
if (jsonDoc.containsKey("rcSwitch")) {
|
if (jsonDoc.containsKey("rcSwitch")) {
|
||||||
runRcSwitchCommand(jsonDoc);
|
Rc::runRcSwitchCommand(jsonDoc["rcSwitch"]);
|
||||||
|
serializeJson(jsonDoc, Serial);
|
||||||
|
Serial.println();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user