2019-03-05 03:10:15 +00:00
|
|
|
'use strict';
|
2019-03-21 18:58:52 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2023-07-24 00:00:42 +00:00
|
|
|
/**
|
|
|
|
* 2023-07-16 Clarification
|
|
|
|
*
|
|
|
|
* Previous versions of this script intermingled and even
|
|
|
|
* swapped the usage of the terms 'serial' and 'device'.
|
|
|
|
* From here on out, the terms will be used as such:
|
|
|
|
*
|
|
|
|
* serial - a hardware address of a serial port
|
|
|
|
* device - common name of a type of mcopy device (eg. camera,
|
|
|
|
* projector, light) that is aliased to a serial port
|
|
|
|
*
|
|
|
|
**/
|
2019-03-21 22:03:53 +00:00
|
|
|
//import Log = require('log');
|
2019-06-09 01:43:14 +00:00
|
|
|
const delay_1 = require("delay");
|
2022-06-04 15:11:23 +00:00
|
|
|
const { SerialPort } = require('serialport');
|
|
|
|
const { ReadlineParser } = require('@serialport/parser-readline');
|
2019-03-05 03:10:15 +00:00
|
|
|
const exec = require('child_process').exec;
|
2023-06-17 21:57:30 +00:00
|
|
|
const parser = new ReadlineParser({ delimiter: '\r\n' });
|
2019-03-05 03:10:15 +00:00
|
|
|
const newlineRe = new RegExp('\n', 'g');
|
|
|
|
const returnRe = new RegExp('\r', 'g');
|
|
|
|
let eventEmitter;
|
|
|
|
let cfg;
|
2019-03-09 03:32:25 +00:00
|
|
|
let arduino;
|
|
|
|
const KNOWN = [
|
|
|
|
'/dev/tty.usbmodem1a161',
|
|
|
|
'/dev/tty.usbserial-A800f8dk',
|
|
|
|
'/dev/tty.usbserial-A900cebm',
|
|
|
|
'/dev/tty.usbmodem1a131',
|
|
|
|
'/dev/tty.usbserial-a900f6de',
|
|
|
|
'/dev/tty.usbmodem1a141',
|
|
|
|
'/dev/ttyACM0',
|
|
|
|
'COM3'
|
|
|
|
];
|
2019-02-22 21:31:53 +00:00
|
|
|
/**
|
2019-03-09 03:32:25 +00:00
|
|
|
* Class representing the arduino communication features
|
2019-02-22 21:31:53 +00:00
|
|
|
**/
|
2019-03-09 03:32:25 +00:00
|
|
|
class Arduino {
|
2020-09-29 17:57:59 +00:00
|
|
|
constructor(errorState) {
|
2019-03-09 03:32:25 +00:00
|
|
|
this.path = {};
|
|
|
|
this.known = KNOWN;
|
|
|
|
this.alias = {};
|
2023-07-31 02:13:50 +00:00
|
|
|
this.serial = {};
|
2023-07-11 02:33:16 +00:00
|
|
|
this.hasState = {};
|
2019-03-09 03:32:25 +00:00
|
|
|
this.baud = 57600;
|
|
|
|
this.queue = {};
|
|
|
|
this.timer = 0;
|
|
|
|
this.locks = {};
|
2023-07-31 02:13:50 +00:00
|
|
|
this.stateStr = {};
|
2020-09-29 17:57:59 +00:00
|
|
|
this.errorState = errorState;
|
2020-08-18 18:20:43 +00:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
async init() {
|
|
|
|
const Log = require('log');
|
|
|
|
this.log = await Log({ label: 'arduino' });
|
2023-08-06 18:56:42 +00:00
|
|
|
this.keys = Object.keys(cfg.arduino.cmd);
|
|
|
|
this.values = this.keys.map(key => cfg.arduino.cmd[key]);
|
2019-03-09 03:32:25 +00:00
|
|
|
}
|
2019-03-22 01:02:28 +00:00
|
|
|
/**
|
|
|
|
* Enumerate all connected devices that might be Arduinos
|
|
|
|
*
|
2023-08-06 18:56:42 +00:00
|
|
|
* @async
|
2019-03-22 01:02:28 +00:00
|
|
|
* @returns {Promise} Resolves after enumerating
|
|
|
|
**/
|
2019-03-09 03:32:25 +00:00
|
|
|
async enumerate() {
|
2019-12-18 22:17:48 +00:00
|
|
|
let ports;
|
|
|
|
let matches = [];
|
|
|
|
try {
|
|
|
|
ports = await SerialPort.list();
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2020-08-18 18:20:43 +00:00
|
|
|
this.log.info('Available ports:');
|
|
|
|
this.log.info(ports.map((port) => { return port.path; }).join(','));
|
2019-12-18 22:17:48 +00:00
|
|
|
ports.forEach((port) => {
|
|
|
|
if (this.known.indexOf(port.path) !== -1) {
|
|
|
|
matches.push(port.path);
|
|
|
|
}
|
|
|
|
else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) {
|
|
|
|
matches.push(port.path);
|
|
|
|
}
|
|
|
|
else if ((port.path + '').toLowerCase().indexOf('usbserial') !== -1) {
|
|
|
|
matches.push(port.path);
|
|
|
|
}
|
|
|
|
else if ((port.path + '').toLowerCase().indexOf('usbmodem') !== -1) {
|
|
|
|
matches.push(port.path);
|
|
|
|
}
|
|
|
|
else if ((port.path + '').toLowerCase().indexOf('ttyusb') !== -1) {
|
|
|
|
matches.push(port.path);
|
|
|
|
}
|
2019-03-05 03:10:15 +00:00
|
|
|
});
|
2019-12-18 22:17:48 +00:00
|
|
|
if (matches.length === 0) {
|
|
|
|
throw new Error('No USB devices found');
|
|
|
|
}
|
|
|
|
else if (matches.length > 0) {
|
|
|
|
return matches;
|
|
|
|
}
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
/**
|
|
|
|
* Send a command to an Arduino using async/await
|
|
|
|
*
|
2023-08-06 18:56:42 +00:00
|
|
|
* @param {string} device The Arduino device identifier
|
2019-03-09 03:32:25 +00:00
|
|
|
* @param {string} cmd Single character command to send
|
|
|
|
*
|
2023-08-06 18:56:42 +00:00
|
|
|
* @async
|
2019-03-09 03:32:25 +00:00
|
|
|
* @returns {Promise} Resolves after sending
|
|
|
|
**/
|
|
|
|
async sendAsync(device, cmd) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2023-07-31 02:13:50 +00:00
|
|
|
this.log.info(`sendAsync ${cmd} -> ${device}`);
|
2019-03-09 03:32:25 +00:00
|
|
|
this.queue[cmd] = (ms) => {
|
|
|
|
return resolve(ms);
|
|
|
|
};
|
2023-07-31 02:13:50 +00:00
|
|
|
this.log.info(`Device: ${device}`);
|
|
|
|
return this.serial[this.alias[device]].write(cmd, (err, results) => {
|
2019-03-09 03:32:25 +00:00
|
|
|
if (err) {
|
2023-06-13 02:45:25 +00:00
|
|
|
//this.log.error(err)
|
2019-03-09 03:32:25 +00:00
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2023-07-24 00:00:42 +00:00
|
|
|
/**
|
2023-08-06 18:56:42 +00:00
|
|
|
* Sends a command to the specified Arduino and waits for a response.
|
|
|
|
* Handles the communication lock to prevent sending multiple commands simultaneously.
|
|
|
|
* Emits an 'arduino_send' event after successfully sending the command.
|
2023-07-24 00:00:42 +00:00
|
|
|
*
|
2023-08-06 18:56:42 +00:00
|
|
|
* @async
|
|
|
|
* @param {string} device - The Arduino device identifier.
|
|
|
|
* @param {string} cmd - The command to be sent to the Arduino.
|
|
|
|
* @returns {Promise<boolean|string>} Returns 'false' if the communication is locked, otherwise returns the response from the device.
|
|
|
|
* @throws {Error} Throws an error if the sendAsync method encounters an error.
|
2023-07-24 00:00:42 +00:00
|
|
|
**/
|
2023-07-31 02:13:50 +00:00
|
|
|
async send(device, cmd) {
|
|
|
|
const serial = this.alias[device];
|
|
|
|
let ms;
|
|
|
|
this.log.info(`send ${cmd} -> ${device}`);
|
|
|
|
if (this.isLocked(serial)) {
|
|
|
|
this.log.warn(`send Serial ${serial} is locked`);
|
|
|
|
return null;
|
2019-03-09 03:32:25 +00:00
|
|
|
}
|
2019-04-04 22:49:07 +00:00
|
|
|
this.timer = new Date().getTime();
|
2023-07-31 02:13:50 +00:00
|
|
|
this.lock(serial);
|
2019-06-09 01:43:14 +00:00
|
|
|
await delay_1.delay(cfg.arduino.serialDelay);
|
2019-03-05 03:10:15 +00:00
|
|
|
try {
|
2023-07-31 02:13:50 +00:00
|
|
|
ms = await this.sendAsync(device, cmd);
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
2023-06-13 02:45:25 +00:00
|
|
|
return this.log.error(e);
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2023-07-31 02:13:50 +00:00
|
|
|
this.unlock(serial);
|
2019-04-04 22:49:07 +00:00
|
|
|
await eventEmitter.emit('arduino_send', cmd);
|
2023-07-31 02:13:50 +00:00
|
|
|
return ms;
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2023-07-24 00:00:42 +00:00
|
|
|
/**
|
2023-08-06 18:56:42 +00:00
|
|
|
* Sends a string to the specified Arduino.
|
|
|
|
* Handles different types of devices, including fake devices for testing purposes.
|
|
|
|
* Waits for a specified delay before sending the string.
|
2023-07-24 00:00:42 +00:00
|
|
|
*
|
2023-08-06 18:56:42 +00:00
|
|
|
* @async
|
|
|
|
* @param {string} device - The Arduino device identifier.
|
|
|
|
* @param {string} str - The string to be sent to the Arduino.
|
|
|
|
* @returns {Promise<boolean|string>} Returns 'true' if the string is sent successfully, otherwise returns an error message.
|
|
|
|
* @throws {Error} Throws an error if the writeAsync method encounters an error.
|
2023-07-24 00:00:42 +00:00
|
|
|
**/
|
2023-07-31 02:13:50 +00:00
|
|
|
async sendString(device, str) {
|
2019-03-09 03:32:25 +00:00
|
|
|
let writeSuccess;
|
2019-06-09 01:43:14 +00:00
|
|
|
await delay_1.delay(cfg.arduino.serialDelay);
|
2023-07-31 02:13:50 +00:00
|
|
|
if (typeof this.serial[this.alias[device]].fake !== 'undefined'
|
|
|
|
&& this.serial[this.alias[device]].fake) {
|
|
|
|
return this.serial[this.alias[device]].string(str);
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
else {
|
2023-07-31 02:13:50 +00:00
|
|
|
this.log.info(`sendString ${str} -> ${device}`);
|
2019-03-09 03:32:25 +00:00
|
|
|
try {
|
|
|
|
writeSuccess = await this.writeAsync(device, str);
|
|
|
|
}
|
|
|
|
catch (e) {
|
2023-06-13 02:45:25 +00:00
|
|
|
return this.log.error(e);
|
2019-03-09 03:32:25 +00:00
|
|
|
}
|
2023-07-31 02:13:50 +00:00
|
|
|
this.unlock(this.alias[device]);
|
2019-03-09 03:32:25 +00:00
|
|
|
return writeSuccess;
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
}
|
2023-07-24 00:00:42 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2023-06-17 03:04:16 +00:00
|
|
|
async stateAsync(device, confirm = false) {
|
2023-06-13 02:45:25 +00:00
|
|
|
const cmd = cfg.arduino.cmd.state;
|
2023-07-31 02:13:50 +00:00
|
|
|
const serial = confirm ? this.alias['connect'] : this.alias[device];
|
2023-06-13 02:45:25 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2023-06-17 03:04:16 +00:00
|
|
|
this.queue[cmd] = (state) => {
|
2023-07-31 02:13:50 +00:00
|
|
|
this.stateStr[device] = state;
|
2023-06-17 03:04:16 +00:00
|
|
|
if (confirm) {
|
|
|
|
this.hasState[device] = true;
|
|
|
|
this.log.info(`Device ${device} supports state [${state}]`);
|
|
|
|
}
|
|
|
|
return resolve(state);
|
2023-06-13 02:45:25 +00:00
|
|
|
};
|
2023-06-17 03:04:16 +00:00
|
|
|
if (confirm) {
|
|
|
|
setTimeout(function () {
|
|
|
|
if (typeof this.queue[cmd] !== 'undefined') {
|
|
|
|
delete this.queue[cmd];
|
|
|
|
this.hasState[device] = false;
|
|
|
|
this.log.info(`Device ${device} does not support state`);
|
|
|
|
return resolve(null);
|
|
|
|
}
|
2023-06-17 21:57:30 +00:00
|
|
|
}.bind(this), 1000);
|
2023-06-17 03:04:16 +00:00
|
|
|
}
|
2023-07-31 02:13:50 +00:00
|
|
|
this.log.info(`stateAsync ${cmd} -> ${device}`);
|
|
|
|
return this.serial[serial].write(cmd, (err, results) => {
|
2023-06-13 02:45:25 +00:00
|
|
|
if (err) {
|
|
|
|
//this.log.error(err)
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2023-07-24 00:00:42 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2023-07-15 01:13:04 +00:00
|
|
|
async state(device, confirm = false) {
|
|
|
|
const serial = confirm ? this.alias['connect'] : this.alias[device];
|
2023-06-13 02:45:25 +00:00
|
|
|
let results;
|
2023-07-31 02:13:50 +00:00
|
|
|
if (this.isLocked(serial)) {
|
|
|
|
this.log.warn(`state Serial ${serial} is locked`);
|
2023-06-17 03:04:16 +00:00
|
|
|
return null;
|
2023-06-13 02:45:25 +00:00
|
|
|
}
|
|
|
|
this.timer = new Date().getTime();
|
2023-07-31 02:13:50 +00:00
|
|
|
this.lock(serial);
|
2023-06-13 02:45:25 +00:00
|
|
|
await delay_1.delay(cfg.arduino.serialDelay);
|
|
|
|
try {
|
2023-06-17 03:04:16 +00:00
|
|
|
results = await this.stateAsync(device, confirm);
|
2023-06-13 02:45:25 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return this.log.error(e);
|
|
|
|
}
|
2023-07-31 02:13:50 +00:00
|
|
|
this.unlock(serial);
|
2023-06-13 02:45:25 +00:00
|
|
|
await eventEmitter.emit('arduino_state', cfg.arduino.cmd.state);
|
|
|
|
return results;
|
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
/**
|
|
|
|
* Send a string to an Arduino using async/await
|
|
|
|
*
|
|
|
|
* @param {string} device Arduino identifier
|
|
|
|
* @param {string} str String to send
|
|
|
|
*
|
|
|
|
* @returns {Promise} Resolves after sending
|
|
|
|
**/
|
|
|
|
async writeAsync(device, str) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2023-07-31 02:13:50 +00:00
|
|
|
this.serial[this.alias[device]].write(str, function (err, results) {
|
2019-03-09 03:32:25 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
return resolve(results);
|
2019-03-05 03:10:15 +00:00
|
|
|
});
|
2019-03-09 03:32:25 +00:00
|
|
|
});
|
|
|
|
}
|
2023-07-24 00:00:42 +00:00
|
|
|
/**
|
2023-08-06 18:56:42 +00:00
|
|
|
* Handles the end of communication with the Arduino.
|
|
|
|
* Calculates the time taken for the communication, executes the callback,
|
|
|
|
* and emits an 'arduino_end' event. Handles errors and stray data received.
|
2023-07-24 00:00:42 +00:00
|
|
|
*
|
2023-08-06 18:56:42 +00:00
|
|
|
* @param {string} serial - The serial address of the Arduino device.
|
|
|
|
* @param {string} data - The data received from the Arduino.
|
|
|
|
* @returns {any} The time taken for the communication in milliseconds.
|
2023-07-24 00:00:42 +00:00
|
|
|
**/
|
2019-03-09 03:32:25 +00:00
|
|
|
end(serial, data) {
|
|
|
|
const end = new Date().getTime();
|
|
|
|
const ms = end - this.timer;
|
|
|
|
let complete;
|
2023-07-31 02:13:50 +00:00
|
|
|
//this.log.info(`end ${serial} -> ${data}`)
|
2019-03-09 03:32:25 +00:00
|
|
|
if (this.queue[data] !== undefined) {
|
2023-07-31 02:13:50 +00:00
|
|
|
this.unlock(serial);
|
2019-03-09 03:32:25 +00:00
|
|
|
complete = this.queue[data](ms); //execute callback
|
|
|
|
eventEmitter.emit('arduino_end', data);
|
|
|
|
delete this.queue[data];
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2023-06-13 02:45:25 +00:00
|
|
|
else if (data[0] === cfg.arduino.cmd.state) {
|
2023-07-31 02:13:50 +00:00
|
|
|
//this.log.info(`end serial -> ${serial}`)
|
|
|
|
this.unlock(serial);
|
2023-07-11 02:33:16 +00:00
|
|
|
complete = this.queue[cfg.arduino.cmd.state](data);
|
2023-07-15 01:13:04 +00:00
|
|
|
eventEmitter.emit('arduino_end', data);
|
2023-06-17 03:04:16 +00:00
|
|
|
delete this.queue[cfg.arduino.cmd.state];
|
|
|
|
return data;
|
2023-06-13 02:45:25 +00:00
|
|
|
}
|
|
|
|
else if (data[0] === cfg.arduino.cmd.error) {
|
2023-06-17 02:07:53 +00:00
|
|
|
this.log.error(`Received error from device ${serial}`);
|
2023-07-31 02:13:50 +00:00
|
|
|
this.unlock(serial);
|
2023-08-06 18:56:42 +00:00
|
|
|
this.error(serial, data);
|
2020-09-29 17:57:59 +00:00
|
|
|
//error state
|
|
|
|
//stop sequence
|
|
|
|
//throw error in ui
|
|
|
|
}
|
2019-03-05 03:10:15 +00:00
|
|
|
else {
|
2023-08-06 18:56:42 +00:00
|
|
|
this.log.info('Received stray "' + data + '"'); //silent to user
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2019-04-04 22:49:07 +00:00
|
|
|
return ms;
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2023-08-06 18:56:42 +00:00
|
|
|
error(serial, data) {
|
|
|
|
this.log.error("ERROR", data);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Associates an alias with an Arduinos serial address.
|
|
|
|
* Used to map multi-purpose devices onto the same serial connection.
|
|
|
|
*
|
|
|
|
* @param {string} device - The serial number of the target Arduino.
|
|
|
|
* @param {string} serial - The alias to be associated with the target device.
|
|
|
|
**/
|
2023-07-31 02:13:50 +00:00
|
|
|
aliasSerial(device, serial) {
|
2023-06-13 02:45:25 +00:00
|
|
|
//this.log.info(`Making "${serial}" an alias of ${device}`)
|
2023-07-31 02:13:50 +00:00
|
|
|
this.alias[device] = serial;
|
2019-03-09 03:32:25 +00:00
|
|
|
}
|
2023-08-06 18:56:42 +00:00
|
|
|
/**
|
|
|
|
* Connects to an Arduino using its serial number.
|
|
|
|
* Sets up the SerialPort instance and path for the device, and handles data communication.
|
|
|
|
* Handles opening the connection and emitting 'arduino_end' or 'confirmEnd' events upon receiving data.
|
|
|
|
*
|
|
|
|
* @async
|
|
|
|
* @param {string} device - The device identifier (common name).
|
|
|
|
* @param {string} serial - The serial address of the target Arduino (e.g., COM port on Windows).
|
|
|
|
* @param {function} confirm - A callback function to be executed upon receiving confirmation data.
|
|
|
|
* @returns {Promise<string>} Resolves with the device path if the connection is successful.
|
|
|
|
* @throws {Error} Rejects with an error message if the connection fails.
|
|
|
|
**/
|
2023-07-31 02:13:50 +00:00
|
|
|
async connect(device, serial, confirm) {
|
|
|
|
//this.log.info(`connect device ${device}`)
|
|
|
|
//this.log.info(`connect serial ${serial}`)
|
2019-03-09 03:32:25 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
let connectSuccess;
|
2023-07-31 02:13:50 +00:00
|
|
|
this.path[device] = serial;
|
|
|
|
this.aliasSerial(device, serial);
|
|
|
|
this.serial[serial] = new SerialPort({
|
|
|
|
path: serial,
|
2019-03-09 03:32:25 +00:00
|
|
|
autoOpen: false,
|
|
|
|
baudRate: cfg.arduino.baud,
|
2023-06-17 21:57:30 +00:00
|
|
|
parser
|
2019-03-09 03:32:25 +00:00
|
|
|
});
|
2023-07-31 02:13:50 +00:00
|
|
|
this.unlock(serial);
|
2019-03-09 03:32:25 +00:00
|
|
|
try {
|
|
|
|
connectSuccess = await this.openArduino(device);
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
catch (e) {
|
2023-08-06 18:56:42 +00:00
|
|
|
this.log.error(`Failed to open ${device} @ ${serial}: ` + e);
|
2019-03-09 03:32:25 +00:00
|
|
|
return reject(e);
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2023-07-31 02:13:50 +00:00
|
|
|
this.log.info(`Opened connection with ${this.path[device]} as ${device}`);
|
2019-03-09 03:32:25 +00:00
|
|
|
if (!confirm) {
|
2023-07-31 02:13:50 +00:00
|
|
|
this.serial[this.alias[device]].on('data', async (data) => {
|
2019-03-09 03:32:25 +00:00
|
|
|
let d = data.toString('utf8');
|
|
|
|
d = d.replace(newlineRe, '').replace(returnRe, '');
|
|
|
|
return this.end(serial, d);
|
|
|
|
});
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
else {
|
2023-07-31 02:13:50 +00:00
|
|
|
this.serial[this.alias[device]].on('data', async (data) => {
|
2019-03-09 03:32:25 +00:00
|
|
|
let d = data.toString('utf8');
|
|
|
|
d = d.replace(newlineRe, '').replace(returnRe, '');
|
|
|
|
return await this.confirmEnd(d);
|
|
|
|
});
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
return resolve(this.path[serial]);
|
|
|
|
});
|
|
|
|
}
|
2023-08-06 18:56:42 +00:00
|
|
|
/**
|
|
|
|
* Handles the confirmation data received from an Arduino.
|
|
|
|
* Executes the confirmation callback function if the received data is present in the list of expected values.
|
|
|
|
*
|
|
|
|
* @param {string} data - The data received from the Arduino.
|
|
|
|
**/
|
2019-03-09 03:32:25 +00:00
|
|
|
confirmEnd(data) {
|
2023-08-06 18:56:42 +00:00
|
|
|
if (this.values.indexOf(data) !== -1) {
|
2019-03-09 03:32:25 +00:00
|
|
|
this.confirmExec(null, data);
|
|
|
|
this.confirmExec = {};
|
2023-07-31 02:13:50 +00:00
|
|
|
this.unlock(this.alias['connect']);
|
2019-03-09 03:32:25 +00:00
|
|
|
}
|
2023-06-17 21:57:30 +00:00
|
|
|
else if (data[0] === cfg.arduino.cmd.state) {
|
2023-07-11 02:33:16 +00:00
|
|
|
this.queue[cfg.arduino.cmd.state](data);
|
2023-06-17 21:57:30 +00:00
|
|
|
delete this.queue[cfg.arduino.cmd.state];
|
2023-07-31 02:13:50 +00:00
|
|
|
this.unlock(this.alias['connect']);
|
2023-06-17 21:57:30 +00:00
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
}
|
2023-08-06 18:56:42 +00:00
|
|
|
/**
|
|
|
|
* Verifies the connection to an Arduino by sending a connect command.
|
|
|
|
* The confirmation callback checks if the received data matches the expected connect command.
|
|
|
|
*
|
|
|
|
* @async
|
|
|
|
* @returns {Promise<boolean>} Resolves with 'true' if the connection is verified successfully.
|
|
|
|
* @throws {Error} Rejects with an error message if the connection verification fails.
|
|
|
|
**/
|
2019-03-09 03:32:25 +00:00
|
|
|
async verify() {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
2023-07-31 02:13:50 +00:00
|
|
|
const device = 'connect';
|
2019-03-09 03:32:25 +00:00
|
|
|
let writeSuccess;
|
|
|
|
this.confirmExec = function (err, data) {
|
|
|
|
if (data === cfg.arduino.cmd.connect) {
|
|
|
|
return resolve(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return reject('Wrong data returned');
|
|
|
|
}
|
|
|
|
};
|
2019-06-09 01:43:14 +00:00
|
|
|
await delay_1.delay(cfg.arduino.serialDelay);
|
2019-03-09 03:32:25 +00:00
|
|
|
try {
|
|
|
|
writeSuccess = await this.sendAsync(device, cfg.arduino.cmd.connect);
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
catch (e) {
|
|
|
|
return reject(e);
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
return resolve(writeSuccess);
|
|
|
|
});
|
|
|
|
}
|
2023-08-06 18:56:42 +00:00
|
|
|
/**
|
|
|
|
* Distinguishes the type of Arduino connected.
|
|
|
|
* Sends a command to the device to identify its type and resolves the promise with the received type.
|
|
|
|
*
|
|
|
|
* @async
|
|
|
|
* @returns {Promise<string>} Resolves with the type of the connected Arduino-based device.
|
|
|
|
* @throws {Error} Rejects with an error message if the distinguish operation fails.
|
|
|
|
**/
|
2019-03-09 03:32:25 +00:00
|
|
|
async distinguish() {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
2023-07-31 02:13:50 +00:00
|
|
|
const device = 'connect';
|
2019-03-09 03:32:25 +00:00
|
|
|
let writeSuccess;
|
|
|
|
let type;
|
|
|
|
this.confirmExec = function (err, data) {
|
2019-03-22 01:02:28 +00:00
|
|
|
if (data === cfg.arduino.cmd.projector_identifier) {
|
2019-03-09 03:32:25 +00:00
|
|
|
type = 'projector';
|
|
|
|
}
|
2019-03-22 01:02:28 +00:00
|
|
|
else if (data === cfg.arduino.cmd.camera_identifier) {
|
2019-03-09 03:32:25 +00:00
|
|
|
type = 'camera';
|
|
|
|
}
|
|
|
|
else if (data === cfg.arduino.cmd.light_identifier) {
|
|
|
|
type = 'light';
|
|
|
|
}
|
2019-03-22 01:02:28 +00:00
|
|
|
else if (data === cfg.arduino.cmd.projector_light_identifier) {
|
2019-03-09 03:32:25 +00:00
|
|
|
type = 'projector,light';
|
|
|
|
}
|
2019-03-22 01:02:28 +00:00
|
|
|
else if (data === cfg.arduino.cmd.projector_camera_light_identifier) {
|
2019-03-09 03:32:25 +00:00
|
|
|
type = 'projector,camera,light';
|
|
|
|
}
|
2019-03-22 01:02:28 +00:00
|
|
|
else if (data === cfg.arduino.cmd.projector_camera_identifier) {
|
2019-03-09 03:32:25 +00:00
|
|
|
type = 'projector,camera';
|
|
|
|
}
|
2019-04-04 22:49:07 +00:00
|
|
|
else if (data === cfg.arduino.cmd.projector_second_identifier) {
|
2019-03-09 03:32:25 +00:00
|
|
|
type = 'projector_second';
|
|
|
|
}
|
2019-04-04 22:49:07 +00:00
|
|
|
else if (data === cfg.arduino.cmd.projectors_identifier) {
|
2019-03-20 22:37:00 +00:00
|
|
|
type = 'projector,projector_second';
|
2019-03-18 21:21:17 +00:00
|
|
|
}
|
2019-04-04 22:49:07 +00:00
|
|
|
else if (data === cfg.arduino.cmd.camera_second_identifier) {
|
2019-03-18 21:21:17 +00:00
|
|
|
type = 'camera_second';
|
|
|
|
}
|
2019-04-04 22:49:07 +00:00
|
|
|
else if (data === cfg.arduino.cmd.cameras_identifier) {
|
2019-03-20 22:37:00 +00:00
|
|
|
type = 'camera,camera_second';
|
2019-03-18 21:21:17 +00:00
|
|
|
}
|
2019-04-04 22:49:07 +00:00
|
|
|
else if (data === cfg.arduino.cmd.camera_projectors_identifier) {
|
|
|
|
type = 'camera,projector,projector_second';
|
|
|
|
}
|
|
|
|
else if (data === cfg.arduino.cmd.cameras_projector_identifier) {
|
|
|
|
type = 'camera,camera_second,projector';
|
|
|
|
}
|
|
|
|
else if (data === cfg.arduino.cmd.cameras_projectors_identifier) {
|
|
|
|
type = 'camera,camera_second,projector,projector_second';
|
|
|
|
}
|
2022-06-13 12:08:42 +00:00
|
|
|
else if (data === cfg.arduino.cmd.capper_identifier) {
|
|
|
|
type = 'capper';
|
|
|
|
}
|
2022-06-04 15:11:23 +00:00
|
|
|
else if (data === cfg.arduino.cmd.camera_capper_identifier) {
|
|
|
|
type = 'camera,capper';
|
|
|
|
}
|
|
|
|
else if (data === cfg.arduino.cmd.camera_capper_projector_identifier) {
|
|
|
|
type = 'camera,capper,projector';
|
|
|
|
}
|
|
|
|
else if (data === cfg.arduino.cmd.camera_capper_projectors_identifier) {
|
|
|
|
type = 'camera,capper,projector,projector_second';
|
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
return resolve(type);
|
|
|
|
};
|
2019-06-09 01:43:14 +00:00
|
|
|
await delay_1.delay(cfg.arduino.serialDelay);
|
2019-03-09 03:32:25 +00:00
|
|
|
try {
|
|
|
|
writeSuccess = await this.sendAsync(device, cfg.arduino.cmd.mcopy_identifier);
|
2022-06-13 12:08:42 +00:00
|
|
|
this.log.info(writeSuccess);
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
catch (e) {
|
|
|
|
return reject(e);
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
});
|
|
|
|
}
|
2023-08-06 18:56:42 +00:00
|
|
|
/**
|
|
|
|
* Closes the connection to an Arduino.
|
|
|
|
*
|
|
|
|
* @async
|
|
|
|
* @returns {Promise<boolean>} Resolves with 'true' if the connection is closed successfully.
|
|
|
|
* @throws {Error} Throws an error if the closeArduino method encounters an error.
|
|
|
|
**/
|
2019-03-09 03:32:25 +00:00
|
|
|
async close() {
|
2023-07-31 02:13:50 +00:00
|
|
|
const device = 'connect';
|
2019-03-09 03:32:25 +00:00
|
|
|
let closeSuccess;
|
2019-03-05 03:10:15 +00:00
|
|
|
try {
|
2019-03-09 03:32:25 +00:00
|
|
|
closeSuccess = await this.closeArduino(device);
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
2019-03-21 19:59:50 +00:00
|
|
|
throw e;
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
return closeSuccess;
|
2019-03-05 03:10:15 +00:00
|
|
|
}
|
2023-08-06 18:56:42 +00:00
|
|
|
/**
|
|
|
|
* Establishes a fake connection to an Arduino for testing purposes.
|
|
|
|
* Creates a fake SerialPort instance with custom write and string methods.
|
|
|
|
*
|
|
|
|
* @async
|
|
|
|
* @param {string} serial - The device identifier of the fake Arduino.
|
|
|
|
* @returns {Promise<boolean>} Resolves with 'true' if the fake connection is established successfully.
|
|
|
|
**/
|
2023-07-31 02:13:50 +00:00
|
|
|
async fakeConnect(device) {
|
|
|
|
const serial = '/dev/fake';
|
|
|
|
this.aliasSerial(device, serial);
|
|
|
|
this.serial[serial] = {
|
2019-03-09 03:32:25 +00:00
|
|
|
write: async function (cmd, cb) {
|
|
|
|
const t = {
|
|
|
|
c: cfg.arduino.cam.time + cfg.arduino.cam.delay,
|
2022-07-13 19:21:26 +00:00
|
|
|
p: cfg.arduino.proj.time + cfg.arduino.proj.delay,
|
|
|
|
A: 180,
|
|
|
|
B: 180
|
2019-03-09 03:32:25 +00:00
|
|
|
};
|
|
|
|
let timeout = t[cmd];
|
|
|
|
if (typeof timeout === 'undefined')
|
|
|
|
timeout = 10;
|
|
|
|
arduino.timer = +new Date();
|
2019-06-09 01:43:14 +00:00
|
|
|
await delay_1.delay(timeout);
|
2019-03-05 03:10:15 +00:00
|
|
|
arduino.end(serial, cmd);
|
|
|
|
return cb();
|
2019-03-09 03:32:25 +00:00
|
|
|
},
|
|
|
|
string: async function (str) {
|
|
|
|
//do nothing
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
fake: true
|
|
|
|
};
|
2023-06-13 02:45:25 +00:00
|
|
|
//this.log.info('Connected to fake arduino! Not real! Does not exist!')
|
2019-03-09 03:32:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Connect to an Arduino using async/await
|
|
|
|
*
|
|
|
|
* @param {string} device Arduino identifier
|
|
|
|
*
|
|
|
|
* @returns {Promise} Resolves after opening
|
|
|
|
**/
|
|
|
|
async openArduino(device) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2023-07-31 02:13:50 +00:00
|
|
|
return this.serial[this.alias[device]].open((err) => {
|
2019-03-09 03:32:25 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
return resolve(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Close a connection to an Arduino using async/await
|
|
|
|
*
|
|
|
|
* @param {string} device Arduino identifier
|
|
|
|
*
|
|
|
|
* @returns {Promise} Resolves after closing
|
|
|
|
**/
|
|
|
|
async closeArduino(device) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2023-07-31 02:13:50 +00:00
|
|
|
return this.serial[this.alias[device]].close((err) => {
|
2019-03-09 03:32:25 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
return resolve(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2023-07-31 02:13:50 +00:00
|
|
|
lock(serial) {
|
|
|
|
//this.log.info(`Locked serial ${serial}`)
|
|
|
|
this.locks[serial] = true;
|
|
|
|
}
|
|
|
|
unlock(serial) {
|
|
|
|
//this.log.info(`Unlocked serial ${serial}`)
|
|
|
|
this.locks[serial] = false;
|
|
|
|
}
|
|
|
|
isLocked(serial) {
|
|
|
|
return typeof this.locks[serial] !== 'undefined' && this.locks[serial] === true;
|
|
|
|
}
|
2019-03-09 03:32:25 +00:00
|
|
|
}
|
2016-04-12 06:19:35 +00:00
|
|
|
if (typeof module !== 'undefined' && module.parent) {
|
2020-09-29 17:57:59 +00:00
|
|
|
module.exports = function (c, ee, errorState) {
|
2019-03-05 03:10:15 +00:00
|
|
|
eventEmitter = ee;
|
|
|
|
cfg = c;
|
2020-09-29 17:57:59 +00:00
|
|
|
arduino = new Arduino(errorState);
|
2019-03-05 03:10:15 +00:00
|
|
|
return arduino;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
//# sourceMappingURL=index.js.map
|