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>
<RepositoryUrl>https://github.com/Locxion/GoveeCSharpConnector</RepositoryUrl>
<PackageLicenseUrl>https://github.com/Locxion/GoveeCSharpConnector/blob/main/LICENSE</PackageLicenseUrl>
<Version>1.1.1</Version>
<Version>1.1.2</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />

View File

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

View File

@ -32,6 +32,8 @@ public class GoveeUdpService : IGoveeUdpService
/// <inheritdoc/>
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
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
/// <inheritdoc/>
public async Task<GoveeUdpState> 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;
}
}