filmout_manager/dist/camera/index.js

259 lines
8.7 KiB
JavaScript
Raw Normal View History

"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");
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);
//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:
this._mockSend(Commands.CAMERA, 250);
2024-07-13 01:42:54 +00:00
break;
case Commands.CAMERA_OPEN:
this._mockSend(Commands.CAMERA_OPEN, 125);
2024-07-13 01:42:54 +00:00
break;
case Commands.CAMERA_CLOSE:
this._mockSend(Commands.CAMERA_CLOSE, 125);
2024-07-13 01:42:54 +00:00
break;
default:
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
}
class Camera {
constructor(mock) {
this.ready = false;
2024-07-11 21:26:14 +00:00
this.connected = false;
this.serial = null;
this.baud = 57600;
2024-07-11 21:26:14 +00:00
this.next = null;
this.port = null;
this.prefix = '';
this.mock = false;
this.mock = mock;
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) {
this.log.error(this.prefix + 'Error calling enumerate()', err);
}
if (!this.mock && ports.length > 0) {
2024-07-11 21:26:14 +00:00
for (let port of ports) {
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) {
this.log.error(this.prefix + `Error connecting to ${ports[0]}`, err);
2024-07-11 21:26:14 +00:00
}
}
2024-07-11 21:26:14 +00:00
else {
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) {
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-11 21:26:14 +00:00
await (0, delay_1.delay)(3000);
await this.verify();
}
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) {
this.log.error(this.prefix + 'Error listing serial ports', err);
}
return listed.filter(this.filter).map((port) => port.path);
}
async connect(port) {
2024-07-11 21:26:14 +00:00
this.port = port;
try {
this.serial = new serialport_1.SerialPort({
2024-07-11 21:26:14 +00:00
path: this.port,
baudRate: this.baud,
});
}
catch (err) {
this.log.error(this.prefix + 'Error creating SerialPort 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));
}
2024-07-11 21:26:14 +00:00
return new Promise(function (resolve, reject) {
this.serial.on('open', () => {
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) {
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', () => {
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));
}
onData(data) {
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);
this.log.info(this.prefix + `Confirmed mcopy device`);
2024-07-11 21:26:14 +00:00
}
catch (err) {
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);
this.log.info(this.prefix + `Confirmed mcopy camera`);
2024-07-11 21:26:14 +00:00
}
catch (err) {
this.log.error(this.prefix + `Error identifying device`, err);
2024-07-11 21:26:14 +00:00
return;
}
this.ready = true;
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);
this.log.info(this.prefix + `Send data: "${cmd}"`);
2024-07-11 21:26:14 +00:00
this.serial.write(cmd, (err, results) => {
if (err) {
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-13 01:42:54 +00:00
async frame() {
const start = Date.now();
2024-07-13 01:42:54 +00:00
let ms;
await this.confirm(Commands.CAMERA, Commands.CAMERA);
ms = (Date.now()) - start;
this.log.info(this.prefix + `frame() - ${ms}ms`);
2024-07-13 01:42:54 +00:00
return ms;
}
async open() {
const start = Date.now();
2024-07-13 01:42:54 +00:00
let ms;
await this.confirm(Commands.CAMERA_OPEN, Commands.CAMERA_OPEN);
ms = (Date.now()) - start;
this.log.info(this.prefix + `open() - ${ms}ms`);
2024-07-13 01:42:54 +00:00
return ms;
}
async close() {
const start = Date.now();
2024-07-13 01:42:54 +00:00
let ms;
await this.confirm(Commands.CAMERA_CLOSE, Commands.CAMERA_CLOSE);
ms = (Date.now()) - start;
this.log.info(this.prefix + `close() - ${ms}ms`);
2024-07-13 01:42:54 +00:00
return ms;
}
async direction(dir) {
const start = Date.now();
2024-07-13 01:42:54 +00:00
let ms;
const cmd = dir ? Commands.CAMERA_FORWARD : Commands.CAMERA_BACKWARD;
await this.confirm(cmd, cmd);
ms = (Date.now()) - start;
this.log.info(this.prefix + `direction(${dir}) - ${ms}ms`);
2024-07-13 01:42:54 +00:00
return ms;
}
}
exports.Camera = Camera;
module.exports = { Camera };
//# sourceMappingURL=index.js.map