From 76ccf20c30cf702ffa0e802af049f9e93a751883 Mon Sep 17 00:00:00 2001 From: Locxion Date: Sat, 3 Feb 2024 12:23:34 +0100 Subject: [PATCH] Fixed UdpDeviceList Pick Fixed UdpListener Start --- .../GoveeCSharpConnector.csproj | 2 +- .../Interfaces/IGoveeUdpService.cs | 4 ++-- .../Services/GoveeUdpService.cs | 18 ++++++++++++------ GoveeCsharpConnector.Example/Program.cs | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/GoveeCSharpConnector/GoveeCSharpConnector.csproj b/GoveeCSharpConnector/GoveeCSharpConnector.csproj index d11ed1f..f138a42 100644 --- a/GoveeCSharpConnector/GoveeCSharpConnector.csproj +++ b/GoveeCSharpConnector/GoveeCSharpConnector.csproj @@ -12,7 +12,7 @@ https://github.com/Locxion/GoveeCSharpConnector https://github.com/Locxion/GoveeCSharpConnector https://github.com/Locxion/GoveeCSharpConnector/blob/main/LICENSE - 1.1.1 + 1.1.2 diff --git a/GoveeCSharpConnector/Interfaces/IGoveeUdpService.cs b/GoveeCSharpConnector/Interfaces/IGoveeUdpService.cs index 2c64db2..d2208d3 100644 --- a/GoveeCSharpConnector/Interfaces/IGoveeUdpService.cs +++ b/GoveeCSharpConnector/Interfaces/IGoveeUdpService.cs @@ -12,7 +12,7 @@ public interface IGoveeUdpService /// /// Sends a Scan Command via Udp Multicast. /// - /// Standard 200ms + /// Standard 250ms /// List of GoveeUdpDevices Task> GetDevices(TimeSpan? timeout = null); @@ -21,7 +21,7 @@ public interface IGoveeUdpService /// /// Ip Address of the Device /// Port of the Device. Standard 4003 - /// Standard 200ms + /// Standard 250ms /// Task GetState(string deviceAddress, int uniCastPort = 4003, TimeSpan? timeout = null); diff --git a/GoveeCSharpConnector/Services/GoveeUdpService.cs b/GoveeCSharpConnector/Services/GoveeUdpService.cs index 797f334..a7cdb83 100644 --- a/GoveeCSharpConnector/Services/GoveeUdpService.cs +++ b/GoveeCSharpConnector/Services/GoveeUdpService.cs @@ -32,6 +32,8 @@ public class GoveeUdpService : IGoveeUdpService /// public async Task> GetDevices(TimeSpan? timeout = null) { + if (!_udpListenerActive) + throw new Exception("Udp Listener not started!"); // Block this Method until current call reaches end of Method await _semaphore.WaitAsync(); @@ -48,7 +50,7 @@ public class GoveeUdpService : IGoveeUdpService }; // Subscribe to ScanResultSubject var devicesTask = _scanResultSubject - .TakeUntil(Observable.Timer(timeout ?? TimeSpan.FromMilliseconds(300))) + .TakeUntil(Observable.Timer(timeout ?? TimeSpan.FromMilliseconds(250))) .ToList() .ToTask(); @@ -73,6 +75,8 @@ public class GoveeUdpService : IGoveeUdpService /// public async Task GetState(string deviceAddress, int uniCastPort = 4003, TimeSpan? timeout = null) { + if (!_udpListenerActive) + throw new Exception("Udp Listener not started!"); try { // Build Message @@ -86,7 +90,7 @@ public class GoveeUdpService : IGoveeUdpService }; // Subscribe to ScanResultSubject var devicesTask = _stateResultSubject - .TakeUntil(Observable.Timer(timeout ?? TimeSpan.FromMilliseconds(200))) + .TakeUntil(Observable.Timer(timeout ?? TimeSpan.FromMilliseconds(250))) .ToTask(); // Send Message @@ -228,6 +232,8 @@ public class GoveeUdpService : IGoveeUdpService public void StopUdpListener() { _udpListenerActive = false; + _udpClient.DropMulticastGroup(IPAddress.Parse(GoveeMulticastAddress)); + _udpClient.Close(); } private static void SendUdpMessage(string message, string receiverAddress, int receiverPort) @@ -249,12 +255,13 @@ public class GoveeUdpService : IGoveeUdpService } } - private void SetupUdpClientListener() + private async void SetupUdpClientListener() { _udpClient.ExclusiveAddressUse = false; var localEndPoint = new IPEndPoint(IPAddress.Any, GoveeMulticastPortListen); _udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); _udpClient.Client.Bind(localEndPoint); + await StartListener(); } private async Task StartListener() @@ -277,10 +284,9 @@ public class GoveeUdpService : IGoveeUdpService } }); } - finally + catch(Exception ex) { - _udpClient.DropMulticastGroup(IPAddress.Parse(GoveeMulticastAddress)); - _udpClient.Close(); + throw ex; } } diff --git a/GoveeCsharpConnector.Example/Program.cs b/GoveeCsharpConnector.Example/Program.cs index 7148b1d..da57610 100644 --- a/GoveeCsharpConnector.Example/Program.cs +++ b/GoveeCsharpConnector.Example/Program.cs @@ -243,7 +243,7 @@ public class Program return GetUdpDeviceSelection(); } - return _udpDevices[result]; + return _udpDevices[result-1]; } private static void HandleApiInput()