Skip to content

Commit

Permalink
Added sample scenario for changing lights while listening to event st…
Browse files Browse the repository at this point in the history
…ream
  • Loading branch information
michielpost committed Nov 29, 2024
1 parent 6add85d commit 6fa17cf
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/HueApi.ConsoleSample/HueApi.ConsoleSample.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HueApi.ColorConverters\HueApi.ColorConverters.csproj" />
<ProjectReference Include="..\HueApi\HueApi.csproj" />
</ItemGroup>

Expand Down
73 changes: 55 additions & 18 deletions src/HueApi.ConsoleSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// See https://aka.ms/new-console-template for more information
using HueApi;
using HueApi.Models;
using HueApi.Models.Requests;
using HueApi.Models.Responses;
using Microsoft.Extensions.Configuration;
using HueApi.ColorConverters.Original.Extensions;

Console.WriteLine("HueApi Console Sample");

Expand All @@ -17,34 +18,37 @@

var localHueClient = new LocalHueApi(ip, key);

Console.WriteLine("Getting all resources...");
//Console.WriteLine("Getting all resources...");

var resources = await localHueClient.GetResourcesAsync();
var roots = resources.Data.Where(x => x.Owner == null);
//var resources = await localHueClient.GetResourcesAsync();
//var roots = resources.Data.Where(x => x.Owner == null);

PrintChildren(resources, null, 0);
//PrintChildren(resources, null, 0);

void PrintChildren(HueResponse<HueResource> resources, Guid? owner, int level)
{
var children = resources.Data.Where(x => x.Owner?.Rid == owner);

foreach (var child in children)
{
string spaces = new string(' ', level);
Console.WriteLine(spaces + $"- {child.Type}");

PrintChildren(resources, child.Id, level + 1);
}
}
var allLights = await localHueClient.GetLightsAsync();
var firstLightId = allLights.Data.First().Id; //First

var allGroups = await localHueClient.GetGroupedLightsAsync();
var allGroupId = allGroups.Data.OrderBy(x=> x.IdV1).First().Id; //All

localHueClient.OnEventStreamMessage += EventStreamMessage;
localHueClient.StartEventStream();

Console.WriteLine("Waiting for Hue Bridge events...");

//await Task.Delay(TimeSpan.FromHours(1));

Console.WriteLine("Press enter to change the lights to red..");
Console.ReadLine();
await ChangeColorForGroup(allGroupId, "FF0000");
//await ChangeColorForLight(firstLightId, "FF0000");

Console.WriteLine("Press enter to change the lights to green..");
Console.ReadLine();
await ChangeColorForGroup(allGroupId, "00FF00");
//await ChangeColorForLight(firstLightId, "00FF00");


Console.WriteLine("Press enter to stop listening for events...");
Console.ReadLine();
localHueClient.StopEventStream();

Expand Down Expand Up @@ -73,3 +77,36 @@ void EventStreamMessage(string bridgeIp, List<EventStreamResponse> events)
}
}

void PrintChildren(HueResponse<HueResource> resources, Guid? owner, int level)
{
var children = resources.Data.Where(x => x.Owner?.Rid == owner);

foreach (var child in children)
{
string spaces = new string(' ', level);
Console.WriteLine(spaces + $"- {child.Type}");

PrintChildren(resources, child.Id, level + 1);
}
}



Task ChangeColorForGroup(Guid id, string hex)
{
var req = new UpdateGroupedLight()
.TurnOn()
.SetColor(new HueApi.ColorConverters.RGBColor(hex));

return localHueClient.UpdateGroupedLightAsync(id, req);
}

Task ChangeColorForLight(Guid id, string hex)
{
var req = new UpdateLight()
.TurnOn()
.SetColor(new HueApi.ColorConverters.RGBColor(hex));

return localHueClient.UpdateLightAsync(id, req);
}

0 comments on commit 6fa17cf

Please sign in to comment.