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