-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
589 lines (559 loc) · 12.9 KB
/
index.ts
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// imports
import { randomBytes } from "node:crypto";
import axios, { AxiosInstance, AxiosResponse } from "axios";
import delay from "delay";
import * as udp from "node:dgram";
// create error
let errNoToken = Error("No valid token");
/**
* Represents a Twinkly device
* @public
*
*/
export class Light {
ipaddr: string;
challenge: string;
net: AxiosInstance;
token: AuthenticationToken | undefined;
activeLoginCall: boolean;
nleds: number | undefined;
udpClient: udp.Socket;
/**
* Creates an instance of Light.
*
* @constructor
* @param {string} ipaddr IP Address of the Twinkly device
*/
constructor(ipaddr: string, timeout: number = 1000) {
this.ipaddr = ipaddr;
this.challenge = randomBytes(256).toString("hex");
this.net = axios.create({
baseURL: `http://${this.ipaddr}/xled/v1/`,
timeout: timeout,
});
this.activeLoginCall = false;
this.udpClient = udp.createSocket("udp4");
}
async autoEndLoginCall(): Promise<void> {
await delay(1000);
this.activeLoginCall = false;
}
/**
* Sends a login request
*
* @returns {*}
*/
async login(): Promise<void> {
this.activeLoginCall = true;
this.autoEndLoginCall();
let res: AxiosResponse;
try {
res = await this.net.post("/login", {
challenge: this.challenge,
});
} catch (err) {
throw err;
}
this.token = new AuthenticationToken(res);
this.net.defaults.headers["X-Auth-Token"] = this.token.getToken();
if (res.data.code != 1000) {
throw Error("Login request failed");
}
console.log("Login request successful");
try {
this.verify();
} catch (err) {
throw err;
}
this.activeLoginCall = false;
}
/**
* Check that we are logged in to the device
*/
async verify(): Promise<void> {
let res: AxiosResponse;
if (this.token === undefined) throw errNoToken;
try {
res = await this.net.post("/verify", {
"challenge-response": this.token.getChallengeResponse(),
});
} catch (err) {
throw err;
}
if (res.data.code != 1000) {
throw errNoToken;
}
}
/**
* Ensure that we are logged into to the device, and if not initiate a login request
*/
async ensureLoggedIn(): Promise<void> {
try {
await this.verify();
} catch (err) {
if (err != errNoToken) {
throw err;
}
let i = 0;
while (this.activeLoginCall && i < 5) {
await delay(1200);
i++;
}
await this.login();
}
}
/**
* Gets details about the device
*
* @returns {Promise<object>} Results vary, see https://xled-docs.readthedocs.io/en/latest/rest_api.html#device-details
*/
async getDeviceDetails(): Promise<object> {
let data = await this.sendGetRequest("/gestalt", undefined, false);
return data;
}
/**
* Turns the device off
*
* @returns {unknown}
*/
async setOff(): Promise<void> {
return this.setMode(deviceMode.off);
}
/**
* Sets the state
* @experimental
* @param {boolean} state - Set on/off
*/
async setState(state: boolean): Promise<void> {
return this.setMode(state ? deviceMode.color : deviceMode.off);
}
/**
* Get the name of the device
*
* @returns {Promise<string>} Name of device
*/
async getName(): Promise<string> {
let data = await this.sendGetRequest("/device_name");
let res: string = data.name;
return res;
}
/**
* Sets the name of the device
*
* @param {string} name Desired device name, max 32 charachters
* @returns {Promise<void>}
*/
async setName(name: string): Promise<void> {
if (name.length > 32)
throw new Error("Name is too long - must be 32 char or less");
await this.sendPostRequest("/led/out/brightness", {
name: name,
});
}
/**
* Gets time when lights will turn on and off
*
* @returns {Promise<timer>}
*/
async getTimer(): Promise<timer> {
let data: timer = await this.sendGetRequest("/timer");
return data;
}
/**
* Sets the brightness level
*
* @param {number} value
* @param {string} [mode="enabled"]
* @param {string} [type="A"]
* @returns {}
*/
async setBrightness(
value: number,
mode: string = "enabled",
type: string = "A"
): Promise<void> {
await this.sendPostRequest("/led/out/brightness", {
mode: mode,
type: type,
value: value,
});
}
/**
* Gets the current brightness level
*
* @returns {number} Current brightness in range 0..100
*/
async getBrightness(): Promise<number> {
let data = await this.sendGetRequest("/led/out/brightness", {});
return data.value;
}
/**
* Gets the current colour in HSV
*/
async getHSVColour(): Promise<hsvColour> {
let data = await this.sendGetRequest("/led/color", {});
let res: hsvColour = {
hue: data.hue,
saturation: data.saturation,
value: data.value,
};
return res;
}
/**
* Gets the current colour in RGB
*/
async getRGBColour(): Promise<rgbColour> {
let data = await this.sendGetRequest("/led/color", {});
let res: rgbColour = { red: data.red, green: data.green, blue: data.blue };
return res;
}
/**
* Sets the colour in RGB when in colour mode
*
* @param {rgbColour} colour A RGB colour
*/
async setRGBColour(colour: rgbColour): Promise<void> {
await this.sendPostRequest("/led/color", {
red: colour.red,
green: colour.green,
blue: colour.blue,
});
}
async setRGBColourRealTime(colour: rgbColour): Promise<void> {
let frame = new OneColourFrame(colour, await this.getNLeds());
await this.sendRealTimeFrame(frame);
}
/**
* Sets the colour in HSV when in colour mode
*
* @param {hsvColour} colour A HSV colour
*/
async setHSVColour(colour: hsvColour): Promise<void> {
await this.sendPostRequest("/led/color", {
hue: Math.round(colour.hue),
saturation: Math.round(colour.saturation),
value: Math.round(colour.value),
});
}
/**
* Gets the LED operation mode
*
* @returns {deviceMode} mode
*/
async getMode(): Promise<deviceMode> {
let res = await this.sendGetRequest("/led/mode", {});
let mode: deviceMode = (<any>deviceMode)[res.mode];
return mode;
}
/**
* Sets the LED operation mode
*
* @param {deviceMode} mode
*/
async setMode(mode: deviceMode): Promise<void> {
await this.sendPostRequest("/led/mode", { mode: mode });
}
/**
* Sends a POST request to the device, appending the required tokens
*
* @param {string} url
* @param {object} params
*/
async sendPostRequest(url: string, params: object): Promise<any> {
if (!this.token) throw errNoToken;
let res: AxiosResponse;
try {
res = await this.net.post(url, params);
} catch (err) {
throw err;
}
if (res.data.code != 1000) {
throw Error("Mode set failed");
}
return res.data;
}
/**
* Sends a GET request to the device, appending the required tokens
*
* @param {string} url
* @param {object} params
*/
async sendGetRequest(
url: string,
params?: object,
requiresToken: boolean = true
): Promise<any> {
if (!this.token && requiresToken) throw errNoToken;
let res: AxiosResponse;
try {
res = await this.net.get(url, params || {});
} catch (err) {
throw err;
}
if (res.data.code != 1000) {
throw Error("Get Request failed");
}
return res.data;
}
async sendRealTimeFrame(frame: Frame) {
if (!this.token) throw errNoToken;
let res: AxiosResponse;
try {
res = await this.net.post("/led/rt/frame", frame.toOctet(), {
headers: {
"Content-Type": "application/octet-stream",
},
});
} catch (err) {
throw err;
}
if (res.data.code != 1000) {
throw Error("Failed to send RT frame");
}
return res.data;
}
async sendRealTimeFrameUDP(frame: Frame) {
if (!this.token) throw errNoToken;
// Generate the header
let tokenArray = this.token.getTokenDecoded();
let udpHeader = Buffer.alloc(tokenArray.length + 4);
udpHeader.writeUInt8(0x03); // the version number
udpHeader.fill(tokenArray, 1); // the actual token, 8 bytes
udpHeader.writeUInt8(0x00, tokenArray.length + 1); // zero blanking
udpHeader.writeUInt8(0x00, tokenArray.length + 2); // zero blanking
udpHeader.writeUInt8(0x00, tokenArray.length + 3); // number of packets (currently only 1 as i only hav 250 leds)
// Generate the body
const data = Buffer.alloc(udpHeader.length + frame.getNLeds() * 3);
data.fill(udpHeader);
data.fill(frame.toOctet(), udpHeader.length);
this.udpClient.send(data, 7777, this.ipaddr, (error) => {
if (error) {
console.warn(error);
}
});
}
async getListOfMovies() {
let res = await this.sendGetRequest("/movies", {});
let movies: Movie[] = res.movies.map((data: any) => {
return new Movie(data);
});
return movies;
}
async addMovie(movie: Movie) {
await this.sendPostRequest("/movies/new", movie.export());
}
async getLayout() {
let res = await this.sendGetRequest("/led/layout/full", {});
return res;
}
async getNLeds() {
if (this.nleds) return this.nleds;
let res: any = await this.getDeviceDetails();
let nleds: number = res.number_of_led;
this.nleds = nleds;
return nleds;
}
}
export class Movie {
id: number;
name: string;
unique_id: string;
descriptor_type: string;
leds_per_frame: number;
frames_number: number;
fps: number;
constructor(data: any) {
this.id = data.id;
this.name = data.name;
this.unique_id = data.unique_id;
this.descriptor_type = data.descriptor_type;
this.leds_per_frame = data.leds_per_frame;
this.frames_number = data.frames_number;
this.fps = data.fps;
}
export() {
return {
id: this.id,
name: this.name,
unique_id: this.unique_id,
descriptor_type: this.descriptor_type,
leds_per_frame: this.leds_per_frame,
frames: this.frames_number,
fps: this.fps,
};
}
}
/**
* Represents an authentication token used to login to an xled instance
* @internal
*/
export class AuthenticationToken {
token: string;
expiry: Date;
challengeResponse: string;
/**
* Creates an instance of AuthenticationToken.
*
* @constructor
* @param {AxiosResponse} res Response from POST request
*/
constructor(res: AxiosResponse) {
this.token = res.data.authentication_token;
this.expiry = new Date(
Date.now() + res.data.authentication_token_expires_in * 1000
);
this.challengeResponse = res.data.challenge_response;
}
/**
*
* @returns Token as string
*/
getToken(): string {
return this.token;
}
/**
*
* @returns Token as buffer, for UDP use
*/
getTokenDecoded() {
return Buffer.from(this.getToken(), "base64");
}
/**
*
* @returns Challenge response generated by the XLED instance
*/
getChallengeResponse(): string {
return this.challengeResponse;
}
}
export interface rgbColour {
/** Red 0..255 */
red: number;
/** Green 0..255 */
green: number;
/** Blue 0..255 */
blue: number;
}
export interface hsvColour {
/** Hue 0..359 */
hue: number;
/** Saturation 0..255 */
saturation: number;
/** Value (brightness) 0..255 */
value: number;
}
export enum deviceMode {
demo = "demo",
color = "color",
off = "off",
effect = "effect",
movie = "movie",
playlist = "playlist",
rt = "rt",
}
export interface timer {
/** Current time according to the device, seconds after midnight */
time_now: number;
/** Time to switch lights on, seconds after midnight. -1 if not set. */
time_on: number;
/** Time to switch lights off, seconds after midnight. -1 if not set. */
time_off: number;
}
/**
* A frame of LEDs, used when you wish to set colour pixel by pixel
*
* @export
* @class Frame
* @typedef {Frame}
*/
export class Frame {
leds: Led[];
/**
* Creates an instance of Frame.
*
* @constructor
* @param {Led[]} leds Array of Led, of same length as nleds
*/
constructor(leds: Led[]) {
this.leds = leds;
}
/**
* Output the frame as a Uint8Array of bytes
*
* @returns {Uint8Array}
*/
toOctet(): Uint8Array {
let bytes = this.leds.map((led) => {
return led.toOctet();
});
let output = new Uint8Array(this.leds.length * 3);
let offset = 0;
bytes.forEach((item) => {
output.set(item, offset);
offset += item.length;
});
return output;
}
/**
* Get the number of LEDs in this frame
*
* @returns {number}
*/
getNLeds(): number {
return this.leds.length;
}
}
/**
* Easy way to create an entire frame of one colour
*
* @export
* @class OneColourFrame
* @typedef {OneColourFrame}
* @extends {Frame}
*/
export class OneColourFrame extends Frame {
/**
* Creates an instance of OneColourFrame.
*
* @constructor
* @param {rgbColour} rgb
* @param {number} nleds Number of LEDs to include in this frame (probably the number of LEDs in the string)
*/
constructor(rgb: rgbColour, nleds: number) {
let leds: Led[] = Array(nleds).fill(new Led(rgb.red, rgb.green, rgb.blue));
super(leds);
}
}
/**
* A RGB led
*
* @export
* @class Led
* @typedef {Led}
*/
export class Led {
red: number;
green: number;
blue: number;
/**
* Creates an instance of Led.
*
* @constructor
* @param {number} red
* @param {number} green
* @param {number} blue
*/
constructor(red: number, green: number, blue: number) {
this.red = red;
this.green = green;
this.blue = blue;
}
/**
* Returns the LED in octet form
*
* @returns {Uint8Array}
*/
toOctet(): Uint8Array {
return new Uint8Array([this.red, this.green, this.blue]);
}
}