From a94b43ee3d4fe0739afb5acf68aea04fc55f20d8 Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Mon, 29 Mar 2021 19:36:23 -0500 Subject: [PATCH] Scan for an open port, so that multiple Yeelight controllers can be in music mode at the same time --- .../YeelightController/YeelightController.cpp | 55 +++++++++++++++---- .../YeelightController/YeelightController.h | 1 + 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/Controllers/YeelightController/YeelightController.cpp b/Controllers/YeelightController/YeelightController.cpp index fcc71d977..7cf77bc20 100644 --- a/Controllers/YeelightController/YeelightController.cpp +++ b/Controllers/YeelightController/YeelightController.cpp @@ -26,20 +26,53 @@ YeelightController::YeelightController(std::string ip, bool music_mode_val) if(music_mode) { - /*-----------------------------------------------------------------*\ - | Open a TCP server for music mode if enabled | - \*-----------------------------------------------------------------*/ - music_mode_server.tcp_server("55444"); + bool port_opened = false; + char port_string[8]; /*-----------------------------------------------------------------*\ - | Command bulb to connect to our TCP server | + | Start searching for open port for music mode at 55444 | \*-----------------------------------------------------------------*/ - SetMusicMode(); + music_mode_port = 55444; - /*-----------------------------------------------------------------*\ - | Get the client socket for the music mode connection | - \*-----------------------------------------------------------------*/ - music_mode_sock = music_mode_server.tcp_server_listen(); + while(!port_opened) + { + /*-----------------------------------------------------------------*\ + | Convert port to string | + \*-----------------------------------------------------------------*/ + snprintf(port_string, 8, "%d", music_mode_port); + + /*-----------------------------------------------------------------*\ + | Open a TCP server for music mode if enabled | + \*-----------------------------------------------------------------*/ + port_opened = music_mode_server.tcp_server(port_string); + + if(!port_opened) + { + music_mode_port++; + + /*-------------------------------------------------------------*\ + | If we've tested the maximum port value, 65535, and it failed, | + | give up and don't use music mode | + \*-------------------------------------------------------------*/ + if(music_mode_port >= 65536) + { + music_mode = false; + break; + } + + continue; + } + + /*-----------------------------------------------------------------*\ + | Command bulb to connect to our TCP server | + \*-----------------------------------------------------------------*/ + SetMusicMode(); + + /*-----------------------------------------------------------------*\ + | Get the client socket for the music mode connection | + \*-----------------------------------------------------------------*/ + music_mode_sock = music_mode_server.tcp_server_listen(); + } } } @@ -102,7 +135,7 @@ void YeelightController::SetMusicMode() command["method"] = "set_music"; command["params"][0] = 1; command["params"][1] = ip_addr; - command["params"][2] = 55444; + command["params"][2] = music_mode_port; /*-----------------------------------------------------------------*\ | Convert the JSON object to a string and write it | diff --git a/Controllers/YeelightController/YeelightController.h b/Controllers/YeelightController/YeelightController.h index bf3f312ae..532ed65ef 100644 --- a/Controllers/YeelightController/YeelightController.h +++ b/Controllers/YeelightController/YeelightController.h @@ -35,6 +35,7 @@ private: std::string location; net_port port; bool music_mode; + unsigned int music_mode_port; net_port music_mode_server; SOCKET * music_mode_sock; };