-
Notifications
You must be signed in to change notification settings - Fork 33
/
Bridge.cs
231 lines (215 loc) · 8.3 KB
/
Bridge.cs
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// ---------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------------------------------------------------------------
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Web.Http;
namespace HueLibrary
{
/// <summary>
/// Represents a Hue bridge.
/// </summary>
public class Bridge
{
/// <summary>
/// Default constructor for the bridge. Requires manually setting the IP and userId.
/// </summary>
public Bridge() { }
/// <summary>
/// Constructor for the bridge with IP provided. Requires manually setting the userId.
/// </summary>
public Bridge(string ip)
{
Ip = ip;
}
/// <summary>
/// Constructor for the bridge with IP and username provided.
/// </summary>
public Bridge(string ip, string userId) : this(ip)
{
UserId = userId;
}
/// <summary>
/// Attempts to find a bridge on your network and create an object for it. Returns null if no bridge is found.
/// </summary>
public static async Task<Bridge> FindAsync()
{
using (var client = new HttpClient())
{
try
{
string response = await client.GetStringAsync(new Uri("https://discovery.meethue.com/"));
if (response == "[]")
{
return null;
}
string ip = JArray.Parse(response).First["internalipaddress"].ToString();
return new Bridge(ip);
}
catch (Exception)
{
return null;
}
}
}
/// <summary>
/// Gets or sets the UserId for the bridge. If a registered UserId is not set, all calls to the bridge will fail.
/// </summary>
public string UserId { get; set; }
/// <summary>
/// Gets or sets the IP address of the bridge. If a valid IP is not set, all calls to the bridge will fail.
/// </summary>
public string Ip { get; set; }
/// <summary>
/// Gets the base URL to send bridge commands to.
/// </summary>
public string UrlBase => $"{Ip}/api/{UserId}/";
/// <summary>
/// Gets all lights known to the bridge.
/// </summary>
public async Task<IEnumerable<Light>> GetLightsAsync()
{
var lights = new List<Light>();
HttpResponseMessage response = await HttpGetAsync("lights");
string json = await response.Content.ReadAsStringAsync();
JObject jObject = JObject.Parse(json);
foreach (JProperty property in jObject.Properties())
{
Light light = JsonConvert.DeserializeObject<Light>(property.Value.ToString());
light.Id = property.Name;
light._bridge = this;
light.State._light = light;
if (light.State.Reachable)
{
lights.Add(light);
}
}
return lights;
}
/// <summary>
/// Gets a light with the given id known to the bridge, or null if no light is found.
/// </summary>
public async Task<Light> GetLightAsync(string id)
{
HttpResponseMessage response = await HttpGetAsync($"lights/{id}");
Light light = JsonConvert.DeserializeObject<Light>(await response.Content.ReadAsStringAsync());
if (null != light)
{
light.Id = id;
light._bridge = this;
light.State._light = light;
}
return light;
}
/// <summary>
/// Instructs the bridge to search for new, unknown lights.
/// </summary>
public async Task FindNewLightsAsync() => await HttpPutAsync("lights", String.Empty);
/// <summary>
/// Registers the application with the bridge and returns if the authorization succeeded.
/// </summary>
public async Task<bool> RegisterAsync()
{
try
{
using (var client = new HttpClient())
{
string id = new Random().Next().ToString();
var response = await client.PostAsync(new Uri($"http://{Ip}/api"),
new HttpStringContent($"{{\"devicetype\":\"HueLightController#{id}\"}}"));
string content = await response.Content.ReadAsStringAsync();
JArray json = JArray.Parse(content);
if (null != json.First["success"])
{
UserId = json.First["success"]["username"].ToString();
return true;
}
}
}
catch (Exception)
{
return false;
}
return false;
}
/// <summary>
/// Sends a basic command to the bridge and returns whether it receives the expected response.
/// </summary>
public async Task<BridgeConnectionStatus> PingAsync()
{
try
{
HttpResponseMessage response = await HttpGetAsync("config");
string content = await response.Content.ReadAsStringAsync();
if (content.Contains("zigbeechannel"))
{
return BridgeConnectionStatus.Success;
}
else if (content.Contains("error"))
{
return BridgeConnectionStatus.Unauthorized;
}
return BridgeConnectionStatus.Fail;
}
catch (Exception)
{
return BridgeConnectionStatus.Fail;
}
}
/// <summary>
/// Sends a GET command via HTTP and returns the response.
/// </summary>
internal async Task<HttpResponseMessage> HttpGetAsync(string commandUrl)
{
using (var client = new HttpClient())
{
return await client.GetAsync(new Uri($"http://{UrlBase}{commandUrl}"),
HttpCompletionOption.ResponseContentRead);
}
}
/// <summary>
/// Sends a PUT command via HTTP and returns the response.
/// </summary>
internal async Task<string> HttpPutAsync(string commandUrl, string body)
{
using (var client = new HttpClient())
{
Uri uri = new Uri($"http://{UrlBase}{commandUrl}");
HttpResponseMessage response = await client.PutAsync(uri, new HttpStringContent(body));
return await response.Content.ReadAsStringAsync();
}
}
}
/// <summary>
/// Represents the status of the connection to the bridge.
/// </summary>
public enum BridgeConnectionStatus
{
Success,
Unauthorized,
Fail
}
}