canon_ble #82

Merged
mattmcw merged 149 commits from canon_ble into main 2023-08-01 03:38:52 +00:00
3 changed files with 65 additions and 17 deletions
Showing only changes of commit 71e4bea384 - Show all commits

View File

@ -30,6 +30,7 @@ class Arduino {
this.known = KNOWN; this.known = KNOWN;
this.alias = {}; this.alias = {};
this.serial = { connect: {}, projector: {}, camera: {}, light: {} }; this.serial = { connect: {}, projector: {}, camera: {}, light: {} };
this.hasState = { projector: false, camera: false, light: false };
this.baud = 57600; this.baud = 57600;
this.queue = {}; this.queue = {};
this.timer = 0; this.timer = 0;
@ -141,12 +142,26 @@ class Arduino {
return writeSuccess; return writeSuccess;
} }
} }
async stateAsync(device) { 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) => {
this.queue[cmd] = (ms) => { this.queue[cmd] = (state) => {
return resolve(ms); if (confirm) {
this.hasState[device] = true;
this.log.info(`Device ${device} supports state [${state}]`);
}
return resolve(state);
}; };
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);
}
}.bind(this), 100);
}
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)
@ -155,17 +170,17 @@ class Arduino {
}); });
}); });
} }
async state(serial) { async state(serial, confirm = false) {
const device = this.alias[serial]; const device = this.alias[serial];
let results; let results;
if (this.locks[serial]) { if (this.locks[serial]) {
return -1; return null;
} }
this.timer = new Date().getTime(); this.timer = new Date().getTime();
this.locks[serial] = true; this.locks[serial] = true;
await delay_1.delay(cfg.arduino.serialDelay); await delay_1.delay(cfg.arduino.serialDelay);
try { try {
results = await this.stateAsync(device); results = await this.stateAsync(device, confirm);
} }
catch (e) { catch (e) {
return this.log.error(e); return this.log.error(e);
@ -204,7 +219,9 @@ 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) {
complete = this.queue[data](ms); complete = this.queue[cfg.arduino.cmd.state](ms);
delete this.queue[cfg.arduino.cmd.state];
return data;
} }
else if (data[0] === cfg.arduino.cmd.error) { else if (data[0] === cfg.arduino.cmd.error) {
this.log.error(`Received error from device ${serial}`); this.log.error(`Received error from device ${serial}`);
@ -255,6 +272,12 @@ class Arduino {
return await this.confirmEnd(d); return await this.confirmEnd(d);
}); });
} }
try {
await this.state(serial, true);
}
catch (e) {
this.log.error(`failed to establish state capabilities` + e);
}
return resolve(this.path[serial]); return resolve(this.path[serial]);
}); });
} }

File diff suppressed because one or more lines are too long

View File

@ -37,6 +37,7 @@ class Arduino {
private known : string[] = KNOWN; private known : string[] = KNOWN;
private alias : any = {}; private alias : any = {};
private serial : any = { connect : {}, projector : {}, camera : {}, light : {} }; private serial : any = { connect : {}, projector : {}, camera : {}, light : {} };
private hasState : any = { projector : false, camera : false, light : false };
private baud : number = 57600; private baud : number = 57600;
private queue : any = {}; private queue : any = {};
private timer : number = 0; private timer : number = 0;
@ -150,11 +151,25 @@ class Arduino {
} }
} }
async stateAsync (device : string) : Promise<number> { 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) => {
this.queue[cmd] = (ms : number) => { this.queue[cmd] = (state : string) => {
return resolve(ms) if (confirm) {
this.hasState[device] = true
this.log.info(`Device ${device} supports state [${state}]`)
}
return resolve(state)
}
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)
}
}.bind(this), 100)
} }
return this.serial[device].write(cmd, (err : any, results : any) => { return this.serial[device].write(cmd, (err : any, results : any) => {
if (err) { if (err) {
@ -165,18 +180,18 @@ class Arduino {
}) })
} }
async state (serial : string) : Promise<number>{ async state (serial : string, confirm : boolean = false) : Promise<string>{
const device : any = this.alias[serial] const device : any = this.alias[serial]
let results : number let results : string
if (this.locks[serial]) { if (this.locks[serial]) {
return -1 return null
} }
this.timer = new Date().getTime() this.timer = new Date().getTime()
this.locks[serial] = true this.locks[serial] = true
await delay(cfg.arduino.serialDelay) await delay(cfg.arduino.serialDelay)
try { try {
results = await this.stateAsync(device) results = await this.stateAsync(device, confirm)
} catch (e) { } catch (e) {
return this.log.error(e) return this.log.error(e)
} }
@ -205,7 +220,7 @@ class Arduino {
}) })
} }
end (serial : string, data : string) : number { 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
let complete : any let complete : any
@ -216,9 +231,12 @@ 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) {
complete = this.queue[data](ms) complete = this.queue[cfg.arduino.cmd.state](ms)
delete this.queue[cfg.arduino.cmd.state]
return data
} else if (data[0] === cfg.arduino.cmd.error) { } else if (data[0] === cfg.arduino.cmd.error) {
this.log.error(`Received error from device ${serial}`) this.log.error(`Received error from device ${serial}`)
//error state //error state
//stop sequence //stop sequence
//throw error in ui //throw error in ui
@ -265,6 +283,13 @@ class Arduino {
return await this.confirmEnd(d) return await this.confirmEnd(d)
}) })
} }
try {
await this.state(serial, true)
} catch (e) {
this.log.error(`failed to establish state capabilities` + e)
}
return resolve(this.path[serial]) return resolve(this.path[serial])
}) })
} }