2024-07-09 04:02:45 +00:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
exports.Camera = void 0;
|
|
|
|
const log_1 = require("../log");
|
2024-07-11 21:26:14 +00:00
|
|
|
const delay_1 = require("../delay");
|
2024-07-09 04:02:45 +00:00
|
|
|
const serialport_1 = require("serialport");
|
|
|
|
const parser_readline_1 = require("@serialport/parser-readline");
|
2024-07-13 01:42:54 +00:00
|
|
|
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 = {}));
|
2024-07-12 03:01:44 +00:00
|
|
|
class CameraSerialPortMock extends serialport_1.SerialPortMock {
|
2024-07-13 01:42:54 +00:00
|
|
|
constructor(options, openCallback = null, parser) {
|
2024-07-12 03:01:44 +00:00
|
|
|
super(options, openCallback);
|
|
|
|
this.log = (0, log_1.createLog)('mock');
|
2024-07-13 01:42:54 +00:00
|
|
|
this.parser = parser;
|
2024-07-12 03:01:44 +00:00
|
|
|
}
|
|
|
|
write(buffer) {
|
|
|
|
super.write(buffer);
|
2024-07-31 19:07:09 +00:00
|
|
|
//this.log.info(this.prefix + `Received data: "${buffer}"`);
|
2024-07-13 01:42:54 +00:00
|
|
|
switch (buffer) {
|
|
|
|
case Commands.CONNECT:
|
|
|
|
this._mockSend(Commands.CONNECT, 3);
|
|
|
|
break;
|
|
|
|
case Commands.MCOPY_IDENTIFIER:
|
|
|
|
this._mockSend(Commands.CAMERA_IDENTIFIER, 2);
|
|
|
|
break;
|
|
|
|
case Commands.CAMERA:
|
2024-07-31 18:39:21 +00:00
|
|
|
this._mockSend(Commands.CAMERA, 250);
|
2024-07-13 01:42:54 +00:00
|
|
|
break;
|
|
|
|
case Commands.CAMERA_OPEN:
|
2024-07-31 18:39:21 +00:00
|
|
|
this._mockSend(Commands.CAMERA_OPEN, 125);
|
2024-07-13 01:42:54 +00:00
|
|
|
break;
|
|
|
|
case Commands.CAMERA_CLOSE:
|
2024-08-01 15:57:49 +00:00
|
|
|
this._mockSend(Commands.CAMERA_CLOSE, 125);
|
2024-07-13 01:42:54 +00:00
|
|
|
break;
|
|
|
|
default:
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.warn(`[MOCK] Command "${buffer}" does not exist on mock`);
|
2024-07-12 03:01:44 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2024-07-13 01:42:54 +00:00
|
|
|
_mockSend(buffer, delay) {
|
|
|
|
setTimeout(function () {
|
|
|
|
this.parser._events.data(buffer);
|
|
|
|
}.bind(this), delay);
|
|
|
|
}
|
2024-07-12 03:01:44 +00:00
|
|
|
}
|
2024-07-09 04:02:45 +00:00
|
|
|
class Camera {
|
2024-08-24 20:26:49 +00:00
|
|
|
constructor(mock) {
|
2024-07-09 04:02:45 +00:00
|
|
|
this.ready = false;
|
2024-07-11 21:26:14 +00:00
|
|
|
this.connected = false;
|
2024-07-09 04:02:45 +00:00
|
|
|
this.serial = null;
|
|
|
|
this.baud = 57600;
|
2024-07-11 21:26:14 +00:00
|
|
|
this.next = null;
|
|
|
|
this.port = null;
|
2024-07-31 19:07:09 +00:00
|
|
|
this.prefix = '';
|
2024-08-24 20:26:49 +00:00
|
|
|
this.mock = false;
|
|
|
|
this.mock = mock;
|
2024-07-09 04:02:45 +00:00
|
|
|
this.log = (0, log_1.createLog)('camera');
|
|
|
|
this.parser = new parser_readline_1.ReadlineParser({ delimiter: '\r\n' });
|
|
|
|
this.begin();
|
|
|
|
}
|
|
|
|
async begin() {
|
|
|
|
let ports = [];
|
|
|
|
let selected = false;
|
|
|
|
try {
|
|
|
|
ports = await this.enumerate();
|
|
|
|
}
|
|
|
|
catch (err) {
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.error(this.prefix + 'Error calling enumerate()', err);
|
2024-07-09 04:02:45 +00:00
|
|
|
}
|
2024-08-24 20:26:49 +00:00
|
|
|
if (!this.mock && ports.length > 0) {
|
2024-07-11 21:26:14 +00:00
|
|
|
for (let port of ports) {
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.info(this.prefix + `Found USB serial device: ${port} ${selected ? '*' : ''}`);
|
2024-07-11 21:26:14 +00:00
|
|
|
selected = false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await this.connect(ports[0]);
|
|
|
|
}
|
|
|
|
catch (err) {
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.error(this.prefix + `Error connecting to ${ports[0]}`, err);
|
2024-07-11 21:26:14 +00:00
|
|
|
}
|
2024-07-09 04:02:45 +00:00
|
|
|
}
|
2024-07-11 21:26:14 +00:00
|
|
|
else {
|
2024-08-24 20:26:49 +00:00
|
|
|
if (this.mock) {
|
|
|
|
this.log.info(`Starting camera in MOCK mode due to system setting`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.log.warn(this.prefix + `No USB serial devices found, connecting to MOCK...`);
|
|
|
|
}
|
2024-07-11 21:26:14 +00:00
|
|
|
try {
|
|
|
|
await this.connectMock();
|
|
|
|
}
|
|
|
|
catch (err) {
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.error(this.prefix + `Error connecting to MOCK USB serial device "/dev/fake"`, err);
|
2024-07-11 21:26:14 +00:00
|
|
|
}
|
2024-07-09 04:02:45 +00:00
|
|
|
}
|
2024-07-11 21:26:14 +00:00
|
|
|
await (0, delay_1.delay)(3000);
|
|
|
|
await this.verify();
|
2024-07-09 04:02:45 +00:00
|
|
|
}
|
|
|
|
filter(port) {
|
|
|
|
if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1 ||
|
|
|
|
(port.path + '').toLowerCase().indexOf('usbserial') !== -1 ||
|
|
|
|
(port.path + '').toLowerCase().indexOf('usbmodem') !== -1 ||
|
|
|
|
(port.path + '').toLowerCase().indexOf('ttyusb') !== -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
async enumerate() {
|
|
|
|
let listed = [];
|
|
|
|
try {
|
|
|
|
listed = await serialport_1.SerialPort.list();
|
|
|
|
}
|
|
|
|
catch (err) {
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.error(this.prefix + 'Error listing serial ports', err);
|
2024-07-09 04:02:45 +00:00
|
|
|
}
|
|
|
|
return listed.filter(this.filter).map((port) => port.path);
|
|
|
|
}
|
|
|
|
async connect(port) {
|
2024-07-11 21:26:14 +00:00
|
|
|
this.port = port;
|
2024-07-09 04:02:45 +00:00
|
|
|
try {
|
|
|
|
this.serial = new serialport_1.SerialPort({
|
2024-07-11 21:26:14 +00:00
|
|
|
path: this.port,
|
2024-07-09 04:02:45 +00:00
|
|
|
baudRate: this.baud,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (err) {
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.error(this.prefix + 'Error creating SerialPort object', err);
|
2024-07-11 21:26:14 +00:00
|
|
|
return;
|
2024-07-09 04:02:45 +00:00
|
|
|
}
|
|
|
|
if (this.serial !== null) {
|
|
|
|
this.serial.pipe(this.parser);
|
|
|
|
this.parser.on('data', this.onData.bind(this));
|
|
|
|
}
|
2024-07-11 21:26:14 +00:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
this.serial.on('open', () => {
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.info(this.prefix + `Connected to USB serial device ${this.port} @ ${this.baud} baud`);
|
2024-07-11 21:26:14 +00:00
|
|
|
this.connected = true;
|
|
|
|
return resolve(true);
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
async connectMock() {
|
|
|
|
this.port = '/dev/fake';
|
|
|
|
try {
|
|
|
|
serialport_1.SerialPortMock.binding.createPort(this.port);
|
2024-07-12 03:01:44 +00:00
|
|
|
this.serial = new CameraSerialPortMock({
|
2024-07-11 21:26:14 +00:00
|
|
|
path: this.port,
|
|
|
|
baudRate: this.baud
|
2024-07-13 01:42:54 +00:00
|
|
|
}, null, this.parser);
|
2024-07-11 21:26:14 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.error(this.prefix + 'Error creating SerialPortMock object', err);
|
2024-07-11 21:26:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.serial !== null) {
|
|
|
|
this.serial.pipe(this.parser);
|
|
|
|
this.parser.on('data', this.onData.bind(this));
|
|
|
|
}
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
this.serial.on('open', () => {
|
2024-07-31 19:07:09 +00:00
|
|
|
this.prefix = '[MOCK] ';
|
|
|
|
this.log.info(this.prefix + `Connected to MOCK USB serial device ${this.port} @ ${this.baud} baud`);
|
2024-07-11 21:26:14 +00:00
|
|
|
this.connected = true;
|
|
|
|
return resolve(true);
|
|
|
|
});
|
|
|
|
}.bind(this));
|
2024-07-09 04:02:45 +00:00
|
|
|
}
|
|
|
|
onData(data) {
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.info(this.prefix + `Received data: "${data}"`);
|
2024-07-11 21:26:14 +00:00
|
|
|
if (this.next !== null) {
|
|
|
|
this.next(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async verify() {
|
|
|
|
try {
|
2024-07-13 01:42:54 +00:00
|
|
|
await this.confirm(Commands.CONNECT, Commands.CONNECT);
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.info(this.prefix + `Confirmed mcopy device`);
|
2024-07-11 21:26:14 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.error(this.prefix + `Error connecting to mcopy device`, err);
|
2024-07-11 21:26:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2024-07-13 01:42:54 +00:00
|
|
|
await this.confirm(Commands.MCOPY_IDENTIFIER, Commands.CAMERA_IDENTIFIER);
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.info(this.prefix + `Confirmed mcopy camera`);
|
2024-07-11 21:26:14 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.error(this.prefix + `Error identifying device`, err);
|
2024-07-11 21:26:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.ready = true;
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.info(this.prefix + `Camera connected and ready`);
|
2024-07-11 21:26:14 +00:00
|
|
|
}
|
|
|
|
async confirm(cmd, res) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
this.next = function (data) {
|
|
|
|
this.next = null;
|
|
|
|
if (data === res) {
|
|
|
|
return resolve(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return reject(new Error(`Response ${data} !== ${res}`));
|
|
|
|
}
|
|
|
|
}.bind(this);
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.info(this.prefix + `Send data: "${cmd}"`);
|
2024-07-11 21:26:14 +00:00
|
|
|
this.serial.write(cmd, (err, results) => {
|
|
|
|
if (err) {
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.error(this.prefix + 'Error writing to device', err);
|
2024-07-11 21:26:14 +00:00
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}.bind(this));
|
2024-07-09 04:02:45 +00:00
|
|
|
}
|
2024-07-13 01:42:54 +00:00
|
|
|
async frame() {
|
|
|
|
const start = +new Date();
|
|
|
|
let ms;
|
|
|
|
await this.confirm(Commands.CAMERA, Commands.CAMERA);
|
|
|
|
ms = (+new Date()) - start;
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.info(this.prefix + `frame() - ${ms}ms`);
|
2024-07-13 01:42:54 +00:00
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
async open() {
|
|
|
|
const start = +new Date();
|
|
|
|
let ms;
|
|
|
|
await this.confirm(Commands.CAMERA_OPEN, Commands.CAMERA_OPEN);
|
|
|
|
ms = (+new Date()) - start;
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.info(this.prefix + `open() - ${ms}ms`);
|
2024-07-13 01:42:54 +00:00
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
async close() {
|
|
|
|
const start = +new Date();
|
|
|
|
let ms;
|
|
|
|
await this.confirm(Commands.CAMERA_CLOSE, Commands.CAMERA_CLOSE);
|
|
|
|
ms = (+new Date()) - start;
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.info(this.prefix + `close() - ${ms}ms`);
|
2024-07-13 01:42:54 +00:00
|
|
|
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;
|
2024-07-31 19:07:09 +00:00
|
|
|
this.log.info(this.prefix + `direction(${dir}) - ${ms}ms`);
|
2024-07-13 01:42:54 +00:00
|
|
|
return ms;
|
|
|
|
}
|
2024-07-09 04:02:45 +00:00
|
|
|
}
|
|
|
|
exports.Camera = Camera;
|
|
|
|
module.exports = { Camera };
|
|
|
|
//# sourceMappingURL=index.js.map
|