-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhue-callionica-connect.html
More file actions
424 lines (353 loc) · 16.5 KB
/
hue-callionica-connect.html
File metadata and controls
424 lines (353 loc) · 16.5 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Hue Connect</title>
<link rel="stylesheet" href="hue-callionica.css">
<script type="module">
import { loadCurrentBridge, storeCurrentBridge, bridgeByIP, bridgeFromAddress, bridgeIPsByDiscovery, bridgesByDiscovery, bridgesByLocalDiscovery, bridgesByRemoteDiscovery, loadConnection, storeConnection, testConnection, register } from "./hue-callionica-connect.js";
const currentBridge = loadCurrentBridge();
class UIBridge {
constructor(root, ip, id, name) {
this.root = root;
this.ip = ip;
this.id = id;
this.name_ = name || ip || "Bridge";
this.status_ = "";
this.root.innerHTML = `<div id="${ip}" class="connection"><p><span class="bridgename">${this.name_}</span></p><p>ID: <span class="bridgeid">${id}</p><p>Address: <span class="bridgeip">${ip}</span></p><p>Status: <span class="bridgestatus">Not connected</span></p></div>`;
//`<span class="bridgename">${this.name_}</span>: <span class="bridgeip">${ip}</span> <span class="bridgestatus"></span>`;
this.rootName = this.root.querySelector(".bridgename");
this.rootIP = this.root.querySelector(".bridgeip");
this.rootStatus = this.root.querySelector(".bridgestatus");
}
get name() {
return this.name_;
}
set name(value) {
this.name_ = value;
this.rootName.innerText = value;
}
get status() {
return this.status_;
}
set status(value) {
this.status_ = value;
this.rootStatus.innerText = value;
}
connect(connection) {
let isDefault = false;
if (currentBridge) {
isDefault = connection.bridge.id === currentBridge.id;
}
this.root.innerHTML = `<div id="${connection.bridge.id}" class="connection"><p><span class="bridgename">${connection.bridge.name}</span></p><p>ID: <span class="bridgeid">${connection.bridge.id}</p><p>Address: <span class="bridgeip">${connection.bridge.ip}</span></p><!--<p>Password: <span class="token">${connection.token}</span></p>--><p>Status: <span class="bridgestatus"></span></p><p><!--<button data-id="${connection.bridge.id}" data-op="set-default" ${isDefault ? "disabled" : ""}>Set as default</button>--><a href="hue-callionica-bridges.html#${connection.bridge.id}">Details</a></p></div>`;
this.rootName = this.root.querySelector(".bridgename");
this.rootIP = this.root.querySelector(".bridgeip");
this.rootStatus = this.root.querySelector(".bridgestatus");
}
remove() {
this.root.remove();
}
}
class UIBridges {
constructor(root) {
this.root = root;
this.bridges = [];
}
getBridge(ip, id) {
if (id) {
const idMatch = this.bridges.find(bridge => bridge.id === id);
if (idMatch) {
return idMatch;
}
}
if (ip) {
const ipMatch = this.bridges.find(bridge => bridge.ip === ip);
if (ipMatch && ((id === undefined) || (ipMatch.id === undefined))) {
return ipMatch;
}
}
}
addBridge(ip, id, name) {
const existing = this.getBridge(ip, id);
if (existing) {
return existing;
}
const bridgeRoot = this.root.ownerDocument.createElement("div");
bridgeRoot.id = id || ip;
this.root.appendChild(bridgeRoot);
const current = new UIBridge(bridgeRoot, ip, id, name);
this.bridges.push(current);
return current;
}
}
class BridgeConnector {
constructor() {
this.didManualIP = false;
this.ui = {
bridges: new UIBridges(document.getElementById("bridges")),
status: document.getElementById("status"),
help: document.getElementById("help"),
};
this.tokens = [];
this.ips = [];
this.unavailable = [];
this.bridges = [];
this.unconnected = [];
this.connections = [];
}
addConnection(connection) {
this.connections.push(connection);
storeConnection(connection);
if (!loadCurrentBridge()) {
storeCurrentBridge(connection.bridge);
}
const ip = connection.bridge.ip;
const id = connection.bridge.id;
const ui = this.ui.bridges.getBridge(ip, id);
ui.connect(connection);
ui.status = `Connected`;
const removals = [];
for (const b of this.ui.bridges.bridges) {
if (b !== ui && b.ip === ip) {
b.remove();
removals.push(b);
}
}
this.ui.bridges.bridges = this.ui.bridges.bridges.filter(b => !removals.includes(b));
}
addUnconnected(bridge) {
if (!this.didManualIP) {
this.didManualIP = true;
document.getElementById("manual-ip").value = bridge.ip;
}
if (this.unconnected.find(b => b.id === bridge.id)) {
return;
}
this.unconnected.push(bridge);
const index = this.unconnected.length - 1;
const ui = this.ui.bridges.getBridge(bridge.ip, bridge.id).rootStatus;
ui.innerHTML = `<button class="connect" data-unconnected="${index}">Connect</button>`;
}
addIP(ip, id, name, token) {
if (token) {
this.tokens.push(token);
}
if (this.ips.find(i => i === ip)) { // TODO
return;
}
this.ui.bridges.addBridge(ip, id, name);
const isFirst = this.ips.length === 0;
this.ips.push(ip);
if (isFirst) {
this.processIPs().then(x => console.log("IPs"));;
}
}
addUnavailable(ip) {
if (!this.didManualIP) {
this.didManualIP = true;
document.getElementById("manual-ip").value = ip;
}
this.unavailable.push(ip);
const ui = this.ui.bridges.getBridge(ip).rootStatus;
ui.innerHTML = `<a href="https://${ip}/api/unauthenticated/config">Check certificate</a>`;
this.ui.help.innerHTML = `
<h2>Check certificates</h2>
<p>Not all of the reported bridges were available.</p>
<p>A bridge may be unavailable if your browser does not trust the self-signed certificate of that bridge.</p>
<p>You can check the certificate and fix the problem by clicking <a href="https://${ip}/api/unauthenticated/config">Check certificate</a> for each bridge.</p>
<p>If you see an error page in the browser that says "Your connection is not private" or "This connection is not private", it means that your browser does not trust the Hue bridge's self-signed certificate.</p>
<p>When the browser does trust the Hue bridge's self-signed certificate, you'll see some data displayed instead of an error page.</p>
<b>To trust the certificate and fix the problem:</b>
<ol>
<li><p>Click <a href="https://${ip}/api/unauthenticated/config">Check certificate</a></p></li>
<li>
<p>When the error page appears:</p>
<p>In Chrome, click "<b>Advanced</b>", then "<b>Proceed to ${ip} (unsafe)</b>"</p>
<p>In Safari, click "<b>visit this website</b>"</p>
<p>Other browsers will allow you to trust the Hue bridge's self-signed certificate in a similar way.</p>
</li>
<li><p>Use your browser's back button to return to this page.</p></li>
</ol>
<p>You will need to do this once only for each bridge that you want to use in this browser on desktop browsers.</p>
<p>On mobile browsers, you may need to do this each time the browser restarts.</p>
<p>If you switch browsers, you will need to trust the self-signed certificate again in the new browser.</p>
`;
}
addBridge(bridge) {
const ui = this.ui.bridges.addBridge(bridge.ip, bridge.id);
ui.id = bridge.id;
ui.name = bridge.name;
const isFirst = this.bridges.length === 0;
this.bridges.push(bridge);
if (isFirst) {
this.processBridges().then(x => console.log("Bridges"));
}
}
async processIP(ip) {
const { bridge, status } = await bridgeFromAddress(ip);
if (status === "reachable") {
this.addBridge(bridge);
} else if (status === "certificate-failure") {
this.addUnavailable(ip);
} else {
// TODO
// Don't need to do anything if the bridge can't be reached
}
}
async processIPs() {
// this.ui.status.innerText = "Contacting bridges...";
let ip;
while (ip = this.ips.pop()) {
this.processIP(ip).then(x => console.log("IP"));;
}
}
async processBridge(bridge) {
// this.ui.status.innerText = `Connecting to bridge ${bridge.name}...`;
const app = "Callionica";
async function test(connector, connection) {
let working = await testConnection(connection);
if (working) {
connector.addConnection(connection);
return connection;
}
}
let connection = loadConnection(app, bridge);
if (connection) {
// Bridge may have changed name or IP since last stored
let currentConnection = { bridge, app, token: connection.token };
if (await test(this, currentConnection)) {
return;
}
}
for (const token of this.tokens) {
let currentConnection = { bridge, app, token };
if (await test(this, currentConnection)) {
return;
}
}
this.addUnconnected(bridge);
}
async processBridges() {
// this.ui.status.innerText = "Connecting to bridges...";
let bridge;
while (bridge = this.bridges.pop()) {
this.processBridge(bridge).then(x => console.log("Bridge"));;
}
}
async registerUnconnected(index) {
const app = "Callionica";
const bridge = this.unconnected[index];
console.log(bridge);
const key = `hue-bridge:${bridge.id}`;
sessionStorage.setItem(key, JSON.stringify(bridge));
const url = `hue-callionica-bridge-sign-in.html?bridge=${bridge.id}`;
document.location = url;
return;
let connection;
try {
connection = await register(bridge, app);
} catch (e) {
}
if (connection) {
connector.addConnection(connection);
return;
}
const doIt = confirm(`Press the button on top of the bridge "${bridge.name}" to connect`);
if (doIt) {
try {
connection = await register(bridge, app);
} catch (e) {
}
if (connection) {
connector.addConnection(connection);
return;
}
}
}
}
const connector = new BridgeConnector();
function onclick(evt) {
const src = evt.srcElement;
const id = evt.srcElement.id;
if (id === "manual-button") {
const ipInput = document.getElementById("manual-ip");
const ip = ipInput.value;
const tokenInput = document.getElementById("manual-token") || undefined;
const token = tokenInput?.value || undefined;
connector.addIP(ip, undefined, undefined, token);
return;
}
{const op = src.getAttribute("data-unconnected");
if (op) {
const index = parseInt(op, 10);
if ((0 <= index) && (index < connector.unconnected.length)) {
connector.registerUnconnected(index).then(x => console.log(x));
}
return;
}}
const op = src.getAttribute("data-op");
if (op === "set-default") {
const id = src.getAttribute("data-id");
const connection = connector.connections.find(c => c.bridge.id === id);
if (connection) {
storeCurrentBridge(connection.bridge);
document.location = document.location;
}
}
}
async function main() {
const accepted = localStorage.getItem("hue-callionica-disclaimer");
if (accepted !== "accepted") {
document.location = "hue-callionica-disclaimer.html";
return;
}
window.addEventListener('pageshow', (event) => {
const backOrForward = event.persisted || (window.performance?.navigation?.type === 2);
if (backOrForward) {
location.reload();
}
});
document.onclick = onclick;
const locals = await bridgesByLocalDiscovery();
const remotes = await bridgesByRemoteDiscovery();
// Later addresses override earlier ones (if they still work)
// so put the locals after the remotes
const bridges = [...remotes, ...locals];
// let bridges = await bridgeIPsByDiscovery();
connector.ui.status.innerText = "";
bridges.forEach(bridge => connector.addIP(bridge.ip, bridge.id, bridge.name));
}
main().then(x => console.log("Done"));
</script>
</head>
<body>
<h1>Hue Bridges › Connect</h1>
<p>Here you can see the Hue bridges on your network and connect to them.</p>
<hr/>
<h2>Hue Bridges</h2>
<div id="bridges"></div>
<div id="status">Looking for bridges...</div>
<!--<p><a href="hue-callionica-bridges.html">View all bridges...</a></p>-->
<hr/>
<div id="manual">
<h2>Advanced</h2>
<p>If you know your bridge's IP address or name, you can search for it:</p>
<p><label for="manual-ip">Address:</label> <input id="manual-ip" type="text" maxlength="40" placeholder="philips-hue.local" value="philips-hue.local"></p>
<p></p><button id="manual-button">Search</button></p>
</div>
<hr/>
<div id="related">
<h2>Related</h2>
<p><a href="hue-callionica-bridges.html">Bridges › Details</a></p>
</div>
<hr/>
<div id="help"></div>
<div id="tips">
<h2>Tips</h2>
<p>If you haven't already, we recommend you set a static IP address for each of your bridges. You can usually do this from your router.</p>
</div>
<hr/>
</body>
</html>