-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
408 lines (390 loc) · 15.1 KB
/
main.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
'use strict';
const utils = require('@iobroker/adapter-core');
const WebSocket = require('ws');
let adapter, pingTimer, timeoutTimer, rgbw, isAlive = false, flag = false, list_modes = null, mclighting, state_current = {}, timeOutRGB, timeOutSend, timeOutReconnect;
function startAdapter(options){
return adapter = utils.adapter(Object.assign({}, options, {
systemConfig: true,
name: 'mclighting',
ready: main,
unload: (callback) => {
try {
adapter.log.debug('cleaned everything up...');
mclighting && mclighting.close();
pingTimer && clearInterval(pingTimer);
timeoutTimer && clearInterval(timeoutTimer);
timeOutRGB && clearTimeout(timeOutRGB);
timeOutSend && clearTimeout(timeOutSend);
timeOutReconnect && clearTimeout(timeOutReconnect);
callback();
} catch (e) {
callback();
}
},
stateChange: (id, state) => {
if (id && state && !state.ack){
adapter.log.debug(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
let sendChar = '*';
if (state_current.ws2812fx_mode !== 0){
sendChar = '#';
}
let ids = id.split(".");
let name = ids[ids.length - 2].toString();
let command = ids[ids.length - 1].toString();
let val = state.val;
if (command === 'power'){
if (val){
send('%' + 255);
} else {
send('%' + 0);
}
}
if (command === 'mode'){
send('=' + val);
}
if (command === 'fx_mode'){
send('/' + val);
}
if (command === 'color'){
let c = val.split(",");
if (c.length >= 3){
let r = c[0] || 0;
let g = c[1] || 0;
let b = c[2] || 0;
if (c.length >= 4 && rgbw){
let w = c[3] || 0;
send(sendChar + rgbwToHex(parseInt(r), parseInt(g), parseInt(b), parseInt(w)));
} else {
send(sendChar + rgbwToHex(parseInt(r), parseInt(g), parseInt(b)));
}
}
}
if (command === 'color_R' || command === 'color_G' || command === 'color_B' || command === 'color_W'){
if (!flag){
flag = true;
timeOutRGB = setTimeout(() => {
let r, g, b, w;
timeOutRGB = null;
adapter.getState('color_R', (err, state_r) => {
if (!err){
r = state_r !== null ? state_r.val :0;
adapter.getState('color_G', (err, state_g) => {
if (!err){
g = state_g !== null ? state_g.val :0;
adapter.getState('color_B', (err, state_b) => {
if (!err){
b = state_b !== null ? state_b.val :0;
if (rgbw){
adapter.getState('color_W', (err, state_w) => {
if (!err){
w = state_w !== null ? state_w.val :0;
send(sendChar + rgbwToHex(r, g, b, w));
send('$');
}
});
} else {
send(sendChar + rgbwToHex(r, g, b));
send('$');
}
}
});
}
});
}
});
flag = false;
}, 1000);
}
}
if (command === (rgbw ? 'color_RGBW' :'color_RGB')){
val = val.replace('#', '');
if (rgbw){
val = val.slice(6, 8) + val.slice(0, 6);
}
send(sendChar + val);
}
if (command === (rgbw ? 'set_all_RGBW' :'set_all_RGB')){
val = val.replace('#', '');
if (rgbw){
val = val.slice(6, 8) + val.slice(0, 6);
}
send('*' + val);
}
if (command === (rgbw ? 'single_RGBW' :'single_RGB')){
val = val.replace('#', '');
if (rgbw){
val = val.slice(0, 2) + val.slice(8, 10) + val.slice(2, 8);
}
send('!' + val);
}
if (command === (rgbw ? 'array_RGBW' :'array_RGB')){
if (~val.indexOf('+')){
if (val[0] !== '+'){
val = '+' + val;
}
} else {
val = '+' + val.replace(/\s/g, '').replace(',', '+').replace('[', '').replace(']', '');
adapter.log.debug('Send ' + command + ': ' + val);
}
if (rgbw){
for (let i = 0; i < val.length; i = i + 11) {
const part = val.slice(i, i + 3) + val.slice(i + 9, i + 11) + val.slice(i + 3, i + 9);
val = val.slice(0, i) + part + val.slice(i + 11, val.length)
}
}
send(val);
}
if (command === (rgbw ? 'range_RGBW' :'range_RGB')){
if (~val.indexOf('R')){
if (val[0] !== 'R'){
val = 'R' + val;
}
} else {
val = 'R' + val.replace(/\s/g, '').replace(',', 'R').replace('[', '').replace(']', '');
adapter.log.debug('Send ' + command + ': ' + val);
}
if (rgbw){
for (let i = 0; i < val.length; i = i + 13) {
const part = val.slice(i, i + 5) + val.slice(i + 11, i + 13) + val.slice(i + 5, i + 11);
val = val.slice(0, i) + part + val.slice(i + 13, val.length)
}
}
send(val);
}
if (command === 'speed'){
if (val > 255) val = 255;
if (val < 0) val = 0;
send('?' + val);
}
if (command === 'brightness'){
if (val > 255) val = 255;
if (val < 0) val = 0;
send('%' + val);
}
if (!flag){
send('$');
}
}
}
}));
}
let connect = () => {
mclighting && mclighting.close();
let host = adapter.config.host ? adapter.config.host :'127.0.0.1';
let port = adapter.config.port ? adapter.config.port :81;
adapter.log.info('McLighting connect to: ' + host + ':' + port + ' ' + (rgbw ? ' with RGBW' :''));
mclighting = new WebSocket('ws://' + host + ':' + port, {
perMessageDeflate: false
});
mclighting.on('open', () => {
adapter.log.info(mclighting.url + ' McLighting connected');
send('$');
timeOutSend = setTimeout(() => {
timeOutSend = null;
send('~');
}, 5000);
pingTimer = setInterval(() => {
mclighting.ping('ping'); // Работает только на "ws": "^5.1.0", на последних версиях возращает ошибку.
}, 10000);
timeoutTimer = setInterval(() => {
if (!isAlive){
mclighting && mclighting.close();
} else {
isAlive = false;
}
}, 60000);
});
mclighting.on('pong', (msg) => {
isAlive = true;
adapter.log.debug(mclighting.url + ' receive a pong : ' + msg);
});
mclighting.on('message', (msg) => {
adapter.log.debug('Response message - ' + msg);
isAlive = true;
if (msg === 'Connected'){
adapter.setState('info.connection', true, true);
}
if (msg !== "OK" && msg !== 'Connected'){
parse(msg);
}
});
mclighting.on('error', (e) => {
adapter.log.debug('Error WS - ' + e);
});
mclighting.on('close', (e) => {
pingTimer && clearInterval(pingTimer);
timeoutTimer && clearInterval(timeoutTimer);
adapter.log.debug('ERROR! WS CLOSE, CODE - ' + e);
adapter.log.debug('McLighting reconnect after 10 seconds');
adapter.setState('info.connection', false, true);
timeOutReconnect = setTimeout(() => {
timeOutReconnect = null;
connect();
}, 10000);
});
};
function send(data){
if (mclighting){
mclighting.send(data, (e) => {
if (e){
adapter.log.error('Send command: {' + data + '}, ERROR - ' + e);
if (~e.toString().indexOf('CLOSED')){
adapter.setState('info.connection', false, true);
connect();
}
} else {
adapter.log.debug('Send command:{' + data + '}');
}
});
}
}
function parse(data){
let obj;
try {
obj = JSON.parse(data);
if (obj.mode && obj.brightness){
state_current = obj;
for (let key in obj) {
if (obj.hasOwnProperty(key)){
if (key === 'color'){
const length = obj[key].length;
if (length >= 3){
setStates('color_R', obj[key][(rgbw ? 1 :0)]);
setStates('color_G', obj[key][1 + (rgbw ? 1 :0)]);
setStates('color_B', obj[key][2 + (rgbw ? 1 :0)]);
if (length >= 4 && rgbw){
setStates('color_W', obj[key][0]);
const hex = rgbwToHex(obj[key][2], obj[key][3], obj[key][0], obj[key][1]);
setStates('color_RGBW', hex);
setStates('color', obj[key][1] + ',' + obj[key][2] + ',' + obj[key][3] + ',' + obj[key][0]);
} else {
setStates('color_RGB', rgbwToHex(obj[key][0], obj[key][1], obj[key][2]));
setStates('color', obj[key][0] + ',' + obj[key][1] + ',' + obj[key][2]);
}
}
} else {
setStates(key, obj[key]);
}
}
if (key === 'ws2812fx_mode'){
setStates('fx_mode', obj['ws2812fx_mode']);
}
if (key === 'ws2812fx_mode_name'){
setStates('fx_mode_name', obj['ws2812fx_mode_name']);
}
}
}
if (typeof obj[0] === 'object'){
setStates('list_modes', JSON.stringify(obj));
list_modes = obj;
}
} catch (err) {
adapter.log.debug('Error parse - ' + err);
}
}
function setStates(name, val){
adapter.log.debug("set state name: " + name + " val: " + val);
adapter.getState(name, (err, state) => {
if (err){
adapter.log.warn('Send this data to developers ' + name);
} else {
adapter.setState(name, {
val: val,
ack: true
});
}
});
}
function rgbwToHex(r, g, b, w){
return componentToHex(w) + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function componentToHex(c){
if (c !== null && c !== undefined){
let hex = c.toString(16);
return hex.length === 1 ? "0" + hex :hex;
} else {
return '';
}
}
function createStates(){
rgbw = adapter.config.rgbw;
adapter.setObjectNotExists(rgbw ? "color_RGBW" :"color_RGB", {
type: "state",
common: {
role: "level.color.rgb",
name: "Set default color of the lamp",
type: "string",
read: true,
write: true,
def: ""
}
});
adapter.setObjectNotExists(rgbw ? "set_all_RGBW" :"set_all_RGB", {
type: "state",
common: {
role: "level.color.rgb",
name: "Set default color of the lamp and light all LEDs in that color",
type: "string",
read: true,
write: true,
def: ""
}
});
adapter.setObjectNotExists(rgbw ? "single_RGBW" :"single_RGB", {
type: "state",
common: {
role: "state",
name: "Light single LEDs in the given color",
type: "string",
read: true,
write: true,
def: ""
}
});
adapter.setObjectNotExists(rgbw ? "array_RGBW" :"array_RGB", {
type: "state",
common: {
role: "state",
name: "Light multiple LEDs in the given colors",
type: "string",
read: true,
write: true,
def: ""
}
});
adapter.setObjectNotExists(rgbw ? "range_RGBW" :"range_RGB", {
type: "state",
common: {
role: "state",
name: "Light multiple LED ranges in the given colors",
type: "string",
read: true,
write: true,
def: ""
}
});
if (rgbw){
adapter.setObjectNotExists("color_W", {
type: "state",
common: {
role: "level.color.white",
name: "Set default White of the lamp",
type: "number",
min: 0,
max: 255,
read: true,
write: true
}
});
}
}
function main(){
createStates();
adapter.subscribeStates('*');
connect();
}
if (module.parent){
module.exports = startAdapter;
} else {
startAdapter();
}