forked from linkineo/tinky-twinkly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmas.js
243 lines (210 loc) · 5.98 KB
/
xmas.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
const request = require("request");
const dgram = require('dgram');
const Color = require('color');
var IP = "192.168.0.62"; // ADD YOUR OWN LAMP IP HERE
var CHALLENGE = {'challenge':'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\a'}; // This challenge will do all the time.
var XAUTHTOKEN = "";
var UDP_PORT_RT=7777;
var RT_BUFFER = Buffer.alloc(1+9+175*3,0);
var UDP_RT_HEADER = 0x02;
UDP_RT_HEADER[0] = UDP_RT_HEADER;
var PRODUCT_NAME="Twinkly";
var OFF = {"mode":"off"};
var ON = {"mode": "movie","code": 1000};
var RT = {"mode":"rt"};
var LAMP_STATUS = {
UNREACHABLE: 0,
DETECTED :1,
CHALLENGE_ERROR: 2,
CHALLENGE_OK: 3,
AUTHENTICATE_ERROR: 4,
AUTHENTICATE_OK:5,
ACTION_ERROR: 6,
ACTION_SUCCESS: 7
}
var gestalt = function(callback){
request("http://" + IP + "/xled/v1/gestalt", function(error, response, body){
if(response.statusCode == 200)
{
try{
var product = JSON.parse(body);
if(product.product_name == PRODUCT_NAME)
{
callback(LAMP_STATUS.DETECTED);
}else
{
callback(LAMP_STATUS.UNREACHABLE);
}
}catch(error)
{
callback(LAMP_STATUS.UNREACHABLE);
}
}else{
callback(LAMP_STATUS.UNREACHABLE);
}
})
}
var login = function(callback){
var options = {
method: "POST",
uri: "http://" + IP + "/xled/v1/login",
json: true,
body: CHALLENGE,
headers: {
"Content-Length" : JSON.stringify(CHALLENGE).length
}
};
request(options,function(error,response,body){
if(response.statusCode == 200)
{
try{
XAUTHTOKEN = body.authentication_token;
callback(LAMP_STATUS.CHALLENGE_OK)
}catch(error)
{
callback(LAMP_STATUS.CHALLENGE_ERROR);
}
}else{
callback(LAMP_STATUS.UNREACHABLE);
}
})
}
var verify = function(callback){
var options = {
method: "POST",
uri: "http://" + IP + "/xled/v1/verify",
json: true,
body: {},
headers: {
"X-Auth-Token" : XAUTHTOKEN,
"Content-Length" : 2
}
};
request(options,function(error,response,body){
if(response.statusCode == 200)
{
try{
if(body.code == 1000)
{
callback(LAMP_STATUS.AUTHENTICATE_OK)
}else
{
callback(LAMP_STATUS.AUTHENTICATE_ERROR)
}
}catch(error)
{
callback(LAMP_STATUS.AUTHENTICATE_ERROR);
}
}else{
callback(LAMP_STATUS.UNREACHABLE);
}
})
}
var action = function(mode,callback){
var options = {
method: "POST",
uri: "http://" + IP + "/xled/v1/led/mode",
json: true,
body: mode,
headers: {
"X-Auth-Token" : XAUTHTOKEN,
"Content-Length" : JSON.stringify(mode).length
}
};
request(options,function(error,response,body){
if(response.statusCode == 200)
{
try{
if(body.code == 1000)
{
callback(LAMP_STATUS.ACTION_SUCCESS)
}else
{
callback(LAMP_STATUS.ACTION_ERROR)
}
}catch(error)
{
callback(LAMP_STATUS.ACTION_ERROR);
}
}else{
callback(LAMP_STATUS.UNREACHABLE);
}
})
}
var connect = function(callback){
gestalt(function(status){
if(status == LAMP_STATUS.DETECTED)
{
login(function(status){
if(status == LAMP_STATUS.CHALLENGE_OK)
{
verify(function(status){
if(status == LAMP_STATUS.AUTHENTICATE_OK)
{
callback(status);
}else
{
console.log("ERROR CODE=" + status);
}
})
}else
{
console.log("ERROR CODE=" + status);
}
})
}else
{
console.log("ERROR CODE=" + status);
}
})
}
var paint_single_color = function(color_value)
{
var udp_token = Buffer.from(XAUTHTOKEN,"base64");
udp_token.copy(RT_BUFFER,1,0,udp_token.length);
var color_buffer=Buffer.alloc(3);
color_buffer[0] = color_value.red();
color_buffer[1] = color_value.green();
color_buffer[2] = color_value.blue();
RT_BUFFER.fill(color_buffer,10,RT_BUFFER.length);
var client = dgram.createSocket('udp4');
client.send(RT_BUFFER, UDP_PORT_RT, IP, (err) => {
client.close();
});
}
var xmas_paint = function(color_value)
{
connect(function(status) {
if(status == LAMP_STATUS.AUTHENTICATE_OK)
{
action(RT, function(status){
if(status == LAMP_STATUS.ACTION_SUCCESS)
{
paint_single_color(color_value);
}
})
}
})
}
var xmas = function(mode)
{
connect(function(status) {
if(status == LAMP_STATUS.AUTHENTICATE_OK)
{
action(mode, function(status){
if(status == LAMP_STATUS.ACTION_SUCCESS)
{
console.log("SUCCESSFUL MODE CHANGE");
}
})
}
})
}
//TURN THE LAMPS ON
//xmas(ON);
//TURN THE LAMPS OFF
//xmas(OFF);
//TURN ON AND PAINT A COLOR (The LEDs will automatically revert to their normal ON mode (movie) after a few seconds)
xmas_paint(Color("red"));
//TURN ON AND PAINT ANY RGB COLOR
//xmas_paint(Color.rgb(118,36,242));