canon_ble #82

Merged
mattmcw merged 149 commits from canon_ble into main 2023-08-01 03:38:52 +00:00
7 changed files with 123 additions and 48 deletions
Showing only changes of commit 672db32917 - Show all commits

View File

@ -97,7 +97,7 @@ class Arduino {
};
return this.serial[device].write(cmd, (err, results) => {
if (err) {
//console.error(err)
//this.log.error(err)
return reject(err);
}
});
@ -106,7 +106,7 @@ class Arduino {
async send(serial, cmd) {
const device = this.alias[serial];
let results;
//console.log(`${cmd} -> ${serial}`)
//this.log.info(`${cmd} -> ${serial}`)
if (this.locks[serial]) {
return false;
}
@ -117,13 +117,13 @@ class Arduino {
results = await this.sendAsync(device, cmd);
}
catch (e) {
return console.error(e);
return this.log.error(e);
}
this.locks[serial] = false;
await eventEmitter.emit('arduino_send', cmd);
return results;
}
async string(serial, str) {
async sendString(serial, str) {
const device = this.alias[serial];
let writeSuccess;
await delay_1.delay(cfg.arduino.serialDelay);
@ -136,11 +136,44 @@ class Arduino {
writeSuccess = await this.writeAsync(device, str);
}
catch (e) {
return console.error(e);
return this.log.error(e);
}
return writeSuccess;
}
}
async stateAsync(device) {
const cmd = cfg.arduino.cmd.state;
return new Promise((resolve, reject) => {
this.queue[cmd] = (ms) => {
return resolve(ms);
};
return this.serial[device].write(cmd, (err, results) => {
if (err) {
//this.log.error(err)
return reject(err);
}
});
});
}
async state(serial) {
const device = this.alias[serial];
let results;
if (this.locks[serial]) {
return -1;
}
this.timer = new Date().getTime();
this.locks[serial] = true;
await delay_1.delay(cfg.arduino.serialDelay);
try {
results = await this.stateAsync(device);
}
catch (e) {
return this.log.error(e);
}
this.locks[serial] = false;
await eventEmitter.emit('arduino_state', cfg.arduino.cmd.state);
return results;
}
/**
* Send a string to an Arduino using async/await
*
@ -163,25 +196,28 @@ class Arduino {
const end = new Date().getTime();
const ms = end - this.timer;
let complete;
//console.log(`${serial} -> ${data}`);
//this.log.info(`${serial} -> ${data}`);
if (this.queue[data] !== undefined) {
this.locks[serial] = false;
complete = this.queue[data](ms); //execute callback
eventEmitter.emit('arduino_end', data);
delete this.queue[data];
}
else if (data === 'E') {
else if (data[0] === cfg.arduino.cmd.state) {
complete = this.queue[data](ms);
}
else if (data[0] === cfg.arduino.cmd.error) {
//error state
//stop sequence
//throw error in ui
}
else {
//console.log('Received stray "' + data + '"'); //silent to user
//this.log.info('Received stray "' + data + '"') //silent to user
}
return ms;
}
aliasSerial(serial, device) {
//this.log.info(`Making "${serial}" an alias of ${device}`);
//this.log.info(`Making "${serial}" an alias of ${device}`)
this.alias[serial] = device;
}
async connect(serial, device, confirm) {
@ -200,10 +236,10 @@ class Arduino {
connectSuccess = await this.openArduino(device);
}
catch (e) {
console.error('failed to open: ' + e);
this.log.error('failed to open: ' + e);
return reject(e);
}
//console.log(`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');
@ -222,7 +258,7 @@ class Arduino {
});
}
confirmEnd(data) {
//console.dir(data)
//this.log.info(data)
if (data === cfg.arduino.cmd.connect
|| data === cfg.arduino.cmd.projector_identifier
|| data === cfg.arduino.cmd.camera_identifier
@ -352,7 +388,6 @@ class Arduino {
}
return closeSuccess;
}
;
async fakeConnect(serial) {
const device = '/dev/fake';
this.alias[serial] = device;
@ -378,7 +413,7 @@ class Arduino {
},
fake: true
};
//console.log('Connected to fake arduino! Not real! Does not exist!');
//this.log.info('Connected to fake arduino! Not real! Does not exist!')
return true;
}
/**

File diff suppressed because one or more lines are too long

View File

@ -64,7 +64,7 @@ class Light {
}
await delay_1.delay(1);
try {
this.arduino.string(this.id, str);
this.arduino.sendString(this.id, str);
}
catch (err) {
this.log.error('Error sending light string', err);

View File

@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/light/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,iCAA8B;AAC9B,2BAA4B;AAE5B,MAAM,KAAK;IAYV;;QAEI;IACJ,YAAa,OA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105,7 +105,7 @@ class Arduino {
}
return this.serial[device].write(cmd, (err : any, results : any) => {
if (err) {
//console.error(err)
//this.log.error(err)
return reject(err)
}
})
@ -115,7 +115,7 @@ class Arduino {
async send (serial : string, cmd : string) {
const device : any = this.alias[serial]
let results : any
//console.log(`${cmd} -> ${serial}`)
//this.log.info(`${cmd} -> ${serial}`)
if (this.locks[serial]) {
return false
}
@ -125,7 +125,7 @@ class Arduino {
try {
results = await this.sendAsync(device, cmd)
} catch (e) {
return console.error(e)
return this.log.error(e)
}
this.locks[serial] = false
@ -133,7 +133,7 @@ class Arduino {
return results
}
async string (serial : string, str : string) {
async sendString (serial : string, str : string) : Promise<any> {
const device : any = this.alias[serial]
let writeSuccess : any
await delay(cfg.arduino.serialDelay)
@ -144,12 +144,48 @@ class Arduino {
try {
writeSuccess = await this.writeAsync(device, str)
} catch (e) {
return console.error(e)
return this.log.error(e)
}
return writeSuccess
}
}
async stateAsync (device : string) : Promise<number> {
const cmd : string = cfg.arduino.cmd.state
return new Promise ((resolve, reject) => {
this.queue[cmd] = (ms : number) => {
return resolve(ms)
}
return this.serial[device].write(cmd, (err : any, results : any) => {
if (err) {
//this.log.error(err)
return reject(err)
}
})
})
}
async state (serial : string) : Promise<number>{
const device : any = this.alias[serial]
let results : number
if (this.locks[serial]) {
return -1
}
this.timer = new Date().getTime()
this.locks[serial] = true
await delay(cfg.arduino.serialDelay)
try {
results = await this.stateAsync(device)
} catch (e) {
return this.log.error(e)
}
this.locks[serial] = false
await eventEmitter.emit('arduino_state', cfg.arduino.cmd.state)
return results
}
/**
* Send a string to an Arduino using async/await
*
@ -158,7 +194,7 @@ class Arduino {
*
* @returns {Promise} Resolves after sending
**/
async writeAsync (device : string, str : string) {
async writeAsync (device : string, str : string) : Promise<any> {
return new Promise ((resolve, reject) => {
this.serial[device].write(str, function (err : any, results : any) {
if (err) {
@ -169,32 +205,34 @@ class Arduino {
})
}
end (serial : string, data : string) {
const end : number = new Date().getTime();
const ms : number = end - this.timer;
let complete : any;
//console.log(`${serial} -> ${data}`);
end (serial : string, data : string) : number {
const end : number = new Date().getTime()
const ms : number = end - this.timer
let complete : any
//this.log.info(`${serial} -> ${data}`);
if (this.queue[data] !== undefined) {
this.locks[serial] = false;
complete = this.queue[data](ms); //execute callback
eventEmitter.emit('arduino_end', data);
delete this.queue[data];
} else if (data === 'E') {
this.locks[serial] = false
complete = this.queue[data](ms) //execute callback
eventEmitter.emit('arduino_end', data)
delete this.queue[data]
} else if (data[0] === cfg.arduino.cmd.state) {
complete = this.queue[data](ms)
} else if (data[0] === cfg.arduino.cmd.error) {
//error state
//stop sequence
//throw error in ui
} else {
//console.log('Received stray "' + data + '"'); //silent to user
//this.log.info('Received stray "' + data + '"') //silent to user
}
return ms;
return ms
}
aliasSerial (serial : string, device : string) {
//this.log.info(`Making "${serial}" an alias of ${device}`);
//this.log.info(`Making "${serial}" an alias of ${device}`)
this.alias[serial] = device;
}
async connect (serial : string, device : string, confirm : any) {
async connect (serial : string, device : string, confirm : any) : Promise<any> {
return new Promise(async (resolve, reject) => {
let connectSuccess : any
this.path[serial] = device
@ -209,10 +247,10 @@ class Arduino {
try {
connectSuccess = await this.openArduino(device)
} catch (e) {
console.error('failed to open: ' + e)
this.log.error('failed to open: ' + e)
return reject(e)
}
//console.log(`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')
@ -231,7 +269,7 @@ class Arduino {
}
confirmEnd (data : string) {
//console.dir(data)
//this.log.info(data)
if ( data === cfg.arduino.cmd.connect
|| data === cfg.arduino.cmd.projector_identifier
|| data === cfg.arduino.cmd.camera_identifier
@ -259,8 +297,8 @@ class Arduino {
|| data === cfg.arduino.cmd.camera_capper_projector_identifier
|| data === cfg.arduino.cmd.camera_capper_projectors_identifier) {
this.confirmExec(null, data);
this.confirmExec = {};
this.confirmExec(null, data)
this.confirmExec = {}
}
}
@ -348,10 +386,10 @@ class Arduino {
try {
closeSuccess = await this.closeArduino(device)
} catch (e) {
throw e;
throw e
}
return closeSuccess
};
}
async fakeConnect (serial : string) {
const device : string = '/dev/fake'
@ -379,8 +417,8 @@ class Arduino {
return true
},
fake : true
};
//console.log('Connected to fake arduino! Not real! Does not exist!');
}
//this.log.info('Connected to fake arduino! Not real! Does not exist!')
return true
}

4
src/globals.d.ts vendored
View File

@ -20,12 +20,14 @@ interface Device {
interface Arduino {
send (id : string, cmd : string) : number;
string (id : string, str : string) : any;
sendString (id : string, str : string) : any;
enumerate () : any;
connect (id : string, device : Device, state : boolean) : any;
verify () : any;
distinguish () : any;
fakeConnect ( id : string) : any;
stateAsync (device : string) : any;
state (serial: string) : number;
close () : any;
aliasSerial ( id : string, device : Device) : any;
}

View File

@ -75,7 +75,7 @@ class Light {
}
await delay(1);
try {
this.arduino.string(this.id, str);
this.arduino.sendString(this.id, str);
} catch (err) {
this.log.error('Error sending light string', err);
}