diff --git a/gateway/include/Protocol.h b/gateway/include/Protocol.h index c923a0a..a00580d 100644 --- a/gateway/include/Protocol.h +++ b/gateway/include/Protocol.h @@ -2,22 +2,34 @@ #include #include -class Protocol { +class ProtocolNo { protected: unsigned int no; public: - constexpr Protocol(unsigned int protocol) : no(protocol) {} - + constexpr ProtocolNo(unsigned int protocol) : no(protocol) {} constexpr operator unsigned int() const { return no; } + ProtocolNo& operator=(unsigned int p){ no = p; return *this;} +}; +constexpr ProtocolNo NO_PROTOCOL{ 0 }; +constexpr ProtocolNo PROTOCOL_1{ 1 }; +constexpr ProtocolNo PROTOCOL_2{ 2 }; +constexpr ProtocolNo PROTOCOL_13{ 13 }; - Protocol& setProtocol(unsigned int p) { +class Protocol { +protected: + ProtocolNo no; + +public: + Protocol(ProtocolNo protocol) : no(protocol) {} + + Protocol& setProtocol(ProtocolNo p) { no = p; return *this; } virtual void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) { - unsigned int protocol = rcSwitch["protocol"]; + ProtocolNo protocol = rcSwitch["protocol"]; rcDevice.setProtocol(protocol); rcDevice.send(rcSwitch["value"]); } @@ -27,4 +39,4 @@ public: rcSwitch["protocol"] = no; rcSwitch["value"] = value; } -} fallbackProtocol{ 0 }; +} fallbackProtocol{ NO_PROTOCOL }; diff --git a/gateway/include/Protocol_1.h b/gateway/include/Protocol_1.h index f759c79..ba57038 100644 --- a/gateway/include/Protocol_1.h +++ b/gateway/include/Protocol_1.h @@ -5,10 +5,10 @@ class Protocol_1 : public Protocol { public: - constexpr Protocol_1() : Protocol(1) {} + Protocol_1() : Protocol(PROTOCOL_1) {} void fromJson(JsonObjectConst& rcSwitch, RCSwitch& rcDevice) override { - unsigned int protocol = rcSwitch["protocol"]; + ProtocolNo protocol = rcSwitch["protocol"]; rcDevice.setProtocol(protocol); const char* group = rcSwitch["group"]; int channel = rcSwitch["channel"]; @@ -33,5 +33,4 @@ public: return std::string{ uId }; } #endif -}; -constexpr Protocol_1 protocol1; \ No newline at end of file +} protocol1; \ No newline at end of file diff --git a/gateway/include/Protocol_2.h b/gateway/include/Protocol_2.h index 024e2ec..a051345 100644 --- a/gateway/include/Protocol_2.h +++ b/gateway/include/Protocol_2.h @@ -5,7 +5,7 @@ class Protocol_2 : public Protocol { public: - constexpr Protocol_2() : Protocol(2) {} + Protocol_2() : Protocol(PROTOCOL_2) {} void toJson(unsigned long value, JsonDocument& jsonDoc) override { switch (value) { @@ -32,5 +32,4 @@ public: } } -}; -constexpr Protocol_2 protocol2; +} protocol2; diff --git a/gateway/include/Protocol_Doorbell.h b/gateway/include/Protocol_Doorbell.h index bd4230f..eb3fc74 100644 --- a/gateway/include/Protocol_Doorbell.h +++ b/gateway/include/Protocol_Doorbell.h @@ -8,7 +8,7 @@ class Protocol_Doorbell : public Protocol { public: - constexpr Protocol_Doorbell() : Protocol(16) {} + Protocol_Doorbell() : Protocol(PROTOCOL_13) {} static void ring(const char* value) { preamble(); @@ -55,5 +55,4 @@ private: } interrupts(); } -}; -constexpr Protocol_Doorbell doorbell; +} doorbell; diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index 2491e3e..33287b8 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -31,13 +31,13 @@ void setup() { delay(1000); } -const Protocol& findProtocol(unsigned int protocol) { +Protocol& findProtocol(unsigned int protocol) { switch (protocol) { - case protocol1: + case PROTOCOL_1: return protocol1; - case protocol2: + case PROTOCOL_2: return protocol2; - case doorbell: + case PROTOCOL_13: return doorbell; default: return fallbackProtocol.setProtocol(protocol);