Fixed UdpDeviceList Pick

Fixed UdpListener Start
This commit is contained in:
Locxion
2024-02-03 12:23:34 +01:00
parent a069133a68
commit 76ccf20c30
4 changed files with 16 additions and 10 deletions

View File

@ -12,7 +12,7 @@
<PackageProjectUrl>https://github.com/Locxion/GoveeCSharpConnector</PackageProjectUrl> <PackageProjectUrl>https://github.com/Locxion/GoveeCSharpConnector</PackageProjectUrl>
<RepositoryUrl>https://github.com/Locxion/GoveeCSharpConnector</RepositoryUrl> <RepositoryUrl>https://github.com/Locxion/GoveeCSharpConnector</RepositoryUrl>
<PackageLicenseUrl>https://github.com/Locxion/GoveeCSharpConnector/blob/main/LICENSE</PackageLicenseUrl> <PackageLicenseUrl>https://github.com/Locxion/GoveeCSharpConnector/blob/main/LICENSE</PackageLicenseUrl>
<Version>1.1.1</Version> <Version>1.1.2</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" /> <PackageReference Include="System.Net.Http.Json" Version="8.0.0" />

View File

@ -12,7 +12,7 @@ public interface IGoveeUdpService
/// <summary> /// <summary>
/// Sends a Scan Command via Udp Multicast. /// Sends a Scan Command via Udp Multicast.
/// </summary> /// </summary>
/// <param name="timeout">Standard 200ms</param> /// <param name="timeout">Standard 250ms</param>
/// <returns>List of GoveeUdpDevices</returns> /// <returns>List of GoveeUdpDevices</returns>
Task<List<GoveeUdpDevice>> GetDevices(TimeSpan? timeout = null); Task<List<GoveeUdpDevice>> GetDevices(TimeSpan? timeout = null);
@ -21,7 +21,7 @@ public interface IGoveeUdpService
/// </summary> /// </summary>
/// <param name="deviceAddress">Ip Address of the Device</param> /// <param name="deviceAddress">Ip Address of the Device</param>
/// <param name="uniCastPort">Port of the Device. Standard 4003</param> /// <param name="uniCastPort">Port of the Device. Standard 4003</param>
/// <param name="timeout">Standard 200ms</param> /// <param name="timeout">Standard 250ms</param>
/// <returns></returns> /// <returns></returns>
Task<GoveeUdpState> GetState(string deviceAddress, int uniCastPort = 4003, TimeSpan? timeout = null); Task<GoveeUdpState> GetState(string deviceAddress, int uniCastPort = 4003, TimeSpan? timeout = null);

View File

@ -32,6 +32,8 @@ public class GoveeUdpService : IGoveeUdpService
/// <inheritdoc/> /// <inheritdoc/>
public async Task<List<GoveeUdpDevice>> GetDevices(TimeSpan? timeout = null) public async Task<List<GoveeUdpDevice>> GetDevices(TimeSpan? timeout = null)
{ {
if (!_udpListenerActive)
throw new Exception("Udp Listener not started!");
// Block this Method until current call reaches end of Method // Block this Method until current call reaches end of Method
await _semaphore.WaitAsync(); await _semaphore.WaitAsync();
@ -48,7 +50,7 @@ public class GoveeUdpService : IGoveeUdpService
}; };
// Subscribe to ScanResultSubject // Subscribe to ScanResultSubject
var devicesTask = _scanResultSubject var devicesTask = _scanResultSubject
.TakeUntil(Observable.Timer(timeout ?? TimeSpan.FromMilliseconds(300))) .TakeUntil(Observable.Timer(timeout ?? TimeSpan.FromMilliseconds(250)))
.ToList() .ToList()
.ToTask(); .ToTask();
@ -73,6 +75,8 @@ public class GoveeUdpService : IGoveeUdpService
/// <inheritdoc/> /// <inheritdoc/>
public async Task<GoveeUdpState> GetState(string deviceAddress, int uniCastPort = 4003, TimeSpan? timeout = null) public async Task<GoveeUdpState> GetState(string deviceAddress, int uniCastPort = 4003, TimeSpan? timeout = null)
{ {
if (!_udpListenerActive)
throw new Exception("Udp Listener not started!");
try try
{ {
// Build Message // Build Message
@ -86,7 +90,7 @@ public class GoveeUdpService : IGoveeUdpService
}; };
// Subscribe to ScanResultSubject // Subscribe to ScanResultSubject
var devicesTask = _stateResultSubject var devicesTask = _stateResultSubject
.TakeUntil(Observable.Timer(timeout ?? TimeSpan.FromMilliseconds(200))) .TakeUntil(Observable.Timer(timeout ?? TimeSpan.FromMilliseconds(250)))
.ToTask(); .ToTask();
// Send Message // Send Message
@ -228,6 +232,8 @@ public class GoveeUdpService : IGoveeUdpService
public void StopUdpListener() public void StopUdpListener()
{ {
_udpListenerActive = false; _udpListenerActive = false;
_udpClient.DropMulticastGroup(IPAddress.Parse(GoveeMulticastAddress));
_udpClient.Close();
} }
private static void SendUdpMessage(string message, string receiverAddress, int receiverPort) 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; _udpClient.ExclusiveAddressUse = false;
var localEndPoint = new IPEndPoint(IPAddress.Any, GoveeMulticastPortListen); var localEndPoint = new IPEndPoint(IPAddress.Any, GoveeMulticastPortListen);
_udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); _udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
_udpClient.Client.Bind(localEndPoint); _udpClient.Client.Bind(localEndPoint);
await StartListener();
} }
private async Task StartListener() private async Task StartListener()
@ -277,10 +284,9 @@ public class GoveeUdpService : IGoveeUdpService
} }
}); });
} }
finally catch(Exception ex)
{ {
_udpClient.DropMulticastGroup(IPAddress.Parse(GoveeMulticastAddress)); throw ex;
_udpClient.Close();
} }
} }

View File

@ -243,7 +243,7 @@ public class Program
return GetUdpDeviceSelection(); return GetUdpDeviceSelection();
} }
return _udpDevices[result]; return _udpDevices[result-1];
} }
private static void HandleApiInput() private static void HandleApiInput()