Files
govee-csharp-connector/GoveeCSharpConnector/Services/GoveeService.cs

149 lines
6.7 KiB
C#

using GoveeCSharpConnector.Interfaces;
using GoveeCSharpConnector.Objects;
using System.Collections.ObjectModel;
namespace GoveeCSharpConnector.Services;
public class GoveeService(IGoveeApiService apiService, IGoveeUdpService udpService) : IGoveeService {
public string GoveeApiKey { get; set; }
private readonly IGoveeApiService _APIService = apiService ??
throw new ArgumentNullException(nameof(apiService));
private readonly IGoveeUdpService _UDPService = udpService ??
throw new ArgumentNullException(nameof(udpService));
public List<GoveeDevice> GetDevices(bool onlyLan = true) {
if (string.IsNullOrWhiteSpace(GoveeApiKey)) {
throw new Exception("No Govee Api Key Set!");
}
_APIService.SetApiKey(GoveeApiKey);
Task<GoveeResponse> goveeResponse = _APIService.GetDevicesResponseAsync();
goveeResponse.Wait();
List<GoveeApiDevice> apiDevices = goveeResponse.Result.Data.Devices;
List<GoveeDevice> devices = apiDevices.Select(apiDevice => new GoveeDevice() {
DeviceId = apiDevice.DeviceId,
DeviceName = apiDevice.DeviceName,
Model = apiDevice.Model,
Address = "onlyAvailableOnUdpRequest"
}).ToList();
if (!onlyLan) {
return devices;
}
if (!_UDPService.IsListening()) {
Task task = _UDPService.StartUdpListenerAsync();
task.Wait();
}
Task<IList<GoveeUdpDevice>> goveeUdpDevicesTask = _UDPService.GetDevicesAsync();
goveeUdpDevicesTask.Wait();
ReadOnlyCollection<GoveeUdpDevice> udpDevices = new(goveeUdpDevicesTask.Result);
List<GoveeDevice> combinedDevices = (from goveeDevice in devices
let matchingDevice = udpDevices.FirstOrDefault(x => x.Device == goveeDevice.DeviceId)
where matchingDevice is not null
select
new GoveeDevice {
DeviceId = goveeDevice.DeviceId,
DeviceName = goveeDevice.DeviceName,
Model = goveeDevice.Model,
Address = matchingDevice.IP
}).ToList();
return combinedDevices;
}
public GoveeState GetDeviceState(GoveeDevice goveeDevice, bool useUdp = true) {
if (useUdp) {
if (!_UDPService.IsListening()) {
Task task = _UDPService.StartUdpListenerAsync();
task.Wait();
}
if (string.IsNullOrWhiteSpace(goveeDevice.Address)) {
throw new Exception("Device not available via Udp/Lan");
}
Task<GoveeUdpState> goveeUdpStateTask = _UDPService.GetStateAsync(goveeDevice.Address);
goveeUdpStateTask.Wait();
return new GoveeState() {
State = goveeUdpStateTask.Result.OnOff,
Brightness = goveeUdpStateTask.Result.Brightness,
Color = goveeUdpStateTask.Result.Color,
ColorTempInKelvin = goveeUdpStateTask.Result.ColorTempInKelvin
};
}
if (string.IsNullOrWhiteSpace(GoveeApiKey)) {
throw new Exception("No Govee Api Key Set!");
}
_APIService.SetApiKey(GoveeApiKey);
Task<GoveeApiState> goveeApiStateTask = _APIService.GetDeviceStateAsync(goveeDevice.DeviceId, goveeDevice.Model);
goveeApiStateTask.Wait();
return new GoveeState {
State = goveeApiStateTask.Result.Properties.PowerState,
Brightness = goveeApiStateTask.Result.Properties.Brightness,
Color = goveeApiStateTask.Result.Properties.Color,
ColorTempInKelvin = goveeApiStateTask.Result.Properties.ColorTemp
};
}
public void ToggleState(GoveeDevice goveeDevice, bool on, bool useUdp = true) {
if (useUdp) {
if (string.IsNullOrWhiteSpace(goveeDevice.Address)) {
throw new Exception("Device not available via Udp/Lan");
}
_UDPService.ToggleDevice(goveeDevice.Address, on);
return;
}
if (string.IsNullOrWhiteSpace(GoveeApiKey)) {
throw new Exception("No Govee Api Key Set!");
}
_APIService.SetApiKey(GoveeApiKey);
Task<HttpResponseMessage> httpResponseMessageTask = _APIService.ToggleStateAsync(goveeDevice.DeviceId, goveeDevice.Model, on);
httpResponseMessageTask.Wait();
}
public void SetBrightness(GoveeDevice goveeDevice, int value, bool useUdp = true) {
if (useUdp) {
if (string.IsNullOrWhiteSpace(goveeDevice.Address)) {
throw new Exception("Device not available via Udp/Lan");
}
_UDPService.SetBrightness(goveeDevice.Address, value);
return;
}
if (string.IsNullOrWhiteSpace(GoveeApiKey)) {
throw new Exception("No Govee Api Key Set!");
}
_APIService.SetApiKey(GoveeApiKey);
Task<HttpResponseMessage> httpResponseMessageTask = _APIService.SetBrightnessAsync(goveeDevice.DeviceId, goveeDevice.Model, value);
httpResponseMessageTask.Wait();
}
public void SetColor(GoveeDevice goveeDevice, RgbColor color, bool useUdp = true) {
if (useUdp) {
if (string.IsNullOrWhiteSpace(goveeDevice.Address)) {
throw new Exception("Device not available via Udp/Lan");
}
_UDPService.SetColor(goveeDevice.Address, color);
return;
}
if (string.IsNullOrWhiteSpace(GoveeApiKey)) {
throw new Exception("No Govee Api Key Set!");
}
_APIService.SetApiKey(GoveeApiKey);
Task<HttpResponseMessage> httpResponseMessageTask = _APIService.SetColorAsync(goveeDevice.DeviceId, goveeDevice.Model, color);
httpResponseMessageTask.Wait();
}
public void SetColorTemp(GoveeDevice goveeDevice, int value, bool useUdp = true) {
if (useUdp) {
if (string.IsNullOrWhiteSpace(goveeDevice.Address)) {
throw new Exception("Device not available via Udp/Lan");
}
_UDPService.SetColorTemp(goveeDevice.Address, value);
return;
}
if (string.IsNullOrWhiteSpace(GoveeApiKey)) {
throw new Exception("No Govee Api Key Set!");
}
_APIService.SetApiKey(GoveeApiKey);
Task<HttpResponseMessage> httpResponseMessageTask = _APIService.SetColorTempAsync(goveeDevice.DeviceId, goveeDevice.Model, value);
httpResponseMessageTask.Wait();
}
}