Typos, format and async consistency
This commit is contained in:
@ -1,308 +1,213 @@
|
||||
using System.Net;
|
||||
using GoveeCSharpConnector.Interfaces;
|
||||
using GoveeCSharpConnector.Objects;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reactive.Subjects;
|
||||
using System.Reactive.Threading.Tasks;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using GoveeCSharpConnector.Interfaces;
|
||||
using GoveeCSharpConnector.Objects;
|
||||
namespace GoveeCSharpConnector.Services;
|
||||
|
||||
public class GoveeUdpService : IGoveeUdpService
|
||||
{
|
||||
private const string GoveeMulticastAddress = "239.255.255.250";
|
||||
private const int GoveeMulticastPortListen = 4002;
|
||||
private const int GoveeMulticastPortSend = 4001;
|
||||
private readonly UdpClient _udpClient = new();
|
||||
private bool _udpListenerActive = true;
|
||||
public class GoveeUdpService : IGoveeUdpService {
|
||||
|
||||
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
|
||||
private readonly Subject<string> _messageSubject = new();
|
||||
private readonly Subject<GoveeUdpDevice> _scanResultSubject = new();
|
||||
private readonly Subject<GoveeUdpState> _stateResultSubject = new();
|
||||
private bool _UDPListenerActive = true;
|
||||
private readonly UdpClient _UDPClient = new();
|
||||
private const int _GoveeMulticastPortSend = 4001;
|
||||
private const int _GoveeMulticastPortListen = 4002;
|
||||
private const string _GoveeMulticastAddress = "239.255.255.250";
|
||||
|
||||
public IObservable<string> Messages => _messageSubject;
|
||||
private readonly Subject<string> _MessageSubject = new();
|
||||
private readonly SemaphoreSlim _SemaphoreSlim = new(1, 1);
|
||||
private readonly Subject<GoveeUdpDevice> _ScanResultSubject = new();
|
||||
private readonly Subject<GoveeUdpState> _StateResultSubject = new();
|
||||
|
||||
public GoveeUdpService()
|
||||
{
|
||||
SetupUdpClientListener();
|
||||
}
|
||||
public bool IsListening() =>
|
||||
_UDPListenerActive;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<List<GoveeUdpDevice>> GetDevices(TimeSpan? timeout = null)
|
||||
{
|
||||
if (!_udpListenerActive)
|
||||
public GoveeUdpService() =>
|
||||
SetupUdpClientListenerAsync();
|
||||
|
||||
public IObservable<string> Messages =>
|
||||
_MessageSubject;
|
||||
|
||||
public Task<IList<GoveeUdpDevice>> GetDevicesAsync(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();
|
||||
|
||||
try
|
||||
{
|
||||
_SemaphoreSlim.Wait();
|
||||
try {
|
||||
// Build Message
|
||||
var message = new GoveeUdpMessage
|
||||
{
|
||||
msg = new msg
|
||||
{
|
||||
cmd = "scan",
|
||||
data = new { account_topic = "reserve" }
|
||||
GoveeUdpMessage message = new() {
|
||||
Msg = new Msg {
|
||||
Cmd = "scan",
|
||||
Data = new { account_topic = "reserve" }
|
||||
}
|
||||
};
|
||||
// Subscribe to ScanResultSubject
|
||||
var devicesTask = _scanResultSubject
|
||||
Task<IList<GoveeUdpDevice>> devicesTask = _ScanResultSubject
|
||||
.TakeUntil(Observable.Timer(timeout ?? TimeSpan.FromMilliseconds(250)))
|
||||
.ToList()
|
||||
.ToTask();
|
||||
|
||||
// Send Message
|
||||
SendUdpMessage(JsonSerializer.Serialize(message), GoveeMulticastAddress, GoveeMulticastPortSend);
|
||||
|
||||
SendUdpMessage(JsonSerializer.Serialize(message), _GoveeMulticastAddress, _GoveeMulticastPortSend);
|
||||
// Return List
|
||||
return (await devicesTask).ToList();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return devicesTask;
|
||||
} catch (Exception e) {
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
} finally {
|
||||
// Release Method Block
|
||||
_semaphore.Release();
|
||||
_ = _SemaphoreSlim.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<GoveeUdpState> GetState(string deviceAddress, int uniCastPort = 4003, TimeSpan? timeout = null)
|
||||
{
|
||||
if (!_udpListenerActive)
|
||||
public Task<GoveeUdpState> GetStateAsync(string deviceAddress, int uniCastPort = 4003, TimeSpan? timeout = null) {
|
||||
if (!_UDPListenerActive) {
|
||||
throw new Exception("Udp Listener not started!");
|
||||
try
|
||||
{
|
||||
// Build Message
|
||||
var message = new GoveeUdpMessage
|
||||
{
|
||||
msg = new msg
|
||||
{
|
||||
cmd = "devStatus",
|
||||
data = new { }
|
||||
}
|
||||
// Build Message
|
||||
GoveeUdpMessage message = new() {
|
||||
Msg = new Msg {
|
||||
Cmd = "devStatus",
|
||||
Data = new { }
|
||||
}
|
||||
};
|
||||
// Subscribe to ScanResultSubject
|
||||
Task<GoveeUdpState> devicesTask = _StateResultSubject
|
||||
.TakeUntil(Observable.Timer(timeout ?? TimeSpan.FromMilliseconds(250)))
|
||||
.ToTask();
|
||||
// Send Message
|
||||
SendUdpMessage(JsonSerializer.Serialize(message), deviceAddress, uniCastPort);
|
||||
// Return state
|
||||
return devicesTask;
|
||||
}
|
||||
|
||||
public void ToggleDevice(string deviceAddress, bool on, int uniCastPort = 4003) {
|
||||
// Build Message
|
||||
GoveeUdpMessage message = new() {
|
||||
Msg = new Msg {
|
||||
Cmd = "turn",
|
||||
Data = new { value = on ? 1 : 0 }
|
||||
}
|
||||
};
|
||||
// Send Message
|
||||
SendUdpMessage(JsonSerializer.Serialize(message), deviceAddress, uniCastPort);
|
||||
}
|
||||
|
||||
public void SetBrightness(string deviceAddress, int brightness, int uniCastPort = 4003) {
|
||||
// Build Message
|
||||
GoveeUdpMessage message = new() {
|
||||
Msg = new Msg {
|
||||
Cmd = "brightness",
|
||||
Data = new { value = brightness }
|
||||
}
|
||||
};
|
||||
// Send Message
|
||||
SendUdpMessage(JsonSerializer.Serialize(message), deviceAddress, uniCastPort);
|
||||
}
|
||||
|
||||
public void SetColor(string deviceAddress, RgbColor color, int uniCastPort = 4003) {
|
||||
// Build Message
|
||||
GoveeUdpMessage message = new() {
|
||||
Msg = new Msg {
|
||||
Cmd = "colorwc",
|
||||
Data = new {
|
||||
color = new {
|
||||
r = color.R,
|
||||
g = color.G,
|
||||
b = color.B
|
||||
},
|
||||
colorTempInKelvin = 0
|
||||
}
|
||||
};
|
||||
// Subscribe to ScanResultSubject
|
||||
var devicesTask = _stateResultSubject
|
||||
.TakeUntil(Observable.Timer(timeout ?? TimeSpan.FromMilliseconds(250)))
|
||||
.ToTask();
|
||||
|
||||
// Send Message
|
||||
SendUdpMessage(JsonSerializer.Serialize(message), deviceAddress, uniCastPort);
|
||||
|
||||
// Return state
|
||||
return await devicesTask;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
};
|
||||
// Send Message
|
||||
SendUdpMessage(JsonSerializer.Serialize(message), deviceAddress, uniCastPort);
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public async Task ToggleDevice(string deviceAddress, bool on, int uniCastPort = 4003)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Build Message
|
||||
var message = new GoveeUdpMessage
|
||||
{
|
||||
msg = new msg
|
||||
{
|
||||
cmd = "turn",
|
||||
data = new { value = on ? 1 : 0 }
|
||||
|
||||
public void SetColorTemp(string deviceAddress, int colorTempInKelvin, int uniCastPort = 4003) {
|
||||
// Build Message
|
||||
GoveeUdpMessage message = new() {
|
||||
Msg = new Msg {
|
||||
Cmd = "colorwc",
|
||||
Data = new {
|
||||
color = new {
|
||||
r = 0,
|
||||
g = 0,
|
||||
b = 0
|
||||
},
|
||||
colorTempInKelvin
|
||||
}
|
||||
};
|
||||
// Send Message
|
||||
SendUdpMessage(JsonSerializer.Serialize(message), deviceAddress, uniCastPort);
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public async Task SetBrightness(string deviceAddress, int brightness, int uniCastPort = 4003)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Build Message
|
||||
var message = new GoveeUdpMessage
|
||||
{
|
||||
msg = new msg
|
||||
{
|
||||
cmd = "brightness",
|
||||
data = new { value = brightness }
|
||||
}
|
||||
};
|
||||
// Send Message
|
||||
SendUdpMessage(JsonSerializer.Serialize(message), deviceAddress, uniCastPort);
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public async Task SetColor(string deviceAddress, RgbColor color, int uniCastPort = 4003)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Build Message
|
||||
var message = new GoveeUdpMessage
|
||||
{
|
||||
msg = new msg
|
||||
{
|
||||
cmd = "colorwc",
|
||||
data = new
|
||||
{ color = new
|
||||
{
|
||||
r = color.R,
|
||||
g = color.G,
|
||||
b = color.B
|
||||
},
|
||||
colorTempInKelvin = 0
|
||||
}
|
||||
}
|
||||
};
|
||||
// Send Message
|
||||
SendUdpMessage(JsonSerializer.Serialize(message), deviceAddress, uniCastPort);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
};
|
||||
// Send Message
|
||||
SendUdpMessage(JsonSerializer.Serialize(message), deviceAddress, uniCastPort);
|
||||
}
|
||||
|
||||
public async Task SetColorTemp(string deviceAddress, int colorTempInKelvin, int uniCastPort = 4003)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Build Message
|
||||
var message = new GoveeUdpMessage
|
||||
{
|
||||
msg = new msg
|
||||
{
|
||||
cmd = "colorwc",
|
||||
data = new
|
||||
{
|
||||
color = new
|
||||
{
|
||||
r = 0,
|
||||
g = 0,
|
||||
b = 0
|
||||
},
|
||||
colorTempInKelvin = colorTempInKelvin
|
||||
}
|
||||
}
|
||||
};
|
||||
// Send Message
|
||||
SendUdpMessage(JsonSerializer.Serialize(message), deviceAddress, uniCastPort);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
public Task StartUdpListenerAsync() {
|
||||
_UDPListenerActive = true;
|
||||
return StartListenerAsync();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async void StartUdpListener()
|
||||
{
|
||||
_udpListenerActive = true;
|
||||
await StartListener();
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public bool IsListening()
|
||||
{
|
||||
return _udpListenerActive;
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public void StopUdpListener()
|
||||
{
|
||||
_udpListenerActive = false;
|
||||
_udpClient.DropMulticastGroup(IPAddress.Parse(GoveeMulticastAddress));
|
||||
_udpClient.Close();
|
||||
public void StopUdpListener() {
|
||||
_UDPListenerActive = false;
|
||||
_UDPClient.DropMulticastGroup(IPAddress.Parse(_GoveeMulticastAddress));
|
||||
_UDPClient.Close();
|
||||
}
|
||||
|
||||
private static void SendUdpMessage(string message, string receiverAddress, int receiverPort)
|
||||
{
|
||||
var client = new UdpClient();
|
||||
try
|
||||
{
|
||||
private static void SendUdpMessage(string message, string receiverAddress, int receiverPort) {
|
||||
UdpClient client = new();
|
||||
try {
|
||||
byte[] data = Encoding.UTF8.GetBytes(message);
|
||||
client.Send(data, data.Length, receiverAddress, receiverPort);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
Task<int> task = client.SendAsync(data, data.Length, receiverAddress, receiverPort);
|
||||
task.Wait();
|
||||
} catch (Exception) {
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
} finally {
|
||||
client.Close();
|
||||
}
|
||||
}
|
||||
|
||||
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 Task SetupUdpClientListenerAsync() {
|
||||
_UDPClient.ExclusiveAddressUse = false;
|
||||
IPEndPoint localEndPoint = new(IPAddress.Any, _GoveeMulticastPortListen);
|
||||
_UDPClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
||||
_UDPClient.Client.Bind(localEndPoint);
|
||||
return StartListenerAsync();
|
||||
}
|
||||
|
||||
private async Task StartListener()
|
||||
{
|
||||
try
|
||||
{
|
||||
_udpClient.JoinMulticastGroup(IPAddress.Parse(GoveeMulticastAddress));
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
while (_udpListenerActive)
|
||||
{
|
||||
var remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
var data = _udpClient.Receive(ref remoteEndPoint);
|
||||
|
||||
var message = Encoding.UTF8.GetString(data);
|
||||
|
||||
UdPMessageReceived(message);
|
||||
_messageSubject.OnNext(message);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
private Task StartListenerAsync() {
|
||||
_UDPClient.JoinMulticastGroup(IPAddress.Parse(_GoveeMulticastAddress));
|
||||
return StartListenerTask();
|
||||
}
|
||||
|
||||
private void UdPMessageReceived(string message)
|
||||
{
|
||||
var response = JsonSerializer.Deserialize<GoveeUdpMessage>(message);
|
||||
switch (response.msg.cmd)
|
||||
{
|
||||
private Task StartListenerTask() {
|
||||
while (_UDPListenerActive) {
|
||||
IPEndPoint remoteEndPoint = new(IPAddress.Any, 0);
|
||||
byte[] data = _UDPClient.Receive(ref remoteEndPoint);
|
||||
|
||||
string message = Encoding.UTF8.GetString(data);
|
||||
|
||||
UdPMessageReceived(message);
|
||||
_MessageSubject.OnNext(message);
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void UdPMessageReceived(string message) {
|
||||
GoveeUdpMessage response = JsonSerializer.Deserialize<GoveeUdpMessage>(message);
|
||||
switch (response.Msg.Cmd) {
|
||||
case "scan":
|
||||
var device = JsonSerializer.Deserialize<GoveeUdpDevice>(response.msg.data.ToString());
|
||||
_scanResultSubject.OnNext(device);
|
||||
GoveeUdpDevice device = JsonSerializer.Deserialize<GoveeUdpDevice>(response.Msg.Data.ToString());
|
||||
_ScanResultSubject.OnNext(device);
|
||||
break;
|
||||
case "devStatus":
|
||||
var state = JsonSerializer.Deserialize<GoveeUdpState>(response.msg.data.ToString());
|
||||
_stateResultSubject.OnNext(state);
|
||||
GoveeUdpState state = JsonSerializer.Deserialize<GoveeUdpState>(response.Msg.Data.ToString());
|
||||
_StateResultSubject.OnNext(state);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user