YeelightAPI icon indicating copy to clipboard operation
YeelightAPI copied to clipboard

[BUG] DeviceGroup.StartMusicMode() won't work for multiple devices

Open TipicoDev opened this issue 3 years ago • 6 comments

Describe the bug When using the method DeviceGroup.StartMusicMode(), the parameters are a single hostname and port. The problem is that starting multiple TcpListener using the same port will throw an exception: System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted

To Reproduce Steps to reproduce the behavior:

  1. Find or create multiple new Device() (at least 2)
  2. Group them with new DeviceGroup(IEnumerable<Device>)
  3. Call DeviceGroup.Connect() and then call DeviceGroup.StartMusicMode("x.x.x.x", 6969);
  4. See error

Expected behavior The lib should start the music mode in every device added.

Stacktrace System.Net.Sockets.SocketException: Normalmente é permitida apenas uma utilização de cada endereço de soquete (protocolo/endereço de rede/porta) em System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) em System.Net.Sockets.Socket.Bind(EndPoint localEP) em System.Net.Sockets.TcpListener.Start(Int32 backlog) em System.Net.Sockets.TcpListener.Start() em YeelightAPI.Device.<StartMusicMode>d__97.MoveNext()

Environment (please complete the following information):

  • OS: Windows 10 x64
  • .NET Framework 4.8

Additional context You can bypass this error by looping through each device and calling Device.StartMusicMode(null, uniquePort);

TipicoDev avatar Mar 27 '21 21:03 TipicoDev

Well, I have to admit I haven't tested enough the music mode, in fact I don't use it in my personal setup.

In this case we should let the user decide wether he wants to manually provide unique ports, or let the API pick random ports for him. I'm thinking of something like

public async Task<bool> StartMusicMode (hostName = null, IEnumerable<int> ports) ; // use the specified ports, ports.length must be equal to group.Length otherwise we throw an exception
public async Task<bool> StartMusicMode (hostName = null, int? startingPort );  // pick next availables ports after the startingPort parameter, il startingPort is null, pick any available port

In addition, user must be able to know which port is used by devices (in the group or not), so I will add on device a property containing music mode informations (enabled, hostname and port)

roddone avatar Mar 28 '21 11:03 roddone

@roddone Nice! You can count on me for testing this. I guess it makes sense to set startingPort with a default value of 5000, or something around that.

TipicoDev avatar Apr 01 '21 21:04 TipicoDev

Why port 5000 specifically ?

I was thinking of setting default port to 0, so it could take the first available port.

roddone avatar Apr 02 '21 13:04 roddone

Why port 5000 specifically ?

I was thinking of setting default port to 0, so it could take the first available port.

Usually the initial ports are already taken or are default protocol ports, which can be used at any time. For example: https://www.utilizewindows.com/list-of-common-network-port-numbers/ 5000 was a high number example, iirc it's the default port for node applications.

TipicoDev avatar Apr 05 '21 08:04 TipicoDev

Why port 5000 specifically ? I was thinking of setting default port to 0, so it could take the first available port.

Usually the initial ports are already taken or are default protocol ports, which can be used at any time. For example: https://www.utilizewindows.com/list-of-common-network-port-numbers/ 5000 was a high number example, iirc it's the default port for node applications.

I'm super interested in a desktop music mode, please let me know if you have a project of that!

0x-y4c0 avatar Apr 19 '21 01:04 0x-y4c0

Hey, its been a while since this issue has been opened. But with global pandemic, kids, my new job and so on I didn't had any time to work on YeelightAPI until recently. There is a work in progress on "feat/group-music-mode" that includes several ways to use music mode with DeviceGroup (and Device), feel free to give it a try ;)

roddone avatar Mar 21 '22 18:03 roddone