-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhue-callionica-connect.js
304 lines (254 loc) · 8.75 KB
/
hue-callionica-connect.js
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"use strict";
import { getConfig, delay, TimeoutExpired, TimeoutCanceled, fetchJSON } from "./hue-callionica.js";
// bridge: { id, ip, name }
// connection: { bridge, app, token }
const KEY_DISCOVERY = "hue-bridges";
const KEY_BRIDGE = "hue-bridge-current";
const KEY_BRIDGES = "hue-bridges-current";
export function loadConnection(app, bridge) {
const key = `hue-connection:${app}:${bridge.id}`;
const json = localStorage.getItem(key);
if (json) {
return JSON.parse(json);
}
}
export function forgetConnection(connection) {
const key = `hue-connection:${connection.app}:${connection.bridge.id}`;
localStorage.removeItem(key);
}
export function loadConnections() {
return Object.keys(localStorage).filter(key => key.startsWith("hue-connection:")).map(key => JSON.parse(localStorage[key]));
}
function storeBridge(bridge) {
const key = KEY_DISCOVERY;
const json = localStorage.getItem(key);
const item = bridge;
let list = [item];
if (json) {
list = JSON.parse(json);
const existing = list.find(existing => existing.id === item.id);
if (!existing) {
list.push(item);
} else {
existing.name = item.name;
existing.ip = item.ip;
}
}
const data = JSON.stringify(list);
localStorage.setItem(key, data);
localStorage.setItem(`hue-bridge:${bridge.id}`, JSON.stringify(bridge, null, 2));
}
export function storeCurrentBridge(bridge) {
let currentBridges = loadCurrentBridges();
if (currentBridges !== undefined) {
const otherBridges = currentBridges.filter(b => b.id !== bridge.id);
currentBridges = [bridge, ...otherBridges];
storeCurrentBridges(currentBridges);
} else {
storeCurrentBridges([bridge]);
}
}
function loadCurrentBridge_() {
const key = KEY_BRIDGE;
const json = localStorage.getItem(key);
if (json) {
return JSON.parse(json);
}
}
export function loadCurrentBridge() {
const bridges = loadCurrentBridges();
const bridge = bridges?.[0];
if (bridge !== undefined) {
return bridge;
}
return loadCurrentBridge_();
}
export function storeCurrentBridges(bridges) {
const key = KEY_BRIDGES;
const data = JSON.stringify(bridges);
localStorage.setItem(key, data);
}
export function loadCurrentBridges() {
const key = KEY_BRIDGES;
const json = localStorage.getItem(key);
if (json) {
return JSON.parse(json);
}
const bridge = loadCurrentBridge_();
if (bridge !== undefined) {
return [bridge];
}
return Object.keys(localStorage).filter(key => key.startsWith("hue-bridge:")).map(key => JSON.parse(localStorage[key]));
}
export function storeConnection(connection) {
const data = JSON.stringify(connection);
const keys = [
`hue-connection:${connection.app}:${connection.bridge.id}`
];
keys.forEach(key => localStorage.setItem(key, data));
storeBridge(connection.bridge);
}
// Given an IP, we can get the bridge ID and name without authenticating
export async function bridgeByIP(ip) {
const connection = { bridge: {ip}, token: "unauthenticated" };
const config = await getConfig(connection);
return { id: config.bridgeid.toLowerCase(), ip, name: config.name };
}
export async function diagnoseConnection(connection) {
try {
const result = await getConfig(connection);
const id = result.bridgeid?.toLowerCase();
if (id === undefined) {
return "not-a-bridge-error";
}
if (id !== connection.bridge.id) {
return "wrong-bridge-error";
}
if (result.whitelist === undefined) {
return "authentication-error";
}
return "success";
} catch (error) {
if (error.e instanceof TimeoutExpired) {
return "unreachable";
}
if (error.e instanceof TimeoutCanceled) {
return "request-canceled";
}
return "certificate-error";
}
}
// // Return the bridge name and serial number obtained from the description XML over HTTP
// // This method avoids certificate issues by relying on HTTP only
// export async function bridgeFromDescriptionXML(address) {
// const response = await fetch(`http://${address}/description.xml`);
// const data = await response.text();
// const parser = new DOMParser();
// const dom = parser.parseFromString(data, "application/xml");
// let serialNumber = dom.querySelector("serialNumber").textContent.toLowerCase();
// const id = serialNumber.substring(0, 6) + "fffe" + serialNumber.substring(6);
// const name = dom.querySelector("friendlyName").textContent;
// const bridge = { id, ip: address, name };
// return bridge;
// }
export async function bridgeFromAddress(address) {
try {
const bridge = await bridgeByIP(address);
return { bridge, status: "reachable" };
} catch (error) {
if (error.e instanceof TimeoutExpired) {
return { status: "unreachable" };
}
if (error.e instanceof TimeoutCanceled) {
return { status: "request-canceled" };
}
}
return { status: "certificate-failure" };
// MIXED CONTENT ERROR PRODUCED BY CODE BELOW SO CAN'T USE
// try {
// const bridge = await bridgeFromDescriptionXML(address);
// return { bridge, status: "certificate-failure" };
// } catch (e) {
// }
// return { status: "unreachable" };
}
async function jsonFetch(address, ms) {
var result;
try {
result = await fetchJSON(address, undefined, ms);
} catch (e) {
console.log(e);
throw { address, e };
}
return result;
}
// Philips Hue bridges report internal IP addresses to meethue
// The server will send you back the internal IP addresses of any bridges whose public IP matches the public IP address of your request
export async function bridgesByRemoteDiscovery() {
const result = await jsonFetch("https://discovery.meethue.com", 6000);
return result.map(item => { return { id: item.id, ip: item.internalipaddress }; });
}
// export async function bridgesByStandardMdnsName() {
// const host = "philips-hue.local";
// }
// Every time we get a working connection, we store the bridge ID and local IP address
export async function bridgesByLocalDiscovery() {
const key = KEY_DISCOVERY;
const json = localStorage.getItem(key);
if (json) {
return JSON.parse(json);
}
return [];
}
export async function bridgeIPsByDiscovery() {
const locals = await bridgesByLocalDiscovery();
const remotes = await bridgesByRemoteDiscovery();
// Add unknown local items to the remote items
for (const local of locals) {
const remote = remotes.find(remote => remote.id === local.id);
if (remote) {
remote.name = local.name;
} else {
const ip = local.ip || local.internalipaddress;
remotes.push({...local, ip});
}
}
return remotes;
}
export async function bridgesByDiscovery() {
const discoveredBridges = await bridgeIPsByDiscovery();
const result = [];
for (const discoveredBridge of discoveredBridges) {
const bridge = await bridgeByIP(discoveredBridge.ip);
result.push(bridge);
}
return result;
}
////////
async function get(address) {
let bridgeResult;
try {
bridgeResult = await fetchJSON(address);
} catch (e) {
console.log(address);
console.log(body);
console.log(e);
throw { body, e };
}
return bridgeResult;
}
async function send(method, address, body) {
let bridgeResult;
try {
bridgeResult = await fetchJSON(address, { method, body });
} catch (e) {
console.log(address);
console.log(body);
console.log(e);
throw { body, e };
}
if (Array.isArray(bridgeResult) && (bridgeResult.length === 1) && bridgeResult[0].success) {
return bridgeResult;
}
console.log(address);
console.log(body);
console.log(bridgeResult);
throw { body, bridgeResult };
}
// A connection is working if we can get the whitelist and the responding bridge has the expected ID
export async function testConnection(connection) {
const address = `https://${connection.bridge.ip}/api/${connection.token}/config`;
try {
const result = await get(address);
return (result.whitelist !== undefined) && (result.bridgeid.toLowerCase() === connection.bridge.id);
} catch (e) {
}
return false;
}
export async function register(bridge, app) {
const address = `https://${bridge.ip}/api/`;
const body = `{"devicetype": "${app}"}`;
const method = "POST";
let bridgeResult = await send(method, address, body);
return { bridge, app, token: bridgeResult[0].success.username };
}