2019-03-20 02:33:15 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-03-24 01:58:22 +00:00
|
|
|
import uuid from 'uuid/v4';
|
2019-03-22 07:22:33 +00:00
|
|
|
import Log = require('log');
|
2019-10-04 18:36:26 +00:00
|
|
|
import { powerSaveBlocker } from 'electron'
|
|
|
|
|
2019-03-22 07:22:33 +00:00
|
|
|
|
2019-07-26 23:03:42 +00:00
|
|
|
/** @module lib/sequencer **/
|
|
|
|
|
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-22 21:06:57 +00:00
|
|
|
private running : boolean = false;
|
|
|
|
private paused : boolean = false;
|
2019-03-20 02:33:15 +00:00
|
|
|
|
2019-03-30 00:44:12 +00:00
|
|
|
private grid : any[] = [];
|
|
|
|
private gridLoops : number = 1;
|
|
|
|
|
|
|
|
private arr : any[] = []; //store sequence from gui
|
2019-03-22 07:22:33 +00:00
|
|
|
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;
|
2019-03-24 01:58:22 +00:00
|
|
|
private ui : any;
|
2019-03-22 07:22:33 +00:00
|
|
|
private log : any;
|
2019-03-22 08:33:53 +00:00
|
|
|
private id : string = 'sequence';
|
2019-07-26 23:03:42 +00:00
|
|
|
private alerted : boolean = false;
|
2019-10-04 18:36:26 +00:00
|
|
|
private psbId : any;
|
2019-07-26 23:03:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* Create a new sequencer and assign command and UI as private sub-classes
|
|
|
|
*
|
|
|
|
* @param {object} cfg Configuration object
|
|
|
|
* @param {object} cmd Shared command class
|
|
|
|
* @param {object} ui Electron UI, browser window
|
|
|
|
**/
|
2019-03-22 07:22:33 +00:00
|
|
|
|
2019-03-24 01:58:22 +00:00
|
|
|
constructor (cfg : any, cmd : any, ui : any) {
|
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();
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:03:42 +00:00
|
|
|
/**
|
|
|
|
* Take configuration object and assign all commands as keys
|
|
|
|
* in the internal CMDS object.
|
|
|
|
*
|
|
|
|
* @param {object} obj Configuration object
|
|
|
|
**/
|
2019-03-22 07:22:33 +00:00
|
|
|
private cmds (obj : any) {
|
|
|
|
let keys : string[] = 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-07-26 23:03:42 +00:00
|
|
|
//
|
|
|
|
//
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
2019-03-22 07:22:33 +00:00
|
|
|
|
2019-07-26 23:03:42 +00:00
|
|
|
/**
|
|
|
|
* Initialize the class by requiring ipcMain from electron
|
|
|
|
* and creating logger.
|
|
|
|
*
|
|
|
|
**/
|
2019-03-22 07:22:33 +00:00
|
|
|
private 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();
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:03:42 +00:00
|
|
|
/**
|
|
|
|
* Bind ipc listener to channel 'sequencer' or current id of
|
|
|
|
* class.
|
|
|
|
**/
|
2019-03-22 07:22:33 +00:00
|
|
|
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-07-26 23:03:42 +00:00
|
|
|
/**
|
|
|
|
* Listener callback function. Called whenever ipc
|
|
|
|
* message is sent to channel 'sequencer'.
|
|
|
|
*
|
|
|
|
* @param {object} event IPC message event
|
|
|
|
* @param {object} arg Arguments provided in message
|
|
|
|
**/
|
2019-03-22 07:22:33 +00:00
|
|
|
private async listener (event : any, arg : any) {
|
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) {
|
2019-03-24 01:58:22 +00:00
|
|
|
this.setLoops(arg.loops);
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
|
|
|
event.returnValue = true;
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:03:42 +00:00
|
|
|
/**
|
|
|
|
* Sets the value of the loops in the grid sequence
|
|
|
|
* to value sent by UI in ipc message.
|
|
|
|
*
|
|
|
|
* @param {integer} count Number of loops to set grid sequence to
|
|
|
|
**/
|
2019-03-24 01:58:22 +00:00
|
|
|
public setLoops (count : number) {
|
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-20 02:33:15 +00:00
|
|
|
}
|
2019-03-22 21:06:57 +00:00
|
|
|
|
2019-07-26 23:03:42 +00:00
|
|
|
/**
|
|
|
|
* Sets multiple steps at once
|
|
|
|
*
|
|
|
|
* @param {array} steps Array of steps to set or update
|
|
|
|
**/
|
2019-03-24 01:58:22 +00:00
|
|
|
public setSteps (steps : any[]) {
|
|
|
|
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-22 21:06:57 +00:00
|
|
|
}
|
2019-03-24 01:58:22 +00:00
|
|
|
|
2019-07-26 23:03:42 +00:00
|
|
|
/**
|
|
|
|
* Resets multiple steps to default 'undefined' state
|
|
|
|
*
|
|
|
|
* @param {array} steps Array containing the x location of steps to unset
|
|
|
|
**/
|
2019-03-24 01:58:22 +00:00
|
|
|
public unsetSteps (steps : number[]) {
|
|
|
|
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-07-26 23:03:42 +00:00
|
|
|
/**
|
|
|
|
* Starts a sequence with the existing grid sequence,
|
|
|
|
* or if one is provided in the arg object, starts
|
|
|
|
* that sequence.
|
|
|
|
*
|
|
|
|
* @param {object} arg Arguments from ipc message
|
|
|
|
**/
|
2019-03-22 07:22:33 +00:00
|
|
|
public async start (arg : any) {
|
2019-05-28 21:48:42 +00:00
|
|
|
let startTime : number = +new Date();
|
|
|
|
let ms : number;
|
|
|
|
|
2019-10-04 18:36:26 +00:00
|
|
|
this.psbId = powerSaveBlocker.start('prevent-display-sleep');
|
|
|
|
|
2019-03-22 07:22:33 +00:00
|
|
|
if (arg && arg.arr) {
|
2019-03-22 21:06:57 +00:00
|
|
|
this.arr = arg.arr; //overwrite sequence
|
2019-03-30 00:44:12 +00:00
|
|
|
} else {
|
|
|
|
this.arr = this.grid;
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
2019-03-31 00:29:01 +00:00
|
|
|
|
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-30 00:44:12 +00:00
|
|
|
} else {
|
|
|
|
this.loops = this.gridLoops;
|
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-24 01:58:22 +00:00
|
|
|
//start sequence
|
2019-07-26 23:03:42 +00:00
|
|
|
this.log.info(`Starting sequence...`);
|
|
|
|
this.ui.send(this.id, { start : true });
|
2019-03-24 01:58:22 +00:00
|
|
|
|
2019-06-18 20:54:08 +00:00
|
|
|
if (this.cmd.proj.filmout.state.enabled === true) {
|
2019-07-26 23:03:42 +00:00
|
|
|
await this.cmd.proj.filmout.display.open();
|
2019-06-15 15:06:57 +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-07-26 23:03:42 +00:00
|
|
|
this.log.info(`Starting loop ${x + 1}`);
|
2019-03-24 01:58:22 +00:00
|
|
|
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-07-26 23:03:42 +00:00
|
|
|
break;
|
2019-03-24 04:25:16 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:03:42 +00:00
|
|
|
//UI initiates pause, not planned
|
2019-03-24 04:25:16 +00:00
|
|
|
while (this.paused) {
|
|
|
|
await delay(42);
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:49:07 +00:00
|
|
|
if (typeof this.arr[y] === 'undefined') {
|
2019-07-26 23:03:42 +00:00
|
|
|
continue;
|
2019-04-04 22:49:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:03:42 +00:00
|
|
|
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-24 01:58:22 +00:00
|
|
|
|
2019-03-22 21:06:57 +00:00
|
|
|
//end step
|
2019-07-26 23:03:42 +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) {
|
2019-07-26 23:03:42 +00:00
|
|
|
break;
|
2019-03-24 04:25:16 +00:00
|
|
|
}
|
2019-03-22 21:06:57 +00:00
|
|
|
//end loop
|
2019-07-26 23:03:42 +00:00
|
|
|
this.log.info(`Ended loop ${x + 1}`);
|
2019-03-24 01:58:22 +00:00
|
|
|
this.ui.send(this.id, { loop : x, stop : true });
|
2019-03-22 07:22:33 +00:00
|
|
|
}
|
2019-05-28 21:48:42 +00:00
|
|
|
|
2019-06-18 20:54:08 +00:00
|
|
|
if (this.cmd.proj.filmout.state.enabled === true) {
|
|
|
|
await this.cmd.proj.filmout.display.close();
|
2019-06-15 15:06:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 18:36:26 +00:00
|
|
|
powerSaveBlocker.stop(this.psbId)
|
|
|
|
this.psbId = null
|
|
|
|
|
2019-05-28 21:48:42 +00:00
|
|
|
ms = ( +new Date() ) - startTime;
|
2019-03-24 01:58:22 +00:00
|
|
|
//end sequence
|
2019-07-26 23:03:42 +00:00
|
|
|
this.log.info(`Ended sequence`);
|
|
|
|
this.ui.send(this.id, { stop : true, ms });
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
2019-03-24 01:58:22 +00:00
|
|
|
|
2019-07-26 23:03:42 +00:00
|
|
|
/**
|
|
|
|
* Pauses sequence from UI.
|
|
|
|
**/
|
2019-03-20 02:33:15 +00:00
|
|
|
public pause () {
|
2019-03-22 21:06:57 +00:00
|
|
|
this.paused = true;
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|
2019-03-24 01:58:22 +00:00
|
|
|
|
2019-03-20 02:33:15 +00:00
|
|
|
/**
|
2019-07-26 23:03:42 +00:00
|
|
|
* Stops the sequence
|
2019-03-20 02:33:15 +00:00
|
|
|
**/
|
|
|
|
public stop () {
|
2019-06-18 20:54:08 +00:00
|
|
|
if (this.cmd.proj.filmout.state.enabled === true) {
|
|
|
|
this.cmd.proj.filmout.display.close();
|
2019-06-15 15:06:57 +00:00
|
|
|
}
|
2019-03-22 01:02:28 +00:00
|
|
|
this.running = false;
|
2019-10-04 18:36:26 +00:00
|
|
|
if (this.psbId) {
|
|
|
|
powerSaveBlocker.stop(this.psbId);
|
|
|
|
}
|
2019-03-22 07:22:33 +00:00
|
|
|
//clear?
|
2019-03-20 19:30:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:03:42 +00:00
|
|
|
/**
|
|
|
|
* Execute command @ step x. Wrapper with try catch.
|
|
|
|
*
|
|
|
|
* @param {integer} x Step to execute command at
|
|
|
|
**/
|
|
|
|
private async step ( x: number) {
|
2019-03-22 07:22:33 +00:00
|
|
|
try {
|
2019-07-26 23:03:42 +00:00
|
|
|
await this.cmdExec(x)
|
2019-03-22 07:22:33 +00:00
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2019-03-20 19:30:45 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:03:42 +00:00
|
|
|
/**
|
|
|
|
* Locate step @ position x and execute the command.
|
|
|
|
*
|
|
|
|
* @param {integer} x Step to execute command at
|
|
|
|
**/
|
|
|
|
private async cmdExec (x : number) {
|
2019-03-24 01:58:22 +00:00
|
|
|
const cmdOriginal : string = this.arr[x].cmd;
|
2019-03-22 07:22:33 +00:00
|
|
|
const cmd : string = 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 : any, cmd : any, ui : any) {
|
|
|
|
return new Sequencer(cfg, cmd, ui);
|
2019-03-20 02:33:15 +00:00
|
|
|
}
|