Typos, format and async consistency

This commit is contained in:
2025-08-12 18:28:00 -07:00
parent 76ccf20c30
commit 17cb171e36
35 changed files with 1937 additions and 907 deletions

View File

@ -1,108 +1,149 @@
using GoveeCSharpConnector.Interfaces;
using GoveeCSharpConnector.Objects;
using System.Collections.ObjectModel;
namespace GoveeCSharpConnector.Services;
public class GoveeService : IGoveeService
{
public class GoveeService(IGoveeApiService apiService, IGoveeUdpService udpService) : IGoveeService {
public string GoveeApiKey { get; set; }
private readonly IGoveeApiService _apiService;
private readonly IGoveeUdpService _udpService;
private readonly IGoveeApiService _APIService = apiService ??
throw new ArgumentNullException(nameof(apiService));
private readonly IGoveeUdpService _UDPService = udpService ??
throw new ArgumentNullException(nameof(udpService));
public GoveeService(IGoveeApiService apiService,IGoveeUdpService udpService)
{
_apiService = apiService ?? throw new ArgumentNullException(nameof(apiService));
_udpService = udpService ?? throw new ArgumentNullException(nameof(udpService));
}
public async Task<List<GoveeDevice>> GetDevices(bool onlyLan = true)
{
if (string.IsNullOrWhiteSpace(GoveeApiKey)) throw new Exception("No Govee Api Key Set!");
_apiService.SetApiKey(GoveeApiKey);
var apiDevices = await _apiService.GetDevices();
var devices = apiDevices.Select(apiDevice => new GoveeDevice() { DeviceId = apiDevice.DeviceId, DeviceName = apiDevice.DeviceName, Model = apiDevice.Model, Address = "onlyAvailableOnUdpRequest" }).ToList();
if (!onlyLan)
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())
_udpService.StartUdpListener();
var udpDevices = await _udpService.GetDevices();
var 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();
}
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 async Task<GoveeState> GetDeviceState(GoveeDevice goveeDevice, bool useUdp = true)
{
if (useUdp)
{
if (!_udpService.IsListening())
_udpService.StartUdpListener();
if (string.IsNullOrWhiteSpace(goveeDevice.Address)) throw new Exception("Device not available via Udp/Lan");
var udpState = await _udpService.GetState(goveeDevice.Address);
return new GoveeState() { State = udpState.onOff, Brightness = udpState.brightness, Color = udpState.color, ColorTempInKelvin = udpState.colorTempInKelvin };
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);
var apiState = await _apiService.GetDeviceState(goveeDevice.DeviceId, goveeDevice.Model);
return new GoveeState{State = apiState.Properties.PowerState, Brightness = apiState.Properties.Brightness, Color = apiState.Properties.Color, ColorTempInKelvin = apiState.Properties.ColorTemp};
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 async Task ToggleState(GoveeDevice goveeDevice, bool on, bool useUdp = true)
{
if (useUdp)
{
if (string.IsNullOrWhiteSpace(goveeDevice.Address)) throw new Exception("Device not available via Udp/Lan");
await _udpService.ToggleDevice(goveeDevice.Address, on);
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);
await _apiService.ToggleState(goveeDevice.DeviceId, goveeDevice.Model, on);
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 async Task SetBrightness(GoveeDevice goveeDevice, int value, bool useUdp = true)
{
if (useUdp)
{
if (string.IsNullOrWhiteSpace(goveeDevice.Address)) throw new Exception("Device not available via Udp/Lan");
await _udpService.SetBrightness(goveeDevice.Address, value);
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);
await _apiService.SetBrightness(goveeDevice.DeviceId, goveeDevice.Model, value);
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 async Task SetColor(GoveeDevice goveeDevice, RgbColor color, bool useUdp = true)
{
if (useUdp)
{
if (string.IsNullOrWhiteSpace(goveeDevice.Address)) throw new Exception("Device not available via Udp/Lan");
await _udpService.SetColor(goveeDevice.Address, color);
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!");
if (string.IsNullOrWhiteSpace(GoveeApiKey)) {
throw new Exception("No Govee Api Key Set!");
}
_apiService.SetApiKey(GoveeApiKey);
await _apiService.SetColor(goveeDevice.DeviceId, goveeDevice.Model, color);
_APIService.SetApiKey(GoveeApiKey);
Task<HttpResponseMessage> httpResponseMessageTask = _APIService.SetColorAsync(goveeDevice.DeviceId, goveeDevice.Model, color);
httpResponseMessageTask.Wait();
}
public async Task SetColorTemp(GoveeDevice goveeDevice, int value, bool useUdp = true)
{
if (useUdp)
{
if (string.IsNullOrWhiteSpace(goveeDevice.Address)) throw new Exception("Device not available via Udp/Lan");
await _udpService.SetColorTemp(goveeDevice.Address, value);
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);
await _apiService.SetColorTemp(goveeDevice.DeviceId, goveeDevice.Model, value);
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();
}
}