2019-03-20 02:33:15 +00:00
|
|
|
'use strict';
|
2019-03-22 07:22:33 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const Log = require("log");
|
2019-03-20 02:33:15 +00:00
|
|
|
let seq;
|
2019-03-22 08:33:53 +00:00
|
|
|
class Sequencer {
|
2019-03-24 01:58:22 +00:00
|
|
|
constructor(cfg, cmd, ui) {
|
2019-03-22 21:06:57 +00:00
|
|
|
this.running = false;
|
|
|
|
this.paused = false;
|
2019-03-30 00:44:12 +00:00
|
|
|
this.grid = [];
|
|
|
|
this.gridLoops = 1;
|
|
|
|
this.arr = []; //store sequence from gui
|
2019-03-22 07:22:33 +00:00
|
|
|
this.loops = 1;
|
|
|
|
this.CMDS = {};
|
2019-03-22 08:33:53 +00:00
|
|
|
this.id = 'sequence';
|
2019-03-20 02:33:15 +00:00
|
|
|
this.cfg = cfg;
|
|
|
|
this.cmd = cmd;
|
2019-03-24 01:58:22 +00:00
|
|
|
this.ui = ui;
|
2019-03-22 07:22:33 +00:00
|
|
|
this.cmds(cfg.cmd);
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
cmds(obj) {
|
|
|
|
let keys = Object.keys(obj);
|
2019-03-24 01:58:22 +00:00
|
|
|
for (let key of keys) {
|
|
|
|
this.CMDS[obj[key]] = key;
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
|
|
|
//currently called by ui
|
2019-03-22 07:22:33 +00:00
|
|
|
async init() {
|
2019-03-24 01:58:22 +00:00
|
|
|
this.log = await Log({ label: this.id });
|
2019-03-22 07:22:33 +00:00
|
|
|
this.ipc = require('electron').ipcMain;
|
|
|
|
this.listen();
|
|
|
|
}
|
|
|
|
listen() {
|
2019-03-22 08:33:53 +00:00
|
|
|
this.ipc.on(this.id, this.listener.bind(this));
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
|
|
|
async listener(event, arg) {
|
2019-03-22 21:06:57 +00:00
|
|
|
if (arg && arg.start) {
|
|
|
|
this.start(arg);
|
|
|
|
}
|
|
|
|
else if (arg && arg.stop) {
|
|
|
|
this.stop();
|
|
|
|
}
|
|
|
|
else if (arg && arg.pause) {
|
|
|
|
this.pause();
|
|
|
|
}
|
|
|
|
else if (arg && arg.set) {
|
2019-03-22 08:33:53 +00:00
|
|
|
this.setSteps(arg.set);
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
2019-03-22 21:06:57 +00:00
|
|
|
else if (arg && arg.unset) {
|
|
|
|
this.unsetSteps(arg.unset);
|
|
|
|
}
|
2019-03-22 07:22:33 +00:00
|
|
|
else if (arg && arg.loops) {
|
2019-03-24 01:58:22 +00:00
|
|
|
this.setLoops(arg.loops);
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
|
|
|
event.returnValue = true;
|
|
|
|
}
|
2019-03-24 01:58:22 +00:00
|
|
|
setLoops(count) {
|
2019-03-30 00:44:12 +00:00
|
|
|
this.gridLoops = count;
|
2019-03-24 01:58:22 +00:00
|
|
|
this.log.info(`Set loop count to ${count}`);
|
|
|
|
}
|
2019-03-22 08:33:53 +00:00
|
|
|
setSteps(steps) {
|
2019-03-24 01:58:22 +00:00
|
|
|
for (let step of steps) {
|
2019-03-30 00:44:12 +00:00
|
|
|
this.grid[step.x] = step;
|
2019-03-24 01:58:22 +00:00
|
|
|
}
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
2019-03-22 21:06:57 +00:00
|
|
|
unsetSteps(steps) {
|
2019-03-24 01:58:22 +00:00
|
|
|
for (let x of steps) {
|
2019-03-30 00:44:12 +00:00
|
|
|
this.grid[x] = undefined;
|
2019-03-24 01:58:22 +00:00
|
|
|
}
|
2019-03-22 21:06:57 +00:00
|
|
|
}
|
|
|
|
//new, replaces exec and init
|
2019-03-22 07:22:33 +00:00
|
|
|
async start(arg) {
|
|
|
|
if (arg && arg.arr) {
|
2019-03-22 21:06:57 +00:00
|
|
|
this.arr = arg.arr; //overwrite sequence
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
2019-03-30 00:44:12 +00:00
|
|
|
else {
|
|
|
|
this.arr = this.grid;
|
|
|
|
}
|
2019-03-22 07:22:33 +00:00
|
|
|
if (arg && arg.loops) {
|
2019-03-22 21:06:57 +00:00
|
|
|
this.loops = arg.loops; //overwrite loops
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
2019-03-30 00:44:12 +00:00
|
|
|
else {
|
|
|
|
this.loops = this.gridLoops;
|
|
|
|
}
|
2019-03-22 21:06:57 +00:00
|
|
|
this.running = true;
|
|
|
|
this.paused = false;
|
2019-03-24 01:58:22 +00:00
|
|
|
//start sequence
|
|
|
|
this.log.info(`Starting sequence...`);
|
2019-03-31 00:29:01 +00:00
|
|
|
this.ui.send(this.id, { start: true });
|
2019-03-22 07:22:33 +00:00
|
|
|
for (let x = 0; x < this.loops; x++) {
|
2019-03-22 21:06:57 +00:00
|
|
|
//start loop
|
2019-03-24 01:58:22 +00:00
|
|
|
this.log.info(`Starting loop ${x + 1}`);
|
|
|
|
this.ui.send(this.id, { loop: x, start: true });
|
2019-03-22 07:22:33 +00:00
|
|
|
for (let y = 0; y < this.arr.length; y++) {
|
2019-03-22 21:06:57 +00:00
|
|
|
//start step
|
2019-03-24 04:25:16 +00:00
|
|
|
if (!this.running) {
|
2019-03-24 01:58:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-03-24 04:25:16 +00:00
|
|
|
while (this.paused) {
|
|
|
|
await delay(42);
|
|
|
|
}
|
|
|
|
this.log.info(`Starting step ${y + 1} of loop ${x + 1}`);
|
2019-03-31 00:29:01 +00:00
|
|
|
this.ui.send(this.id, { step: y, loop: x, start: true });
|
2019-03-24 04:25:16 +00:00
|
|
|
await this.step(y);
|
2019-03-22 21:06:57 +00:00
|
|
|
//end step
|
2019-03-24 01:58:22 +00:00
|
|
|
this.log.info(`Ended step ${y + 1} of loop ${x + 1}`);
|
2019-03-31 00:29:01 +00:00
|
|
|
this.ui.send(this.id, { step: y, loop: x, stop: true });
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
2019-03-24 04:25:16 +00:00
|
|
|
if (!this.running) {
|
|
|
|
break;
|
|
|
|
}
|
2019-03-22 21:06:57 +00:00
|
|
|
//end loop
|
2019-03-24 01:58:22 +00:00
|
|
|
this.log.info(`Ended loop ${x + 1}`);
|
|
|
|
this.ui.send(this.id, { loop: x, stop: true });
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
2019-03-24 01:58:22 +00:00
|
|
|
//end sequence
|
|
|
|
this.log.info(`Ended sequence`);
|
2019-03-31 00:29:01 +00:00
|
|
|
this.ui.send(this.id, { stop: true });
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
|
|
|
//new
|
|
|
|
pause() {
|
2019-03-22 21:06:57 +00:00
|
|
|
this.paused = true;
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Stop the sequence
|
|
|
|
**/
|
|
|
|
stop() {
|
2019-03-22 01:02:28 +00:00
|
|
|
this.running = false;
|
2019-03-22 07:22:33 +00:00
|
|
|
//clear?
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
2019-03-24 01:58:22 +00:00
|
|
|
async step(x) {
|
2019-03-22 07:22:33 +00:00
|
|
|
try {
|
2019-03-24 01:58:22 +00:00
|
|
|
await this.cmdMap(x);
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2019-03-20 19:30:45 +00:00
|
|
|
}
|
2019-03-24 01:58:22 +00:00
|
|
|
async cmdMap(x) {
|
|
|
|
const cmdOriginal = this.arr[x].cmd;
|
2019-03-22 07:22:33 +00:00
|
|
|
const cmd = this.CMDS[cmdOriginal];
|
2019-03-24 01:58:22 +00:00
|
|
|
this.log.info(`CMD: '${cmdOriginal}' -> ${cmd}`);
|
|
|
|
//I wrote this when I was very tired and delirious
|
|
|
|
return await this.cmd[cmd]();
|
2019-03-20 19:30:45 +00:00
|
|
|
}
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
2019-03-24 01:58:22 +00:00
|
|
|
module.exports = function (cfg, cmd, ui) {
|
|
|
|
return new Sequencer(cfg, cmd, ui);
|
2019-03-20 02:33:15 +00:00
|
|
|
};
|
|
|
|
//# sourceMappingURL=index.js.map
|