311 lines
13 KiB
C#
311 lines
13 KiB
C#
using GoveeCSharpConnector.Objects;
|
|
using GoveeCSharpConnector.Services;
|
|
using System.Collections.ObjectModel;
|
|
using System.Reflection;
|
|
|
|
namespace GoveeCsharpConnector.Example;
|
|
|
|
public class Program {
|
|
|
|
private static List<GoveeApiDevice> _APIDevices = [];
|
|
private static ReadOnlyCollection<GoveeUdpDevice>? _UDPDevices;
|
|
private static readonly GoveeApiService _GoveeApiService = new();
|
|
private static readonly GoveeUdpService _GoveeUdpService = new();
|
|
|
|
public static void Main(string[] _) {
|
|
while (true) {
|
|
PrintWelcomeMessage();
|
|
string? input = Console.ReadLine();
|
|
if (input == "1") {
|
|
InputOne();
|
|
} else if (input == "2") {
|
|
InputTwo();
|
|
} else if (input == "3") {
|
|
InputThree();
|
|
} else if (input == "4") {
|
|
InputFour();
|
|
} else if (input == "5") {
|
|
InputFive();
|
|
} else if (input == "6") {
|
|
InputSix();
|
|
} else if (input == "7") {
|
|
InputSeven(input);
|
|
} else if (input == "8") {
|
|
InputEight();
|
|
} else if (input == "9") {
|
|
InputNine();
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void InputOne() {
|
|
HandleApiInput();
|
|
EndSegment();
|
|
}
|
|
|
|
private static void InputTwo() {
|
|
Console.WriteLine("Requesting Devices ...");
|
|
Task<GoveeResponse> goveeResponseTask = _GoveeApiService.GetDevicesResponseAsync();
|
|
goveeResponseTask.Wait();
|
|
_APIDevices = goveeResponseTask.Result.Data.Devices;
|
|
Console.WriteLine("Devices:");
|
|
foreach (GoveeApiDevice device in _APIDevices) {
|
|
Console.WriteLine($"Name: {device.DeviceName}, Device Id: {device.DeviceId}, Model: {device.Model}, Controllable {device.Controllable}");
|
|
}
|
|
Console.WriteLine($"Total: {_APIDevices.Count} Devices.");
|
|
EndSegment();
|
|
}
|
|
|
|
private static void InputThree() {
|
|
if (_APIDevices.Count == 0) {
|
|
Console.WriteLine("No Devices discovered! Please use Option 2 first!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
Console.WriteLine("Please enter the Name of the Device:");
|
|
string? nameInput = Console.ReadLine();
|
|
GoveeApiDevice? goveeApiDevice = string.IsNullOrWhiteSpace(nameInput) ? null : _APIDevices.FirstOrDefault(x => x.DeviceName.EqualWhenIgnoringCase(nameInput));
|
|
if (goveeApiDevice is null) {
|
|
Console.WriteLine("Device Name Invalid!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
Console.WriteLine($"Do you want to turn the Device {nameInput} on or off?");
|
|
string? onOffInput = Console.ReadLine();
|
|
if (string.IsNullOrWhiteSpace(onOffInput) || (!onOffInput.EqualWhenIgnoringCase("on") && !onOffInput.EqualWhenIgnoringCase("off"))) {
|
|
Console.WriteLine("Invalid Input!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
Task<HttpResponseMessage> httpResponseMessageTask;
|
|
if (onOffInput.EqualWhenIgnoringCase("on")) {
|
|
httpResponseMessageTask = _GoveeApiService.ToggleStateAsync(goveeApiDevice.DeviceId, goveeApiDevice.Model, true);
|
|
} else {
|
|
httpResponseMessageTask = _GoveeApiService.ToggleStateAsync(goveeApiDevice.DeviceId, goveeApiDevice.Model, false);
|
|
}
|
|
CheckHttpResponseMessage(httpResponseMessageTask);
|
|
EndSegment();
|
|
}
|
|
|
|
private static void CheckHttpResponseMessage(Task<HttpResponseMessage> httpResponseMessageTask) {
|
|
httpResponseMessageTask.Wait();
|
|
if (!httpResponseMessageTask.Result.IsSuccessStatusCode)
|
|
throw new Exception($"Govee Api Request failed. Status code: {httpResponseMessageTask.Result.StatusCode}, Message: {httpResponseMessageTask.Result.Content}");
|
|
}
|
|
|
|
private static void InputFour() {
|
|
if (_APIDevices.Count == 0) {
|
|
Console.WriteLine("No Devices discovered! Please use Option 2 first!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
Console.WriteLine("Please enter the Name of the Device:");
|
|
string? nameInput2 = Console.ReadLine();
|
|
GoveeApiDevice? goveeApiDevice = string.IsNullOrWhiteSpace(nameInput2) ? null : _APIDevices.FirstOrDefault(x => x.DeviceName.EqualWhenIgnoringCase(nameInput2));
|
|
if (goveeApiDevice is null) {
|
|
Console.WriteLine("Device Name Invalid!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
Console.WriteLine($"Please enter a Brightness Value for Device {nameInput2}. 0-100");
|
|
string? brightnessInput = Console.ReadLine();
|
|
int value = Convert.ToInt16(brightnessInput);
|
|
if (string.IsNullOrWhiteSpace(brightnessInput) || value < 0 || value > 100) {
|
|
Console.WriteLine("Invalid Input!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
Task<HttpResponseMessage> httpResponseMessageTask = _GoveeApiService.SetBrightnessAsync(goveeApiDevice.DeviceId, goveeApiDevice.Model, value);
|
|
httpResponseMessageTask.Wait();
|
|
CheckHttpResponseMessage(httpResponseMessageTask);
|
|
EndSegment();
|
|
}
|
|
|
|
private static void InputFive() {
|
|
if (_APIDevices.Count == 0) {
|
|
Console.WriteLine("No Devices discovered! Please use Option 2 first!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
Console.WriteLine("Please enter the Name of the Device:");
|
|
string? nameInput3 = Console.ReadLine();
|
|
GoveeApiDevice? goveeApiDevice = string.IsNullOrWhiteSpace(nameInput3) ? null : _APIDevices.FirstOrDefault(x => x.DeviceName.EqualWhenIgnoringCase(nameInput3));
|
|
if (goveeApiDevice is null) {
|
|
Console.WriteLine("Device Name Invalid!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
Console.WriteLine($"Please choose a Color to set {nameInput3} to ... (blue, red, green)");
|
|
string? colorInput = Console.ReadLine();
|
|
if (string.IsNullOrWhiteSpace(colorInput) || (!colorInput.EqualWhenIgnoringCase("blue") && !colorInput.EqualWhenIgnoringCase("green") && !colorInput.EqualWhenIgnoringCase("red"))) {
|
|
Console.WriteLine("Invalid Input!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
RgbColor? color = null;
|
|
if (colorInput == "blue") {
|
|
color = new RgbColor(0, 0, 254);
|
|
} else if (colorInput == "green") {
|
|
color = new RgbColor(0, 254, 0);
|
|
} else if (colorInput == "red") {
|
|
color = new RgbColor(254, 0, 0);
|
|
}
|
|
if (color is null) {
|
|
Console.WriteLine("Invalid Color Input!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
Task<HttpResponseMessage> httpResponseMessageTask = _GoveeApiService.SetColorAsync(goveeApiDevice.DeviceId, goveeApiDevice.Model, color);
|
|
httpResponseMessageTask.Wait();
|
|
CheckHttpResponseMessage(httpResponseMessageTask);
|
|
EndSegment();
|
|
}
|
|
|
|
private static void InputSix() {
|
|
Console.WriteLine("Requesting Devices ...");
|
|
Task<IList<GoveeUdpDevice>> goveeUdpDevicesTask = _GoveeUdpService.GetDevicesAsync();
|
|
goveeUdpDevicesTask.Wait();
|
|
_UDPDevices = goveeUdpDevicesTask.Result.AsReadOnly();
|
|
Console.WriteLine("Devices:");
|
|
foreach (GoveeUdpDevice device in _UDPDevices) {
|
|
Console.WriteLine($"IpAddress: {device.IP}, Device Id: {device.Device}, Model: {device.Sku}");
|
|
}
|
|
Console.WriteLine($"Total: {_UDPDevices.Count} Devices.");
|
|
EndSegment();
|
|
}
|
|
|
|
private static void InputSeven(string? input) {
|
|
GoveeUdpDevice? goveeUdpDevice = GetUdpDeviceSelection();
|
|
if (goveeUdpDevice is null) {
|
|
Console.WriteLine("No Devices discovered! Please use Option 6 first!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
Console.WriteLine($"Do you want to turn the Device {goveeUdpDevice.IP} on or off?");
|
|
string? onOffInput2 = Console.ReadLine()?.ToLower();
|
|
if (string.IsNullOrWhiteSpace(onOffInput2) || (onOffInput2 != "on" && onOffInput2 != "off")) {
|
|
Console.WriteLine("Invalid Input!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
if (input == "on") {
|
|
_GoveeUdpService.ToggleDevice(goveeUdpDevice.IP, true);
|
|
} else {
|
|
_GoveeUdpService.ToggleDevice(goveeUdpDevice.IP, false);
|
|
}
|
|
EndSegment();
|
|
}
|
|
|
|
private static void InputEight() {
|
|
GoveeUdpDevice? goveeUdpDevice = GetUdpDeviceSelection();
|
|
if (goveeUdpDevice is null) {
|
|
Console.WriteLine("No Devices discovered! Please use Option 6 first!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
Console.WriteLine($"Please enter a Brightness Value for Device {goveeUdpDevice.IP}. 0-100");
|
|
string? brightnessInput2 = Console.ReadLine();
|
|
int value2 = Convert.ToInt16(brightnessInput2);
|
|
if (string.IsNullOrWhiteSpace(brightnessInput2) || value2 < 0 || value2 > 100) {
|
|
Console.WriteLine("Invalid Input!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
_GoveeUdpService.SetBrightness(goveeUdpDevice.IP, value2);
|
|
Console.WriteLine($"Set Brightness of Device {goveeUdpDevice.IP} to {value2}%!");
|
|
EndSegment();
|
|
}
|
|
|
|
private static void InputNine() {
|
|
RgbColor? color = null;
|
|
GoveeUdpDevice? goveeUdpDevice = GetUdpDeviceSelection();
|
|
if (goveeUdpDevice is null) {
|
|
Console.WriteLine("No Devices discovered! Please use Option 6 first!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
Console.WriteLine($"Please choose a Color to set {goveeUdpDevice.IP} to ... (blue, red, green)");
|
|
string? colorInput = Console.ReadLine()?.ToLower();
|
|
if (string.IsNullOrWhiteSpace(colorInput) || (colorInput != "blue" && colorInput != "green" && colorInput != "red")) {
|
|
Console.WriteLine("Invalid Input!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
if (colorInput == "blue") {
|
|
color = new RgbColor(0, 0, 254);
|
|
} else if (colorInput == "green") {
|
|
color = new RgbColor(0, 254, 0);
|
|
} else if (colorInput == "red") {
|
|
color = new RgbColor(254, 0, 0);
|
|
}
|
|
if (color is null) {
|
|
Console.WriteLine("Invalid Color Input!");
|
|
EndSegment();
|
|
return;
|
|
}
|
|
_GoveeUdpService.SetColor(goveeUdpDevice.IP, color);
|
|
Console.WriteLine($"Set Color of Device {goveeUdpDevice.IP} to {colorInput}!");
|
|
EndSegment();
|
|
}
|
|
|
|
private static GoveeUdpDevice? GetUdpDeviceSelection() {
|
|
int count = 1;
|
|
Console.WriteLine("Please Choose a Device from the List:");
|
|
if (_UDPDevices is not null) {
|
|
foreach (GoveeUdpDevice device in _UDPDevices) {
|
|
Console.WriteLine($"{count} - IpAddress: {device.IP}, Device Id {device.Device}, Model {device.Sku}");
|
|
count++;
|
|
}
|
|
}
|
|
string? input = Console.ReadLine();
|
|
if (string.IsNullOrWhiteSpace(input) || !short.TryParse(input, out short result)) {
|
|
Console.WriteLine("Invalid Input!");
|
|
return GetUdpDeviceSelection();
|
|
}
|
|
return _UDPDevices is null || _UDPDevices.Count == 0 ? null : _UDPDevices[result - 1];
|
|
}
|
|
|
|
private static void HandleApiInput() {
|
|
while (true) {
|
|
Console.WriteLine("Please enter/paste your Govee Api Key ...");
|
|
Console.WriteLine("Your Api Key should look something like this: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
|
|
string? input = Console.ReadLine();
|
|
if (input is null || input.Length != 36) {
|
|
Console.WriteLine("Wrong Api Key Format!");
|
|
continue;
|
|
}
|
|
_GoveeApiService.SetApiKey(input);
|
|
break;
|
|
}
|
|
Console.WriteLine("Api Key saved!");
|
|
}
|
|
|
|
private static void EndSegment() {
|
|
Console.WriteLine("---------------------------Press any Key to continue---------------------------");
|
|
_ = Console.ReadLine();
|
|
}
|
|
|
|
private static void PrintWelcomeMessage() {
|
|
Console.WriteLine();
|
|
Console.WriteLine("Welcome to the GoveeCSharpConnector Example!");
|
|
Console.WriteLine($"Version: {Assembly.GetEntryAssembly()?.GetName().Version}");
|
|
Console.WriteLine($"To test/explore the GoveeCSharpConnector Version: {Assembly.Load("GoveeCSharpConnector").GetName().Version}");
|
|
Console.WriteLine("----------------------------------------------------------");
|
|
if (string.IsNullOrEmpty(_GoveeApiService.GetApiKey())) {
|
|
Console.WriteLine("1 - Enter GoveeApi Key - START HERE (Required for Api Service Options!)");
|
|
} else {
|
|
Console.WriteLine("1 - Enter GoveeApi Key - Already Set!");
|
|
Console.WriteLine("Api Service:");
|
|
Console.WriteLine("2 - Get a List of all Devices connected to the Api Key Account");
|
|
Console.WriteLine("3 - Turn Device On or Off");
|
|
Console.WriteLine("4 - Set Brightness for Device");
|
|
Console.WriteLine("5 - Set Color of Device");
|
|
}
|
|
Console.WriteLine("Udp Service - No Api Key needed!");
|
|
Console.WriteLine("6 - Get a List of all Devices available in the Network");
|
|
Console.WriteLine("7 - Turn Device On or Off");
|
|
Console.WriteLine("8 - Set Brightness for Device");
|
|
Console.WriteLine("9 - Set Color of Device");
|
|
}
|
|
|
|
} |