Refactor light module into typescript-compiled class

This commit is contained in:
mmcwilliams 2019-03-21 15:01:29 -04:00
parent dc080b8566
commit e9ccdfc191
10 changed files with 352 additions and 0 deletions

76
app/lib/light/index.js Normal file
View File

@ -0,0 +1,76 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const delay = require("delay");
class Light {
/**
*
**/
constructor(arduino, cfg, ui) {
this.state = { color: [0, 0, 0] };
this.arduino = arduino;
this.cfg = cfg;
this.ui = ui;
this.init();
}
/**
*
**/
async init() {
this.log = await require('log')({});
this.ipc = require('electron').ipcMain;
this.listen();
}
/**
*
**/
listen() {
this.ipc.on('light', this.listener.bind(this));
}
/**
*
**/
async listener(event, arg) {
try {
await this.set(arg.rgb, arg.id, true);
}
catch (err) {
this.log.error('Error setting light', err);
}
event.returnValue = true;
}
/**
*
**/
async set(rgb, id, on) {
const str = rgb.join(',');
let ms;
this.state.color = rgb;
try {
ms = this.arduino.send('light', this.cfg.arduino.cmd.light);
}
catch (err) {
this.log.error('Error sending light command', err);
}
await delay(1);
try {
this.arduino.string('light', str);
}
catch (err) {
this.log.error('Error sending light string', err);
}
await delay(1);
await ms;
return await this.end(rgb, id, ms);
}
/**
*
**/
async end(rgb, id, ms) {
this.log.info(`Light set to ${rgb.join(',')}`, 'LIGHT', true, true);
return await this.ui.send('light', { rgb: rgb, id: id, ms: ms });
}
}
module.exports = function (arduino, cfg, ui) {
return new Light(arduino, cfg, ui);
};
//# sourceMappingURL=index.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/light/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,+BAAgC;AAEhC,MAAM,KAAK;IASV;;QAEI;IACJ,YAAa,OAAiB,EAAE,GAAS,EAAE,EAAQ;QAX5C,UAAK,GAAS,EAAE,KAAK,EAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAA;QAYzC,IAAI,CAAC,OAAO,GAAG,OAAO,CAA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AAO,GAAG,EAAE;YACb,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAC;SACnD;QACD,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC;QACf,IAAI;YACH,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;SAClC;QAAC,OAAO,GAAG,EAAE;YACb,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,4BAA4B,EAAE,GAAG,CAAC,CAAC;SAClD;QACD,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC;QACf,MAAM,EAAE,CAAC;QACT,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IACpC,CAAC;IAED;;QAEI;IACI,KAAK,CAAC,GAAG,CAAE,GAAc,EAAE,EAAW,EAAE,EAAW;QAC1D,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,gBAAgB,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QACpE,OAAO,MAAM,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IACnE,CAAC;CACD;AAED,MAAM,CAAC,OAAO,GAAG,UAAU,OAAiB,EAAE,GAAS,EAAE,EAAQ;IAChE,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;AACpC,CAAC,CAAA"}

View File

@ -0,0 +1,11 @@
{
"name": "light",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

76
cli/lib/light/index.js Normal file
View File

@ -0,0 +1,76 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const delay = require("delay");
class Light {
/**
*
**/
constructor(arduino, cfg, ui) {
this.state = { color: [0, 0, 0] };
this.arduino = arduino;
this.cfg = cfg;
this.ui = ui;
this.init();
}
/**
*
**/
async init() {
this.log = await require('log')({});
this.ipc = require('electron').ipcMain;
this.listen();
}
/**
*
**/
listen() {
this.ipc.on('light', this.listener.bind(this));
}
/**
*
**/
async listener(event, arg) {
try {
await this.set(arg.rgb, arg.id, true);
}
catch (err) {
this.log.error('Error setting light', err);
}
event.returnValue = true;
}
/**
*
**/
async set(rgb, id, on) {
const str = rgb.join(',');
let ms;
this.state.color = rgb;
try {
ms = this.arduino.send('light', this.cfg.arduino.cmd.light);
}
catch (err) {
this.log.error('Error sending light command', err);
}
await delay(1);
try {
this.arduino.string('light', str);
}
catch (err) {
this.log.error('Error sending light string', err);
}
await delay(1);
await ms;
return await this.end(rgb, id, ms);
}
/**
*
**/
async end(rgb, id, ms) {
this.log.info(`Light set to ${rgb.join(',')}`, 'LIGHT', true, true);
return await this.ui.send('light', { rgb: rgb, id: id, ms: ms });
}
}
module.exports = function (arduino, cfg, ui) {
return new Light(arduino, cfg, ui);
};
//# sourceMappingURL=index.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/light/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,+BAAgC;AAEhC,MAAM,KAAK;IASV;;QAEI;IACJ,YAAa,OAAiB,EAAE,GAAS,EAAE,EAAQ;QAX5C,UAAK,GAAS,EAAE,KAAK,EAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAA;QAYzC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,IAAI,EAAE,CAAC;IACb,CAAC;IAED;;QAEI;IACI,KAAK,CAAC,IAAI;QACjB,IAAI,CAA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,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;SAClC;QAAC,OAAO,GAAG,EAAE;YACb,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,4BAA4B,EAAE,GAAG,CAAC,CAAC;SAClD;QACD,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC;QACf,MAAM,EAAE,CAAC;QACT,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IACpC,CAAC;IAED;;QAEI;IACI,KAAK,CAAC,GAAG,CAAE,GAAc,EAAE,EAAW,EAAE,EAAW;QAC1D,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,gBAAgB,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QACpE,OAAO,MAAM,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IACnE,CAAC;CACD;AAED,MAAM,CAAC,OAAO,GAAG,UAAU,OAAiB,EAAE,GAAS,EAAE,EAAQ;IAChE,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;AACpC,CAAC,CAAA"}

View File

@ -0,0 +1,11 @@
{
"name": "light",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

76
lib/light/index.js Normal file
View File

@ -0,0 +1,76 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const delay = require("delay");
class Light {
/**
*
**/
constructor(arduino, cfg, ui) {
this.state = { color: [0, 0, 0] };
this.arduino = arduino;
this.cfg = cfg;
this.ui = ui;
this.init();
}
/**
*
**/
async init() {
this.log = await require('log')({});
this.ipc = require('electron').ipcMain;
this.listen();
}
/**
*
**/
listen() {
this.ipc.on('light', this.listener.bind(this));
}
/**
*
**/
async listener(event, arg) {
try {
await this.set(arg.rgb, arg.id, true);
}
catch (err) {
this.log.error('Error setting light', err);
}
event.returnValue = true;
}
/**
*
**/
async set(rgb, id, on) {
const str = rgb.join(',');
let ms;
this.state.color = rgb;
try {
ms = this.arduino.send('light', this.cfg.arduino.cmd.light);
}
catch (err) {
this.log.error('Error sending light command', err);
}
await delay(1);
try {
this.arduino.string('light', str);
}
catch (err) {
this.log.error('Error sending light string', err);
}
await delay(1);
await ms;
return await this.end(rgb, id, ms);
}
/**
*
**/
async end(rgb, id, ms) {
this.log.info(`Light set to ${rgb.join(',')}`, 'LIGHT', true, true);
return await this.ui.send('light', { rgb: rgb, id: id, ms: ms });
}
}
module.exports = function (arduino, cfg, ui) {
return new Light(arduino, cfg, ui);
};
//# sourceMappingURL=index.js.map

1
lib/light/index.js.map Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/light/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,+BAAgC;AAEhC,MAAM,KAAK;IASV;;QAEI;IACJ,YAAa,OAAiB,EAAE,GAAS,EAAE,EAAQ;QAX5C,UAAK,GAAS,EAAE,KAAK,EAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAA;QAYzC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,IAAI,EAAE,CAAC;IACb,CAAC;IAED;;QAEI;IACI,KAAK,CAAC,IAAI;QACjB,IAAI,CAAC,GAAG,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;QACpC,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC;QACvC,IAAI,CAAC,MAAM,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AAC,CAAC,CAAC,CAAC;QACf,MAAM,EAAE,CAAC;QACT,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IACpC,CAAC;IAED;;QAEI;IACI,KAAK,CAAC,GAAG,CAAE,GAAc,EAAE,EAAW,EAAE,EAAW;QAC1D,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,gBAAgB,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QACpE,OAAO,MAAM,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IACnE,CAAC;CACD;AAED,MAAM,CAAC,OAAO,GAAG,UAAU,OAAiB,EAAE,GAAS,EAAE,EAAQ;IAChE,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;AACpC,CAAC,CAAA"}

11
lib/light/package.json Normal file
View File

@ -0,0 +1,11 @@
{
"name": "light",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

88
src/light/index.ts Normal file
View File

@ -0,0 +1,88 @@
'use strict';
import delay = require('delay');
class Light {
public state : any = { color : [0, 0, 0] }
private arduino : Arduino;
private cfg : any;
private ui : any;
private log : any;
private ipc : any;
/**
*
**/
constructor (arduino : Arduino, cfg : any, ui : any) {
this.arduino = arduino;
this.cfg = cfg;
this.ui = ui;
this.init();
}
/**
*
**/
private async init () {
this.log = await require('log')({});
this.ipc = require('electron').ipcMain;
this.listen();
}
/**
*
**/
private listen () {
this.ipc.on('light', this.listener.bind(this));
}
/**
*
**/
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 {
ms = this.arduino.send('light', this.cfg.arduino.cmd.light);
} catch (err) {
this.log.error('Error sending light command', err);
}
await delay(1);
try {
this.arduino.string('light', str);
} 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);
return await this.ui.send('light', { rgb: rgb, id : id, ms: ms });
}
}
module.exports = function (arduino : Arduino, cfg : any, ui : any) {
return new Light(arduino, cfg, ui);
}