2019-03-21 19:01:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import delay = require('delay');
|
2019-03-21 22:03:53 +00:00
|
|
|
import Log = require('log');
|
2019-03-21 19:01:29 +00:00
|
|
|
|
|
|
|
class Light {
|
|
|
|
public state : any = { color : [0, 0, 0] }
|
|
|
|
|
|
|
|
private arduino : Arduino;
|
|
|
|
private cfg : any;
|
|
|
|
private ui : any;
|
|
|
|
private log : any;
|
|
|
|
private ipc : any;
|
|
|
|
|
2019-03-21 23:45:48 +00:00
|
|
|
private id : string = 'light';
|
|
|
|
|
2019-03-21 19:01:29 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
constructor (arduino : Arduino, cfg : any, ui : any) {
|
|
|
|
this.arduino = arduino;
|
|
|
|
this.cfg = cfg;
|
|
|
|
this.ui = ui;
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
private async init () {
|
2019-03-21 23:45:48 +00:00
|
|
|
this.log = await Log({ label : this.id });
|
2019-03-21 19:01:29 +00:00
|
|
|
this.ipc = require('electron').ipcMain;
|
|
|
|
this.listen();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
private listen () {
|
2019-03-21 23:45:48 +00:00
|
|
|
this.ipc.on(this.id, this.listener.bind(this));
|
2019-03-21 19:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
private async listener (event : any, arg : any) {
|
|
|
|
try {
|
|
|
|
await this.set(arg.rgb, arg.id, true);
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error('Error setting light', err);
|
|
|
|
|
|
|
|
}
|
|
|
|
event.returnValue = true
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
public async set (rgb : number[], id : string, on : boolean) {
|
|
|
|
const str : string = rgb.join(',');
|
|
|
|
let ms : any;
|
|
|
|
|
|
|
|
this.state.color = rgb;
|
|
|
|
try {
|
2019-03-21 23:45:48 +00:00
|
|
|
ms = this.arduino.send(this.id, this.cfg.arduino.cmd.light);
|
2019-03-21 19:01:29 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.log.error('Error sending light command', err);
|
|
|
|
}
|
|
|
|
await delay(1);
|
|
|
|
try {
|
2019-03-21 23:45:48 +00:00
|
|
|
this.arduino.string(this.id, str);
|
2019-03-21 19:01:29 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.log.error('Error sending light string', err);
|
|
|
|
}
|
|
|
|
await delay(1);
|
|
|
|
await ms;
|
|
|
|
return await this.end(rgb, id, ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
private async end (rgb : number[], id : string, ms : number) {
|
|
|
|
this.log.info(`Light set to ${rgb.join(',')}`, 'LIGHT', true, true);
|
2019-03-21 23:45:48 +00:00
|
|
|
return await this.ui.send(this.id, { rgb: rgb, id : id, ms: ms });
|
2019-03-21 19:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function (arduino : Arduino, cfg : any, ui : any) {
|
|
|
|
return new Light(arduino, cfg, ui);
|
|
|
|
}
|