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