2019-03-21 19:00:47 +00:00
|
|
|
/** class representing the Projector features **/
|
|
|
|
|
2019-03-21 22:03:53 +00:00
|
|
|
import Log = require('log');
|
|
|
|
|
2019-03-21 19:00:47 +00:00
|
|
|
class Projector {
|
2019-03-21 23:34:56 +00:00
|
|
|
private state : any = {
|
|
|
|
pos : 0,
|
2019-06-18 20:52:26 +00:00
|
|
|
dir : true
|
2019-03-21 23:34:56 +00:00
|
|
|
};
|
2019-03-21 19:00:47 +00:00
|
|
|
private arduino : Arduino = null;
|
|
|
|
private log : any;
|
|
|
|
private cfg : any;
|
|
|
|
private ui : any;
|
|
|
|
private ipc : any;
|
2019-06-18 20:52:26 +00:00
|
|
|
private filmout : any;
|
2019-03-21 23:34:56 +00:00
|
|
|
private id : string = 'projector';
|
|
|
|
|
2019-03-21 19:00:47 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2019-06-18 20:52:26 +00:00
|
|
|
constructor (arduino : Arduino, cfg : any, ui : any, filmout : any, second : boolean = false) {
|
2019-03-21 19:00:47 +00:00
|
|
|
this.arduino = arduino;
|
|
|
|
this.cfg = cfg;
|
|
|
|
this.ui = ui;
|
2019-06-18 20:52:26 +00:00
|
|
|
this.filmout = filmout;
|
2019-03-22 01:02:28 +00:00
|
|
|
if (second) this.id += '_second';
|
2019-03-21 19:00:47 +00:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2019-03-21 23:34:56 +00:00
|
|
|
private async init () {
|
2019-03-21 23:45:48 +00:00
|
|
|
this.log = await Log({ label : this.id })
|
2019-03-21 19:00:47 +00:00
|
|
|
this.ipc = require('electron').ipcMain;
|
|
|
|
this.listen();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
private listen () {
|
2019-03-21 23:34:56 +00:00
|
|
|
this.ipc.on(this.id, this.listener.bind(this));
|
2019-03-21 19:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2019-03-21 23:34:56 +00:00
|
|
|
public async set (dir : boolean, id : string) {
|
2019-03-21 19:00:47 +00:00
|
|
|
let cmd : string;
|
|
|
|
let ms : number;
|
|
|
|
if (dir) {
|
2019-03-22 01:02:28 +00:00
|
|
|
cmd = this.cfg.arduino.cmd[`${this.id}_forward`]
|
2019-03-21 19:00:47 +00:00
|
|
|
} else {
|
2019-03-22 01:02:28 +00:00
|
|
|
cmd = this.cfg.arduino.cmd[`${this.id}_backward`]
|
2019-03-21 19:00:47 +00:00
|
|
|
}
|
|
|
|
this.state.dir = dir
|
2019-06-18 20:52:26 +00:00
|
|
|
if (this.filmout.state.enabled) {
|
|
|
|
this.filmout.set(dir)
|
2019-03-21 19:00:47 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2019-03-21 23:34:56 +00:00
|
|
|
ms = await this.arduino.send(this.id, cmd)
|
2019-03-21 19:00:47 +00:00
|
|
|
} catch (err) {
|
2019-03-22 01:02:28 +00:00
|
|
|
this.log.error(`Error setting ${this.id} direction`, err)
|
2019-03-21 19:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return await this.end(cmd, id, ms)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2019-03-21 23:34:56 +00:00
|
|
|
public async move (frame : any, id : string) {
|
2019-03-21 23:45:48 +00:00
|
|
|
const cmd : string = this.cfg.arduino.cmd[this.id];
|
2019-03-21 19:00:47 +00:00
|
|
|
let ms : number;
|
2019-06-18 20:52:26 +00:00
|
|
|
if (this.filmout.state.enabled) {
|
2019-03-21 19:00:47 +00:00
|
|
|
try {
|
2019-06-18 20:52:26 +00:00
|
|
|
ms = await this.filmout.move()
|
2019-03-21 19:00:47 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
2019-03-21 23:34:56 +00:00
|
|
|
ms = await this.arduino.send(this.id, cmd)
|
2019-03-21 19:00:47 +00:00
|
|
|
} catch (err) {
|
2019-03-22 01:02:28 +00:00
|
|
|
this.log.error(`Error moving ${this.id}`, err)
|
2019-03-21 19:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 22:49:07 +00:00
|
|
|
//this.log.info('Projector move time', { ms });
|
|
|
|
return await this.end(cmd, id, ms)
|
|
|
|
}
|
|
|
|
|
|
|
|
public async both (frame : any, id : string) {
|
2019-04-20 14:15:28 +00:00
|
|
|
const cmd : string = this.cfg.arduino.cmd[this.id + 's'];
|
2019-04-04 22:49:07 +00:00
|
|
|
let ms : number;
|
|
|
|
try {
|
|
|
|
ms = await this.arduino.send(this.id, cmd)
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error(`Error moving ${this.id}`, err)
|
|
|
|
}
|
|
|
|
//this.log.info('Projectors move time', { ms });
|
2019-03-21 23:45:48 +00:00
|
|
|
return await this.end(cmd, id, ms)
|
2019-03-21 19:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
private async listener (event : any, arg : any){
|
|
|
|
if (typeof arg.dir !== 'undefined') {
|
|
|
|
try {
|
|
|
|
await this.set(arg.dir, arg.id)
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err)
|
|
|
|
}
|
|
|
|
} else if (typeof arg.frame !== 'undefined') {
|
|
|
|
try {
|
|
|
|
await this.move(arg.frame, arg.id)
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err)
|
|
|
|
}
|
|
|
|
} else if (typeof arg.val !== 'undefined') {
|
2019-03-21 23:34:56 +00:00
|
|
|
this.state.pos = arg.val;
|
2019-06-18 20:52:26 +00:00
|
|
|
this.filmout.state.frame = arg.val
|
2019-03-21 19:00:47 +00:00
|
|
|
}
|
|
|
|
event.returnValue = true
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
async end (cmd : string, id : string, ms : number) {
|
|
|
|
let message : string = '';
|
2019-03-22 01:02:28 +00:00
|
|
|
if (cmd === this.cfg.arduino.cmd.projector_forward) {
|
2019-03-21 19:00:47 +00:00
|
|
|
message = 'Projector set to FORWARD'
|
2019-03-22 01:02:28 +00:00
|
|
|
} else if (cmd === this.cfg.arduino.cmd.projector_backward) {
|
2019-03-21 19:00:47 +00:00
|
|
|
message = 'Projector set to BACKWARD'
|
2019-03-22 01:02:28 +00:00
|
|
|
} else if (cmd === this.cfg.arduino.cmd.projector_second_forward) {
|
|
|
|
message = 'Projector second set to FORWARD'
|
|
|
|
} else if (cmd === this.cfg.arduino.cmd.projector_second_backward) {
|
|
|
|
message = 'Projector second set to BACKWARD'
|
2019-03-21 19:00:47 +00:00
|
|
|
} else if (cmd === this.cfg.arduino.cmd.projector) {
|
|
|
|
message = 'Projector '
|
|
|
|
if (this.state.dir) {
|
|
|
|
message += 'ADVANCED'
|
|
|
|
} else {
|
|
|
|
message += 'REWOUND'
|
|
|
|
}
|
|
|
|
message += ' 1 frame'
|
2019-03-22 01:02:28 +00:00
|
|
|
} else if (cmd === this.cfg.arduino.cmd.projector_second) {
|
2019-04-04 22:49:07 +00:00
|
|
|
message = 'Projector second '
|
2019-03-22 01:02:28 +00:00
|
|
|
if (this.state.dir) {
|
|
|
|
message += 'ADVANCED'
|
|
|
|
} else {
|
|
|
|
message += 'REWOUND'
|
|
|
|
}
|
|
|
|
message += ' 1 frame'
|
2019-04-04 22:49:07 +00:00
|
|
|
} else if (cmd === this.cfg.arduino.cmd.projectors) {
|
|
|
|
message += 'Projectors both MOVED 1 frame each';
|
2019-03-21 19:00:47 +00:00
|
|
|
}
|
2019-04-04 22:49:07 +00:00
|
|
|
message += ` ${ms}ms`
|
2019-03-21 19:00:47 +00:00
|
|
|
this.log.info(message, 'PROJECTOR')
|
2019-03-21 23:34:56 +00:00
|
|
|
return await this.ui.send(this.id, {cmd: cmd, id : id, ms: ms})
|
2019-03-21 19:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 20:52:26 +00:00
|
|
|
module.exports = function (arduino : Arduino, cfg : any, ui : any, filmout : any, second : boolean) {
|
|
|
|
return new Projector(arduino, cfg, ui, filmout, second);
|
2019-03-21 19:00:47 +00:00
|
|
|
}
|