separate fromJson logic

This commit is contained in:
Nicu Hodos 2022-10-26 08:44:33 +02:00
parent 1b984d4987
commit c053cc9310
4 changed files with 70 additions and 78 deletions

View File

@ -11,8 +11,10 @@ public:
this->protocol = protocol;
}
virtual void fromJson(JsonDocument& jsonDoc, RCSwitch& rcDevice) {
virtual void fromJson(JsonObjectConst rcSwitch, RCSwitch& rcDevice) {
unsigned int protocol = rcSwitch["protocol"];
rcDevice.setProtocol(protocol);
rcDevice.send(rcSwitch["value"]);
}
virtual void toJson(unsigned long value, JsonDocument& jsonDoc) {

View File

@ -1,6 +1,9 @@
# pragma once
#include "Protocol.h"
#include "RcDecoder.h"
#define RC_STATE(value) value & 0x1
#define RC_DEVICE(value) (value >> 1) & 0x1F
#define RC_GROUP(value) (value >> 6) & 0x1F
class Protocol_1 : public Protocol {
@ -8,17 +11,58 @@ public:
Protocol_1() : Protocol(1) {
}
void fromJson(JsonDocument& jsonDoc, RCSwitch& rcDevice) {
void fromJson(JsonObjectConst rcSwitch, RCSwitch& rcDevice) override {
unsigned int protocol = rcSwitch["protocol"];
rcDevice.setProtocol(protocol);
char* group = rcSwitch["group"];
int channel = rcSwitch["channel"];
rcSwitch["state"] ? rcDevice.switchOn(group, channel) : rcDevice.switchOff(group, channel);
}
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;
Decoder decoder;
decoder.decode(value);
rcSwitch["state"] = decoder.state;
rcSwitch["group"] = String(decoder.group, BIN);
rcSwitch["channel"] = decoder.device;
}
} protocol_1;
private:
struct Decoder {
bool state;
char group;
byte device;
void 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;
}
state = RC_STATE(res);
group = RC_GROUP(res);
switch (RC_DEVICE(res)) {
case 0b10000:
device = 1;
break;
case 0b01000:
device = 2;
break;
case 0b00100:
device = 3;
break;
case 0b00010:
device = 4;
break;
case 0b00001:
device = 5;
break;
}
}
};
} protocol_1;

View File

@ -1,43 +0,0 @@
#define RC_STATE(value) value & 0x1
#define RC_DEVICE(value) (value >> 1) & 0x1F
#define RC_GROUP(value) (value >> 6) & 0x1F
namespace RcDecoder {
struct RcSwitch {
bool state;
char group;
byte device;
};
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)) {
case 0b10000:
decoded.device = 1;
break;
case 0b01000:
decoded.device = 2;
break;
case 0b00100:
decoded.device = 3;
break;
case 0b00010:
decoded.device = 4;
break;
case 0b00001:
decoded.device = 5;
break;
}
return decoded;
}
}

View File

@ -39,23 +39,24 @@ void loop() {
Dht::read();
}
Protocol findProtocol(unsigned int protocol) {
switch (mySwitch.getReceivedProtocol()) {
case 1:
return protocol_1;
case 2:
return protocol_2;
default:
return Protocol(protocol);
}
}
void readRcSwitch() {
if (mySwitch.available()) {
unsigned long value = mySwitch.getReceivedValue();
mySwitch.resetAvailable();
StaticJsonDocument<200> jsonDoc;
switch (mySwitch.getReceivedProtocol()) {
case 2:
protocol_2.toJson(value, jsonDoc);
break;
case 1:
protocol_1.toJson(value, jsonDoc);
break;
default:
Protocol(mySwitch.getReceivedProtocol()).toJson(value, jsonDoc);
break;
}
findProtocol(mySwitch.getReceivedProtocol()).toJson(value, jsonDoc);
if (!jsonDoc.isNull()) {
serializeJson(jsonDoc, Serial);
Serial.println();
@ -69,19 +70,6 @@ void blink() {
digitalWrite(LED_BUILTIN, LOW);
}
void runRcSwitchCommand(JsonObjectConst 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"]);
}
}
void runJsonCommands(const char* cmd) {
String origCmd = String(cmd);
StaticJsonDocument<512> jsonArray;
@ -90,7 +78,8 @@ void runJsonCommands(const char* cmd) {
JsonArray array = jsonArray.as<JsonArray>();
for (JsonVariant jsonDoc : array) {
if (jsonDoc.containsKey("rcSwitch")) {
runRcSwitchCommand(jsonDoc["rcSwitch"]);
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
findProtocol(rcSwitch["protocol"]).fromJson(rcSwitch, mySwitch);
serializeJson(jsonDoc, Serial);
Serial.println();
}