canon_ble #82
|
@ -1,5 +1,17 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
**/
|
||||||
//import Log = require('log');
|
//import Log = require('log');
|
||||||
const delay_1 = require("delay");
|
const delay_1 = require("delay");
|
||||||
const { SerialPort } = require('serialport');
|
const { SerialPort } = require('serialport');
|
||||||
|
@ -93,7 +105,7 @@ class Arduino {
|
||||||
**/
|
**/
|
||||||
async sendAsync(device, cmd) {
|
async sendAsync(device, cmd) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.log.info(`${device} -> ${cmd}`);
|
this.log.info(`sendAsyc ${device} -> ${cmd}`);
|
||||||
this.queue[cmd] = (ms) => {
|
this.queue[cmd] = (ms) => {
|
||||||
return resolve(ms);
|
return resolve(ms);
|
||||||
};
|
};
|
||||||
|
@ -105,6 +117,9 @@ class Arduino {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
**/
|
||||||
async send(serial, cmd) {
|
async send(serial, cmd) {
|
||||||
const device = this.alias[serial];
|
const device = this.alias[serial];
|
||||||
let results;
|
let results;
|
||||||
|
@ -126,6 +141,9 @@ class Arduino {
|
||||||
await eventEmitter.emit('arduino_send', cmd);
|
await eventEmitter.emit('arduino_send', cmd);
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
**/
|
||||||
async sendString(serial, str) {
|
async sendString(serial, str) {
|
||||||
const device = this.alias[serial];
|
const device = this.alias[serial];
|
||||||
let writeSuccess;
|
let writeSuccess;
|
||||||
|
@ -135,6 +153,7 @@ class Arduino {
|
||||||
return this.serial[device].string(str);
|
return this.serial[device].string(str);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
this.log.info(`sendString ${device} -> ${str}`);
|
||||||
try {
|
try {
|
||||||
writeSuccess = await this.writeAsync(device, str);
|
writeSuccess = await this.writeAsync(device, str);
|
||||||
}
|
}
|
||||||
|
@ -144,6 +163,9 @@ class Arduino {
|
||||||
return writeSuccess;
|
return writeSuccess;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
**/
|
||||||
async stateAsync(device, confirm = false) {
|
async stateAsync(device, confirm = false) {
|
||||||
const cmd = cfg.arduino.cmd.state;
|
const cmd = cfg.arduino.cmd.state;
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@ -164,7 +186,7 @@ class Arduino {
|
||||||
}
|
}
|
||||||
}.bind(this), 1000);
|
}.bind(this), 1000);
|
||||||
}
|
}
|
||||||
//this.log.info(`${device} -> ${cmd}`)
|
this.log.info(`stateAsync ${device} -> ${cmd}`);
|
||||||
return this.serial[device].write(cmd, (err, results) => {
|
return this.serial[device].write(cmd, (err, results) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
//this.log.error(err)
|
//this.log.error(err)
|
||||||
|
@ -173,11 +195,14 @@ class Arduino {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
**/
|
||||||
async state(device, confirm = false) {
|
async state(device, confirm = false) {
|
||||||
const serial = confirm ? this.alias['connect'] : this.alias[device];
|
const serial = confirm ? this.alias['connect'] : this.alias[device];
|
||||||
let results;
|
let results;
|
||||||
this.log.info(serial);
|
this.log.info(`state device ${device}`);
|
||||||
this.log.info(device);
|
this.log.info(`state serial ${serial}`);
|
||||||
console.dir(this.locks);
|
console.dir(this.locks);
|
||||||
if (typeof this.locks[serial] !== 'undefined' && this.locks[serial] === true) {
|
if (typeof this.locks[serial] !== 'undefined' && this.locks[serial] === true) {
|
||||||
this.log.info("Serial is locked");
|
this.log.info("Serial is locked");
|
||||||
|
@ -216,6 +241,9 @@ class Arduino {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
**/
|
||||||
end(serial, data) {
|
end(serial, data) {
|
||||||
const end = new Date().getTime();
|
const end = new Date().getTime();
|
||||||
const ms = end - this.timer;
|
const ms = end - this.timer;
|
||||||
|
@ -228,6 +256,7 @@ class Arduino {
|
||||||
delete this.queue[data];
|
delete this.queue[data];
|
||||||
}
|
}
|
||||||
else if (data[0] === cfg.arduino.cmd.state) {
|
else if (data[0] === cfg.arduino.cmd.state) {
|
||||||
|
this.log.info(`end serial -> ${serial}`);
|
||||||
this.locks[serial] = false;
|
this.locks[serial] = false;
|
||||||
complete = this.queue[cfg.arduino.cmd.state](data);
|
complete = this.queue[cfg.arduino.cmd.state](data);
|
||||||
eventEmitter.emit('arduino_end', data);
|
eventEmitter.emit('arduino_end', data);
|
||||||
|
@ -251,6 +280,8 @@ class Arduino {
|
||||||
this.alias[serial] = device;
|
this.alias[serial] = device;
|
||||||
}
|
}
|
||||||
async connect(serial, device, confirm) {
|
async connect(serial, device, confirm) {
|
||||||
|
this.log.info(`connect device ${device}`);
|
||||||
|
this.log.info(`connect serial ${serial}`);
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let connectSuccess;
|
let connectSuccess;
|
||||||
this.path[serial] = device;
|
this.path[serial] = device;
|
||||||
|
@ -269,7 +300,7 @@ class Arduino {
|
||||||
this.log.error('failed to open: ' + e);
|
this.log.error('failed to open: ' + e);
|
||||||
return reject(e);
|
return reject(e);
|
||||||
}
|
}
|
||||||
//this.log.info(`Opened connection with ${this.path[serial]} as ${serial}`)
|
this.log.info(`Opened connection with ${this.path[serial]} as ${serial}`);
|
||||||
if (!confirm) {
|
if (!confirm) {
|
||||||
this.serial[device].on('data', async (data) => {
|
this.serial[device].on('data', async (data) => {
|
||||||
let d = data.toString('utf8');
|
let d = data.toString('utf8');
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -156,8 +156,8 @@ class Devices {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
async fakeCamera() {
|
async fakeCamera() {
|
||||||
this.connected.camera = '/dev/fake';
|
this.connected.camera = '/dev/fake';
|
||||||
try {
|
try {
|
||||||
|
@ -204,8 +204,8 @@ class Devices {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
async connectDevice(device, type) {
|
async connectDevice(device, type) {
|
||||||
let closeSuccess;
|
let closeSuccess;
|
||||||
let connectSuccess;
|
let connectSuccess;
|
||||||
|
@ -434,8 +434,8 @@ class Devices {
|
||||||
return connectSuccess;
|
return connectSuccess;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
//Cases for 1 or 2 arduinos connected
|
//Cases for 1 or 2 arduinos connected
|
||||||
async all(devices) {
|
async all(devices) {
|
||||||
let c = {};
|
let c = {};
|
||||||
|
@ -505,8 +505,8 @@ class Devices {
|
||||||
return this.ready(p, c, l, cs, ps, capper);
|
return this.ready(p, c, l, cs, ps, capper);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
remember(which, device, type) {
|
remember(which, device, type) {
|
||||||
let deviceEntry;
|
let deviceEntry;
|
||||||
const match = this.settings.state.devices.filter((dev) => {
|
const match = this.settings.state.devices.filter((dev) => {
|
||||||
|
@ -525,8 +525,8 @@ class Devices {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
ready(projector, camera, light, camera_second, projector_second, capper) {
|
ready(projector, camera, light, camera_second, projector_second, capper) {
|
||||||
let args = {
|
let args = {
|
||||||
camera,
|
camera,
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -114,7 +114,7 @@ class Arduino {
|
||||||
**/
|
**/
|
||||||
async sendAsync (device : string, cmd : string) {
|
async sendAsync (device : string, cmd : string) {
|
||||||
return new Promise ((resolve, reject) => {
|
return new Promise ((resolve, reject) => {
|
||||||
this.log.info(`${device} -> ${cmd}`)
|
this.log.info(`sendAsyc ${device} -> ${cmd}`)
|
||||||
this.queue[cmd] = (ms : number) => {
|
this.queue[cmd] = (ms : number) => {
|
||||||
return resolve(ms)
|
return resolve(ms)
|
||||||
}
|
}
|
||||||
|
@ -127,6 +127,9 @@ class Arduino {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
**/
|
||||||
async send (serial : string, cmd : string) {
|
async send (serial : string, cmd : string) {
|
||||||
const device : any = this.alias[serial]
|
const device : any = this.alias[serial]
|
||||||
let results : any
|
let results : any
|
||||||
|
@ -149,6 +152,9 @@ class Arduino {
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
**/
|
||||||
async sendString (serial : string, str : string) : Promise<any> {
|
async sendString (serial : string, str : string) : Promise<any> {
|
||||||
const device : any = this.alias[serial]
|
const device : any = this.alias[serial]
|
||||||
let writeSuccess : any
|
let writeSuccess : any
|
||||||
|
@ -157,6 +163,7 @@ class Arduino {
|
||||||
&& this.serial[device].fake) {
|
&& this.serial[device].fake) {
|
||||||
return this.serial[device].string(str)
|
return this.serial[device].string(str)
|
||||||
} else {
|
} else {
|
||||||
|
this.log.info(`sendString ${device} -> ${str}`)
|
||||||
try {
|
try {
|
||||||
writeSuccess = await this.writeAsync(device, str)
|
writeSuccess = await this.writeAsync(device, str)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -166,6 +173,9 @@ class Arduino {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
**/
|
||||||
async stateAsync (device : string, confirm : boolean = false) : Promise<string> {
|
async stateAsync (device : string, confirm : boolean = false) : Promise<string> {
|
||||||
const cmd : string = cfg.arduino.cmd.state
|
const cmd : string = cfg.arduino.cmd.state
|
||||||
return new Promise ((resolve, reject) => {
|
return new Promise ((resolve, reject) => {
|
||||||
|
@ -186,7 +196,7 @@ class Arduino {
|
||||||
}
|
}
|
||||||
}.bind(this), 1000)
|
}.bind(this), 1000)
|
||||||
}
|
}
|
||||||
//this.log.info(`${device} -> ${cmd}`)
|
this.log.info(`stateAsync ${device} -> ${cmd}`)
|
||||||
return this.serial[device].write(cmd, (err : any, results : any) => {
|
return this.serial[device].write(cmd, (err : any, results : any) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
//this.log.error(err)
|
//this.log.error(err)
|
||||||
|
@ -196,11 +206,14 @@ class Arduino {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
**/
|
||||||
async state (device : string, confirm : boolean = false) : Promise<string>{
|
async state (device : string, confirm : boolean = false) : Promise<string>{
|
||||||
const serial : string = confirm ? this.alias['connect'] : this.alias[device]
|
const serial : string = confirm ? this.alias['connect'] : this.alias[device]
|
||||||
let results : string
|
let results : string
|
||||||
this.log.info(serial)
|
this.log.info(`state device ${device}`)
|
||||||
this.log.info(device)
|
this.log.info(`state serial ${serial}`)
|
||||||
console.dir(this.locks)
|
console.dir(this.locks)
|
||||||
if (typeof this.locks[serial] !== 'undefined' && this.locks[serial] === true) {
|
if (typeof this.locks[serial] !== 'undefined' && this.locks[serial] === true) {
|
||||||
this.log.info("Serial is locked")
|
this.log.info("Serial is locked")
|
||||||
|
@ -240,6 +253,9 @@ class Arduino {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
**/
|
||||||
end (serial : string, data : string) : any {
|
end (serial : string, data : string) : any {
|
||||||
const end : number = new Date().getTime()
|
const end : number = new Date().getTime()
|
||||||
const ms : number = end - this.timer
|
const ms : number = end - this.timer
|
||||||
|
@ -251,6 +267,7 @@ class Arduino {
|
||||||
eventEmitter.emit('arduino_end', data)
|
eventEmitter.emit('arduino_end', data)
|
||||||
delete this.queue[data]
|
delete this.queue[data]
|
||||||
} else if (data[0] === cfg.arduino.cmd.state) {
|
} else if (data[0] === cfg.arduino.cmd.state) {
|
||||||
|
this.log.info(`end serial -> ${serial}`)
|
||||||
this.locks[serial] = false
|
this.locks[serial] = false
|
||||||
complete = this.queue[cfg.arduino.cmd.state](data)
|
complete = this.queue[cfg.arduino.cmd.state](data)
|
||||||
eventEmitter.emit('arduino_end', data)
|
eventEmitter.emit('arduino_end', data)
|
||||||
|
@ -274,6 +291,8 @@ class Arduino {
|
||||||
}
|
}
|
||||||
|
|
||||||
async connect (serial : string, device : string, confirm : any) : Promise<any> {
|
async connect (serial : string, device : string, confirm : any) : Promise<any> {
|
||||||
|
this.log.info(`connect device ${device}`)
|
||||||
|
this.log.info(`connect serial ${serial}`)
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let connectSuccess : any
|
let connectSuccess : any
|
||||||
this.path[serial] = device
|
this.path[serial] = device
|
||||||
|
@ -291,7 +310,7 @@ class Arduino {
|
||||||
this.log.error('failed to open: ' + e)
|
this.log.error('failed to open: ' + e)
|
||||||
return reject(e)
|
return reject(e)
|
||||||
}
|
}
|
||||||
//this.log.info(`Opened connection with ${this.path[serial]} as ${serial}`)
|
this.log.info(`Opened connection with ${this.path[serial]} as ${serial}`)
|
||||||
if (!confirm) {
|
if (!confirm) {
|
||||||
this.serial[device].on('data', async (data : Buffer) => {
|
this.serial[device].on('data', async (data : Buffer) => {
|
||||||
let d = data.toString('utf8')
|
let d = data.toString('utf8')
|
||||||
|
|
|
@ -172,7 +172,7 @@ class Devices {
|
||||||
this.log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true)
|
this.log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
private async fakeCamera () {
|
private async fakeCamera () {
|
||||||
|
@ -218,7 +218,7 @@ class Devices {
|
||||||
this.log.info('Connected to fake CAPPER device', 'SERIAL', true, true)
|
this.log.info('Connected to fake CAPPER device', 'SERIAL', true, true)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
private async connectDevice (device : Device, type : any) {
|
private async connectDevice (device : Device, type : any) {
|
||||||
|
@ -419,7 +419,7 @@ class Devices {
|
||||||
}
|
}
|
||||||
return connectSuccess
|
return connectSuccess
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
//Cases for 1 or 2 arduinos connected
|
//Cases for 1 or 2 arduinos connected
|
||||||
|
@ -498,7 +498,7 @@ class Devices {
|
||||||
}
|
}
|
||||||
return this.ready(p, c, l, cs, ps, capper)
|
return this.ready(p, c, l, cs, ps, capper)
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
private remember (which : string, device : any, type : string) {
|
private remember (which : string, device : any, type : string) {
|
||||||
|
@ -518,7 +518,7 @@ class Devices {
|
||||||
this.settings.save()
|
this.settings.save()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
private ready (projector : any, camera : any, light : any, camera_second : any, projector_second : any, capper : any) {
|
private ready (projector : any, camera : any, light : any, camera_second : any, projector_second : any, capper : any) {
|
||||||
|
@ -566,7 +566,7 @@ class Devices {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue