Refactor cam module into cam module.

This commit is contained in:
mmcwilliams 2019-03-21 14:59:46 -04:00
parent c276525d3a
commit 56aba1eaba
12 changed files with 818 additions and 1 deletions

49
app/lib/cam/Readme.md Normal file
View File

@ -0,0 +1,49 @@
<a name="Camera"></a>
## Camera
class representing camera functions
**Kind**: global class
* [Camera](#Camera)
* [.init()](#Camera+init)
* [.listen()](#Camera+listen)
* [.set()](#Camera+set)
* [.move()](#Camera+move)
* [.exposure()](#Camera+exposure)
* [.connectIntval()](#Camera+connectIntval)
* [.listener()](#Camera+listener)
* [.end()](#Camera+end)
<a name="Camera+init"></a>
### camera.init()
**Kind**: instance method of [<code>Camera</code>](#Camera)
<a name="Camera+listen"></a>
### camera.listen()
**Kind**: instance method of [<code>Camera</code>](#Camera)
<a name="Camera+set"></a>
### camera.set()
**Kind**: instance method of [<code>Camera</code>](#Camera)
<a name="Camera+move"></a>
### camera.move()
**Kind**: instance method of [<code>Camera</code>](#Camera)
<a name="Camera+exposure"></a>
### camera.exposure()
**Kind**: instance method of [<code>Camera</code>](#Camera)
<a name="Camera+connectIntval"></a>
### camera.connectIntval()
**Kind**: instance method of [<code>Camera</code>](#Camera)
<a name="Camera+listener"></a>
### camera.listener()
**Kind**: instance method of [<code>Camera</code>](#Camera)
<a name="Camera+end"></a>
### camera.end()
**Kind**: instance method of [<code>Camera</code>](#Camera)

183
app/lib/cam/index.js Normal file
View File

@ -0,0 +1,183 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const Intval = require("intval");
/** class representing camera functions **/
class Camera {
/**
*
**/
constructor(arduino, cfg, ui) {
this.state = { dir: true, digital: false };
this.arduino = null;
this.intval = null;
this.arduino = arduino;
this.cfg = cfg;
this.ui = ui;
this.init();
}
/**
*
**/
async init() {
this.log = await require('log')({});
this.ipc = require('electron').ipcMain;
this.listen();
}
/**
*
**/
listen() {
this.ipc.on('cam', this.listener.bind(this));
this.ipc.on('intval', this.connectIntval.bind(this));
}
/**
*
**/
async set(dir, id) {
let cmd;
let ms;
if (dir) {
cmd = this.cfg.arduino.cmd.cam_forward;
}
else {
cmd = this.cfg.arduino.cmd.cam_backward;
}
this.state.dir = dir;
if (this.intval) {
try {
ms = await this.intval.setDir(dir);
}
catch (err) {
this.log.error(err);
}
}
else {
try {
ms = await this.arduino.send('camera', cmd);
}
catch (err) {
this.log.error(err);
}
}
return await this.end(cmd, id, ms);
}
/**
*
**/
async move(frame, id) {
const cmd = this.cfg.arduino.cmd.camera;
let ms;
//if (this.state.digital) {
//await this.dig.start()
//}
if (this.intval) {
try {
ms = await this.intval.move();
}
catch (err) {
this.log.error(err);
}
}
else {
try {
ms = await this.arduino.send('camera', cmd);
}
catch (err) {
this.log.error(err);
}
}
//if (this.state.digital) {
// await this.dig.end()
//}
this.log.info('Camera move time', { ms });
return this.end(cmd, id, ms);
}
/**
*
**/
exposure(exposure, id) {
let cmd = 'E';
this.intval.setExposure('camera', exposure, (ms) => {
this.end(cmd, id, ms);
});
}
/**
*
**/
async connectIntval(event, arg) {
return new Promise((resolve, reject) => {
if (arg.connect) {
this.intval = new Intval(arg.url);
this.intval.connect((err, ms, state) => {
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);
}
});
}
/**
*
**/
async listener(event, arg) {
if (typeof arg.dir !== 'undefined') {
try {
await this.set(arg.dir, arg.id);
}
catch (err) {
console.trace();
this.log.error(err);
}
}
else if (typeof arg.frame !== 'undefined') {
try {
await this.move(arg.frame, arg.id);
}
catch (err) {
console.trace();
this.log.error(err);
}
}
event.returnValue = true;
}
/**
*
**/
async end(cmd, id, ms) {
let message = '';
if (cmd === this.cfg.arduino.cmd.cam_forward) {
message = 'Camera set to FORWARD';
}
else if (cmd === this.cfg.arduino.cmd.cam_backward) {
message = 'Camera set to BACKWARD';
}
else if (cmd === this.cfg.arduino.cmd.camera) {
message = 'Camera ';
if (this.state.dir) {
message += 'ADVANCED';
}
else {
message += 'REWOUND';
}
message += ' 1 frame';
}
this.log.info(message, 'CAMERA', true, true);
this.ui.send('cam', { cmd: cmd, id: id, ms: ms });
}
;
}
module.exports = function (arduino, cfg, ui) {
return new Camera(arduino, cfg, ui);
};
//# sourceMappingURL=index.js.map

1
app/lib/cam/index.js.map Normal file

File diff suppressed because one or more lines are too long

11
app/lib/cam/package.json Normal file
View File

@ -0,0 +1,11 @@
{
"name": "cam",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

View File

@ -1,4 +1,4 @@
var cam = {};
const cam = {};
cam.queue = {};
cam.lock = false;

183
cli/lib/cam/index.js Normal file
View File

@ -0,0 +1,183 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const Intval = require("intval");
/** class representing camera functions **/
class Camera {
/**
*
**/
constructor(arduino, cfg, ui) {
this.state = { dir: true, digital: false };
this.arduino = null;
this.intval = null;
this.arduino = arduino;
this.cfg = cfg;
this.ui = ui;
this.init();
}
/**
*
**/
async init() {
this.log = await require('log')({});
this.ipc = require('electron').ipcMain;
this.listen();
}
/**
*
**/
listen() {
this.ipc.on('cam', this.listener.bind(this));
this.ipc.on('intval', this.connectIntval.bind(this));
}
/**
*
**/
async set(dir, id) {
let cmd;
let ms;
if (dir) {
cmd = this.cfg.arduino.cmd.cam_forward;
}
else {
cmd = this.cfg.arduino.cmd.cam_backward;
}
this.state.dir = dir;
if (this.intval) {
try {
ms = await this.intval.setDir(dir);
}
catch (err) {
this.log.error(err);
}
}
else {
try {
ms = await this.arduino.send('camera', cmd);
}
catch (err) {
this.log.error(err);
}
}
return await this.end(cmd, id, ms);
}
/**
*
**/
async move(frame, id) {
const cmd = this.cfg.arduino.cmd.camera;
let ms;
//if (this.state.digital) {
//await this.dig.start()
//}
if (this.intval) {
try {
ms = await this.intval.move();
}
catch (err) {
this.log.error(err);
}
}
else {
try {
ms = await this.arduino.send('camera', cmd);
}
catch (err) {
this.log.error(err);
}
}
//if (this.state.digital) {
// await this.dig.end()
//}
this.log.info('Camera move time', { ms });
return this.end(cmd, id, ms);
}
/**
*
**/
exposure(exposure, id) {
let cmd = 'E';
this.intval.setExposure('camera', exposure, (ms) => {
this.end(cmd, id, ms);
});
}
/**
*
**/
async connectIntval(event, arg) {
return new Promise((resolve, reject) => {
if (arg.connect) {
this.intval = new Intval(arg.url);
this.intval.connect((err, ms, state) => {
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);
}
});
}
/**
*
**/
async listener(event, arg) {
if (typeof arg.dir !== 'undefined') {
try {
await this.set(arg.dir, arg.id);
}
catch (err) {
console.trace();
this.log.error(err);
}
}
else if (typeof arg.frame !== 'undefined') {
try {
await this.move(arg.frame, arg.id);
}
catch (err) {
console.trace();
this.log.error(err);
}
}
event.returnValue = true;
}
/**
*
**/
async end(cmd, id, ms) {
let message = '';
if (cmd === this.cfg.arduino.cmd.cam_forward) {
message = 'Camera set to FORWARD';
}
else if (cmd === this.cfg.arduino.cmd.cam_backward) {
message = 'Camera set to BACKWARD';
}
else if (cmd === this.cfg.arduino.cmd.camera) {
message = 'Camera ';
if (this.state.dir) {
message += 'ADVANCED';
}
else {
message += 'REWOUND';
}
message += ' 1 frame';
}
this.log.info(message, 'CAMERA', true, true);
this.ui.send('cam', { cmd: cmd, id: id, ms: ms });
}
;
}
module.exports = function (arduino, cfg, ui) {
return new Camera(arduino, cfg, ui);
};
//# sourceMappingURL=index.js.map

1
cli/lib/cam/index.js.map Normal file

File diff suppressed because one or more lines are too long

11
cli/lib/cam/package.json Normal file
View File

@ -0,0 +1,11 @@
{
"name": "cam",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

183
lib/cam/index.js Normal file
View File

@ -0,0 +1,183 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const Intval = require("intval");
/** class representing camera functions **/
class Camera {
/**
*
**/
constructor(arduino, cfg, ui) {
this.state = { dir: true, digital: false };
this.arduino = null;
this.intval = null;
this.arduino = arduino;
this.cfg = cfg;
this.ui = ui;
this.init();
}
/**
*
**/
async init() {
this.log = await require('log')({});
this.ipc = require('electron').ipcMain;
this.listen();
}
/**
*
**/
listen() {
this.ipc.on('cam', this.listener.bind(this));
this.ipc.on('intval', this.connectIntval.bind(this));
}
/**
*
**/
async set(dir, id) {
let cmd;
let ms;
if (dir) {
cmd = this.cfg.arduino.cmd.cam_forward;
}
else {
cmd = this.cfg.arduino.cmd.cam_backward;
}
this.state.dir = dir;
if (this.intval) {
try {
ms = await this.intval.setDir(dir);
}
catch (err) {
this.log.error(err);
}
}
else {
try {
ms = await this.arduino.send('camera', cmd);
}
catch (err) {
this.log.error(err);
}
}
return await this.end(cmd, id, ms);
}
/**
*
**/
async move(frame, id) {
const cmd = this.cfg.arduino.cmd.camera;
let ms;
//if (this.state.digital) {
//await this.dig.start()
//}
if (this.intval) {
try {
ms = await this.intval.move();
}
catch (err) {
this.log.error(err);
}
}
else {
try {
ms = await this.arduino.send('camera', cmd);
}
catch (err) {
this.log.error(err);
}
}
//if (this.state.digital) {
// await this.dig.end()
//}
this.log.info('Camera move time', { ms });
return this.end(cmd, id, ms);
}
/**
*
**/
exposure(exposure, id) {
let cmd = 'E';
this.intval.setExposure('camera', exposure, (ms) => {
this.end(cmd, id, ms);
});
}
/**
*
**/
async connectIntval(event, arg) {
return new Promise((resolve, reject) => {
if (arg.connect) {
this.intval = new Intval(arg.url);
this.intval.connect((err, ms, state) => {
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);
}
});
}
/**
*
**/
async listener(event, arg) {
if (typeof arg.dir !== 'undefined') {
try {
await this.set(arg.dir, arg.id);
}
catch (err) {
console.trace();
this.log.error(err);
}
}
else if (typeof arg.frame !== 'undefined') {
try {
await this.move(arg.frame, arg.id);
}
catch (err) {
console.trace();
this.log.error(err);
}
}
event.returnValue = true;
}
/**
*
**/
async end(cmd, id, ms) {
let message = '';
if (cmd === this.cfg.arduino.cmd.cam_forward) {
message = 'Camera set to FORWARD';
}
else if (cmd === this.cfg.arduino.cmd.cam_backward) {
message = 'Camera set to BACKWARD';
}
else if (cmd === this.cfg.arduino.cmd.camera) {
message = 'Camera ';
if (this.state.dir) {
message += 'ADVANCED';
}
else {
message += 'REWOUND';
}
message += ' 1 frame';
}
this.log.info(message, 'CAMERA', true, true);
this.ui.send('cam', { cmd: cmd, id: id, ms: ms });
}
;
}
module.exports = function (arduino, cfg, ui) {
return new Camera(arduino, cfg, ui);
};
//# sourceMappingURL=index.js.map

1
lib/cam/index.js.map Normal file

File diff suppressed because one or more lines are too long

11
lib/cam/package.json Normal file
View File

@ -0,0 +1,11 @@
{
"name": "cam",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

183
src/cam/index.ts Normal file
View File

@ -0,0 +1,183 @@
'use strict';
import Intval = require('intval');
/** class representing camera functions **/
class Camera {
private state : any = { dir : true, digital : false };
private arduino : Arduino = null;
private intval : any = null;
private log : any;
private cfg : any;
private ui : any;
private ipc : any;
/**
*
**/
constructor (arduino : Arduino, cfg : any, ui : any) {
this.arduino = arduino;
this.cfg = cfg;
this.ui = ui;
this.init();
}
/**
*
**/
private async init () {
this.log = await require('log')({});
this.ipc = require('electron').ipcMain;
this.listen();
}
/**
*
**/
private listen () {
this.ipc.on('cam', this.listener.bind(this));
this.ipc.on('intval', this.connectIntval.bind(this));
}
/**
*
**/
public async set (dir : boolean, id : string) {
let cmd : string;
let ms : number;
if (dir) {
cmd = this.cfg.arduino.cmd.cam_forward;
} else {
cmd = this.cfg.arduino.cmd.cam_backward;
}
this.state.dir = dir;
if (this.intval) {
try {
ms = await this.intval.setDir(dir);
} catch (err) {
this.log.error(err);
}
} else {
try {
ms = await this.arduino.send('camera', cmd);
} catch (err) {
this.log.error(err);
}
}
return await this.end(cmd, id, ms);
}
/**
*
**/
public async move (frame : number, id : string) {
const cmd : string = this.cfg.arduino.cmd.camera;
let ms : number;
//if (this.state.digital) {
//await this.dig.start()
//}
if (this.intval) {
try {
ms = await this.intval.move();
} catch (err) {
this.log.error(err);
}
} else {
try {
ms = await this.arduino.send('camera', cmd);
} catch (err) {
this.log.error(err);
}
}
//if (this.state.digital) {
// await this.dig.end()
//}
this.log.info('Camera move time', { ms });
return this.end(cmd, id, ms);
}
/**
*
**/
public exposure (exposure : number, id : string) {
let cmd : string = 'E';
this.intval.setExposure('camera', exposure, (ms : number) => {
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 {
await this.set(arg.dir, arg.id)
} catch (err) {
console.trace()
this.log.error(err)
}
} else if (typeof arg.frame !== 'undefined') {
try {
await this.move(arg.frame, arg.id)
} catch (err) {
console.trace()
this.log.error(err)
}
}
event.returnValue = true
}
/**
*
**/
private async end (cmd : string, id : string, ms : number) {
let message = ''
if (cmd === this.cfg.arduino.cmd.cam_forward) {
message = 'Camera set to FORWARD'
} else if (cmd === this.cfg.arduino.cmd.cam_backward) {
message = 'Camera set to BACKWARD'
} else if (cmd === this.cfg.arduino.cmd.camera) {
message = 'Camera '
if (this.state.dir) {
message += 'ADVANCED'
} else {
message += 'REWOUND'
}
message += ' 1 frame'
}
this.log.info(message, 'CAMERA', true, true)
this.ui.send('cam', {cmd: cmd, id : id, ms: ms})
};
}
module.exports = function (arduino : Arduino, cfg : any, ui : any) {
return new Camera(arduino, cfg, ui);
}