-
Notifications
You must be signed in to change notification settings - Fork 7
/
kingfish.html
682 lines (631 loc) · 29.2 KB
/
kingfish.html
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Kingfish for Philips Hue</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.13.0/themes/ui-darkness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.13.0/jquery-ui.min.js"></script>
<style>
body {
margin: 1em;
}
footer,
header {
position: relative;
clear: both;
}
@media screen and (min-width: 40em) {
.columns-container {
float: left;
}
.left-column {
width: 29em;
margin-right: -30em;
float: left;
}
.right-column {
margin-left: 30em;
float: left;
}
}
div.centered {
text-align: center;
}
div {
margin: .2em;
}
div.box {
border: solid 1px;
padding: .4em;
margin: .4em;
}
span.noun {
font-weight: bold;
}
input,
select {
background: #f8f8ff;
}
button,
.quick-set {
background: #e8ffe8;
}
.send-button {
background: #e8e8ff;
}
.on-button {
background: #a8a8ff;
}
.off-button {
background: #ffaaaa;
}
div.verbiage {
max-width: 45em;
}
div.fineprint {
font-style: italic;
font-size: 80%;
}
.ui-widget {
font-size: 80%;
}
#api-tool {
display: none;
}
</style>
</head>
<body>
<header>
<div class="centered">
<strong>Kingfish for Philips Hue,</strong> version 1.2.3, by Peter Kaminski
<br/>An open-source single-file HTML/JavaScript Hue controller for interacting directly with the API. Quick start instructions at bottom of page.
</div>
</header>
<div class="columns-container">
<div class="left-column">
<div class="box">
<div class="box-header">2. Select <span class="noun">Group</span> or <span class="noun">Light</span></div>
<div>
<span id="lg">
<input class="persistent" type="radio" id="lg-group" name="lg" value="groups" checked>
<label for="lg-group">Group</label>
<input class="persistent" type="radio" id="lg-light" name="lg" value="lights">
<label for="lg-light">Light</label>
</span>
<select class="persistent" id="lg-sel">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
<option value="40">40</option>
<option value="41">41</option>
<option value="42">42</option>
<option value="43">43</option>
<option value="44">44</option>
<option value="45">45</option>
<option value="46">46</option>
<option value="47">47</option>
<option value="48">48</option>
<option value="49">49</option>
<option value="50">50</option>
<option value="51">51</option>
<option value="52">52</option>
<option value="53">53</option>
<option value="54">54</option>
<option value="55">55</option>
<option value="56">56</option>
<option value="57">57</option>
<option value="58">58</option>
<option value="59">59</option>
<option value="60">60</option>
<option value="61">61</option>
<option value="62">62</option>
<option value="63">63</option>
<option value="64">64</option>
<option value="65">65</option>
<option value="66">66</option>
<option value="67">67</option>
<option value="68">68</option>
<option value="69">69</option>
<option value="70">70</option>
<option value="71">71</option>
<option value="72">72</option>
<option value="73">73</option>
<option value="74">74</option>
<option value="75">75</option>
<option value="76">76</option>
<option value="77">77</option>
<option value="78">78</option>
<option value="79">79</option>
<option value="80">80</option>
<option value="81">81</option>
<option value="82">82</option>
<option value="83">83</option>
<option value="84">84</option>
<option value="85">85</option>
<option value="86">86</option>
<option value="87">87</option>
<option value="88">88</option>
<option value="89">89</option>
<option value="90">90</option>
<option value="91">91</option>
<option value="92">92</option>
<option value="93">93</option>
<option value="94">94</option>
<option value="95">95</option>
<option value="96">96</option>
<option value="97">97</option>
<option value="98">98</option>
<option value="99">99</option>
</select>
</div>
<div>
<i>quick set:</i>
<button class="lg-set">All Lights</button>
</div>
</div>
<div class="box">
<div class="box-header">2. Select <span class="noun">Transition Time</span></div>
<div>
<input class="persistent" id="tt-val" type="text" size="3" value="5" />
<select class="persistent" id="tt-sel">
<option value="10">seconds</option>
<option value="600">minutes</option>
</select>
</div>
<div>
<i>quick set:</i>
<button class="tt-set">0</button>
<button class="tt-set">1</button>
<button class="tt-set">2</button>
<button class="tt-set">5</button>
<button class="tt-set">10</button>
<button class="tt-set">20</button>
<button class="tt-set">30</button>
<button class="tt-set">60</button>
</div>
</div>
<div class="box">
<div class="box-header">3. Send <span class="noun">Brightness (0-254) + On</span></div>
<div>
<input class="persistent" id="on-bri" type="text" size="3" value="127" />
<button class="send-button, on-button" id="on">On</button>
</div>
<div>
<i>quick set:</i>
<button class="bri-set">0</button>
<button class="bri-set">36</button>
<button class="bri-set">73</button>
<button class="bri-set">109</button>
<button class="bri-set">145</button>
<button class="bri-set">181</button>
<button class="bri-set">218</button>
<button class="bri-set">254</button>
</div>
</div>
<div class="box">
<div class="box-header">3. Send <span class="noun">Hue (0-65535) + Saturation (0-254)</span></div>
<div>
Hue
<input class="persistent" id="huesat-hue" type="text" size="5" value="32767" /> + Sat
<input class="persistent" id="huesat-sat" type="text" size="3" value="127" />
<button class="send-button" id="huesat">Hue+Sat</button>
</div>
<div>
<i>quick set:</i>
<select class="persistent, quick-set" id="huesat-set">
<option value="0">0 - red</option>
<option value="1">1</option>
<option value="2">2 - orange</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5 - yellow</option>
<option value="6">6</option>
<option value="7">7 - green</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11 - blue</option>
<option value="12">12</option>
<option value="13">13 - violet</option>
<option value="14">14</option>
<option value="15">15</option>
</select>
<button class="sat-set">0</button>
<button class="sat-set">36</button>
<button class="sat-set">73</button>
<button class="sat-set">109</button>
<button class="sat-set">145</button>
<button class="sat-set">181</button>
<button class="sat-set">218</button>
<button class="sat-set">254</button>
</div>
</div>
<div class="box">
<div class="box-header">3. Send <span class="noun">Color Temp K (2000-6500)</span></div>
<div>
<input class="persistent" id="ct-kelvin" type="text" size="4" value="2700" />
<button class="send-button" id="ct">Color Temp</button>
</div>
<div>
<i>quick set:</i>
<button class="ct-set">2000</button>
<button class="ct-set">2700</button>
<button class="ct-set">3000</button>
<button class="ct-set">3500</button>
<button class="ct-set">4000</button>
<button class="ct-set">5000</button>
<button class="ct-set">5700</button>
<button class="ct-set">6500</button>
</div>
</div>
<div class="box">
<div class="box-header">3. Recall <span class="noun">Scene</span></div>
<div>
<input class="persistent" id="sc-id" type="text" size="15" />
<button class="send-button" id="sc">Scene</button>
</div>
</div>
<div class="box">
<div class="box-header">3. Execute <span class="noun">Effect</span></div>
<div>
<button class="send-button" id="blink1">Blink 1</button>
<button class="send-button" id="blink15">Blink 15</button>
<button class="send-button" id="colorloop">Color Loop</button>
<button class="send-button" id="effect-none">None</button>
</div>
</div>
<div class="box">
<div class="box-header">3. Play with <span class="noun">Toys</span></div>
<div>
Drift Interval:
<input class="persistent" id="di-val" type="text" size="3" value="5" />
<select class="persistent" id="di-sel">
<option value="10">seconds</option>
<option value="600">minutes</option>
</select>
<button class="send-button" id="drift-on">Drift On</button>
<button class="send-button" id="drift-off">Drift Off</button>
</div>
</div>
<div class="box">
<div class="box-header">3. Send <span class="noun">Off</span></div>
<div>
<button class="send-button, off-button" id="off">Off</button>
</div>
</div>
<div class="box">
<div class="box-header">Get <span class="noun">Status</span></div>
<div>
<select class="persistent" id="get-sel">
<option value="lights">Lights</option>
<option value="groups">Groups</option>
<option value="schedules">Schedules</option>
<option value="scenes">Scenes</option>
<option value="sensors">Sensors</option>
<option value="rules">Rules</option>
<option value="config">Config</option>
<option value="info/timezones">Timezones</option>
<option value="">Everything</option>
</select>
<button class="send-button" id="get">Get</button>
</div>
</div>
</div>
<div class="right-column">
<div class="box">
<div class="box-header">1. Retrieve or specify <span class="noun">Bridge IP Address</span> and <span class="noun">Username</span></div>
<div>
<button class="send-button" id="query-portal">Get Bridge IP</button>
</div>
<div>
<button class="send-button" id="get-username">Get Username</button> for
<input class="persistent" id="devicetype" type="text" size="40" />
</div>
<div>
IP Address
<input class="persistent" id="bridge-ip" type="text" size="40" />
<span id="api-tool"><a id="api-tool-link" target="kingfish-api-tool" href="">API tool</a></span>
</div>
<div>
Username
<input class="persistent" id="username" type="text" size="40" />
</div>
</div>
<div class="box">
<div class="box-header"><span class="noun">Response</span></div>
<div id="response"><pre>[]</pre></div>
</div>
</div>
</div>
<footer>
<div class="instructions">
<p>You can use this page as is, or save it to and reload it from your device, or load or save <a href="http://peterkaminski.github.io/kingfish/kingfish.html">kingfish.html</a> from the web.</p>
<p>If you keep getting "error sending to bridge - confirm IP address is correct" errors, save it to your device and load it from there.</p>
<p>Commands are sent on your local network directly from your device to your Hue bridge, not through any server. (The "Get Bridge IP" command retrieves your bridge IP address from a Philips server, but does not send any data, nor does the Philips server send any commands.) There are no analytics or tracking bugs in this file.</p>
<p>Start by doing boxes in numbered order, then repeat in any order as desired. <span class="quick-set">Green</span> buttons set <span class="send-button">blue</span> values, but don't send commands. <span class="send-button">Blue</span> and <span class="off-button">red</span> buttons send commands to bridge. You can close and re-open Kingfish at any time. Kingfish uses HTML5 local storage, if available in your browser, to save and reload input values entered on your device, as long as you load it from the same location.</p>
<p>"Drift" toy sends a random hue and saturation at the specified interval. Also sends the "Transition Time" interval to the bridge.</p>
<p>Kingfish uses <a target="_blank" href="http://api.jquery.com/jquery.ajax/">jQuery.ajax()</a> to send commands via HTTP to the bridge. For background on Philips Hue terminology and how the Hue API works, read <a target="_blank" href="https://developers.meethue.com/develop/hue-api/">Philips Hue API</a>. Visit the <a target="_blank" href="http://peterkaminski.github.io/kingfish/">Kingfish project home page</a> or the <a target="_blank" href="https://github.com/peterkaminski/kingfish">Kingfish repo</a> to comment, fork, or download the project files. Distributed under open-source MIT license, see below.</p>
</div>
<div class="fineprint">
<p>Kingfish for Philips Hue
<br/>Copyright 2015-2021 Peter Kaminski</p>
<p>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:</p>
<p>The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</p>
<p>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.</p>
</div>
</footer>
<script>
$(function () {
// load all .persistent elements
$(".persistent").each(function (i, d) {
try {
var val = localStorage.getItem("kingfish." + $(this).attr('id'));
if (val) {
$(this).val(val);
}
} catch (e) {}
});
// initialize devicetype if it's empty
if ("" === $("#devicetype").val()) {
$("#devicetype").val("Kingfish#" + Math.floor(Math.random() * 8999 + 1000).toString());
$("#devicetype").change();
}
// set up light-group buttonset
$("#lg").buttonset();
});
// save .persistent element when it changes
$(".persistent").on("change keyup", function () {
try {
localStorage.setItem("kingfish." + $(this).attr('id'), $(this).val());
} catch (e) {}
});
var displayResponse = function (command, response) {
if (!command && response.length) {
if (response[0].error && response[0].error.description === 'link button not pressed') {
alert("Press link button on bridge and try again.");
} else if (response[0].success) {
try {
$("#username").val(response[0].success.username);
$("#username").change();
} catch (e) {
alert(e)
}
}
}
$("#response").html('<pre>' + JSON.stringify(response, undefined, 4) + '</pre>');
};
var portalResponse = function (response) {
if (response.length) {
try {
$("#bridge-ip").val(response[0].internalipaddress);
$("#bridge-ip").change();
} catch (e) {
alert(e)
}
}
$("#response").html('<pre>' + JSON.stringify(response, undefined, 4) + '</pre>');
};
var queryPortal = function () {
try {
$.ajax({
url: "https://discovery.meethue.com/",
type: "GET",
dataType: "json",
success: function (response) {
portalResponse(response)
},
error: function (jqhxr, textStatus, errorThrown) {
alert(textStatus + " sending to bridge - confirm IP address is correct");
}
});
} catch (e) {
alert(e);
}
};
var doHue = function (type, command, dataObj) {
try {
$.ajax({
url: "http://" + $("#bridge-ip").val() + "/api/" + $("#username").val() + (command ? ("/" + command) : ""),
type: type,
data: JSON.stringify(dataObj),
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function (response) {
displayResponse(command, response);
},
error: function (jqhxr, textStatus, errorThrown) {
alert(textStatus + " sending to bridge - confirm IP address is correct");
}
});
} catch (e) {
alert(e);
}
};
var doDrift = function () {
doHue("PUT", stateAction(), {
"hue": Math.floor(Math.random() * 65536),
"sat": Math.floor(Math.random() * 256),
"transitiontime": transitionTime()
});
console.log("sent drift ", Math.round(Date.now() / 1000));
};
var stateAction = function () {
var lg = $("input[name=lg]:checked").val().replace(/\s*checked\s*/, '');
return (lg + "/" + $("#lg-sel").val() + ((lg === 'lights') ? "/state" : "/action"));
};
var transitionTime = function () {
return ($("#tt-sel").val() * $("#tt-val").val());
};
var updateApiToolLink = function () {
if (/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$/.test($("#bridge-ip").val())) {
$('#api-tool-link').attr("href", "http://" + $("#bridge-ip").val() + "/debug/clip.html")
$('#api-tool').show();
}else{
$('#api-tool-link').attr("href", "")
$('#api-tool').hide();
}
}
$("#bridge-ip").change(function () {
updateApiToolLink();
});
$("#huesat-set").change(function () {
$("#huesat-hue").val(Math.round(65535 * $("#huesat-set").val() / 16));
});
$(".lg-set").mousedown(function () {
$("#lg-group").prop("checked", true);
$("#lg-group").change();
$("#lg-light").change();
$("#lg-sel").val("0");
$("#lg-sel").change();
});
$(".ct-set").mousedown(function () {
$("#ct-kelvin").val($(this).html());
$("#ct-kelvin").change();
});
$(".tt-set").mousedown(function () {
$("#tt-val").val($(this).html());
$("#tt-val").change();
});
$(".bri-set").mousedown(function () {
$("#on-bri").val($(this).html());
$("#on-bri").change();
});
$(".sat-set").mousedown(function () {
$("#huesat-sat").val($(this).html());
$("#huesat-sat").change();
});
$("#query-portal").click(function () {
queryPortal();
});
$("#get-username").click(function () {
doHue("POST", null, {
"devicetype": $("#devicetype").val()
});
});
$("#get").click(function () {
doHue("GET", $("#get-sel").val());
});
$("#colorloop").click(function () {
doHue("PUT", stateAction(), {
"effect": "colorloop"
});
});
$("#effect-none").click(function () {
doHue("PUT", stateAction(), {
"effect": "none"
});
});
$("#blink1").click(function () {
doHue("PUT", stateAction(), {
"alert": "select"
});
});
$("#blink15").click(function () {
doHue("PUT", stateAction(), {
"alert": "lselect"
});
});
var driftHandle;
$("#drift-on").click(function () {
try {
clearInterval(driftHandle);
} catch (e) {}
driftHandle = setInterval(doDrift, $("#di-sel").val() * $("#di-val").val() * 100);
});
$("#drift-off").click(function () {
try {
clearInterval(driftHandle);
} catch (e) {}
});
$("#off").click(function () {
doHue("PUT", stateAction(), {
"on": false,
"transitiontime": transitionTime()
});
});
$("#on").click(function () {
var bri = parseInt($("#on-bri").val());
bri = isNaN(bri) ? 127 : bri;
$("#on-bri").val(bri.toString());
$("#on-bri").change();
doHue("PUT", stateAction(), {
"on": true,
"bri": bri,
"transitiontime": transitionTime()
});
});
$("#huesat").click(function () {
var hue = parseInt($("#huesat-hue").val());
hue = isNaN(hue) ? 32767 : hue;
$("#huesat-hue").val(hue.toString());
var sat = parseInt($("#huesat-sat").val());
sat = isNaN(sat) ? 127 : sat;
$("#huesat-sat").val(sat.toString());
$("#huesat-sat").change();
doHue("PUT", stateAction(), {
"hue": hue,
"sat": sat,
"transitiontime": transitionTime()
});
});
$("#ct").click(function () {
var kelvin = parseInt($("#ct-kelvin").val());
kelvin = isNaN(kelvin) ? 2700 : kelvin;
$("#ct-kelvin").val(kelvin.toString());
$("#ct-kelvin").change();
doHue("PUT", stateAction(), {
"ct": Math.round(1000000 / kelvin),
"transitiontime": transitionTime()
});
});
$("#sc").click(function () {
$("#sc-id").change();
doHue("PUT", stateAction(), {
"scene": $("#sc-id").val()
});
});
// .ready()
$(function() {
updateApiToolLink();
});
</script>
</body>
</html>