2019-03-05 03:09:12 +00:00
|
|
|
'use strict'
|
|
|
|
|
2019-03-21 22:03:53 +00:00
|
|
|
//import Log = require('log');
|
2019-06-09 01:43:14 +00:00
|
|
|
import { delay } from 'delay';
|
2019-03-21 22:03:53 +00:00
|
|
|
|
2019-03-05 03:09:12 +00:00
|
|
|
const SerialPort = require('serialport')
|
|
|
|
const Readline = SerialPort.parsers.Readline
|
|
|
|
const exec = require('child_process').exec
|
|
|
|
|
2019-03-08 16:27:24 +00:00
|
|
|
const parser : any = new Readline('')
|
2019-03-05 03:09:12 +00:00
|
|
|
const newlineRe : RegExp = new RegExp('\n', 'g')
|
|
|
|
const returnRe : RegExp = new RegExp('\r', 'g')
|
|
|
|
|
|
|
|
let eventEmitter : any
|
2019-03-08 16:27:24 +00:00
|
|
|
let cfg : any
|
|
|
|
let arduino : any
|
|
|
|
|
|
|
|
const KNOWN : string[] = [
|
|
|
|
'/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-03-05 03:09:12 +00:00
|
|
|
|
|
|
|
/**
|
2019-03-08 16:27:24 +00:00
|
|
|
* Class representing the arduino communication features
|
|
|
|
**/
|
2019-03-05 03:09:12 +00:00
|
|
|
|
2019-03-08 16:27:24 +00:00
|
|
|
class Arduino {
|
2019-03-05 03:09:12 +00:00
|
|
|
|
2020-08-18 18:20:43 +00:00
|
|
|
private log : any;
|
2019-03-21 18:58:52 +00:00
|
|
|
private path : any = {}
|
|
|
|
private known : string[] = KNOWN
|
|
|
|
private alias : any = {}
|
|
|
|
private serial : any = { connect : {}, projector : {}, camera : {}, light : {} }
|
|
|
|
private baud : number = 57600
|
|
|
|
private queue : any = {}
|
|
|
|
private timer : number = 0
|
|
|
|
private lock : boolean = false
|
|
|
|
private locks : any = {}
|
|
|
|
private confirmExec : any
|
2019-03-05 03:09:12 +00:00
|
|
|
|
2019-03-08 16:27:24 +00:00
|
|
|
constructor () {
|
2020-08-18 18:20:43 +00:00
|
|
|
this.init()
|
2019-03-08 16:27:24 +00:00
|
|
|
}
|
2020-08-18 18:20:43 +00:00
|
|
|
|
|
|
|
async init () {
|
|
|
|
const Log = require('log');
|
|
|
|
this.log = await Log({ label : 'arduino' });
|
|
|
|
}
|
|
|
|
|
2019-03-22 01:02:28 +00:00
|
|
|
/**
|
|
|
|
* Enumerate all connected devices that might be Arduinos
|
|
|
|
*
|
|
|
|
* @returns {Promise} Resolves after enumerating
|
|
|
|
**/
|
2019-03-08 16:27:24 +00:00
|
|
|
async enumerate () {
|
2019-12-18 22:17:48 +00:00
|
|
|
let ports : any[]
|
|
|
|
let matches : string[] = []
|
|
|
|
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 : any) => { return port.path }).join(','))
|
2019-12-18 22:17:48 +00:00
|
|
|
ports.forEach((port : any) => {
|
|
|
|
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:09:12 +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:09:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 16:27:24 +00:00
|
|
|
/**
|
|
|
|
* Send a command to an Arduino using async/await
|
|
|
|
*
|
|
|
|
* @param {string} device Arduino identifier
|
|
|
|
* @param {string} cmd Single character command to send
|
|
|
|
*
|
|
|
|
* @returns {Promise} Resolves after sending
|
|
|
|
**/
|
|
|
|
async sendAsync (device : string, cmd : string) {
|
|
|
|
return new Promise ((resolve, reject) => {
|
|
|
|
this.queue[cmd] = (ms : number) => {
|
|
|
|
return resolve(ms)
|
2019-03-05 03:09:12 +00:00
|
|
|
}
|
2019-03-08 16:27:24 +00:00
|
|
|
return this.serial[device].write(cmd, (err : any, results : any) => {
|
|
|
|
if (err) {
|
|
|
|
//console.error(err)
|
|
|
|
return reject(err)
|
2019-03-05 03:09:12 +00:00
|
|
|
}
|
2019-03-08 16:27:24 +00:00
|
|
|
//
|
2019-03-05 03:09:12 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:27:24 +00:00
|
|
|
async send (serial : string, cmd : string) {
|
|
|
|
const device : any = this.alias[serial]
|
|
|
|
let results : any
|
2020-08-18 18:20:43 +00:00
|
|
|
console.log(`${cmd} -> ${serial}`)
|
2019-03-08 16:27:24 +00:00
|
|
|
if (this.locks[serial]) {
|
|
|
|
return false
|
|
|
|
}
|
2019-04-04 22:49:07 +00:00
|
|
|
this.timer = new Date().getTime()
|
2019-03-08 16:27:24 +00:00
|
|
|
this.locks[serial] = true
|
|
|
|
await delay(cfg.arduino.serialDelay)
|
2019-03-05 03:09:12 +00:00
|
|
|
try {
|
2019-03-08 16:27:24 +00:00
|
|
|
results = await this.sendAsync(device, cmd)
|
2019-03-05 03:09:12 +00:00
|
|
|
} catch (e) {
|
|
|
|
return console.error(e)
|
|
|
|
}
|
2019-03-08 16:27:24 +00:00
|
|
|
this.locks[serial] = false
|
2019-04-04 22:49:07 +00:00
|
|
|
|
|
|
|
await eventEmitter.emit('arduino_send', cmd)
|
|
|
|
return results
|
2019-03-05 03:09:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 16:27:24 +00:00
|
|
|
async string (serial : string, str : string) {
|
|
|
|
const device : any = this.alias[serial]
|
|
|
|
let writeSuccess : any
|
|
|
|
await delay(cfg.arduino.serialDelay)
|
|
|
|
if (typeof this.serial[device].fake !== 'undefined'
|
|
|
|
&& this.serial[device].fake) {
|
|
|
|
return this.serial[device].string(str)
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
writeSuccess = await this.writeAsync(device, str)
|
|
|
|
} catch (e) {
|
|
|
|
return console.error(e)
|
|
|
|
}
|
|
|
|
return writeSuccess
|
2019-03-05 03:09:12 +00:00
|
|
|
}
|
2019-03-08 16:27:24 +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 : string, str : string) {
|
|
|
|
return new Promise ((resolve, reject) => {
|
|
|
|
this.serial[device].write(str, function (err : any, results : any) {
|
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
return resolve(results)
|
2019-03-05 03:09:12 +00:00
|
|
|
})
|
2019-03-08 16:27:24 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
end (serial : string, data : string) {
|
2019-04-04 22:49:07 +00:00
|
|
|
const end = new Date().getTime();
|
|
|
|
const ms = end - this.timer;
|
|
|
|
let complete;
|
2019-03-08 16:27:24 +00:00
|
|
|
if (this.queue[data] !== undefined) {
|
|
|
|
this.locks[serial] = false;
|
2019-04-04 22:49:07 +00:00
|
|
|
complete = this.queue[data](ms); //execute callback
|
|
|
|
eventEmitter.emit('arduino_end', data);
|
|
|
|
delete this.queue[data];
|
2019-03-05 03:09:12 +00:00
|
|
|
} else {
|
2019-03-08 16:27:24 +00:00
|
|
|
//console.log('Received stray "' + data + '"'); //silent to user
|
2019-03-05 03:09:12 +00:00
|
|
|
}
|
2019-04-04 22:49:07 +00:00
|
|
|
return ms;
|
2019-03-08 16:27:24 +00:00
|
|
|
}
|
2019-03-05 03:09:12 +00:00
|
|
|
|
2019-03-08 16:27:24 +00:00
|
|
|
aliasSerial (serial : string, device : string) {
|
2019-04-04 22:49:07 +00:00
|
|
|
//this.log.info(`Making "${serial}" an alias of ${device}`);
|
|
|
|
this.alias[serial] = device;
|
2019-03-05 03:09:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 16:27:24 +00:00
|
|
|
async connect (serial : string, device : string, confirm : any) {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
let connectSuccess : any
|
|
|
|
this.path[serial] = device;
|
|
|
|
this.alias[serial] = device;
|
|
|
|
this.serial[device] = new SerialPort(this.path[serial], {
|
|
|
|
autoOpen : false,
|
|
|
|
baudRate: cfg.arduino.baud,
|
|
|
|
parser: parser
|
|
|
|
})
|
|
|
|
this.locks[device] = false
|
|
|
|
try {
|
|
|
|
connectSuccess = await this.openArduino(device)
|
|
|
|
} catch (e) {
|
|
|
|
console.error('failed to open: ' + e)
|
|
|
|
return reject(e)
|
|
|
|
}
|
2019-03-21 19:59:50 +00:00
|
|
|
//console.log(`Opened connection with ${this.path[serial]} as ${serial}`);
|
2019-03-08 16:27:24 +00:00
|
|
|
if (!confirm) {
|
|
|
|
this.serial[device].on('data', async (data : Buffer) => {
|
|
|
|
let d = data.toString('utf8')
|
|
|
|
d = d.replace(newlineRe, '').replace(returnRe, '')
|
|
|
|
return this.end(serial, d)
|
|
|
|
})
|
2019-03-05 03:09:12 +00:00
|
|
|
} else {
|
2019-03-08 16:27:24 +00:00
|
|
|
this.serial[device].on('data', async (data : Buffer) => {
|
|
|
|
let d = data.toString('utf8')
|
|
|
|
d = d.replace(newlineRe, '').replace(returnRe, '')
|
|
|
|
return await this.confirmEnd(d)
|
|
|
|
})
|
2019-03-05 03:09:12 +00:00
|
|
|
}
|
2019-03-08 16:27:24 +00:00
|
|
|
return resolve(this.path[serial])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
confirmEnd (data : string) {
|
|
|
|
//console.dir(data)
|
2019-03-18 21:21:17 +00:00
|
|
|
if ( data === cfg.arduino.cmd.connect
|
2019-03-22 01:02:28 +00:00
|
|
|
|| data === cfg.arduino.cmd.projector_identifier
|
|
|
|
|| data === cfg.arduino.cmd.camera_identifier
|
2019-03-08 16:27:24 +00:00
|
|
|
|| data === cfg.arduino.cmd.light_identifier
|
2019-03-22 01:02:28 +00:00
|
|
|
|| data === cfg.arduino.cmd.projector_light_identifier
|
|
|
|
|| data === cfg.arduino.cmd.projector_camera_light_identifier
|
|
|
|
|| data === cfg.arduino.cmd.projector_camera_identifier
|
2019-03-18 21:21:17 +00:00
|
|
|
|
2019-03-22 01:02:28 +00:00
|
|
|
|| data === cfg.arduino.cmd.projector_second_identifier
|
|
|
|
|| data === cfg.arduino.cmd.projectors_identifier
|
|
|
|
|| data === cfg.arduino.cmd.projector_second_forward
|
|
|
|
|| data === cfg.arduino.cmd.projector_second_backward
|
2019-03-18 21:21:17 +00:00
|
|
|
|| data === cfg.arduino.cmd.projector_second
|
|
|
|
|| data === cfg.arduino.cmd.projectors
|
|
|
|
|
2019-03-22 01:02:28 +00:00
|
|
|
|| data === cfg.arduino.cmd.camera_second_identifier
|
|
|
|
|| data === cfg.arduino.cmd.cameras_identifier
|
|
|
|
|| data === cfg.arduino.cmd.camera_second_forward
|
|
|
|
|| data === cfg.arduino.cmd.camera_second_backward
|
2019-03-18 21:21:17 +00:00
|
|
|
|| data === cfg.arduino.cmd.camera_second
|
|
|
|
|| data === cfg.arduino.cmd.cameras) {
|
|
|
|
|
2019-03-08 16:27:24 +00:00
|
|
|
this.confirmExec(null, data);
|
|
|
|
this.confirmExec = {};
|
2019-03-05 03:09:12 +00:00
|
|
|
}
|
2019-03-08 16:27:24 +00:00
|
|
|
}
|
2019-03-05 03:09:12 +00:00
|
|
|
|
2019-03-08 16:27:24 +00:00
|
|
|
async verify () {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
const device : any = this.alias['connect']
|
|
|
|
let writeSuccess : any
|
|
|
|
this.confirmExec = function (err : any, data : string) {
|
|
|
|
if (data === cfg.arduino.cmd.connect) {
|
|
|
|
return resolve(true)
|
|
|
|
} else {
|
|
|
|
return reject('Wrong data returned')
|
|
|
|
}
|
2019-03-05 03:09:12 +00:00
|
|
|
}
|
2019-03-08 16:27:24 +00:00
|
|
|
|
|
|
|
await delay(cfg.arduino.serialDelay)
|
|
|
|
|
|
|
|
try {
|
|
|
|
writeSuccess = await this.sendAsync(device, cfg.arduino.cmd.connect)
|
|
|
|
} catch (e) {
|
|
|
|
return reject(e)
|
|
|
|
}
|
|
|
|
return resolve(writeSuccess)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async distinguish () {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
const device : any = this.alias['connect']
|
|
|
|
let writeSuccess : any
|
|
|
|
let type : string
|
|
|
|
this.confirmExec = function (err : any, data : string) {
|
2019-03-22 01:02:28 +00:00
|
|
|
if (data === cfg.arduino.cmd.projector_identifier) {
|
2019-03-08 16:27:24 +00:00
|
|
|
type = 'projector'
|
2019-03-22 01:02:28 +00:00
|
|
|
} else if (data === cfg.arduino.cmd.camera_identifier) {
|
2019-03-08 16:27:24 +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-08 16:27:24 +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-08 16:27:24 +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-08 16:27:24 +00:00
|
|
|
type = 'projector,camera'
|
2019-04-04 22:49:07 +00:00
|
|
|
} else if (data === cfg.arduino.cmd.projector_second_identifier) {
|
2019-03-08 16:27:24 +00:00
|
|
|
type = 'projector_second'
|
2019-04-04 22:49:07 +00:00
|
|
|
} else if (data === cfg.arduino.cmd.projectors_identifier) {
|
2019-03-21 19:56:33 +00:00
|
|
|
type = 'projector,projector_second'
|
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-21 19:56:33 +00:00
|
|
|
type = 'camera,camera_second'
|
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'
|
2019-03-08 16:27:24 +00:00
|
|
|
}
|
|
|
|
return resolve(type)
|
|
|
|
}
|
|
|
|
await delay(cfg.arduino.serialDelay)
|
|
|
|
try {
|
|
|
|
writeSuccess = await this.sendAsync(device, cfg.arduino.cmd.mcopy_identifier)
|
|
|
|
} catch (e) {
|
|
|
|
return reject(e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async close () {
|
|
|
|
const device = this.alias['connect']
|
|
|
|
let closeSuccess
|
2019-03-05 03:09:12 +00:00
|
|
|
try {
|
2019-03-08 16:27:24 +00:00
|
|
|
closeSuccess = await this.closeArduino(device)
|
2019-03-05 03:09:12 +00:00
|
|
|
} catch (e) {
|
2019-03-21 19:59:50 +00:00
|
|
|
throw e;
|
2019-03-05 03:09:12 +00:00
|
|
|
}
|
2019-03-08 16:27:24 +00:00
|
|
|
return closeSuccess
|
|
|
|
};
|
2019-03-05 03:09:12 +00:00
|
|
|
|
2019-03-08 16:27:24 +00:00
|
|
|
async fakeConnect (serial : string) {
|
|
|
|
const device : string = '/dev/fake'
|
|
|
|
this.alias[serial] = device
|
|
|
|
this.serial[device] = {
|
|
|
|
write : async function (cmd : string, cb : any) {
|
|
|
|
const t : any = {
|
|
|
|
c : cfg.arduino.cam.time + cfg.arduino.cam.delay,
|
|
|
|
p : cfg.arduino.proj.time + cfg.arduino.proj.delay
|
|
|
|
}
|
|
|
|
let timeout : number = t[cmd]
|
|
|
|
if (typeof timeout === 'undefined') timeout = 10
|
|
|
|
arduino.timer = +new Date()
|
2019-03-05 03:09:12 +00:00
|
|
|
|
2019-03-08 16:27:24 +00:00
|
|
|
await delay(timeout)
|
|
|
|
|
2019-03-05 03:09:12 +00:00
|
|
|
arduino.end(serial, cmd)
|
|
|
|
return cb()
|
|
|
|
|
2019-03-08 16:27:24 +00:00
|
|
|
},
|
|
|
|
string : async function (str : string) {
|
|
|
|
//do nothing
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
fake : true
|
|
|
|
};
|
|
|
|
//console.log('Connected to fake arduino! Not real! Does not exist!');
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to an Arduino using async/await
|
|
|
|
*
|
|
|
|
* @param {string} device Arduino identifier
|
|
|
|
*
|
|
|
|
* @returns {Promise} Resolves after opening
|
|
|
|
**/
|
|
|
|
async openArduino (device : string) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return this.serial[device].open((err : any) => {
|
|
|
|
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 : string) {
|
|
|
|
return new Promise((resolve : any, reject : any) => {
|
|
|
|
return this.serial[device].close((err : any) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
return resolve(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-03-05 03:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined' && module.parent) {
|
2019-03-08 16:27:24 +00:00
|
|
|
module.exports = function (c : any, ee : any) {
|
2019-03-05 03:09:12 +00:00
|
|
|
eventEmitter = ee
|
|
|
|
cfg = c
|
2019-03-08 16:27:24 +00:00
|
|
|
arduino = new Arduino()
|
2019-03-05 03:09:12 +00:00
|
|
|
return arduino
|
|
|
|
}
|
|
|
|
}
|