-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHueController.cs
More file actions
186 lines (154 loc) · 6.45 KB
/
HueController.cs
File metadata and controls
186 lines (154 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Q42.HueApi;
using Q42.HueApi.Interfaces;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Q42.HueApi.Models;
using Q42.HueApi.Models.Groups;
namespace Functions.Hue
{
public static class HueController
{
// Get values from https://developers.meethue.com/my-apps/
static string _appId = Environment.GetEnvironmentVariable("HueAppId") ?? "azure";
private static string _clientId = Environment.GetEnvironmentVariable("HueClientId");
static string _clientSecret = Environment.GetEnvironmentVariable("HueClientSecret");
static string _instanceId = Environment.GetEnvironmentVariable("HueInstanceId") ?? "PhilipsHue0";
public static IRemoteAuthenticationClient _authClient = new RemoteAuthenticationClient(_clientId, _clientSecret, _appId);
public static RemoteHueClient _hueClient;
private static AccessTokenResponse _lastToken;
[FunctionName("ResetAuthentication")]
public static async Task<IActionResult> ResetAuthentication(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
[DurableClient] IDurableEntityClient client,
ILogger log)
{
var entityId = new EntityId(nameof(HueState), _instanceId);
await client.SignalEntityAsync<IHueState>(entityId, proxy => proxy.Delete());
return new OkObjectResult("OAuth information cleared.");
}
[FunctionName("InitializeAuthentication")]
public static IActionResult InitializeAuthentication(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
[DurableClient] IDurableEntityClient client,
ILogger log)
{
var authCodeUrl = _authClient.BuildAuthorizeUri("initialize", "azure_function", "Azure Function");
var response = $"Visit '{authCodeUrl.AbsoluteUri}' to retrieve an authorization code.";
log.LogWarning(response);
return new RedirectResult(authCodeUrl.AbsoluteUri);
}
[FunctionName("RedeemCode")]
public static async Task<IActionResult> RedeemCode(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
[DurableClient] IDurableEntityClient client,
ILogger log)
{
var authCode = req.Query["code"];
if (string.IsNullOrEmpty(authCode))
{
log.LogWarning("No auth code provided.");
return new BadRequestResult();
}
var accessToken = await _authClient.GetToken(authCode);
if (accessToken.Expires_in == 0)
{
throw new SystemException("Could not get token.");
}
var entityId = new EntityId(nameof(HueState), _instanceId);
await client.SignalEntityAsync<IHueState>(entityId, proxy => proxy.SetAccessToken(accessToken));
return new OkObjectResult("OAuth authorization flow completed.");
}
public static async Task<IActionResult> ConnectToBridge(
HttpRequest req,
IDurableEntityClient client,
ILogger log)
{
var entityId = new EntityId(nameof(HueState), _instanceId);
var state = await client.ReadEntityStateAsync<HueState>(entityId);
if (!state.EntityExists)
{
log.LogWarning("No access token found. Run 'InitializeAuthentication' to start OAuth flow.");
var result = InitializeAuthentication(req, client, log);
return result;
}
var accessToken = await state.EntityState.GetAccessToken();
// Get a new token
if(accessToken.AccessTokenExpireTime() < DateTimeOffset.UtcNow.AddMinutes(-5))
{
accessToken = await _authClient.RefreshToken(accessToken.Refresh_token);
if(accessToken.Expires_in == 0)
{
throw new SystemException("Could not refresh token.");
}
await client.SignalEntityAsync<IHueState>(entityId, proxy => proxy.SetAccessToken(accessToken));
}
_lastToken = accessToken;
_authClient.Initialize(accessToken);
_hueClient = new RemoteHueClient(GetValidToken);
var bridges = await _hueClient.GetBridgesAsync();
if (bridges == null)
{
log.LogWarning("No bridges found. Run 'InitializeAuthentication' to start OAuth flow.");
var result = InitializeAuthentication(req, client, log);
return result;
}
var appKey = await state.EntityState.GetAppKey();
if (string.IsNullOrEmpty(appKey))
{
//Register app
var bridgeId = bridges.First().Id;
appKey = await _hueClient.RegisterAsync(bridgeId, "Azure Functions");
await client.SignalEntityAsync<IHueState>(entityId, proxy => proxy.SetAppKey(appKey));
log.LogInformation($"Your AppKey is '{appKey}' and your Bridge ID is '{bridgeId}'.");
}
else
{
//Or initialize with saved key:
_hueClient.Initialize(bridges.First().Id, appKey);
}
// Test HueClient
await _hueClient.GetCapabilitiesAsync();
return new OkObjectResult("Connection to bridge established.");
}
[FunctionName("SetSensorState")]
public static async Task<IActionResult> SetSensorState(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "SetSensorState/{state}")] HttpRequest req,
string state,
[DurableClient] IDurableEntityClient client,
ILogger log)
{
if(_hueClient == null || !_hueClient.IsInitialized)
{
var connectionResult = await ConnectToBridge(req, client, log);
if (connectionResult is RedirectResult)
{
return connectionResult;
}
}
var results = new HueResults();
// Begin of Hue controller logic
var sensors = await _hueClient.GetSensorsAsync();
var config = new SensorConfig()
{
On = "On".Equals(state, StringComparison.OrdinalIgnoreCase)
};
sensors
.Where(s => s.ModelId == "SML001" && s.Type == "ZLLPresence")
.ToList()
.ForEach(s => results.AddRange(_hueClient.ChangeSensorConfigAsync(s.Id, config).Result));
// End of Hue controller logic
return results.Errors.Any() ? new BadRequestObjectResult(results.Errors) : new OkObjectResult("OK");
}
public static Task<string> GetValidToken()
{
return Task.FromResult<string>(_lastToken?.Access_token);
}
}
}