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,
|
|
|
|
dir : true,
|
|
|
|
digital : false
|
|
|
|
};
|
2019-03-21 19:00:47 +00:00
|
|
|
private arduino : Arduino = null;
|
|
|
|
private log : any;
|
|
|
|
private cfg : any;
|
|
|
|
private ui : any;
|
|
|
|
private ipc : any;
|
|
|
|
private dig : any;
|
2019-03-21 23:34:56 +00:00
|
|
|
private id : string = 'projector';
|
|
|
|
|
2019-03-21 19:00:47 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2019-03-22 01:02:28 +00:00
|
|
|
constructor (arduino : Arduino, cfg : any, ui : any, dig : any, second : boolean = false) {
|
2019-03-21 19:00:47 +00:00
|
|
|
this.arduino = arduino;
|
|
|
|
this.cfg = cfg;
|
|
|
|
this.ui = ui;
|
|
|
|
this.dig = dig;
|
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-03-21 22:03:53 +00:00
|
|
|
if (this.dig.state.enabled) {
|
|
|
|
this.dig.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-03-21 22:03:53 +00:00
|
|
|
if (this.dig.state.enabled) {
|
2019-03-21 19:00:47 +00:00
|
|
|
try {
|
2019-03-21 22:03:53 +00:00
|
|
|
ms = await this.dig.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
|
|
|
}
|
|
|
|
}
|
|
|
|
this.log.info('Projector 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-03-21 19:00:47 +00:00
|
|
|
this.dig.state.frame = arg.val
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
message = 'Projector second'
|
|
|
|
if (this.state.dir) {
|
|
|
|
message += 'ADVANCED'
|
|
|
|
} else {
|
|
|
|
message += 'REWOUND'
|
|
|
|
}
|
|
|
|
message += ' 1 frame'
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function (arduino : Arduino, cfg : any, ui : any, dig : any) {
|
|
|
|
return new Projector(arduino, cfg, ui, dig);
|
|
|
|
}
|