2019-03-21 18:59:46 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-06-14 15:59:47 +00:00
|
|
|
import { Intval } from 'intval';
|
2019-06-09 01:43:14 +00:00
|
|
|
import { delay } from 'delay';
|
2019-03-21 18:59:46 +00:00
|
|
|
|
|
|
|
/** class representing camera functions **/
|
|
|
|
|
|
|
|
class Camera {
|
2019-03-21 23:34:56 +00:00
|
|
|
private state : any = {
|
|
|
|
pos : 0,
|
|
|
|
dir : true,
|
|
|
|
digital : false
|
|
|
|
};
|
2019-03-21 18:59:46 +00:00
|
|
|
private arduino : Arduino = null;
|
|
|
|
private intval : any = null;
|
|
|
|
private log : any;
|
|
|
|
private cfg : any;
|
2019-06-18 20:47:26 +00:00
|
|
|
private filmout : any;
|
2019-03-21 18:59:46 +00:00
|
|
|
private ui : any;
|
|
|
|
private ipc : any;
|
2019-03-21 23:34:56 +00:00
|
|
|
private id : string = 'camera';
|
2019-03-21 18:59:46 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2019-06-18 20:47:26 +00:00
|
|
|
constructor (arduino : Arduino, cfg : any, ui : any, filmout : any, second : boolean = false) {
|
2019-03-21 18:59:46 +00:00
|
|
|
this.arduino = arduino;
|
|
|
|
this.cfg = cfg;
|
|
|
|
this.ui = ui;
|
2019-03-21 22:03:53 +00:00
|
|
|
this.dig = dig;
|
2019-03-22 01:02:28 +00:00
|
|
|
if (second) this.id += '_second';
|
2019-03-21 18:59:46 +00:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
private async init () {
|
2019-06-14 15:59:47 +00:00
|
|
|
const Log = require('log');
|
2019-03-21 23:34:56 +00:00
|
|
|
this.log = await Log({ label : this.id });
|
2019-03-21 18:59:46 +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 18:59:46 +00:00
|
|
|
this.ipc.on('intval', this.connectIntval.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
public async set (dir : boolean, id : string) {
|
|
|
|
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 18:59:46 +00:00
|
|
|
} else {
|
2019-03-22 01:02:28 +00:00
|
|
|
cmd = this.cfg.arduino.cmd[`${this.id}_backward`];
|
2019-03-21 18:59:46 +00:00
|
|
|
}
|
|
|
|
this.state.dir = dir;
|
|
|
|
|
|
|
|
if (this.intval) {
|
|
|
|
try {
|
|
|
|
ms = await this.intval.setDir(dir);
|
|
|
|
} 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 18:59:46 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return await this.end(cmd, id, ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
public async move (frame : number, id : string) {
|
2019-03-21 23:45:48 +00:00
|
|
|
const cmd : string = this.cfg.arduino.cmd[this.id];
|
2019-03-21 18:59:46 +00:00
|
|
|
let ms : number;
|
2019-03-21 22:03:53 +00:00
|
|
|
if (this.dig.state.enabled) {
|
|
|
|
await this.dig.start()
|
|
|
|
}
|
2019-03-21 18:59:46 +00:00
|
|
|
if (this.intval) {
|
|
|
|
try {
|
|
|
|
ms = await this.intval.move();
|
|
|
|
} 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 18:59:46 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err);
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 22:03:53 +00:00
|
|
|
if (this.dig.state.enabled) {
|
2019-03-22 02:33:30 +00:00
|
|
|
//await delay(100 * 1000);
|
|
|
|
await this.dig.end();
|
2019-03-21 22:03:53 +00:00
|
|
|
}
|
2019-04-04 22:49:07 +00:00
|
|
|
//this.log.info('Camera move time', { ms });
|
2019-03-21 18:59:46 +00:00
|
|
|
return this.end(cmd, id, ms);
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:49:07 +00:00
|
|
|
public async both (frame : any, id : string) {
|
|
|
|
const cmd : string = this.cfg.arduino.cmd[id];
|
|
|
|
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('Cameras move time', { ms });
|
|
|
|
return await this.end(cmd, id, ms)
|
|
|
|
}
|
|
|
|
|
2019-03-21 18:59:46 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
public exposure (exposure : number, id : string) {
|
|
|
|
let cmd : string = 'E';
|
2019-03-21 23:34:56 +00:00
|
|
|
this.intval.setExposure(this.id, exposure, (ms : number) => {
|
2019-03-21 18:59:46 +00:00
|
|
|
this.end(cmd, id, ms);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
private async connectIntval (event : any, arg : any) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (arg.connect) {
|
|
|
|
this.intval = new Intval(arg.url)
|
|
|
|
this.intval.connect((err : any, ms : number, state : boolean) => {
|
|
|
|
if (err) {
|
|
|
|
this.ui.send('intval', { connected : false })
|
|
|
|
this.log.info(`Cannot connect to ${arg.url}`, 'INTVAL')
|
|
|
|
this.intval = null
|
|
|
|
} else {
|
|
|
|
this.ui.send('intval', { connected : true, url : arg.url, state : state })
|
|
|
|
this.log.info(`Connected to INTVAL3 @ ${arg.url}`, 'INTVAL')
|
|
|
|
}
|
|
|
|
return resolve(true)
|
|
|
|
})
|
|
|
|
} else if (arg.disconnect) {
|
|
|
|
this.intval = null
|
|
|
|
return resolve(false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
private async listener (event : any, arg : any) {
|
|
|
|
if (typeof arg.dir !== 'undefined') {
|
|
|
|
try {
|
2019-03-21 23:34:56 +00:00
|
|
|
await this.set(arg.dir, arg.id);
|
2019-03-21 18:59:46 +00:00
|
|
|
} catch (err) {
|
2019-03-21 23:34:56 +00:00
|
|
|
this.log.error(err);
|
2019-03-21 18:59:46 +00:00
|
|
|
}
|
|
|
|
} else if (typeof arg.frame !== 'undefined') {
|
|
|
|
try {
|
2019-03-21 23:34:56 +00:00
|
|
|
await this.move(arg.frame, arg.id);
|
2019-03-21 18:59:46 +00:00
|
|
|
} catch (err) {
|
2019-03-21 23:34:56 +00:00
|
|
|
this.log.error(err);
|
2019-03-21 18:59:46 +00:00
|
|
|
}
|
2019-03-21 23:34:56 +00:00
|
|
|
} else if (typeof arg.val !== 'undefined') {
|
|
|
|
this.state.pos = arg.val;
|
2019-03-21 18:59:46 +00:00
|
|
|
}
|
2019-03-21 23:34:56 +00:00
|
|
|
event.returnValue = true;
|
2019-03-21 18:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
private async end (cmd : string, id : string, ms : number) {
|
2019-03-21 23:34:56 +00:00
|
|
|
let message = '';
|
2019-03-22 01:02:28 +00:00
|
|
|
if (cmd === this.cfg.arduino.cmd.camera_forward) {
|
2019-03-21 23:34:56 +00:00
|
|
|
message = 'Camera set to FORWARD';
|
2019-03-22 01:02:28 +00:00
|
|
|
} else if (cmd === this.cfg.arduino.cmd.camera_backward) {
|
2019-03-21 23:34:56 +00:00
|
|
|
message = 'Camera set to BACKWARD';
|
2019-03-22 01:02:28 +00:00
|
|
|
} else if (cmd === this.cfg.arduino.cmd.camera_second_forward) {
|
|
|
|
message = 'Camera second set to FORWARD';
|
|
|
|
} else if (cmd === this.cfg.arduino.cmd.camera_second_backward) {
|
|
|
|
message = 'Camera second set to BACKWARD';
|
2019-03-21 18:59:46 +00:00
|
|
|
} else if (cmd === this.cfg.arduino.cmd.camera) {
|
2019-03-21 23:34:56 +00:00
|
|
|
message = 'Camera ';
|
2019-03-21 18:59:46 +00:00
|
|
|
if (this.state.dir) {
|
2019-03-21 23:34:56 +00:00
|
|
|
message += 'ADVANCED';
|
2019-03-21 18:59:46 +00:00
|
|
|
} else {
|
2019-03-21 23:34:56 +00:00
|
|
|
message += 'REWOUND';
|
2019-03-21 18:59:46 +00:00
|
|
|
}
|
2019-03-21 23:34:56 +00:00
|
|
|
message += ' 1 frame';
|
2019-03-22 01:02:28 +00:00
|
|
|
} else if (cmd === this.cfg.arduino.cmd.camera_second) {
|
|
|
|
message = 'Camera second ';
|
|
|
|
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.camerass) {
|
|
|
|
message += 'Cameras both MOVED 1 frame each';
|
2019-03-21 18:59:46 +00:00
|
|
|
}
|
2019-04-04 22:49:07 +00:00
|
|
|
message += ` ${ms}ms`
|
2019-03-22 01:02:28 +00:00
|
|
|
this.log.info(message);
|
|
|
|
this.ui.send(this.id, {cmd: cmd, id : id, ms: ms});
|
2019-03-21 22:03:53 +00:00
|
|
|
}
|
2019-03-21 18:59:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 22:49:07 +00:00
|
|
|
module.exports = function (arduino : Arduino, cfg : any, ui : any, dig : any, second : boolean) {
|
|
|
|
return new Camera(arduino, cfg, ui, dig, second);
|
2019-03-21 18:59:46 +00:00
|
|
|
}
|