From 04c0e9c447d98de286b5d201d692a8dfbe1cdbbf Mon Sep 17 00:00:00 2001 From: Michiel Post Date: Mon, 7 Nov 2022 17:07:29 +0100 Subject: [PATCH] Rewrite to v2 API --- src/Hue.Blazor.csproj | 7 +-- src/Models/MainViewModel.cs | 4 +- src/Pages/Index.razor | 96 +++++++++++++++++++------------------ 3 files changed, 55 insertions(+), 52 deletions(-) diff --git a/src/Hue.Blazor.csproj b/src/Hue.Blazor.csproj index 53d3b3c..b6d48a0 100644 --- a/src/Hue.Blazor.csproj +++ b/src/Hue.Blazor.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -12,11 +12,12 @@ + + - - + diff --git a/src/Models/MainViewModel.cs b/src/Models/MainViewModel.cs index 221d796..b1869b3 100644 --- a/src/Models/MainViewModel.cs +++ b/src/Models/MainViewModel.cs @@ -12,14 +12,14 @@ public class MainViewModel public class GroupViewModel { - public string Id { get; set; } + public Guid Id { get; set; } public string Name { get; set; } public List Lights { get; set; } = new List(); } public class LightViewModel { - public string Id { get; set; } + public Guid Id { get; set; } public string Name { get; set; } public bool IsOn { get; set; } diff --git a/src/Pages/Index.razor b/src/Pages/Index.razor index a3bd4a0..1bab845 100644 --- a/src/Pages/Index.razor +++ b/src/Pages/Index.razor @@ -1,10 +1,12 @@ @page "/" @inject Blazored.LocalStorage.ILocalStorageService localStorage @using BlazorColorPicker -@using Q42.HueApi -@using Q42.HueApi.ColorConverters -@using Q42.HueApi.ColorConverters.HSB +@using HueApi +@using HueApi.ColorConverters +@using HueApi.ColorConverters.Original +@using HueApi.ColorConverters.Original.Extensions @using Hue.Blazor.Models +@using HueApi.Models.Requests

Hue Blazor

@@ -31,7 +33,7 @@ else if (!string.IsNullOrEmpty(ip)) @if (!string.IsNullOrEmpty(apiKey)) { - @@ -94,7 +96,7 @@ else if (!string.IsNullOrEmpty(ip)) private readonly string STORAGE_KEY_IP = "ip"; private readonly string STORAGE_KEY_API = "api"; - Q42.HueApi.LocalHueClient hueClient; + HueApi.LocalHueApi? hueClient; protected override async Task OnInitializedAsync() @@ -118,20 +120,20 @@ else if (!string.IsNullOrEmpty(ip)) { if (!string.IsNullOrEmpty(apiKey) && !string.IsNullOrEmpty(ip)) { - hueClient = new Q42.HueApi.LocalHueClient(ip, apiKey); + hueClient = new HueApi.LocalHueApi(ip, apiKey, new HttpClient()); await LoadLights(); } else if (!string.IsNullOrEmpty(ip)) { - hueClient = new Q42.HueApi.LocalHueClient(ip); + hueClient = new HueApi.LocalHueApi(ip, null, new HttpClient()); } } private async Task FindBridge() { - var locator = new Q42.HueApi.HttpBridgeLocator(); + var locator = new HueApi.BridgeLocator.HttpBridgeLocator(); var bridges = await locator.LocateBridgesAsync(TimeSpan.FromSeconds(5)); if (bridges.Any()) { @@ -149,10 +151,10 @@ else if (!string.IsNullOrEmpty(ip)) private async Task RegisterApp() { - var result = await hueClient.RegisterAsync("blazorhue", "browser"); - if (!string.IsNullOrEmpty(result)) + var result = await LocalHueApi.RegisterAsync(ip, "blazorhue", "browser"); + if (!string.IsNullOrEmpty(result?.Username)) { - apiKey = result; + apiKey = result.Username; await localStorage.SetItemAsync(STORAGE_KEY_API, apiKey); CreateHueClient(); @@ -163,39 +165,39 @@ else if (!string.IsNullOrEmpty(ip)) private async Task LoadLights() { var lights = await hueClient.GetLightsAsync(); - var groups = await hueClient.GetGroupsAsync(); + var groups = await hueClient.GetGroupedLightsAsync(); - foreach (var group in groups) + foreach (var group in groups.Data) { var groupVm = new GroupViewModel() { Id = group.Id, - Name = group.Name + Name = group.Metadata.Name }; - foreach (var lightId in group.Lights) - { - var lightInfo = lights.Where(x => x.Id == lightId).FirstOrDefault(); + //foreach (var lightId in group.Lights) + //{ + // var lightInfo = lights.Where(x => x.Id == lightId).FirstOrDefault(); - var lightVm = new LightViewModel() - { - Id = lightId, - Name = lightInfo.Name, - IsOn = lightInfo.State.On, - Color = lightInfo.State.ToHex() - }; + // var lightVm = new LightViewModel() + // { + // Id = lightId, + // Name = lightInfo.Name, + // IsOn = lightInfo.State.On, + // Color = lightInfo.State.ToHex() + // }; - groupVm.Lights.Add(lightVm); - } + // groupVm.Lights.Add(lightVm); + //} vm.Groups.Add(groupVm); } } - private Task GroupOn(string groupId) + private Task GroupOn(Guid groupId) { - Q42.HueApi.LightCommand lightCommand = new Q42.HueApi.LightCommand(); - lightCommand.On = true; + var lightCommand = new UpdateGroupedLight(); + lightCommand.TurnOn(); lightCommand.SetColor(new RGBColor("#F1F7E9")); var group = vm.Groups.Where(x => x.Id == groupId).FirstOrDefault(); @@ -204,13 +206,13 @@ else if (!string.IsNullOrEmpty(ip)) light.IsOn = true; } - return hueClient.SendGroupCommandAsync(lightCommand, groupId); + return hueClient.UpdateGroupedLightAsync(groupId, lightCommand); } - private Task GroupOff(string groupId) + private Task GroupOff(Guid groupId) { - Q42.HueApi.LightCommand lightCommand = new Q42.HueApi.LightCommand(); - lightCommand.On = false; + var lightCommand = new UpdateGroupedLight(); + lightCommand.TurnOff(); var group = vm.Groups.Where(x => x.Id == groupId).FirstOrDefault(); foreach (var light in group.Lights) @@ -218,38 +220,38 @@ else if (!string.IsNullOrEmpty(ip)) light.IsOn = false; } - return hueClient.SendGroupCommandAsync(lightCommand, groupId); + return hueClient.UpdateGroupedLightAsync(groupId, lightCommand); } - private Task LightOn(string lightId) + private Task LightOn(Guid lightId) { - Q42.HueApi.LightCommand lightCommand = new Q42.HueApi.LightCommand(); - lightCommand.On = true; + var lightCommand = new UpdateLight(); + lightCommand.TurnOn(); lightCommand.SetColor(new RGBColor("#F1F7E9")); var light = vm.Groups.SelectMany(x => x.Lights).Where(x => x.Id == lightId).FirstOrDefault(); light.IsOn = true; - return hueClient.SendCommandAsync(lightCommand, new List { lightId }); + return hueClient.UpdateLightAsync(lightId, lightCommand); } - private Task LightOff(string lightId) + private Task LightOff(Guid lightId) { - Q42.HueApi.LightCommand lightCommand = new Q42.HueApi.LightCommand(); - lightCommand.On = false; + var lightCommand = new UpdateLight(); + lightCommand.TurnOff(); var light = vm.Groups.SelectMany(x => x.Lights).Where(x => x.Id == lightId).FirstOrDefault(); light.IsOn = false; - return hueClient.SendCommandAsync(lightCommand, new List { lightId }); + return hueClient.UpdateLightAsync(lightId, lightCommand); } bool isOpened = false; string color = "#F1F7E9"; - string colorPickerGroupId; + Guid colorPickerGroupId; - void OpenModal(string groupId) + void OpenModal(Guid groupId) { isOpened = true; colorPickerGroupId = groupId; @@ -260,10 +262,10 @@ else if (!string.IsNullOrEmpty(ip)) color = value; isOpened = false; - Q42.HueApi.LightCommand lightCommand = new Q42.HueApi.LightCommand(); - lightCommand.On = true; + var lightCommand = new UpdateGroupedLight(); + lightCommand.TurnOn(); lightCommand.SetColor(new RGBColor(color)); - return hueClient.SendGroupCommandAsync(lightCommand, colorPickerGroupId); + return hueClient.UpdateGroupedLightAsync(colorPickerGroupId, lightCommand); } }