find protocol based on dynamically created map
This commit is contained in:
parent
1d99c73bdf
commit
b140fd08ac
@ -2,15 +2,24 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <RCSwitch.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Protocol {
|
||||
protected:
|
||||
unsigned int protocol;
|
||||
unsigned int no;
|
||||
|
||||
public:
|
||||
static unordered_map<unsigned int, Protocol*> mapProtocols;
|
||||
|
||||
Protocol(unsigned int protocol) {
|
||||
this->protocol = protocol;
|
||||
no = protocol;
|
||||
mapProtocols.insert({ no, this });
|
||||
}
|
||||
|
||||
Protocol& setProtocol(unsigned int p) {
|
||||
no = p;
|
||||
return *this;
|
||||
}
|
||||
virtual ~Protocol() {}
|
||||
|
||||
virtual void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) {
|
||||
unsigned int protocol = rcSwitch["protocol"];
|
||||
@ -20,8 +29,9 @@ public:
|
||||
|
||||
virtual void toJson(unsigned long value, JsonDocument& jsonDoc) {
|
||||
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
|
||||
rcSwitch["protocol"] = protocol;
|
||||
rcSwitch["protocol"] = no;
|
||||
rcSwitch["value"] = value;
|
||||
}
|
||||
|
||||
};
|
||||
};
|
||||
unordered_map<unsigned int, Protocol*> Protocol::mapProtocols;
|
||||
Protocol fallbackProtocol{ 0 };
|
||||
|
||||
@ -5,8 +5,7 @@
|
||||
class Protocol_1 : public Protocol {
|
||||
|
||||
public:
|
||||
Protocol_1() : Protocol(1) {
|
||||
}
|
||||
Protocol_1() : Protocol(1) {}
|
||||
|
||||
void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) override {
|
||||
unsigned int protocol = rcSwitch["protocol"];
|
||||
@ -18,7 +17,7 @@ public:
|
||||
|
||||
void toJson(unsigned long value, JsonDocument& jsonDoc) override {
|
||||
JsonObject rcSwitch = jsonDoc.createNestedObject("rcSwitch");
|
||||
rcSwitch["protocol"] = protocol;
|
||||
rcSwitch["protocol"] = no;
|
||||
RcDecoder decoder;
|
||||
decoder.decode(value);
|
||||
rcSwitch["state"] = decoder.state;
|
||||
@ -32,4 +31,4 @@ public:
|
||||
sprintf(uId, "%s_%d", group, channel);
|
||||
return std::string{ uId };
|
||||
}
|
||||
};
|
||||
} protocol1;
|
||||
|
||||
@ -5,8 +5,7 @@
|
||||
class Protocol_2 : public Protocol {
|
||||
|
||||
public:
|
||||
Protocol_2() : Protocol(2) {
|
||||
}
|
||||
Protocol_2() : Protocol(2) {}
|
||||
|
||||
void toJson(unsigned long value, JsonDocument& jsonDoc) override {
|
||||
switch (value) {
|
||||
@ -33,4 +32,4 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
} protocol2;
|
||||
@ -31,17 +31,9 @@ void setup() {
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
Protocol* findProtocol(unsigned int protocol) {
|
||||
switch (protocol) {
|
||||
case 1:
|
||||
return new Protocol_1();
|
||||
case 2:
|
||||
return new Protocol_2();
|
||||
case 16:
|
||||
return new Protocol_Doorbell();
|
||||
default:
|
||||
return new Protocol(protocol);
|
||||
}
|
||||
Protocol& findProtocol(unsigned int protocol) {
|
||||
auto p = Protocol::mapProtocols[protocol];
|
||||
return p ? *p : fallbackProtocol.setProtocol(protocol);
|
||||
}
|
||||
|
||||
void readRcSwitch() {
|
||||
@ -56,9 +48,8 @@ void readRcSwitch() {
|
||||
mySwitch.resetAvailable();
|
||||
|
||||
StaticJsonDocument<128> jsonDoc;
|
||||
Protocol* p = findProtocol(mySwitch.getReceivedProtocol());
|
||||
p->toJson(value, jsonDoc);
|
||||
delete p;
|
||||
Protocol& p = findProtocol(mySwitch.getReceivedProtocol());
|
||||
p.toJson(value, jsonDoc);
|
||||
if (!jsonDoc.isNull()) {
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
@ -79,14 +70,13 @@ void handleJsonError(DeserializationError err, const char* cmd) {
|
||||
}
|
||||
|
||||
void runJsonCommand(char* cmd) {
|
||||
StaticJsonDocument<50> jsonDoc;
|
||||
StaticJsonDocument<128> jsonDoc;
|
||||
DeserializationError err = deserializeJson(jsonDoc, cmd);
|
||||
if (err == DeserializationError::Ok) {
|
||||
if (jsonDoc.containsKey("rcSwitch")) {
|
||||
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
||||
Protocol* p = findProtocol(rcSwitch["protocol"]);
|
||||
p->fromJson(rcSwitch, mySwitch);
|
||||
delete p;
|
||||
Protocol& p = findProtocol(rcSwitch["protocol"]);
|
||||
p.fromJson(rcSwitch, mySwitch);
|
||||
serializeJson(jsonDoc, Serial);
|
||||
Serial.println();
|
||||
Board::publishResponse(jsonDoc);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user