separate toJson logic for each protocol

This commit is contained in:
Nicu Hodos 2022-10-26 08:29:11 +02:00
parent ab4e406b2f
commit 06f2186401
4 changed files with 119 additions and 66 deletions

View File

@ -0,0 +1,24 @@
# pragma once
#include <ArduinoJson.h>
#include <RCSwitch.h>
class Protocol {
private:
unsigned int protocol;
public:
Protocol(unsigned int protocol) {
this->protocol = protocol;
}
virtual void fromJson(JsonDocument& jsonDoc, RCSwitch& rcDevice) {
}
virtual void toJson(unsigned long value, JsonDocument& jsonDoc) {
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
rcSwitch["protocol"] = protocol;
rcSwitch["value"] = value;
}
};

View File

@ -0,0 +1,24 @@
# pragma once
#include "Protocol.h"
#include "RcDecoder.h"
class Protocol_1 : public Protocol {
public:
Protocol_1() : Protocol(1) {
}
void fromJson(JsonDocument& jsonDoc, RCSwitch& rcDevice) {
}
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;
}
} protocol_1;

View File

@ -0,0 +1,64 @@
# pragma once
#include "Protocol.h"
#include "Tiny.h"
class Protocol_2 : public Protocol {
public:
Protocol_2() : Protocol(2) {
}
void toJson(unsigned long value, JsonDocument& jsonDoc) override {
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:
if (!buildSensorJson(jsonDoc, value)) {
Protocol::toJson(value, jsonDoc);
}
break;
}
}
private:
bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) {
JsonObject sensor = jsonDoc.createNestedObject("sensor");
sensor["id"] = ID(value);
float voltage = (float)GET_VCC(value) / 1000;
if (voltage != 0) {
JsonObject diagnostic = sensor.createNestedObject("diagnostic");
diagnostic["voltage"] = voltage;
}
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;
}
} protocol_2;

View File

@ -4,7 +4,8 @@
#include "Tiny.h"
#include <ArduinoJson.h>
#include "Dht.h"
#include "RcDecoder.h"
#include "Protocol_1.h"
#include "Protocol_2.h"
#define RESET_PIN 10
#define SEND_PIN 11
@ -38,69 +39,6 @@ void loop() {
Dht::read();
}
bool buildSensorJson(JsonDocument& jsonDoc, unsigned long value) {
JsonObject sensor = jsonDoc.createNestedObject("sensor");
sensor["id"] = ID(value);
float voltage = (float)GET_VCC(value) / 1000;
if (voltage != 0) {
JsonObject diagnostic = sensor.createNestedObject("diagnostic");
diagnostic["voltage"] = voltage;
}
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 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();
@ -108,11 +46,14 @@ void readRcSwitch() {
StaticJsonDocument<200> jsonDoc;
switch (mySwitch.getReceivedProtocol()) {
case 2:
protocol_2.toJson(value, jsonDoc);
break;
case 1:
handleProtocol2(jsonDoc, value);
protocol_1.toJson(value, jsonDoc);
break;
default:
buildRcSwitch(jsonDoc, mySwitch.getReceivedProtocol(), value);
Protocol(mySwitch.getReceivedProtocol()).toJson(value, jsonDoc);
break;
}
if (!jsonDoc.isNull()) {