diff --git a/gateway/include/Protocol.h b/gateway/include/Protocol.h index 9d966c9..056adc8 100644 --- a/gateway/include/Protocol.h +++ b/gateway/include/Protocol.h @@ -2,15 +2,24 @@ #include #include +using namespace std; + class Protocol { protected: - unsigned int protocol; + unsigned int no; public: + static unordered_map 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; } - -}; \ No newline at end of file +}; +unordered_map Protocol::mapProtocols; +Protocol fallbackProtocol{ 0 }; diff --git a/gateway/include/Protocol_1.h b/gateway/include/Protocol_1.h index 6d9dd85..112104d 100644 --- a/gateway/include/Protocol_1.h +++ b/gateway/include/Protocol_1.h @@ -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; diff --git a/gateway/include/Protocol_2.h b/gateway/include/Protocol_2.h index 589154e..9e780f2 100644 --- a/gateway/include/Protocol_2.h +++ b/gateway/include/Protocol_2.h @@ -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: } } -}; \ No newline at end of file +} protocol2; \ No newline at end of file diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index 089bdb4..a3714a4 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -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);