Preliminary state support in arduino library. Currently confirms the negative state. Might want to push it until after verify, out of enumerate.

This commit is contained in:
Matt McWilliams 2023-06-16 23:04:16 -04:00
parent 83aa3099c4
commit 71e4bea384
3 changed files with 65 additions and 17 deletions

View File

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

File diff suppressed because one or more lines are too long

View File

@ -37,6 +37,7 @@ class Arduino {
private known : string[] = KNOWN;
private alias : any = {};
private serial : any = { connect : {}, projector : {}, camera : {}, light : {} };
private hasState : any = { projector : false, camera : false, light : false };
private baud : number = 57600;
private queue : any = {};
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
return new Promise ((resolve, reject) => {
this.queue[cmd] = (ms : number) => {
return resolve(ms)
this.queue[cmd] = (state : string) => {
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) => {
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]
let results : number
let results : string
if (this.locks[serial]) {
return -1
return null
}
this.timer = new Date().getTime()
this.locks[serial] = true
await delay(cfg.arduino.serialDelay)
try {
results = await this.stateAsync(device)
results = await this.stateAsync(device, confirm)
} catch (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 ms : number = end - this.timer
let complete : any
@ -216,9 +231,12 @@ class Arduino {
eventEmitter.emit('arduino_end', data)
delete this.queue[data]
} 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) {
this.log.error(`Received error from device ${serial}`)
//error state
//stop sequence
//throw error in ui
@ -265,6 +283,13 @@ class Arduino {
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])
})
}