2019-03-21 19:01:29 +00:00
|
|
|
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2019-06-09 01:43:14 +00:00
|
|
|
const delay_1 = require("delay");
|
2019-03-21 22:03:53 +00:00
|
|
|
const Log = require("log");
|
2019-03-21 19:01:29 +00:00
|
|
|
class Light {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
constructor(arduino, cfg, ui) {
|
|
|
|
this.state = { color: [0, 0, 0] };
|
2019-03-22 02:55:12 +00:00
|
|
|
this.enabled = true;
|
2019-03-21 23:45:48 +00:00
|
|
|
this.id = 'light';
|
2019-03-21 19:01:29 +00:00
|
|
|
this.arduino = arduino;
|
|
|
|
this.cfg = cfg;
|
|
|
|
this.ui = ui;
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
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
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
async listener(event, arg) {
|
2019-03-22 02:55:12 +00:00
|
|
|
if (typeof arg.rgb !== 'undefined') {
|
|
|
|
try {
|
|
|
|
await this.set(arg.rgb, arg.id, true);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.log.error('Error setting light', err);
|
|
|
|
}
|
2019-03-21 19:01:29 +00:00
|
|
|
}
|
2019-03-22 02:55:12 +00:00
|
|
|
else if (typeof arg.enable !== 'undefined') {
|
|
|
|
this.enabled = true;
|
|
|
|
}
|
|
|
|
else if (typeof arg.disable !== 'undefined') {
|
|
|
|
this.enabled = false;
|
2019-03-21 19:01:29 +00:00
|
|
|
}
|
|
|
|
event.returnValue = true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2020-07-27 01:52:53 +00:00
|
|
|
async set(rgb, id, on = true) {
|
2019-03-21 19:01:29 +00:00
|
|
|
const str = rgb.join(',');
|
|
|
|
let ms;
|
|
|
|
this.state.color = rgb;
|
|
|
|
try {
|
2020-07-27 01:52:53 +00:00
|
|
|
ms = await 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);
|
|
|
|
}
|
2019-06-09 01:43:14 +00:00
|
|
|
await delay_1.delay(1);
|
2019-03-21 19:01:29 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-06-09 01:43:14 +00:00
|
|
|
await delay_1.delay(1);
|
2019-03-21 19:01:29 +00:00
|
|
|
await ms;
|
|
|
|
return await this.end(rgb, id, ms);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
async end(rgb, id, ms) {
|
2020-07-27 01:52:53 +00:00
|
|
|
let res;
|
|
|
|
//console.trace()
|
2019-03-21 19:01:29 +00:00
|
|
|
this.log.info(`Light set to ${rgb.join(',')}`, 'LIGHT', true, true);
|
2020-07-27 01:52:53 +00:00
|
|
|
try {
|
|
|
|
//console.dir({ rgb, id, ms })
|
|
|
|
res = await this.ui.send(this.id, { rgb, id, ms });
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
return res;
|
2019-03-21 19:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = function (arduino, cfg, ui) {
|
|
|
|
return new Light(arduino, cfg, ui);
|
|
|
|
};
|
|
|
|
//# sourceMappingURL=index.js.map
|