-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlants-composer.js
423 lines (423 loc) · 15.7 KB
/
lants-composer.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* ------------------------------------------------------------------
* node-lifx-lan - lifx-lan-composer.js
*
* Copyright (c) 2017-2018, Futomi Hatano, All rights reserved.
* Released under the MIT license
* Date: 2018-07-01
* ---------------------------------------------------------------- */
import * as mCrypto from 'crypto';
import { lifxMsgType } from './lants-parser.js';
import { LifxApply } from './lants-device.js';
import { anyToHsb, } from './lants-color.js';
;
var bx;
(function (bx) {
// Buffer types
bx[bx["byte"] = 0] = "byte";
bx[bx["i16"] = 1] = "i16";
bx[bx["i32"] = 2] = "i32";
bx[bx["h16"] = 3] = "h16";
bx[bx["s32"] = 4] = "s32";
// Add support for colors and a buffer and group and ...
})(bx || (bx = {}));
class bufx {
constructor(size) {
this.buf = Buffer.alloc(size ?? 0);
this.cursor = 0;
}
static make(...vals) {
const size = (bz) => {
switch (bz) {
case bx.byte: return 1;
case bx.i16: return 2;
case bx.i32: return 4;
case bx.h16: return 16; // 32 chars, 16 bytes
case bx.s32: return 32;
}
};
const len = vals.reduce((p, v) => p += size(v.kind), 0);
const mbuf = new bufx(len);
vals.forEach(sv => {
if (sv.val == null)
mbuf.skip(sv.kind);
else {
const n = sv.val; // Guess we coould check but why bother
const s = sv.val;
switch (sv.kind) {
case bx.byte:
mbuf.writeUInt8(n);
break;
case bx.i16:
mbuf.writeUInt16LE(n);
break;
case bx.i32:
mbuf.writeUInt32LE(n);
break;
case bx.h16:
mbuf.append(Buffer.from(s.padEnd(32, '0').substr(0, 32), 'hex'));
break;
// Handle nulls for the strings? Also deal with UTF8 bytes
// ALso deal with crypto alternative
case bx.s32:
mbuf.append(Buffer.from(s.padEnd(32, '0').substr(0, 32), 'utf8'));
break;
}
}
});
return mbuf;
}
get buffer() { return this.buf.slice(0, this.cursor); }
append(b) {
if (b instanceof Buffer)
this.buf = Buffer.concat([this.buf, b]);
else
this.buf = Buffer.concat([this.buf, b.buf]);
}
assure(len) {
const need = this.cursor + len - this.buf.length;
if (need <= 0)
return; // OK
this.append(Buffer.alloc(need)); // Only just enough ... slower but OK
}
skip(span) {
this.cursor += span; // But we don't extend the allocation
}
writeUInt8(val) {
this.assure(1);
this.buf.writeUInt8(val, this.cursor++);
}
writeUInt16LE(val) {
this.assure(2);
this.buf.writeUInt16LE(val, this.cursor);
this.cursor += 2;
}
writeUInt32LE(val) {
this.assure(4);
this.buf.writeUInt32LE(val, this.cursor);
this.cursor += 4;
}
buf;
cursor; // Write position
}
export class LifxLanComposer {
compose(cp) {
const type = cp.type;
const payload = cp.payload;
const sequence = cp.sequence;
const ack_required = cp.ack_required ? 1 : 0;
const res_required = cp.res_required ? 1 : 0;
const target = cp.target;
const tagged = cp.tagged ? 1 : 0;
const source = cp.source;
const payload_buf = this._composePayload(type, payload); // .buffer;
const target_parts = target.match(/[0-9A-F]{2}:?/g);
if (target_parts?.length !== 6)
throw new Error('The value of the parameter `target` is invalid as a MAC address.');
const target_bytes = target_parts?.map(t => parseInt(t.replace(":", ""), 16));
// Frame
const origin = 0;
let addressable = 1;
const protocol = 1024;
let buf1 = Buffer.alloc(8);
let buf1n2 = protocol | (origin << 14) | (tagged << 13) | (addressable << 12);
buf1.writeUInt16LE(buf1n2, 2);
buf1.writeUInt32LE(source, 4);
// Frame Address
let buf2 = Buffer.alloc(16);
target_bytes.forEach((v, i) => {
buf2.writeUInt8(v, i);
});
buf2.writeUInt8((ack_required << 1) | res_required, 14);
buf2.writeUInt8(sequence, 15);
// Protocol Header
let buf3 = Buffer.alloc(12);
buf3.writeUInt16LE(type, 8);
let buf_list = [buf1, buf2, buf3];
if (payload_buf)
buf_list.push(payload_buf);
let buf = Buffer.concat(buf_list);
let size = buf.length;
buf.writeUInt16LE(size, 0);
return buf;
}
;
_composePayload(type, payload) {
switch (type) {
case lifxMsgType.SetPower: return this._composePayloadSetPower(payload);
case lifxMsgType.SetLabel: return this._composePayloadSetLabel(payload);
case lifxMsgType.SetLocation: return this._composePayloadSetLocation(payload);
case lifxMsgType.SetGroup: return this._composePayloadSetGroup(payload);
case lifxMsgType.EchoRequest: return this._composePayloadEchoRequest(payload);
case lifxMsgType.SetColor: return this._composePayloadSetColor(payload);
case lifxMsgType.SetWaveform: return this._composePayloadSetWaveForm(payload);
case lifxMsgType.SetLightPower: return this._composePayloadSetLightPower(payload);
case lifxMsgType.SetInfrared: return this._composePayloadSetInfrared(payload);
case lifxMsgType.SetColorZones: return this._composePayloadSetColorZones(payload);
case lifxMsgType.GetColorZones: return this._composePayloadGetColorZones(payload);
case lifxMsgType.EchoRequest: return this._composePayloadEchoRequest(payload);
case lifxMsgType.GetTileState64: return this._composePayLoadGetTileState64(payload);
default: return null;
}
}
;
_composePayloadSetPower(payload) {
let buf = Buffer.alloc(2);
buf.writeUInt16LE(payload.level == 1 ? 0xffff : 0, 0);
return buf;
}
;
_composePayloadSetLabel(payload) {
return this.composeLabel(payload.label);
}
;
composeLocation(location) {
return location ? Buffer.from(location.padEnd(32, '0').substr(0, 32), 'hex') : mCrypto.randomBytes(16);
}
composeGroup(group) {
return location ? Buffer.from(group.padEnd(32, '0').substr(0, 32), 'hex') : mCrypto.randomBytes(16);
}
composeLabel(label) {
// UTF8 may not be one byte per char
// return Buffer.from(label.padEnd(32, '\x00').substr(0, 32), 'utf8');
const label_buf = Buffer.from(label, 'utf8');
const padding_buf = Buffer.alloc(32 - label_buf.length);
return Buffer.concat([label_buf, padding_buf]).slice(0, 32); // Assure not too long
}
composeUpdated(updated) {
const ub = Buffer.alloc(8);
ub.writeUIntLE((updated ? updated : new Date()).getTime() * 1000000, 0, 8);
return ub;
}
_composePayloadSetLocation(payload) {
const location_buf = this.composeLocation(payload.location);
const label_buf = this.composeLabel(payload.label);
const updated_buf = this.composeUpdated(payload.updated);
const buf = Buffer.concat([location_buf, label_buf, updated_buf]);
return buf;
}
;
_composePayloadSetGroup(payload) {
const group_buf = this.composeGroup(payload.group);
const label_buf = this.composeLabel(payload.label);
const updated_buf = this.composeUpdated(payload.updated);
const buf = Buffer.concat([group_buf, label_buf, updated_buf]);
return buf;
}
;
/* ------------------------------------------------------------------
* Method: _composePayload58(payload) : deviceEchoRequest
* - payload:
* - text | String | Required | up to 64 bytes in UTF-8 encoding
* ---------------------------------------------------------------- */
_composePayloadEchoRequest(payload) {
let text = payload.text;
let text_buf = Buffer.from(text, 'utf8');
let padding_buf = Buffer.alloc(64 - text_buf.length);
let buf = Buffer.concat([text_buf, padding_buf]);
return buf;
}
;
_composePayloadSetColor(payload) {
const hsb = this._convertAnytoPacket(payload.color);
// private _composePayloadSetColor(payload: { color: LifxLanColorHSB, duration: Integer }): Buffer {
// const hsb = this._convertHSBToPacket(payload.color);
const duration = payload.duration || 0;
let buf = Buffer.alloc(13);
buf.writeUInt16LE(hsb.hue, 1);
buf.writeUInt16LE(hsb.saturation, 3);
buf.writeUInt16LE(hsb.brightness, 5);
buf.writeUInt16LE(hsb.kelvin, 7);
buf.writeUInt32LE(duration, 9);
return buf;
}
;
_convertAnytoPacket(dataAny) {
// const hsb = anyToHsb(data);
// return hsb;
// }
// private _convertHSBToPacket(data: LifxLanColorHSB): LifxLanColorHSB {
const data = anyToHsb(dataAny);
const test = {
hue: Math.round(data.hue * 0xffff),
saturation: Math.round(data.saturation * 0xffff),
brightness: Math.round(data.brightness * 0xffff),
kelvin: data.kelvin
};
let color = {
hue: 0,
saturation: 0,
brightness: 0,
};
let color_key_list = Object.keys(color);
for (let i = 0; i < color_key_list.length; i++) {
let k = color_key_list[i];
if (k in data) {
let v = data[k]; // Fuck you typescript
if (typeof (v) !== 'number' || v < 0 || v > 1) {
throw new Error('The `color.' + k + '` must be a float between 0.0 and 1.0.');
}
color[k] = Math.round(v * 65535); // Fuck you typescript
}
else {
throw new Error('The `color.' + k + '` is required.');
}
}
let kelvin = 0;
if ('kelvin' in data) {
kelvin = data['kelvin'];
if (typeof (kelvin) !== 'number' || kelvin % 1 !== 0 || kelvin < 1500 || kelvin > 9000) {
throw new Error('The `color.kelvin` must be an integer between 1500 and 9000.');
}
color['kelvin'] = kelvin;
}
else {
// 2021.03.09 removed kelvin check
// throw new Error('The `color.kelvin` is required.');
}
// return { color: color };
return color;
}
;
_composePayloadSetWaveForm(payload) {
// Check the payload
// Check the `transient`
let transient = 0;
if ('transient' in payload) {
transient = payload['transient'];
if (typeof (transient) !== 'number' || !(transient === 0 || transient === 1)) {
throw new Error('The `transient` must be 0 or 1.');
}
}
else {
throw new Error('The `transient` is required.');
// return;
}
// Check the `color`
if (!('color' in payload)) {
throw new Error('The `color` is required.');
}
// let color_check_res = this._checkColorValues(payload['color']);
// if (color_check_res['error']) {
// throw color_check_res['error'];
// }
// let color = color_check_res['color'];
const color = payload.color;
// Check the `period`
let period = 0;
if ('period' in payload) {
period = payload['period'];
if (typeof (period) !== 'number' || period % 1 !== 0 || period < 0 || period > 0xffffffff) {
throw new Error('The `period` must be an integer between 0 and 0xffffffff.');
}
}
else {
throw new Error('The `period` is required.');
}
// Check the `cycles`
let cycles = 0;
if ('cycles' in payload) {
cycles = payload['cycles'];
if (typeof (cycles) !== 'number') {
throw new Error('The `cycles` must be a float.');
}
}
else {
throw new Error('The `cycles` is required.');
}
// Checke the `waveform`
let waveform = 0;
if ('waveform' in payload) {
waveform = payload['waveform'];
if (typeof (waveform) !== 'number' || !/^(0|1|2|3|4)$/.test(waveform.toString())) {
throw new Error('The `waveform` must be 0, 1, 2, 3, or 4.');
}
}
else {
throw new Error('The `waveform` is required.');
}
// Check the `skew_ratio`
let skew_ratio = 0;
if (waveform === 4) {
if ('skew_ratio' in payload) {
skew_ratio = payload['skew_ratio'];
if (typeof (skew_ratio) !== 'number' || skew_ratio < 0 || skew_ratio > 1) {
throw new Error('The `skew_ratio` must be a float between 0.0 and 1.0.');
}
skew_ratio = skew_ratio * 65535 - 32768;
}
else {
throw new Error('The `skew_ratio` is required.');
}
}
// Compose a payload
let buf = Buffer.alloc(21);
buf.writeUInt8(transient, 1);
buf.writeUInt16LE(color['hue'], 2);
buf.writeUInt16LE(color['saturation'], 4);
buf.writeUInt16LE(color['brightness'], 6);
buf.writeUInt16LE(color['kelvin'], 8);
buf.writeUInt32LE(period, 10);
buf.writeFloatLE(cycles, 14);
buf.writeInt16LE(skew_ratio, 18);
buf.writeUInt8(waveform, 20);
return buf;
}
;
_composePayloadSetLightPower(payload) {
let level = payload.level == 1 ? 0xffff : 0;
const duration = payload.duration || 0;
let buf = Buffer.alloc(6);
buf.writeUInt16LE(level, 0);
buf.writeUInt32LE(duration, 2);
return buf;
}
;
_composePayloadSetInfrared(payload) {
const brightness = Math.round((payload.brightness || 0.0) * 65535);
let buf = Buffer.alloc(2);
buf.writeUInt16LE(brightness, 0);
return buf;
}
;
_composePayloadSetColorZones(payload) {
// Check the payload
// Check the `start`
let start = payload.start || 0;
let end = payload.end || 0;
if (start > end)
({ start, end } = { end, start });
const hsb = this._convertAnytoPacket(payload.color); // hack conversion!
let duration = payload.duration || 0;
let apply = payload.apply || LifxApply.APPLY;
let buf = Buffer.alloc(15);
buf.writeUInt8(start, 0);
buf.writeUInt8(end, 1);
buf.writeUInt16LE(hsb.hue, 2);
buf.writeUInt16LE(hsb.saturation, 4);
buf.writeUInt16LE(hsb.brightness, 6);
buf.writeUInt16LE(hsb.kelvin, 8);
buf.writeUInt32LE(duration, 10);
buf.writeUInt8(apply, 14);
return buf;
}
;
_composePayloadGetColorZones(payload) {
// Compose a payload
let buf = Buffer.alloc(2);
buf.writeUInt8(payload.start, 0);
buf.writeUInt8(payload.end, 1);
return buf;
}
;
_composePayLoadGetTileState64(payload) {
let buf = Buffer.alloc(6);
buf.writeUInt8(payload.tile_index, 0);
buf.writeUInt8(payload.length, 1);
// reserved
buf.writeUInt8(payload.x, 3);
buf.writeUInt8(payload.y, 4);
buf.writeUInt8(payload.width, 5);
return buf;
}
}
//# sourceMappingURL=lants-composer.js.map