-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifxWrapper.js
167 lines (165 loc) · 4.75 KB
/
lifxWrapper.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
const request = require('request-promise');
const v = require('@mdombrock/verbose-zero');
const api_url = "https://api.lifx.com/v1/";
var token = "null";//DO NOT SET THIS TOKEN
/*
MAIN WRAPPER METHODS
*/
//https://api.developer.lifx.com/docs/authentication
exports.setToken = function(user_token){
var pre = "Bearer ";
token = pre+user_token;
};
//https://api.developer.lifx.com/docs/list-lights
exports.list = function (selector="all"){
const options = {
url: api_url+'lights/'+selector,
method: 'GET',
headers: {
'Authorization': token,
}
};
return request(options)
.then(function(parsedBody){
v.logm(["list finished",JSON.parse(parsedBody)]);
return JSON.parse(parsedBody);
})
.catch(function (err) {
v.logm(["list finished with an error",err]);
return err;
});
}
//https://api.lifx.com/v1/lights/:selector/toggle
exports.togglePower = async function (selector="all"){
const options = {
url: api_url+'lights/'+selector+'/toggle',
method: 'POST',
headers: {
'Authorization': token,
}
};
return request(options)
.then(function(parsedBody){
v.logm(["power.toggle finished",JSON.parse(parsedBody)]);
return JSON.parse(parsedBody);
})
.catch(function (err) {
v.logm(["power.toggle finished with an error",err]);
return err;
});
}
//https://api.developer.lifx.com/docs/set-state
exports.state = async function (state, selector="all"){
const options = {
url: api_url+'lights/'+selector+'/state',
method: 'PUT',
headers: {
'Authorization': token,
},
form: state,
};
return request(options)
.then(function(parsedBody){
v.logm(["state finished",JSON.parse(parsedBody)]);
return JSON.parse(parsedBody);
})
.catch(function (err) {
v.logm(["state finished with an error",err]);
return err;
});
}
//https://api.developer.lifx.com/docs/effects-off
exports.effectsOff = async function(power_off=false, selector="all"){
const options = {
url: api_url+'lights/'+selector+'/effects/off',
method: 'POST',
headers: {
'Authorization': token,
},
form: {
'power_off':power_off
},
};
return request(options)
.then(function(parsedBody){
v.logm(["effects.off finished",JSON.parse(parsedBody)]);
return JSON.parse(parsedBody);
})
.catch(function (err) {
v.logm(["effects.off finished with an error",err]);
return err;
});
}
//https://api.developer.lifx.com/docs/breathe-effect
exports.breatheEffect = async function(data = {"color": "blue","period": 1,"cycles": 1,"persist": false,"power_on": true,"peak": 0.4}, selector="all"){
/*{"color": "blue","period": 1,"cycles": 1,"persist": false,"power_on": true,"peak": 0.4}*/
const options = {
url: api_url+'lights/'+selector+'/effects/breathe',
method: 'POST',
headers: {
'Authorization': token,
},
form: data,
};
return request(options)
.then(function(parsedBody){
v.logm(["effects.breathe finished",JSON.parse(parsedBody)]);
return JSON.parse(parsedBody);
})
.catch(function (err) {
v.logm(["effects.breathe finished with an error",err]);
return err;
});
}
/*
HELPER METHODS
*/
exports.set = {};
exports.set.brightness = async function (brightness, selector="all"){
var newState = {
'brightness':brightness
};
return module.exports.state(newState, selector);
}
exports.set.color = async function (color, selector="all"){
var newState = {
'color':color
};
return module.exports.state(newState, selector);
}
exports.set.on = async function (selector="all"){
var newState = {
'power':'on'
};
return module.exports.state(newState, selector);
}
exports.set.off = async function (selector="all"){
var newState = {
'power':'off'
};
return module.exports.state(newState, selector);
}
/*
PRESET COLORS
*/
exports.set.red = function(selector="all"){
return module.exports.state({'color':'red'},selector);
}
exports.set.green = function(selector="all"){
return module.exports.state({'color':'green'},selector);
}
exports.set.blue = function(selector="all"){
return module.exports.state({'color':'blue'},selector);
}
exports.set.white = function(selector="all"){
return module.exports.state({'color':'white'},selector);
}
/*
PRESET BRIGHTNESS
*/
exports.set.dim = function(selector="all"){
return module.exports.state({'brightness':0.5},selector);
}
exports.set.bright = function(selector="all"){
return module.exports.state({'brightness':1.0},selector);
}