Completed Camera class with mock

This commit is contained in:
Matt McWilliams 2024-07-12 21:42:54 -04:00
parent 61bbeee491
commit 4a32d7b84b
5 changed files with 164 additions and 18 deletions

View File

@ -16,4 +16,8 @@ export declare class Camera {
private onData; private onData;
private verify; private verify;
private confirm; private confirm;
frame(): Promise<number>;
open(): Promise<number>;
close(): Promise<number>;
direction(dir: boolean): Promise<number>;
} }

84
dist/camera/index.js vendored
View File

@ -5,20 +5,55 @@ const log_1 = require("../log");
const delay_1 = require("../delay"); const delay_1 = require("../delay");
const serialport_1 = require("serialport"); const serialport_1 = require("serialport");
const parser_readline_1 = require("@serialport/parser-readline"); const parser_readline_1 = require("@serialport/parser-readline");
var Commands;
(function (Commands) {
Commands["CONNECT"] = "i";
Commands["MCOPY_IDENTIFIER"] = "m";
Commands["CAMERA_IDENTIFIER"] = "k";
Commands["CAMERA"] = "c";
Commands["CAMERA_FORWARD"] = "e";
Commands["CAMERA_BACKWARD"] = "f";
Commands["CAMERA_OPEN"] = "J";
Commands["CAMERA_CLOSE"] = "K";
})(Commands || (Commands = {}));
class CameraSerialPortMock extends serialport_1.SerialPortMock { class CameraSerialPortMock extends serialport_1.SerialPortMock {
constructor(options, openCallback = null) { constructor(options, openCallback = null, parser) {
super(options, openCallback); super(options, openCallback);
this.log = (0, log_1.createLog)('mock'); this.log = (0, log_1.createLog)('mock');
console.dir(this); this.parser = parser;
} }
write(buffer) { write(buffer) {
super.write(buffer); super.write(buffer);
this.log.info(`Received data: "${buffer}"`); //this.log.info(`Received data: "${buffer}"`);
if (buffer === 'i') { switch (buffer) {
this.emit('i'); case Commands.CONNECT:
this._mockSend(Commands.CONNECT, 3);
break;
case Commands.MCOPY_IDENTIFIER:
this._mockSend(Commands.CAMERA_IDENTIFIER, 2);
break;
case Commands.CAMERA:
this._mockSend(Commands.CAMERA, 300);
break;
case Commands.CAMERA_OPEN:
this._mockSend(Commands.CAMERA_OPEN, 150);
break;
case Commands.CAMERA_CLOSE:
this._mockSend(Commands.CAMERA_FORWARD, 3);
break;
case Commands.CAMERA_OPEN:
this._mockSend(Commands.CAMERA_BACKWARD, 2);
break;
default:
this.log.warn(`Command "${buffer}" does not exist on mock`);
} }
return true; return true;
} }
_mockSend(buffer, delay) {
setTimeout(function () {
this.parser._events.data(buffer);
}.bind(this), delay);
}
} }
class Camera { class Camera {
constructor() { constructor() {
@ -115,7 +150,7 @@ class Camera {
this.serial = new CameraSerialPortMock({ this.serial = new CameraSerialPortMock({
path: this.port, path: this.port,
baudRate: this.baud baudRate: this.baud
}); }, null, this.parser);
} }
catch (err) { catch (err) {
this.log.error('Error creating SerialPortMock object', err); this.log.error('Error creating SerialPortMock object', err);
@ -141,7 +176,7 @@ class Camera {
} }
async verify() { async verify() {
try { try {
await this.confirm('i', 'i'); await this.confirm(Commands.CONNECT, Commands.CONNECT);
this.log.info(`Confirmed mcopy device`); this.log.info(`Confirmed mcopy device`);
} }
catch (err) { catch (err) {
@ -149,7 +184,7 @@ class Camera {
return; return;
} }
try { try {
await this.confirm('m', 'k'); await this.confirm(Commands.MCOPY_IDENTIFIER, Commands.CAMERA_IDENTIFIER);
this.log.info(`Confirmed mcopy camera`); this.log.info(`Confirmed mcopy camera`);
} }
catch (err) { catch (err) {
@ -179,6 +214,39 @@ class Camera {
}); });
}.bind(this)); }.bind(this));
} }
async frame() {
const start = +new Date();
let ms;
await this.confirm(Commands.CAMERA, Commands.CAMERA);
ms = (+new Date()) - start;
this.log.info(`frame() - ${ms}ms`);
return ms;
}
async open() {
const start = +new Date();
let ms;
await this.confirm(Commands.CAMERA_OPEN, Commands.CAMERA_OPEN);
ms = (+new Date()) - start;
this.log.info(`open() - ${ms}ms`);
return ms;
}
async close() {
const start = +new Date();
let ms;
await this.confirm(Commands.CAMERA_CLOSE, Commands.CAMERA_CLOSE);
ms = (+new Date()) - start;
this.log.info(`close() - ${ms}ms`);
return ms;
}
async direction(dir) {
const start = +new Date();
let ms;
const cmd = dir ? Commands.CAMERA_FORWARD : Commands.CAMERA_BACKWARD;
await this.confirm(cmd, cmd);
ms = (+new Date()) - start;
this.log.info(`direction(${dir}) - ${ms}ms`);
return ms;
}
} }
exports.Camera = Camera; exports.Camera = Camera;
module.exports = { Camera }; module.exports = { Camera };

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/display/index.ts"],"names":[],"mappings":";;;AAEA,MAAM,UAAU;IAIf,YAAa,KAAc,EAAE,MAAe;QAC3C,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACtB,CAAC;IAEM,QAAQ;QACd,OAAO,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC;IACjC,CAAC;CACD;AAOD,MAAa,OAAO;IAQnB,YAAa,KAAc,EAAE,MAAe;QAC3C,IAAI,CAAC,MAAM,GAAG,IAAI,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC5C,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAG,CAAC,EAAE,CAAA;IAC9B,CAAC;IAEM,SAAS,CAAE,KAAc,EAAE,MAAe;QAChD,IAAI,CAAC,MAAM,GAAG,IAAI,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC5C,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC;YACrD,IAAI,CAAC,OAAO,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAChG,CAAC;aAAM,CAAC;YACP,IAAI,CAAC,OAAO,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,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E,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC;YACrD,IAAI,CAAC,OAAO,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAChG,CAAC;aAAM,CAAC;YACP,IAAI,CAAC,OAAO,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC9F,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAG,CAAC,EAAG,CAAC,EAAE,CAAA;IAC5D,CAAC;IAEM,aAAa;QACnB,OAAO;YACN,CAAC,EAAG,IAAI,CAAC,OAAO,CAAC,KAAK;YACtB,CAAC,EAAG,IAAI,CAAC,OAAO,CAAC,MAAM;YACvB,CAAC,EAAG,IAAI,CAAC,MAAM,CAAC,CAAC;YACjB,CAAC,EAAG,IAAI,CAAC,MAAM,CAAC,CAAC;SACjB,CAAA;IACF,CAAC;CACD;AA/BD,0BA+BC;AAED,MAAM,CAAC,OAAO,GAAG,EAAE,OAAO,EAAE,CAAC"}

View File

@ -16,22 +16,59 @@ interface PortInfo {
vendorId: string | undefined; vendorId: string | undefined;
} }
enum Commands {
CONNECT = 'i',
MCOPY_IDENTIFIER = 'm',
CAMERA_IDENTIFIER = 'k',
CAMERA = 'c',
CAMERA_FORWARD = 'e',
CAMERA_BACKWARD = 'f',
CAMERA_OPEN = 'J',
CAMERA_CLOSE = 'K'
}
class CameraSerialPortMock extends SerialPortMock { class CameraSerialPortMock extends SerialPortMock {
private log : Logger; private log : Logger;
constructor (options : any, openCallback : ErrorCallback = null) { private parser : ReadlineParser;
constructor (options : any, openCallback : ErrorCallback = null, parser : ReadlineParser) {
super(options, openCallback); super(options, openCallback);
this.log = createLog('mock'); this.log = createLog('mock');
console.dir(this); this.parser = parser;
} }
write (buffer : any) : boolean { write (buffer : any) : boolean {
super.write(buffer) super.write(buffer)
this.log.info(`Received data: "${buffer}"`); //this.log.info(`Received data: "${buffer}"`);
if (buffer === 'i') { switch (buffer) {
this.emit('i'); case Commands.CONNECT :
this._mockSend(Commands.CONNECT, 3);
break;
case Commands.MCOPY_IDENTIFIER :
this._mockSend(Commands.CAMERA_IDENTIFIER, 2);
break;
case Commands.CAMERA :
this._mockSend(Commands.CAMERA, 300);
break;
case Commands.CAMERA_OPEN :
this._mockSend(Commands.CAMERA_OPEN, 150);
break;
case Commands.CAMERA_CLOSE :
this._mockSend(Commands.CAMERA_FORWARD, 3);
break;
case Commands.CAMERA_OPEN :
this._mockSend(Commands.CAMERA_BACKWARD, 2);
break;
default:
this.log.warn(`Command "${buffer}" does not exist on mock`);
} }
return true; return true;
} }
_mockSend (buffer : string, delay : number) {
setTimeout(function () {
this.parser._events.data(buffer);
}.bind(this), delay);
}
} }
export class Camera { export class Camera {
@ -132,7 +169,7 @@ export class Camera {
this.serial = new CameraSerialPortMock({ this.serial = new CameraSerialPortMock({
path : this.port, path : this.port,
baudRate: this.baud baudRate: this.baud
}); }, null, this.parser);
} catch (err) { } catch (err) {
this.log.error('Error creating SerialPortMock object', err); this.log.error('Error creating SerialPortMock object', err);
return; return;
@ -159,14 +196,14 @@ export class Camera {
private async verify () { private async verify () {
try { try {
await this.confirm('i', 'i'); await this.confirm(Commands.CONNECT, Commands.CONNECT);
this.log.info(`Confirmed mcopy device`); this.log.info(`Confirmed mcopy device`);
} catch (err) { } catch (err) {
this.log.error(`Error connecting to mcopy device`, err); this.log.error(`Error connecting to mcopy device`, err);
return; return;
} }
try { try {
await this.confirm('m', 'k'); await this.confirm(Commands.MCOPY_IDENTIFIER, Commands.CAMERA_IDENTIFIER);
this.log.info(`Confirmed mcopy camera`); this.log.info(`Confirmed mcopy camera`);
} catch (err) { } catch (err) {
this.log.error(`Error identifying device`, err); this.log.error(`Error identifying device`, err);
@ -195,6 +232,43 @@ export class Camera {
}); });
}.bind(this)); }.bind(this));
} }
public async frame () : Promise<number> {
const start : number = +new Date();
let ms : number;
await this.confirm(Commands.CAMERA, Commands.CAMERA);
ms = (+new Date()) - start;
this.log.info(`frame() - ${ms}ms`);
return ms;
}
public async open () : Promise<number> {
const start : number = +new Date();
let ms : number;
await this.confirm(Commands.CAMERA_OPEN, Commands.CAMERA_OPEN);
ms = (+new Date()) - start;
this.log.info(`open() - ${ms}ms`);
return ms;
}
public async close () : Promise<number> {
const start : number = +new Date();
let ms : number;
await this.confirm(Commands.CAMERA_CLOSE, Commands.CAMERA_CLOSE);
ms = (+new Date()) - start;
this.log.info(`close() - ${ms}ms`);
return ms;
}
public async direction (dir : boolean) : Promise<number> {
const start : number = +new Date();
let ms : number;
const cmd : string = dir ? Commands.CAMERA_FORWARD : Commands.CAMERA_BACKWARD;
await this.confirm(cmd, cmd);
ms = (+new Date()) - start;
this.log.info(`direction(${dir}) - ${ms}ms`);
return ms;
}
} }
module.exports = { Camera }; module.exports = { Camera };