-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhuepi.js
executable file
·1953 lines (1797 loc) · 60.2 KB
/
huepi.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
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(){
'use strict';
////////////////////////////////////////////////////////////////////////////////
//
// hue (Philips Wireless Lighting) Api interface for JavaScript
// +-> HUEPI sounds like Joepie which makes me smile during development...
//
// Requires jQuery 1.5+ for ajax calls and Deferreds
//
////////////////////////////////////////////////////////////////////////////////
/**
* huepi Object, Entry point for all interaction with Lights etc via the Bridge.
*
* @class
* @alias huepi
*/
var huepi = function() {
/** @member {string} - version of the huepi interface */
this.version = '1.2.2';
/** @member {array} - Array of all Bridges on the local network */
this.LocalBridges = [];
/** @member {bool} - get: local network scan in progress / set:proceed with scan */
this.ScanningNetwork = false;
/** @member {string} - IP address of the Current(active) Bridge */
this.BridgeIP = '';
/** @member {string} - ID (Unique, is MAC address) of the Current(active) Bridge */
this.BridgeID = '';
/** @member {string} - Username for Whitelisting, generated by the Bridge */
this.Username = '';
/** @member {object} - Cache Hashmap of huepi BridgeID and Whitelisted Username */
this.BridgeCache = {};
/** @member {boolean} - Autosave Cache Hasmap of huepi BridgeID and Whitelisted Username */
this.BridgeCacheAutosave = true;
this._BridgeCacheLoad(); // Load BridgeCache on creation by Default
/** @member {object} - Configuration of the Current(active) Bridge */
this.BridgeConfig = {};
/** @member {string} - Name of the Current(active) Bridge */
this.BridgeName = '';
/** @member {array} - Array of all Lights of the Current(active) Bridge */
this.Lights = [];
/** @member {array} - Array of all LightIds of the Current(active) Bridge */
this.LightIds = [];
/** @member {array} - Array of all Groups of the Current(active) Bridge */
this.Groups = [];
/** @member {array} - Array of all GroupIds of the Current(active) Bridge */
this.GroupIds = [];
// To Do: Add Schedules, Scenes, Sensors & Rules manupulation functions, they are read only for now
/** @member {array} - Array of all Schedules of the Current(active) Bridge, NOTE: There are no Setter functions yet */
this.Schedules = [];
/** @member {array} - Array of all Scenes of the Current(active) Bridge, NOTE: There are no Setter functions yet */
this.Scenes = [];
/** @member {array} - Array of all Sensors of the Current(active) Bridge, NOTE: There are no Setter functions yet */
this.Sensors = [];
/** @member {array} - Array of all Rules of the Current(active) Bridge, NOTE: There are no Setter functions yet */
this.Rules = [];
};
////////////////////////////////////////////////////////////////////////////////
//
// Detect Running in NodeJS; module exisists and module.exports exists
// and type of global.process = object process
//
// requires domino window to create jquery with window attached.
//
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined')
{
var $;
var XMLHttpRequest;
if (typeof global !== 'undefined' && typeof global.process !== 'undefined' &&
Object.prototype.toString.call(global.process) === '[object process]') {
$ = require('jquery')(require('domino').createWindow('<html>huepi</html>'));
XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
$.support.cors = true; // cross domain, Cross-origin resource sharing
$.ajaxSettings.xhr = function() {
return new XMLHttpRequest();
};
}
module.exports = huepi;
} else if (typeof define === 'function' && define.amd) {
$ = require('jquery')(require('domino').createWindow('<html>huepi</html>'));
XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
$.support.cors = true; // cross domain, Cross-origin resource sharing
$.ajaxSettings.xhr = function() {
return new XMLHttpRequest();
};
define([], function() { return huepi; });
} else {
$ = jQuery;
window.huepi = huepi;
}
////////////////////////////////////////////////////////////////////////////////
//
// Private _BridgeCache Functions, Internal Used
//
//
/**
* Loads the BridgeCache, typically on startup
*/
huepi.prototype._BridgeCacheLoad = function()
{
this.BridgeCache = { };
try {
if (typeof navigator !== 'undefined' && typeof window !== 'undefined') { // running in Browser
var huepiBridgeCache = localStorage.huepiBridgeCache || '{}';
this.BridgeCache = JSON.parse(huepiBridgeCache); // Load
} else if (typeof module !== 'undefined' && module.exports) { // running in NodeJS
var fs = require('fs');
var buffer = fs.readFileSync('huepiBridgeCache.json');
this.BridgeCache = JSON.parse(buffer.toString());
}
//console.log('huepi._BridgeCacheLoad()-ed : \n '+ JSON.stringify(this.BridgeCache));
} catch (error) {
console.log('Unable to huepi._BridgeCacheLoad() ' + error);
}
};
huepi.prototype._BridgeCacheAddCurrent = function()
{
console.log('_BridgeCacheAddCurrent ' + this.BridgeID +' '+ this.Username);
this.BridgeCache[this.BridgeID] = this.Username;
if (this.BridgeCacheAutosave) {
this._BridgeCacheSave();
}
};
huepi.prototype._BridgeCacheRemoveCurrent = function()
{
if (this.BridgeCache[this.BridgeID] === this.Username) {
console.log('_BridgeCacheRemoveCurrent ' + this.BridgeID +' '+ this.Username);
delete this.BridgeCache[this.BridgeID];
if (this.BridgeCacheAutosave) {
this._BridgeCacheSave();
}
}
};
/**
* Selects the first Bridge from LocalBridges found in BridgeCache and stores in BridgeIP
* defaults to 1st Bridge in LocalBridges if no bridge from LocalBridges is found in BridgeCache
*
* Internally called in PortalDiscoverLocalBridges and NetworkDiscoverLocalBridges
*/
huepi.prototype._BridgeCacheSelectFromLocalBridges = function()
{
if (this.LocalBridges.length > 0) { // Local Bridges are found
this.BridgeIP = this.LocalBridges[0].internalipaddress || ''; // Default to 1st Bridge Found
this.BridgeID = this.LocalBridges[0].id.toLowerCase() || '';
if (!this.BridgeCache[this.BridgeID]) { // if this.BridgeID not found in BridgeCache
for (var BridgeNr=1; BridgeNr<this.LocalBridges.length; BridgeNr++) { // Search and store Found
this.BridgeID = this.LocalBridges[BridgeNr].id.toLowerCase();
if (this.BridgeCache[this.BridgeID]) {
this.BridgeIP = this.LocalBridges[BridgeNr].internalipaddress;
break;
} else {
this.BridgeID = '';
}
}
}
}
this.Username = this.BridgeCache[this.BridgeID] || '';
};
/**
* Saves the BridgeCache, typically on Whitelist new Device or Device no longer whitelisted
* as is the case with with @BridgeCacheAutosave on @_BridgeCacheAddCurrent and @_BridgeCacheRemoveCurrent
* NOTE: Saving this cache might be considered a security issue
* To counter this security issue, arrange your own load/save code with proper encryption
*/
huepi.prototype._BridgeCacheSave = function()
{
try {
if (typeof navigator !== 'undefined' && typeof window !== 'undefined') { // running in Browser
localStorage.huepiBridgeCache = JSON.stringify(this.BridgeCache); // Save
} else if (typeof module !== 'undefined' && module.exports) { // running in NodeJS
var fs = require('fs');
fs.writeFileSync('huepiBridgeCache.json',JSON.stringify(this.BridgeCache));
}
//console.log('huepi._BridgeCacheSave()-ed : \n '+ JSON.stringify(this.BridgeCache));
} catch (error) {
console.log('Unable to huepi._BridgeCacheSave() ' + error);
}
};
////////////////////////////////////////////////////////////////////////////////
//
// Network Functions
//
//
/**
* Creates the list of hue-Bridges on the local network
*/
huepi.prototype.NetworkDiscoverLocalBridges = function()
{
var self = this;
var LocalIPs = [];
var OverallDeferred = $.Deferred();
self.ScanningNetwork = true;
self.BridgeIP =
self.BridgeID =
self.BridgeName =
self.Username = '';
self.LocalBridges = [];
function DiscoverLocalIPs() {
var IPDeferred = $.Deferred();
var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
var PeerConnection = new RTCPeerConnection({iceServers: [] });
PeerConnection.createDataChannel('');
PeerConnection.onicecandidate = function(e) {
if (!e.candidate) {
PeerConnection.close();
return IPDeferred.resolve();
}
var LocalIP = /^candidate:.+ (\S+) \d+ typ/.exec(e.candidate.candidate)[1];
if (LocalIPs.indexOf(LocalIP) === -1) {
LocalIPs.push(LocalIP);
}
};
PeerConnection.createOffer(function(sdp) {
PeerConnection.setLocalDescription(sdp);
}, function onerror() {});
return IPDeferred.promise();
}
function DiscoverLocalBridges() {
var BridgeDeferred = $.Deferred();
var Parallel = 16;
function CheckIP(IPAddress) {
self.BridgeGetConfig(IPAddress, 3000).then(function(data){
self.LocalBridges.push({'internalipaddress': IPAddress, 'id': data.bridgeid.toLowerCase()});
}).always(function(){
var Segment = IPAddress.slice(0, IPAddress.lastIndexOf('.')+1);
var Nr = parseInt(IPAddress.slice(IPAddress.lastIndexOf('.')+1, IPAddress.length));
OverallDeferred.notify(Math.floor(100*Nr/255));
if (self.ScanningNetwork === false) {
Nr = 256; // Stop scanning if (self.ScanningNetwork = false)
}
if ((Nr+Parallel)<256) {
CheckIP(Segment+(Nr+Parallel));
} else {
self.ScanningNetwork = false;
BridgeDeferred.resolve();
}
});
}
for (var IPs=0; IPs<LocalIPs.length; IPs++) {
var InitialIP = LocalIPs[IPs].slice(0, LocalIPs[IPs].lastIndexOf('.')+1);
for (var P=1; P<=Parallel; P++) {
CheckIP(InitialIP+P);
}
}
return BridgeDeferred.promise();
}
DiscoverLocalIPs().then(function() {
DiscoverLocalBridges().then(function() {
if (self.LocalBridges.length > 0) {
self._BridgeCacheSelectFromLocalBridges();
OverallDeferred.resolve();
} else {
OverallDeferred.reject();
}
});
});
return OverallDeferred.promise();
};
////////////////////////////////////////////////////////////////////////////////
//
// Portal Functions
//
//
/**
* Retreives the list of hue-Bridges on the local network from the hue Portal
*/
huepi.prototype.PortalDiscoverLocalBridges = function()
{
var self = this;
var deferred = $.Deferred();
self.BridgeIP =
self.BridgeID =
self.BridgeName =
self.Username = '';
self.LocalBridges = [];
$.ajax({ type: 'GET', url: 'https://www.meethue.com/api/nupnp', success: function(data) {
if (data.length > 0) {
if (data[0].internalipaddress) { // Bridge(s) Discovered
self.LocalBridges = data;
self._BridgeCacheSelectFromLocalBridges();
deferred.resolve();
} else {
deferred.reject();
}
} else {
deferred.reject();
}
}, error: function(/*xhr,status,error*/) {
deferred.reject();
} });
return deferred.promise();
};
////////////////////////////////////////////////////////////////////////////////
//
// Bridge Functions
//
//
/**
* Function to retreive BridgeConfig before Checking Whitelisting.
* ONCE call BridgeGetConfig Before BridgeGetData to validate we are talking to a hue Bridge
* available members (as of 'apiversion': '1.11.0'):
* name, apiversion, swversion, mac, bridgeid, replacesbridgeid, factorynew, modelid
*
* @param {string} ConfigBridgeIP - Optional BridgeIP to GetConfig from, otherwise uses huepi.BridgeIP (this/self).
* @param {string} ConfigTimeOut - Optional TimeOut for network request, otherwise uses 60 seconds.
*/
huepi.prototype.BridgeGetConfig = function(ConfigBridgeIP, ConfigTimeOut)
{ // GET /api/config -> data.config.whitelist.username
var self = this;
var deferred = $.Deferred();
ConfigBridgeIP = ConfigBridgeIP || self.BridgeIP;
ConfigTimeOut = ConfigTimeOut || 60000;
$.ajax({ type: 'GET', timeout: ConfigTimeOut, url: 'http://' + ConfigBridgeIP + '/api/config/', success: function(data) {
if (data.bridgeid) {
if (self.BridgeIP === ConfigBridgeIP) {
self.BridgeConfig = data;
if (self.BridgeConfig.bridgeid) // SteveyO/Hue-Emulator doesn't supply bridgeid as of yet.
self.BridgeID = self.BridgeConfig.bridgeid.toLowerCase();
else {
self.BridgeID = '';
}
self.BridgeName = self.BridgeConfig.name;
self.Username = self.BridgeCache[self.BridgeID];
if (typeof self.Username === 'undefined') {
self.Username = '';
}
}
deferred.resolve(data);
} else { // this BridgeIP is not a hue Bridge
deferred.reject();
}
}, error: function(/*xhr,status,error*/) { // $.ajax failed
deferred.reject();
} });
return deferred.promise();
};
/**
* Function to retreive BridgeDescription before Checking Whitelisting.
* ONCE call BridgeGetDescription Before BridgeGetData to validate we are talking to a hue Bridge
*
* REMARK: Needs a fix of the hue bridge to allow CORS on xml endpoint too, just like on json endpoints already is implemented.
*
* @param {string} ConfigBridgeIP - Optional BridgeIP to GetConfig from, otherwise uses huepi.BridgeIP (this/self).
* @param {string} ConfigTimeOut - Optional TimeOut for network request, otherwise uses 60 seconds.
*/
huepi.prototype.BridgeGetDescription = function(ConfigBridgeIP, ConfigTimeOut)
{ // GET /description.xml -> /device/serialNumber
var self = this;
var deferred = $.Deferred();
ConfigBridgeIP = ConfigBridgeIP || self.BridgeIP;
ConfigTimeOut = ConfigTimeOut || 60000;
//$.support.cors = true; // cross domain, Cross-origin resource sharing
//$.ajaxSetup( { contentType: 'text/plain', xhrFields: { withCredentials: false }, headers: { 'Origin': ConfigBridgeIP } });
$.ajax({ type: 'GET', timeout: ConfigTimeOut, url: 'http://' + ConfigBridgeIP + '/description.xml', dataType: 'json', success: function(data) {
var $data = $(data);
if ($data.find('url').text() === 'hue_logo_0.png') {
if (ConfigBridgeIP === self.BridgeIP) {
if ($data.find('serialNumber').text() !== '')
self.BridgeID = $data.find('serialNumber').text().toLowerCase();
else {
self.BridgeID = '';
}
self.BridgeName = $data.find('friendlyName').text();
self.Username = self.BridgeCache[self.BridgeID];
if (typeof self.Username === 'undefined') {
// Correct 001788[....]200xxx -> 001788FFFE200XXX short and long serialnumer difference
self.BridgeID = self.BridgeID.slice(0,6) + 'fffe' + self.BridgeID.slice(6,12);
self.Username = self.BridgeCache[self.BridgeID];
if (typeof self.Username === 'undefined') {
self.Username = '';
}
}
}
deferred.resolve(data);
} else { // this BridgeIP is not a hue Bridge
deferred.reject();
}
}, error: function(/*xhr,status,error*/) { // $.ajax failed
deferred.reject();
} });
return deferred.promise();
};
/**
* Update function to retreive Bridge data and store it in this object.
* Consider this the main 'Get' function.
* Typically used for Heartbeat or manual updates of local data.
*/
huepi.prototype.BridgeGetData = function()
{ // GET /api/username -> data.config.whitelist.username
var self = this;
var deferred = $.Deferred();
if (this.Username === '') {
deferred.reject();
} else $.ajax({ type: 'GET', url: 'http://' + this.BridgeIP + '/api/' + this.Username, success: function(data) {
if (typeof data.config !== 'undefined') { // if able to read Config, Username must be Whitelisted
self.BridgeConfig = data.config;
if (self.BridgeConfig.bridgeid) // SteveyO/Hue-Emulator doesn't supply bridgeid as of yet.
self.BridgeID = self.BridgeConfig.bridgeid.toLowerCase();
else {
self.BridgeID = '';
}
self.BridgeName = self.BridgeConfig.name;
self.Lights = data.lights;
self.LightIds = [];
for (var key in self.Lights)
self.LightIds.push(key);
self.Groups = data.groups;
self.GroupIds = [];
for (key in self.Groups)
self.GroupIds.push(key);
self.Schedules = data.schedules;
self.Scenes = data.scenes;
self.Sensors = data.sensors;
self.Rules = data.rules;
self.BridgeName = self.BridgeConfig.name;
deferred.resolve();
} else { // Username is no longer whitelisted
if (self.Username !== '')
self._BridgeCacheRemoveCurrent();
self.Username = '';
deferred.reject();
}
}, error: function(/*xhr,status,error*/) { // $.ajax failed
deferred.reject();
} });
return deferred.promise();
};
/**
* Whitelists the Username stored in this object.
* Note: a buttonpress on the bridge is requered max 30 sec before this to succeed.
* please only use this once per device, Username is stored in cache.
*
* @param {string} DeviceName - Optional device name to Whitelist.
*/
huepi.prototype.BridgeCreateUser = function(DeviceName)
{ // POST /api {'devicetype': 'AppName#DeviceName' }
var self = this;
var deferred = $.Deferred();
DeviceName = DeviceName || 'WebInterface';
$.ajax({ type: 'POST', dataType: 'json', contentType: 'application/json', url: 'http://' + this.BridgeIP + '/api',
data: '{"devicetype": "huepi#' + DeviceName + '"}', success: function(data) {
if (data[0]) {
if (data[0].success) {
self.Username = data[0].success.username;
self._BridgeCacheAddCurrent();
deferred.resolve();
} else {
deferred.reject();
}
} else {
deferred.reject();
}
}, error: function(/*xhr,status,error*/) { // $.ajax failed
deferred.reject();
} });
return deferred.promise();
};
/**
* @param {string} UsernameToDelete - Username that will be revoked from the Whitelist.
* Note: Username stored in this object need to be Whitelisted to succeed.
*/
huepi.prototype.BridgeDeleteUser = function(UsernameToDelete)
{ // DELETE /api/username/config/whitelist/username {'devicetype': 'iPhone', 'username': '1234567890'}
return $.ajax({
type: 'DELETE',
dataType: 'json',
contentType: 'application/json',
url: 'http://' + this.BridgeIP + '/api/' + this.Username + '/config/whitelist/' + UsernameToDelete,
//data: '{'devicetype': 'WebInterface', 'username': '' + this.Username + ''}'
});
};
////////////////////////////////////////////////////////////////////////////////
//
// Helper Functions
//
//
/**
* @param {string} Model
* @returns {boolean} Model is capable of CT
*/
huepi.HelperModelCapableCT = function(Model)
{ // CT Capable LCT* LLM* LTW* LLC020 LST002
var ModelType = Model.slice(0,3);
return ((ModelType === 'LCT') || (ModelType === 'LLM') || (ModelType === 'LTW') || (Model === 'LLC020') || (Model === 'LST002'));
};
/**
* @param {string} Model
* @returns {boolean} Model is capable of XY
*/
huepi.HelperModelCapableXY = function(Model)
{ // XY Capable LCT* LLC* LST* LLM001 LLC020 LST002
var ModelType = Model.slice(0,3);
return ((ModelType === 'LCT') || (ModelType === 'LLC') || (ModelType === 'LST') || (Model === 'LLM001') || (Model === 'LLC020') || (Model === 'LST002'));
};
/**
* @param {float} Red - Range [0..1]
* @param {float} Green - Range [0..1]
* @param {float} Blue - Range [0..1]
* @returns {object} [Ang, Sat, Bri] - Ranges [0..360] [0..1] [0..1]
*/
huepi.HelperRGBtoHueAngSatBri = function(Red, Green, Blue)
{
var Ang, Sat, Bri;
var Min = Math.min(Red, Green, Blue);
var Max = Math.max(Red, Green, Blue);
if (Min !== Max) {
if (Red === Max) {
Ang = (0 + ((Green - Blue) / (Max - Min))) * 60;
} else if (Green === Max) {
Ang = (2 + ((Blue - Red) / (Max - Min))) * 60;
} else {
Ang = (4 + ((Red - Green) / (Max - Min))) * 60;
}
Sat = (Max - Min) / Max;
Bri = Max;
} else { // Max === Min
Ang = 0;
Sat = 0;
Bri = Max;
}
return {Ang: Ang, Sat: Sat, Bri: Bri};
};
/**
* @param {float} Ang - Range [0..360]
* @param {float} Sat - Range [0..1]
* @param {float} Bri - Range [0..1]
* @returns {object} [Red, Green, Blue] - Ranges [0..1] [0..1] [0..1]
*/
huepi.HelperHueAngSatBritoRGB = function(Ang, Sat, Bri)
{ // Range 360, 1, 1, return .Red, .Green, .Blue
var Red, Green, Blue;
if (Sat === 0) {
Red = Bri;
Green = Bri;
Blue = Bri;
} else
{
var Sector = Math.floor(Ang / 60) % 6;
var Fraction = (Ang / 60) - Sector;
var p = Bri * (1 - Sat);
var q = Bri * (1 - Sat * Fraction);
var t = Bri * (1 - Sat * (1 - Fraction));
switch (Sector) {
case 0:
Red = Bri;
Green = t;
Blue = p;
break;
case 1:
Red = q;
Green = Bri;
Blue = p;
break;
case 2:
Red = p;
Green = Bri;
Blue = t;
break;
case 3:
Red = p;
Green = q;
Blue = Bri;
break;
case 4:
Red = t;
Green = p;
Blue = Bri;
break;
default: // case 5:
Red = Bri;
Green = p;
Blue = q;
break;
}
}
return {Red: Red, Green: Green, Blue: Blue};
};
/**
* @param {float} Red - Range [0..1]
* @param {float} Green - Range [0..1]
* @param {float} Blue - Range [0..1]
* @returns {number} Temperature ranges [2200..6500]
*/
huepi.HelperRGBtoColortemperature = function(Red, Green, Blue)
{ // Approximation from https://github.com/neilbartlett/color-temperature/blob/master/index.js
var Temperature;
var TestRGB;
var Epsilon = 0.4;
var MinTemperature = 2200;
var MaxTemperature = 6500;
while ( (MaxTemperature - MinTemperature) > Epsilon) {
Temperature = (MaxTemperature + MinTemperature) / 2;
TestRGB = huepi.HelperColortemperaturetoRGB(Temperature);
if ((TestRGB.Blue / TestRGB.Red) >= (Blue / Red)) {
MaxTemperature = Temperature;
} else {
MinTemperature = Temperature;
}
}
return Math.round(Temperature);
};
/**
* @param {number} Temperature ranges [1000..6600]
* @returns {object} [Red, Green, Blue] ranges [0..1] [0..1] [0..1]
*/
huepi.HelperColortemperaturetoRGB = function(Temperature)
{ // http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/
// Update Available: https://github.com/neilbartlett/color-temperature/blob/master/index.js
var Red, Green, Blue;
Temperature = Temperature / 100;
if (Temperature <= 66)
Red = /*255;*/ 165+90*((Temperature)/(66));
else {
Red = Temperature - 60;
Red = 329.698727466 * Math.pow(Red, -0.1332047592);
if (Red < 0)
Red = 0;
if (Red > 255)
Red = 255;
}
if (Temperature <= 66) {
Green = Temperature;
Green = 99.4708025861 * Math.log(Green) - 161.1195681661;
if (Green < 0)
Green = 0;
if (Green > 255)
Green = 255;
} else {
Green = Temperature - 60;
Green = 288.1221695283 * Math.pow(Green, -0.0755148492);
if (Green < 0)
Green = 0;
if (Green > 255)
Green = 255;
}
if (Temperature >= 66)
Blue = 255;
else {
if (Temperature <= 19)
Blue = 0;
else {
Blue = Temperature - 10;
Blue = 138.5177312231 * Math.log(Blue) - 305.0447927307;
if (Blue < 0)
Blue = 0;
if (Blue > 255)
Blue = 255;
}
}
return {Red: Red/255, Green: Green/255, Blue: Blue/255};
};
/**
* @param {float} Red - Range [0..1]
* @param {float} Green - Range [0..1]
* @param {float} Blue - Range [0..1]
* @returns {object} [x, y] - Ranges [0..1] [0..1]
*/
huepi.HelperRGBtoXY = function(Red, Green, Blue)
{ // Source: https://github.com/PhilipsHue/PhilipsHueSDK-iOS-OSX/blob/master/ApplicationDesignNotes/RGB%20to%20xy%20Color%20conversion.md
// Apply gamma correction
if (Red > 0.04045)
Red = Math.pow((Red + 0.055) / (1.055), 2.4);
else
Red = Red / 12.92;
if (Green > 0.04045)
Green = Math.pow((Green + 0.055) / (1.055), 2.4);
else
Green = Green / 12.92;
if (Blue > 0.04045)
Blue = Math.pow((Blue + 0.055) / (1.055), 2.4);
else
Blue = Blue / 12.92;
// RGB to XYZ [M] for Wide RGB D65, http://www.developers.meethue.com/documentation/color-conversions-rgb-xy
var X = Red * 0.664511 + Green * 0.154324 + Blue * 0.162028;
var Y = Red * 0.283881 + Green * 0.668433 + Blue * 0.047685;
var Z = Red * 0.000088 + Green * 0.072310 + Blue * 0.986039;
// But we don't want Capital X,Y,Z you want lowercase [x,y] (called the color point) as per:
if ((X + Y + Z) === 0)
return {x: 0, y: 0};
return {x: X / (X + Y + Z), y: Y / (X + Y + Z)};
};
/**
* @param {float} x
* @param {float} y
* @param {float} Brightness Optional
* @returns {object} [Red, Green, Blue] - Ranges [0..1] [0..1] [0..1]
*/
huepi.HelperXYtoRGB = function(x, y, Brightness)
{ // Source: https://github.com/PhilipsHue/PhilipsHueSDK-iOS-OSX/blob/master/ApplicationDesignNotes/RGB%20to%20xy%20Color%20conversion.md
Brightness = Brightness || 1.0; // Default full brightness
var z = 1.0 - x - y;
var Y = Brightness;
var X = (Y / y) * x;
var Z = (Y / y) * z;
// XYZ to RGB [M]-1 for Wide RGB D65, http://www.developers.meethue.com/documentation/color-conversions-rgb-xy
var Red = X * 1.656492 - Y * 0.354851 - Z * 0.255038;
var Green = -X * 0.707196 + Y * 1.655397 + Z * 0.036152;
var Blue = X * 0.051713 - Y * 0.121364 + Z * 1.011530;
// Limit RGB on [0..1]
if (Red > Blue && Red > Green && Red > 1.0) { // Red is too big
Green = Green / Red;
Blue = Blue / Red;
Red = 1.0;
}
if (Red < 0)
Red = 0;
if (Green > Blue && Green > Red && Green > 1.0) { // Green is too big
Red = Red / Green;
Blue = Blue / Green;
Green = 1.0;
}
if (Green < 0)
Green = 0;
if (Blue > Red && Blue > Green && Blue > 1.0) { // Blue is too big
Red = Red / Blue;
Green = Green / Blue;
Blue = 1.0;
}
if (Blue < 0)
Blue = 0;
// Apply reverse gamma correction
if (Red <= 0.0031308) {
Red = Red * 12.92;
} else {
Red = 1.055 * Math.pow(Red, (1.0 / 2.4)) - 0.055;
}
if (Green <= 0.0031308) {
Green = Green * 12.92;
} else {
Green = 1.055 * Math.pow(Green, (1.0 / 2.4)) - 0.055;
}
if (Blue <= 0.0031308) {
Blue = Blue * 12.92;
} else {
Blue = 1.055 * Math.pow(Blue, (1.0 / 2.4)) - 0.055;
}
// Limit RGB on [0..1]
if (Red > Blue && Red > Green && Red > 1.0) { // Red is too big
Green = Green / Red;
Blue = Blue / Red;
Red = 1.0;
}
if (Red < 0)
Red = 0;
if (Green > Blue && Green > Red && Green > 1.0) { // Green is too big
Red = Red / Green;
Blue = Blue / Green;
Green = 1.0;
}
if (Green < 0)
Green = 0;
if (Blue > Red && Blue > Green && Blue > 1.0) { // Blue is too big
Red = Red / Blue;
Green = Green / Blue;
Blue = 1.0;
}
if (Blue < 0)
Blue = 0;
return {Red: Red, Green: Green, Blue: Blue};
};
/**
* @param {float} x
* @param {float} y
* @param {float} Brightness Optional
* @param {string} Model - Modelname of the Light
* @returns {object} [Red, Green, Blue] - Ranges [0..1] [0..1] [0..1]
*/
huepi.HelperXYtoRGBforModel = function(x, y, Brightness, Model)
{
var GamutCorrected = huepi.HelperGamutXYforModel(x, y, Model);
return huepi.HelperXYtoRGB(GamutCorrected.x, GamutCorrected.y, Brightness);
};
/**
* Tests if the Px,Py resides within the Gamut for the model.
* Otherwise it will calculated the closesed point on the Gamut.
* @param {float} Px - Range [0..1]
* @param {float} Py - Range [0..1]
* @param {string} Model - Modelname of the Light to Gamutcorrect Px, Py for
* @returns {object} [x, y] - Ranges [0..1] [0..1]
*/
huepi.HelperGamutXYforModel = function(Px, Py, Model)
{ // https://developers.meethue.com/documentation/supported-lights
Model = Model || 'LCT001'; // default hue Bulb 2012
var ModelType = Model.slice(0,3);
var PRed, PGreen, PBlue;
var NormDot;
if ( ((ModelType === 'LST') || (ModelType === 'LLC')) && (Model !=='LLC020') && (Model !=='LLC002') && (Model !== 'LST002') ) { // For LivingColors Bloom, Aura and Iris etc the triangle corners are:
PRed = {x: 0.704, y: 0.296}; // Gamut A
PGreen = {x: 0.2151, y: 0.7106};
PBlue = {x: 0.138, y: 0.080};
} else if ( ((ModelType === 'LCT') || (ModelType === 'LLM')) && (Model !=='LCT010') && (Model !=='LCT014') && (Model !=='LCT011') && (Model !=='LCT012') ) { // For the hue bulb and beyond led modules etc the corners of the triangle are:
PRed = {x: 0.675, y: 0.322}; // Gamut B
PGreen = {x: 0.409, y: 0.518};
PBlue = {x: 0.167, y: 0.040};
} else { // Exceptions and Unknown default to
PRed = {x: 0.692, y: 0.308}; // Gamut C
PGreen = {x: 0.17, y: 0.7};
PBlue = {x: 0.153, y: 0.048};
}
var VBR = {x: PRed.x - PBlue.x, y: PRed.y - PBlue.y}; // Blue to Red
var VRG = {x: PGreen.x - PRed.x, y: PGreen.y - PRed.y}; // Red to Green
var VGB = {x: PBlue.x - PGreen.x, y: PBlue.y - PGreen.y}; // Green to Blue
var GBR = (PGreen.x - PBlue.x) * VBR.y - (PGreen.y - PBlue.y) * VBR.x; // Sign Green on Blue to Red
var BRG = (PBlue.x - PRed.x) * VRG.y - (PBlue.y - PRed.y) * VRG.x; // Sign Blue on Red to Green
var RGB = (PRed.x - PGreen.x) * VGB.y - (PRed.y - PGreen.y) * VGB.x; // Sign Red on Green to Blue
var VBP = {x: Px - PBlue.x, y: Py - PBlue.y}; // Blue to Point
var VRP = {x: Px - PRed.x, y: Py - PRed.y}; // Red to Point
var VGP = {x: Px - PGreen.x, y: Py - PGreen.y}; // Green to Point
var PBR = VBP.x * VBR.y - VBP.y * VBR.x; // Sign Point on Blue to Red
var PRG = VRP.x * VRG.y - VRP.y * VRG.x; // Sign Point on Red to Green
var PGB = VGP.x * VGB.y - VGP.y * VGB.x; // Sign Point on Green to Blue
if ((GBR * PBR >= 0) && (BRG * PRG >= 0) && (RGB * PGB >= 0)) // All Signs Match so Px,Py must be in triangle
return {x: Px, y: Py};
// Outside Triangle, Find Closesed point on Edge or Pick Vertice...
else if (GBR * PBR <= 0) { // Outside Blue to Red
NormDot = (VBP.x * VBR.x + VBP.y * VBR.y) / (VBR.x * VBR.x + VBR.y * VBR.y);
if ((NormDot >= 0.0) && (NormDot <= 1.0)) // Within Edge
return {x: PBlue.x + NormDot * VBR.x, y: PBlue.y + NormDot * VBR.y};
else if (NormDot < 0.0) // Outside Edge, Pick Vertice
return {x: PBlue.x, y: PBlue.y}; // Start
else
return {x: PRed.x, y: PRed.y}; // End
}
else if (BRG * PRG <= 0) { // Outside Red to Green
NormDot = (VRP.x * VRG.x + VRP.y * VRG.y) / (VRG.x * VRG.x + VRG.y * VRG.y);
if ((NormDot >= 0.0) && (NormDot <= 1.0)) // Within Edge
return {x: PRed.x + NormDot * VRG.x, y: PRed.y + NormDot * VRG.y};
else if (NormDot < 0.0) // Outside Edge, Pick Vertice
return {x: PRed.x, y: PRed.y}; // Start
else
return {x: PGreen.x, y: PGreen.y}; // End
}
else if (RGB * PGB <= 0) { // Outside Green to Blue
NormDot = (VGP.x * VGB.x + VGP.y * VGB.y) / (VGB.x * VGB.x + VGB.y * VGB.y);
if ((NormDot >= 0.0) && (NormDot <= 1.0)) // Within Edge
return {x: PGreen.x + NormDot * VGB.x, y: PGreen.y + NormDot * VGB.y};
else if (NormDot < 0.0) // Outside Edge, Pick Vertice
return {x: PGreen.x, y: PGreen.y}; // Start
else
return {x: PBlue.x, y: PBlue.y}; // End
}
};
/**
* @param {float} Ang - Range [0..360]
* @param {float} Sat - Range [0..1]
* @param {float} Bri - Range [0..1]
* @returns {number} Temperature ranges [2200..6500]
*/
huepi.HelperHueAngSatBritoColortemperature = function(Ang, Sat, Bri)
{
var RGB = huepi.HelperHueAngSatBritoRGB(Ang, Sat, Bri);
return huepi.HelperRGBtoColortemperature(RGB.Red, RGB.Green, RGB.Blue);
};
/**
* @param {number} Temperature ranges [1000..6600]
* @returns {object} [Ang, Sat, Bri] - Ranges [0..360] [0..1] [0..1]
*/
huepi.HelperColortemperaturetoHueAngSatBri = function(Temperature)
{
var RGB = huepi.HelperColortemperaturetoRGB(Temperature);
return huepi.HelperRGBtoHueAngSatBri(RGB.Red, RGB.Green, RGB.Blue);
};
/**
* @param {float} x
* @param {float} y
* @param {float} Brightness Optional
* @returns {number} Temperature ranges [1000..6600]
*/
huepi.HelperXYtoColortemperature = function(x, y, Brightness)
{
var RGB = huepi.HelperXYtoRGB(x, y, Brightness);
return huepi.HelperRGBtoColortemperature(RGB.Red, RGB.Green, RGB.Blue);
};
/**
* @param {number} Temperature ranges [1000..6600]
* @returns {object} [x, y] - Ranges [0..1] [0..1]
*/
huepi.HelperColortemperaturetoXY = function(Temperature)
{
var RGB = huepi.HelperColortemperaturetoRGB(Temperature);
return huepi.HelperRGBtoXY(RGB.Red, RGB.Green, RGB.Blue);
};
/**
* @param {number} CT in Mired (micro reciprocal degree)
* @returns {number} ColorTemperature
*/
huepi.HelperCTtoColortemperature = function(CT)
{
return Math.round(1000000 / CT);
};
/**
* @param {number} ColorTemperature
* @returns {number} CT in Mired (micro reciprocal degree)
*/
huepi.HelperColortemperaturetoCT = function(Temperature)
{
return Math.round(1000000 / Temperature);
};
/**
* @param {multiple} Items - Items to convert to StringArray
* @returns {string} String array containing Items
*/
huepi.HelperToStringArray = function(Items) {
if (typeof Items === 'number') {
return '"' + Items.toString() + '"';
} else if (Object.prototype.toString.call(Items) === '[object Array]') {
var Result = '[';
for (var ItemNr = 0; ItemNr < Items.length; ItemNr++) {
Result += huepi.HelperToStringArray(Items[ItemNr]);
if (ItemNr < Items.length - 1)
Result += ',';
}
Result = Result + ']';
return Result;
} else if (typeof Items === 'string') {
return '"' + Items + '"';
}
};
////////////////////////////////////////////////////////////////////////////////
//
// huepi.Lightstate Object
//
//
/**
* huepi.Lightstate Object.
* Internal object to recieve all settings that are about to be send to the Bridge as a string.