-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_lifx_adapter.js
101 lines (90 loc) · 2.62 KB
/
node_lifx_adapter.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
/*
* Adapter for https://github.com/MariusRumpf/node-lifx
*/
var colors = require('colors');
function LifxAdapter(lifx) {
var that = this;
this.lifx = lifx;
this.groups = {};
var lights = this.lifx.lights();
for (var lightIndex in lights) {
var light = lights[lightIndex];
console.log('############### light ###############');
console.log(light.id + ' ' + light.label);
// console.log(light);
light.getState(function(error, data) {
console.log('######## light state ########');
console.log(data);
console.log('#############################');
});
light.getHardwareVersion(function(error, data) {
console.log('######## light hardware version ########');
console.log(data);
console.log('#############################');
});
console.log('####################################');
}
this.lifx.on('light-new', function(light) {
console.log('New light found. ID:' + light.id + ' Label: ' + light.label + ', IP:' + light.address + ':' + light.port);
// console.log(light);
});
this.lifx.on('message', function(msg, rinfo) {
if (typeof msg.type === 'string') {
// Known packages send by the lights as broadcast
switch (msg.type) {
case 'stateLocation':
// console.log(msg);
var lightId = msg.target;
var group = msg.location;
that.group[group][lightId] = true;
console.log(this.group);
break;
case 'stateGroup':
case 'getLocation':
case 'stateTemperature':
case 'statePower':
// console.log(msg.type.red);
// console.log(msg, ' from ' + rinfo.address);
break;
default:
break;
}
}
});
}
LifxAdapter.prototype.fade = function(fadeObj) {
var lights = this.lifx.lights();
console.log('********** fadeObj *************'.red);
console.log(fadeObj);
console.log('********************************'.red);
var tvBulbId = 'd073d501f660';
// // red
// fadeObj = {
// hue: 0,
// saturation: 100,
// brightness: 50,
// kelvin: 2500,
// duration: 1000
// }
for (var lightIndex in lights) {
var light = lights[lightIndex];
// console.log('############### light ###############');
// console.log(light.id + ' ' + light.label);
// // console.log(light);
// light.getState(function(error, data) {
// console.log(data);
// });
//
// light.getHardwareVersion(function(error, data) {
// console.log(data);
// });
if (light.id == tvBulbId) {
console.log('light found!');
light.color(fadeObj.hue, fadeObj.saturation, fadeObj.brightness, fadeObj.kelvin, fadeObj.duration);
}
}
}
LifxAdapter.prototype.getBulbs = function() {
return this.lifx.bulbs;
}
module.exports = LifxAdapter;