-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisco.js
349 lines (314 loc) · 9.11 KB
/
disco.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
'use strict';
var dgram = require('dgram');
var DEFAULT_HOST = "10.1.1.2";
var DEFAULT_PORT = "5987";
var PREAMPLE = [0x80,0x00,0x00,0x00,0x11]
var FILLER = 0x00
var CMDS={
ON: [0x31,0,0,0x08,0x04,0x01,0,0,0],
OFF: [0x31,0,0,0x08,0x04,0x02,0,0,0],
NIGHT: [0x31,0,0,0x08,0x04,0x05,0,0,0],
WHITEON: [0x31,0,0,0x08,0x05,0x64,0,0,0],
REG: [0x33,0,0,0,0,0,0,0,0,0],
BON: [0x31,0x00,0x00,0x00,0x03,0x03,0x00,0x00,0x00],
BOFF:[0x31,0x00,0x00,0x00,0x03,0x04,0x00,0x00,0x00],
BWHITE:[0x31 ,0x00 ,0x00 ,0x00 ,0x03 ,0x05 ,0x00 ,0x00 ,0x00],
};
var zoneCtlRGBW=function(zoneID){
var color=0x7A;
var brightness=0x32;
var saturation=0x32;
var colorTemp=0x4B;
var zone=zoneID;
if(zone > 4 || zone <0 ) console.log("invalid zone");
return {
on: function(){
return [0x31,0,0,0x08,0x04,0x01,0,0,0,zoneID];
},
off: function(){
return [0x31,0,0,0x08,0x04,0x02,0,0,0,zoneID];
},
nightMode: function() {
return [0x31,0,0,0x08,0x04,0x05,0,0,0,zoneID];
},
whiteMode:function() {
return [0x31,0,0,0x08,0x05,0x64,0,0,0,zoneID];
},
brightnessUp:function(){
brightness=Math.min(brightness+5,0x64);
return [0x31,0x00,0x00,0x08,0x03,brightness,0x00,0x00,0x00,zoneID]
},
brightnessDown:function(){
brightness=Math.max(brightness-5,0x00);
return [0x31,0x00,0x00,0x08,0x03,brightness,0x00,0x00,0x00,zoneID]
},
brightnessSet:function(b){
brightness=Math.max(b,0x00);
brightness=Math.min(b,0xFF);
return [0x31,0x00,0x00,0x08,0x03,brightness,0x00,0x00,0x00,zoneID]
},
colorUp:function(){
color=Math.min(color+5,0xFF);
return [0x31,0x00,0x00,0x08,0x01,color,color,color,color,zoneID]
},
colorDown:function(){
color=Math.max(color-5,0x00);
return [0x31,0x00,0x00,0x08,0x01,color,color,color,color,zoneID]
},
colorSet:function(c){
color=c;
return [0x31,0x00,0x00,0x08,0x01,color,color,color,color,zoneID]
},
colorTempUp:function(){
colorTemp=Math.min(colorTemp+5,0x64);
return [0x31,0x00,0x00,0x08,0x05,colorTemp,0,0,0,zoneID]
},
colorTempDown:function(){
colorTemp=Math.max(colorTemp-5,0x00);
return [0x31,0x00,0x00,0x08,0x05,colorTemp,0,0,0,zoneID]
},
colorTempSet:function(c){
colorTemp=c;
return [0x31,0x00,0x00,0x08,0x05,colorTemp,0,0,0,zoneID]
},
mode:function(mode){
return [0x31,0x00,0x00,0x08,0x06,mode,0,0,0,zoneID]
},
modeSpeedUp:function(){
return [0x31,0,0,0x08,0x04,0x03,0,0,0,zoneID]
},
modeSpeedDown:function(){
return [0x31,0,0,0x08,0x04,0x04,0,0,0,zoneID]
},
link:function(){
return [0x3D,0,0,0x08,0,0,0,0,0,zoneID]
},
unlink:function(){
return [0x3E,0,0,0x08,0,0,0,0,0,zoneID]
},
}
}
var baseCtlFactory=function(){
var color=0x7A;
var brightness=0x32;
var zoneID=0x01;
return {
on:function(){return [0x31,0x00,0x00,0x00,0x03,0x03,0x00,0x00,0x00,zoneID]},
off:function(){return [0x31,0x00,0x00,0x00,0x03,0x04,0x00,0x00,0x00,zoneID]},
whiteMode:function(){return [0x31 ,0x00 ,0x00 ,0x00 ,0x03 ,0x05 ,0x00 ,0x00 ,0x00,zoneID]},
brightnessUp:function(){
brightness=Math.min(brightness+5,0x64);
return [0x31,0x00,0x00,0x00,0x02,brightness,0x00,0x00,0x00,zoneID]
},
brightnessDown:function(){
brightness=Math.max(brightness-5,0x00);
return [0x31,0x00,0x00,0x00,0x02,brightness,0x00,0x00,0x00,zoneID]
},
brightnessSet:function(b){
brightness=Math.max(b,0x00);
brightness=Math.min(b,0xFF);
return [0x31,0x00,0x00,0x00,0x02,brightness,0x00,0x00,0x00,zoneID]
},
colorUp:function(){
color=Math.min(color+5,0xFF);
return [0x31,0x00,0x00,0x00,0x01,color,color,color,color,zoneID]
},
colorDown:function(){
color=Math.max(color-5,0x00);
return [0x31,0x00,0x00,0x00,0x01,color,color,color,color,zoneID]
},
colorSet:function(c){
color=c;
return [0x31,0x00,0x00,0x00,0x01,color,color,color,color,zoneID]
},
mode:function(mode){
return [0x31,0x00,0x00,0x00,0x04,mode,0,0,0,zoneID]
}
}
};
var bridgeID;
var seqNum=0x02;
var sendCmd = function(WB,CMD){
var out=[];
//console.log("#"+WB.toString('hex')+"-"+CMD.toString("hex"));
out=out.concat(PREAMPLE,WB,0x00,0x00,seqNum,FILLER,CMD)
var chkSum=calcCheckSum(out);
out = out.concat(chkSum);
//console.log(JSON.stringify(out));
//console.log("#"+out.toString('hex'));
out=new Buffer(out);
seqNum=(seqNum+1)%256;
console.log("Sending: " + out.toString('hex'));
socket.send(out,0,out.length,DEFAULT_PORT,DEFAULT_HOST,function(){});
}
var buildFrame = function(WB,CMD,ZONE){
var out=[];
//console.log("#"+WB.toString('hex')+"-"+CMD.toString("hex"));
out=out.concat(PREAMPLE,WB,0x00,0x00,seqNum,FILLER,CMD,ZONE)
var chkSum=calcCheckSum(out);
out = out.concat(chkSum);
//console.log(JSON.stringify(out));
//console.log("#"+out.toString('hex'));
seqNum=(seqNum+1)%256;
return new Buffer(out);
}
var sendFrame=function(payload){
console.log("Sending: " + payload.toString('hex'));
socket.send(payload,0,payload.length,DEFAULT_PORT,DEFAULT_HOST,function(){});
}
var sendKeepAlive=function(){
var out=new Buffer([0xD0,0x00,0x00,0x00,0x02,bridgeID,0x00]);
sendFrame(out);
}
var calcCheckSum=function(aFrame){
var add=function(a,b){
return a+b;
};
var sub = aFrame.slice(Math.max(aFrame.length - 11, 0)) ;
var val=sub.reduce(add,0)
var val1=Math.floor(val / 0xff)
var val2=val % 0xff
return [val1, val2]
}
var _func={};
_func['2800000011']=function(msg){
//response to initiate
var unknown1=msg.slice(5,7);
var mac=msg.slice(7,13);
var fixed=msg.slice(13,15);
var unknown2=msg.slice(15,19);
var counter=msg.slice(19,20);
var padding=msg.slice(20);
/*
console.log("0:" +msg.toString('hex'));
console.log("1:" +unknown1.toString('hex'));
console.log("2:" +mac.toString('hex'));
console.log("2a:" +fixed.toString('hex'));
console.log("3:" +unknown2.toString('hex'));
console.log("4:" +counter.toString('hex'));
console.log("5:" +padding.toString('hex'));
*/
bridgeID=new Uint8Array(counter);
//80:00:00:00:11:c1:01:00:0b:00:33:00:00:00:00:00:00:00:00:00:00:33
//complete initiation
var nFrame=buildFrame(bridgeID,[0x33,0,0,0,0,0,0,0,0,0],0x00);
sendFrame(nFrame);
//start keepalive
setInterval(sendKeepAlive,10000);
};
_func['8800000003']=function(msg){
//ERROR - Confirmation?
var code= msg.slice(0,5);
var unknown1 = msg.slice(5,8);
console.log("0:" +msg.toString('hex'));
console.log("1:" +unknown1.toString('hex'));
console.log("ID:"+bridgeID.toString('hex'));
var nFrame=buildFrame(bridgeID,CMDS.ON,0x01);
}
_func['d800000007']=function(){
//keepalive response
}
var socket=dgram.createSocket('udp4');
socket.bind(DEFAULT_PORT);
socket.on("message", (msg, rinfo) => {
//console.log('Received %d bytes from %s:%d\n',
// msg.length, rinfo.address, rinfo.port);
var hmsg=msg.toString('hex');
var resp=(msg.toString('hex').substring(0,10));
if (_func[resp]) {
_func[resp](msg)
} else {
console.log("Unknown code");
console.log(hmsg);
}
});
var initiate=function(){
//socket.send(payload,0,payload.length,DEFAULT_PORT,DEFAULT_HOST,function(a,b){});
var payload=new Buffer([0x20,0x00,0x00,0x00,0x16,0x02,0x62,0x3a,0xd5,0xed,0xa3,0x01,0xae,0x08,0x2d,0x46,0x61,0x41,0xa7,0xf6,0xdc,0xaf,0xfe,0xf7,0x00,0x00,0x1e]);
console.log(payload.toString('hex'));
sendFrame(payload);
};
initiate();
var baseCtl=baseCtlFactory();
var zone1Ctl=zoneCtlRGBW(0x01);
var readline = require('readline'),
rl = readline.createInterface(process.stdin, process.stdout),
prefix = 'OHAI> ';
var cmd;
rl.on('line', function(line) {
switch(line.trim()) {
case 'hello':
console.log('world!');
break;
case 'zone1 on':
cmd=zone1Ctl.on();
break;
case 'zone1 off':
cmd=zone1Ctl.off();
break;
case 'zone1 white':
cmd=zone1Ctl.whiteMode();
break;
case 'zone1 brighter':
cmd=zone1Ctl.brightnessUp();
break;
case 'zone1 dimmer':
cmd=zone1Ctl.brightnessDown();
//var nFrame=buildFrame(bridgeID,zone1Ctl.brightnessDown(),0x01);
//sendFrame(nFrame);
break;
case 'zone1 colorUp':
cmd=zone1Ctl.colorUp();
break;
case 'zone1 colorDown':
cmd=zone1Ctl.colorDown();
break;
case 'zone1 mode1':
cmd=zone1Ctl.mode(1);
break;
case 'zone1 mode2':
cmd=zone1Ctl.mode(2);
break;
case 'base on':
cmd=baseCtl.on();
break;
case 'base off':
cmd=baseCtl.off();
break;
case 'base white':
cmd=baseCtl.whiteMode();
break;
case 'base brighter':
cmd=baseCtl.brightnessUp();
break;
case 'base dimmer':
cmd=baseCtl.brightnessDown();
//var nFrame=buildFrame(bridgeID,baseCtl.brightnessDown(),0x01);
//sendFrame(nFrame);
break;
case 'base colorUp':
cmd=baseCtl.colorUp();
break;
case 'base colorDown':
cmd=baseCtl.colorDown();
break;
case 'base mode1':
cmd=baseCtl.mode(1);
break;
case 'base mode2':
cmd=baseCtl.mode(2);
break;
default:
console.log('Say what? I might have heard `' + line.trim() + '`');
break;
}
sendCmd(bridgeID,cmd);
rl.setPrompt(prefix, prefix.length);
rl.prompt();
}).on('close', function() {
console.log('Have a great day!');
process.exit(0);
});
console.log(prefix + 'Good to see you. Try typing stuff.');
rl.setPrompt(prefix, prefix.length);
rl.prompt();