Quellcode durchsuchen

Refactor for multiple UDP servers

Chris Mullins vor 8 Jahren
Ursprung
Commit
0a762689ec

+ 51 - 0
lib/Udp/MiLightUdpServer.cpp

@@ -0,0 +1,51 @@
+#include <MiLightUdpServer.h>
+#include <V5MiLightUdpServer.h>
+
+MiLightUdpServer::MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId)
+  : client(client), 
+    port(port),
+    deviceId(deviceId),
+    lastGroup(0)
+{ }
+
+MiLightUdpServer::~MiLightUdpServer() {
+  stop();
+}
+
+void MiLightUdpServer::begin() {
+  socket.begin(this->port);
+}
+
+void MiLightUdpServer::stop() {
+  socket.stop();
+}
+
+void MiLightUdpServer::handleClient() {
+  const size_t packetSize = socket.parsePacket();
+  
+  if (packetSize) {
+    socket.read(packetBuffer, packetSize);
+    
+#ifdef MILIGHT_UDP_DEBUG
+    Serial.print("Handling packet: ");
+    for (size_t i = 0; i < packetSize; i++) {
+      Serial.printf("%02X ", packetBuffer[0])
+    }
+    Serial.println();
+#endif
+    
+    size_t responseSize = handlePacket(packetBuffer, packetSize, responseBuffer);
+    
+    if (responseSize > 0) {
+      socket.write(responseBuffer, responseSize);
+    }
+  }
+}
+
+MiLightUdpServer* MiLightUdpServer::fromVersion(uint8_t version, MiLightClient*& client, uint16_t port, uint16_t deviceId) {
+  if (version == 0 || version == 5) {
+    return new V5MiLightUdpServer(client, port, deviceId);
+  }
+  
+  return NULL;
+}

+ 40 - 0
lib/Udp/MiLightUdpServer.h

@@ -0,0 +1,40 @@
+#include <Arduino.h>
+#include <MiLightClient.h>
+#include <WiFiUdp.h>
+
+// This protocol is documented here:
+// http://www.limitlessled.com/dev/
+
+#define MILIGHT_PACKET_BUFFER_SIZE 10
+
+// Uncomment to enable Serial printing of packets
+//#define MILIGHT_UDP_DEBUG
+
+#ifndef _MILIGHT_UDP_SERVER
+#define _MILIGHT_UDP_SERVER 
+
+class MiLightUdpServer {
+public:
+  MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId);
+  ~MiLightUdpServer();
+    
+  void stop();
+  void begin();
+  void handleClient();
+  
+  static MiLightUdpServer* fromVersion(uint8_t version, MiLightClient*&, uint16_t port, uint16_t deviceId);
+    
+protected:
+  WiFiUDP socket;
+  MiLightClient*& client;
+  uint16_t port;
+  uint16_t deviceId;
+  uint8_t lastGroup;
+  uint8_t packetBuffer[MILIGHT_PACKET_BUFFER_SIZE];
+  uint8_t responseBuffer[MILIGHT_PACKET_BUFFER_SIZE];
+  
+  // Should return size of the response packet
+  virtual size_t handlePacket(uint8_t* packet, size_t packetSize, uint8_t* responseBuffer) = 0;
+};
+
+#endif

+ 14 - 44
lib/MiLight/MiLightUdpServer.cpp

@@ -1,47 +1,17 @@
-#include <MiLightUdpServer.h>
+#include <V5MiLightUdpServer.h>
 
-MiLightUdpServer::MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId)
-  : client(client), 
-    port(port),
-    deviceId(deviceId),
-    lastGroup(0)
-{ }
-
-MiLightUdpServer::~MiLightUdpServer() {
-  stop();
-}
-
-void MiLightUdpServer::begin() {
-  socket.begin(this->port);
-}
-
-void MiLightUdpServer::stop() {
-  socket.stop();
-}
-
-void MiLightUdpServer::handleClient() {
-  const size_t packetSize = socket.parsePacket();
-  
-  if (packetSize) {
-    if (packetSize >= 2 && packetSize <= 3) {
-      socket.read(packetBuffer, packetSize);
-      
-#ifdef MILIGHT_UDP_DEBUG
-      Serial.print("Handling command: ");
-      Serial.print(String(packetBuffer[0], HEX));
-      Serial.print(" ");
-      Serial.println(String(packetBuffer[1], HEX));
-#endif
-      
-      handleCommand(packetBuffer[0], packetBuffer[1]);
-    } else {
-      Serial.print("Error, unexpected packet length (should always be 2-3, was: ");
-      Serial.println(packetSize);
-    }
+size_t V5MiLightUdpServer::handlePacket(uint8_t* packet, size_t packetSize, uint8_t* responseBuffer) {
+  if (packetSize == 2 || packetSize == 3) {
+    handleCommand(packet[0], packet[1]);
+  } else {
+    Serial.print("V5MilightUdpServer: unexpected packet length. Should always be 2-3, was: ");
+    Serial.println(packetSize);  
   }
+  
+  return 0;
 }
 
-void MiLightUdpServer::handleCommand(uint8_t command, uint8_t commandArg) {
+void V5MiLightUdpServer::handleCommand(uint8_t command, uint8_t commandArg) {
   if (command >= UDP_RGBW_GROUP_1_ON && command <= UDP_RGBW_GROUP_4_OFF) {
     const MiLightStatus status = (command % 2) == 1 ? ON : OFF;
     const uint8_t groupId = (command - UDP_RGBW_GROUP_1_ON + 2)/2;
@@ -123,18 +93,18 @@ void MiLightUdpServer::handleCommand(uint8_t command, uint8_t commandArg) {
         
       default:
         if (!handled) {
-          Serial.print("MiLightUdpServer - Unhandled command: ");
+          Serial.print("V5MiLightUdpServer - Unhandled command: ");
           Serial.println(command);
         }
     }
   }
 }
 
-void MiLightUdpServer::pressButton(uint8_t button) {
+void V5MiLightUdpServer::pressButton(uint8_t button) {
   client->command(button, 0);
 }  
 
-uint8_t MiLightUdpServer::cctCommandIdToGroup(uint8_t command) {
+uint8_t V5MiLightUdpServer::cctCommandIdToGroup(uint8_t command) {
   switch (command) {
     case UDP_CCT_GROUP_1_ON:
     case UDP_CCT_GROUP_1_OFF:
@@ -153,7 +123,7 @@ uint8_t MiLightUdpServer::cctCommandIdToGroup(uint8_t command) {
   return 0;
 }  
   
-MiLightStatus MiLightUdpServer::cctCommandToStatus(uint8_t command) {
+MiLightStatus V5MiLightUdpServer::cctCommandToStatus(uint8_t command) {
   switch (command) {
     case UDP_CCT_GROUP_1_ON:
     case UDP_CCT_GROUP_2_ON:

+ 14 - 22
lib/MiLight/MiLightUdpServer.h

@@ -1,18 +1,17 @@
+// This protocol is documented here:
+// http://www.limitlessled.com/dev/
+
 #include <Arduino.h>
 #include <MiLightClient.h>
 #include <WiFiUdp.h>
+#include <MiLightUdpServer.h>
 
-// This protocol is documented here:
-// http://www.limitlessled.com/dev/
-
-#define MILIGHT_PACKET_BUFFER_SIZE 10
+#ifndef _V5_MILIGHT_UDP_SERVER
+#define _V5_MILIGHT_UDP_SERVER 
 
 // Uncomment to enable Serial printing of packets
 //#define MILIGHT_UDP_DEBUG
 
-#ifndef _MILIGHT_UDP_SERVER
-#define _MILIGHT_UDP_SERVER 
-
 enum MiLightUdpCommands {
   UDP_CCT_GROUP_1_ON         = 0x38,
   UDP_CCT_GROUP_1_OFF        = 0x3B,
@@ -49,27 +48,20 @@ enum MiLightUdpCommands {
   UDP_RGBW_COLOR             = 0x40
 };
 
-class MiLightUdpServer {
+class V5MiLightUdpServer : public MiLightUdpServer {
 public:
-  MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId);
-  ~MiLightUdpServer();
-    
-  void stop();
-  void begin();
-  void handleClient();
+  V5MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId)
+    : MiLightUdpServer(client, port, deviceId)
+  { }
+  
+  // Should return size of the response packet
+  virtual size_t handlePacket(uint8_t* packet, size_t packetSize, uint8_t* responseBuffer);
     
 protected:
-  WiFiUDP socket;
-  MiLightClient*& client;
-  uint16_t port;
-  uint16_t deviceId;
-  uint8_t lastGroup;
-  char packetBuffer[MILIGHT_PACKET_BUFFER_SIZE];
-  
   void handleCommand(uint8_t command, uint8_t commandArg);
   void pressButton(uint8_t button);
   uint8_t cctCommandIdToGroup(uint8_t command);
   MiLightStatus cctCommandToStatus(uint8_t command);
 };
 
-#endif
+#endif

+ 11 - 5
src/main.cpp

@@ -36,13 +36,19 @@ void initMilightUdpServers() {
   
   for (size_t i = 0; i < settings.numGatewayConfigs; i++) {
     GatewayConfig* config = settings.gatewayConfigs[i];
+    MiLightUdpServer* server = MiLightUdpServer::fromVersion(
+      config->protocolVersion,
+      milightClient,
+      config->port,
+      config->deviceId
+    );
     
-    if (config->protocolVersion == 0) {
-      udpServers[i] = new MiLightUdpServer(milightClient, config->port, config->deviceId);
-      udpServers[i]->begin();
-    } else {
-      Serial.print("Error initializing milight UDP server - Unsupported protocolVersion: ");
+    if (server == NULL) {
+      Serial.print("Error creating UDP server with protocol version: ");
       Serial.println(config->protocolVersion);
+    } else {
+      udpServers[i] = server;
+      udpServers[i]->begin();
     }
   }
 }