Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite to v2 API #2

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Hue.Blazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
<ItemGroup>
<PackageReference Include="BlazorColorPicker" Version="2.3.0" />
<PackageReference Include="Blazored.LocalStorage" Version="4.4.0" />
<PackageReference Include="HueApi" Version="1.4.0" />
<PackageReference Include="HueApi.ColorConverters" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.0" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
<PackageReference Include="Q42.HueApi" Version="3.23.0" />
<PackageReference Include="Q42.HueApi.ColorConverters" Version="3.23.0" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/Models/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LightViewModel> Lights { get; set; } = new List<LightViewModel>();
}

public class LightViewModel
{
public string Id { get; set; }
public Guid Id { get; set; }
public string Name { get; set; }

public bool IsOn { get; set; }
Expand Down
96 changes: 49 additions & 47 deletions src/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -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

<h1>Hue Blazor</h1>

Expand All @@ -31,7 +33,7 @@ else if (!string.IsNullOrEmpty(ip))

@if (!string.IsNullOrEmpty(apiKey))
{
<button class="btn btn-primary" @onclick="@(e => OpenModal("0"))">
<button class="btn btn-primary" @onclick="@(e => OpenModal(Guid.Empty))">
<div style="background-color:@color" class="buttonColor"></div> Select a Color
</button>

Expand Down Expand Up @@ -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()
Expand All @@ -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())
{
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -204,52 +206,52 @@ 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)
{
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<string> { 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<string> { 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;
Expand All @@ -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);
}
}