MiLightUdpServer.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <MiLightUdpServer.h>
  2. #include <V5MiLightUdpServer.h>
  3. #include <V6MiLightUdpServer.h>
  4. MiLightUdpServer::MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId)
  5. : client(client),
  6. port(port),
  7. deviceId(deviceId),
  8. lastGroup(0)
  9. { }
  10. MiLightUdpServer::~MiLightUdpServer() {
  11. stop();
  12. }
  13. void MiLightUdpServer::begin() {
  14. socket.begin(this->port);
  15. }
  16. void MiLightUdpServer::stop() {
  17. socket.stop();
  18. }
  19. void MiLightUdpServer::handleClient() {
  20. const size_t packetSize = socket.parsePacket();
  21. if (packetSize) {
  22. socket.read(packetBuffer, packetSize);
  23. #ifdef MILIGHT_UDP_DEBUG
  24. printf("Handling packet: ");
  25. for (size_t i = 0; i < packetSize; i++) {
  26. printf("%02X ", packetBuffer[i]);
  27. }
  28. printf("\n");
  29. #endif
  30. handlePacket(packetBuffer, packetSize);
  31. }
  32. }
  33. MiLightUdpServer* MiLightUdpServer::fromVersion(uint8_t version, MiLightClient*& client, uint16_t port, uint16_t deviceId) {
  34. if (version == 0 || version == 5) {
  35. return new V5MiLightUdpServer(client, port, deviceId);
  36. } else if (version == 6) {
  37. return new V6MiLightUdpServer(client, port, deviceId);
  38. }
  39. return NULL;
  40. }