2019-03-20 02:33:15 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-03-22 07:22:33 +00:00
|
|
|
import Log = require('log');
|
|
|
|
|
2019-03-22 08:33:53 +00:00
|
|
|
let seq : Sequencer;
|
2019-03-20 02:33:15 +00:00
|
|
|
|
2019-03-22 08:33:53 +00:00
|
|
|
class Sequencer {
|
2019-03-20 02:33:15 +00:00
|
|
|
private time : number;
|
2019-03-22 21:06:57 +00:00
|
|
|
private running : boolean = false;
|
|
|
|
private paused : boolean = false;
|
2019-03-20 02:33:15 +00:00
|
|
|
|
2019-03-22 07:22:33 +00:00
|
|
|
private arr : any[] = [];
|
|
|
|
private loops : number = 1;
|
|
|
|
|
2019-03-20 02:33:15 +00:00
|
|
|
private cfg : any;
|
|
|
|
private cmd : any;
|
2019-03-22 07:22:33 +00:00
|
|
|
private CMDS : any = {};
|
|
|
|
private ipc : any;
|
|
|
|
private log : any;
|
2019-03-22 08:33:53 +00:00
|
|
|
private id : string = 'sequence';
|
2019-03-22 07:22:33 +00:00
|
|
|
|
2019-03-20 02:33:15 +00:00
|
|
|
constructor (cfg : any, cmd : any) {
|
|
|
|
this.cfg = cfg;
|
|
|
|
this.cmd = cmd;
|
2019-03-22 07:22:33 +00:00
|
|
|
this.cmds(cfg.cmd);
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
private cmds (obj : any) {
|
|
|
|
let keys : string[] = Object.keys(obj);
|
|
|
|
let key : string;
|
|
|
|
for (key in keys) {
|
|
|
|
this.CMDS[keys[key]] = key;
|
|
|
|
}
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
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
|
|
|
private async init () {
|
2019-03-22 08:33:53 +00:00
|
|
|
this.log = Log({ label : this.id })
|
2019-03-22 07:22:33 +00:00
|
|
|
this.ipc = require('electron').ipcMain;
|
|
|
|
this.listen();
|
|
|
|
}
|
|
|
|
|
|
|
|
private 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
|
|
|
}
|
2019-03-20 02:33:15 +00:00
|
|
|
|
2019-03-22 07:22:33 +00:00
|
|
|
private async listener (event : any, arg : any) {
|
2019-03-22 08:33:53 +00:00
|
|
|
console.dir(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 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) {
|
|
|
|
this.loops = arg.loops;
|
|
|
|
}
|
|
|
|
event.returnValue = true;
|
|
|
|
}
|
|
|
|
|
2019-03-22 08:33:53 +00:00
|
|
|
public setSteps (steps : any) {
|
|
|
|
console.dir(steps)
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
2019-03-22 21:06:57 +00:00
|
|
|
public unsetSteps (steps : number[]) {
|
|
|
|
|
|
|
|
}
|
|
|
|
//new, replaces exec and init
|
2019-03-22 07:22:33 +00:00
|
|
|
public async start (arg : any) {
|
|
|
|
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
|
|
|
}
|
|
|
|
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-22 21:06:57 +00:00
|
|
|
|
|
|
|
this.running = true;
|
|
|
|
this.paused = false;
|
2019-03-20 02:33:15 +00:00
|
|
|
|
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-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-22 07:22:33 +00:00
|
|
|
if (this.running) {
|
2019-03-22 21:06:57 +00:00
|
|
|
while (this.paused) {
|
|
|
|
await delay(42);
|
|
|
|
}
|
2019-03-22 07:22:33 +00:00
|
|
|
await this.step(y);
|
|
|
|
}
|
2019-03-22 21:06:57 +00:00
|
|
|
//end step
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
2019-03-22 21:06:57 +00:00
|
|
|
//end loop
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
|
|
|
//new
|
|
|
|
public pause () {
|
2019-03-22 21:06:57 +00:00
|
|
|
this.paused = true;
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Stop the sequence
|
|
|
|
**/
|
|
|
|
public stop () {
|
2019-03-22 01:02:28 +00:00
|
|
|
this.running = false;
|
2019-03-22 07:22:33 +00:00
|
|
|
//clear?
|
2019-03-20 19:30:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-22 07:22:33 +00:00
|
|
|
private async step (index : number) {
|
|
|
|
try {
|
|
|
|
await this.cmdMap(index)
|
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2019-03-20 19:30:45 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 07:22:33 +00:00
|
|
|
private async cmdMap (index : number) {
|
|
|
|
const cmdOriginal : string = this.arr[index].cmd;
|
|
|
|
const cmd : string = this.CMDS[cmdOriginal];
|
|
|
|
return await this.cmd[cmd];
|
2019-03-20 19:30:45 +00:00
|
|
|
}
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 07:22:33 +00:00
|
|
|
module.exports = function (cfg : any, cmd : any) {
|
2019-03-22 08:33:53 +00:00
|
|
|
return new Sequencer(cfg, cmd);
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|