2019-03-21 19:01:29 +00:00
|
|
|
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2024-05-23 22:49:18 +00:00
|
|
|
exports.Light = void 0;
|
2024-05-24 00:51:35 +00:00
|
|
|
const electron_1 = require("electron");
|
2019-06-09 01:43:14 +00:00
|
|
|
const delay_1 = require("delay");
|
2024-05-19 22:14:33 +00:00
|
|
|
const log_1 = require("log");
|
2019-03-21 19:01:29 +00:00
|
|
|
class Light {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
constructor(arduino, cfg, ui) {
|
|
|
|
this.state = { color: [0, 0, 0] };
|
2024-05-24 00:51:35 +00:00
|
|
|
this.ipc = electron_1.ipcMain;
|
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() {
|
2024-05-19 22:14:33 +00:00
|
|
|
this.log = await (0, log_1.Log)({ label: this.id });
|
2019-03-21 19:01:29 +00:00
|
|
|
this.listen();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
listen() {
|
2024-04-07 03:37:30 +00:00
|
|
|
this.ipc.handle(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
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
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);
|
|
|
|
}
|
2024-04-06 23:09:50 +00:00
|
|
|
await (0, delay_1.delay)(1);
|
2019-03-21 19:01:29 +00:00
|
|
|
try {
|
2024-05-24 03:36:51 +00:00
|
|
|
ms += await this.arduino.sendString(this.id, str);
|
2019-03-21 19:01:29 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.log.error('Error sending light string', err);
|
|
|
|
}
|
2024-04-06 23:09:50 +00:00
|
|
|
await (0, delay_1.delay)(1);
|
2024-05-24 03:36:51 +00:00
|
|
|
ms += 2;
|
|
|
|
await this.end(rgb, id, ms);
|
|
|
|
return ms;
|
2019-03-21 19:01:29 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
async end(rgb, id, ms) {
|
2020-07-27 01:52:53 +00:00
|
|
|
let res;
|
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 {
|
|
|
|
res = await this.ui.send(this.id, { rgb, id, ms });
|
|
|
|
}
|
|
|
|
catch (err) {
|
2024-05-24 03:36:51 +00:00
|
|
|
this.log.error(`Error ending light`, err);
|
2020-07-27 01:52:53 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
return res;
|
2019-03-21 19:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-23 22:49:18 +00:00
|
|
|
exports.Light = Light;
|
2024-05-24 00:51:35 +00:00
|
|
|
module.exports = { Light };
|
2019-03-21 19:01:29 +00:00
|
|
|
//# sourceMappingURL=index.js.map
|