Can now detect if state is available on a device

This commit is contained in:
Matt McWilliams 2023-06-17 17:57:30 -04:00
parent 95ede98623
commit c0d6cbccec
9 changed files with 49 additions and 28 deletions

View File

@ -681,6 +681,9 @@ button:focus {
min-width: 200px;
width: 200px;
}
#settings > div.right input[readonly] {
cursor: not-allowed;
}
#settings > div.right .spacer {
height: 62px;
}

View File

@ -15,6 +15,9 @@
min-width: 200px;
width: 200px;
}
input[readonly]{
cursor: not-allowed;
}
.spacer{
height: 62px;
}

View File

@ -5,7 +5,7 @@ const delay_1 = require("delay");
const { SerialPort } = require('serialport');
const { ReadlineParser } = require('@serialport/parser-readline');
const exec = require('child_process').exec;
const parser = new ReadlineParser({});
const parser = new ReadlineParser({ delimiter: '\r\n' });
const newlineRe = new RegExp('\n', 'g');
const returnRe = new RegExp('\r', 'g');
let eventEmitter;
@ -93,6 +93,7 @@ class Arduino {
**/
async sendAsync(device, cmd) {
return new Promise((resolve, reject) => {
console.log(`${device} -> ${cmd}`);
this.queue[cmd] = (ms) => {
return resolve(ms);
};
@ -160,8 +161,9 @@ class Arduino {
this.log.info(`Device ${device} does not support state`);
return resolve(null);
}
}.bind(this), 100);
}.bind(this), 1000);
}
console.log(`${device} -> ${cmd}`);
return this.serial[device].write(cmd, (err, results) => {
if (err) {
//this.log.error(err)
@ -171,7 +173,7 @@ class Arduino {
});
}
async state(serial, confirm = false) {
const device = this.alias[serial];
const device = confirm ? this.alias['connect'] : this.alias[serial];
let results;
if (this.locks[serial]) {
return null;
@ -211,7 +213,7 @@ class Arduino {
const end = new Date().getTime();
const ms = end - this.timer;
let complete;
//this.log.info(`${serial} -> ${data}`);
this.log.info(`${serial} -> ${data}`);
if (this.queue[data] !== undefined) {
this.locks[serial] = false;
complete = this.queue[data](ms); //execute callback
@ -247,7 +249,7 @@ class Arduino {
path: this.path[serial],
autoOpen: false,
baudRate: cfg.arduino.baud,
parser: parser
parser
});
this.locks[device] = false;
try {
@ -272,17 +274,11 @@ 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]);
});
}
confirmEnd(data) {
//this.log.info(data)
this.log.info(data);
if (data === cfg.arduino.cmd.connect
|| data === cfg.arduino.cmd.projector_identifier
|| data === cfg.arduino.cmd.camera_identifier
@ -309,6 +305,10 @@ class Arduino {
this.confirmExec(null, data);
this.confirmExec = {};
}
else if (data[0] === cfg.arduino.cmd.state) {
this.queue[cfg.arduino.cmd.state](0);
delete this.queue[cfg.arduino.cmd.state];
}
}
async verify() {
return new Promise(async (resolve, reject) => {

File diff suppressed because one or more lines are too long

View File

@ -127,6 +127,13 @@ class Devices {
}
this.remember('arduino', device, type);
this.log.info(`Determined ${device} to be ${type}`, 'SERIAL', true, true);
await delay_1.delay(100);
try {
await this.arduino.state(device.toString(), true);
}
catch (err) {
this.log.error('Error checking state capability', err);
}
return type;
}
/**

File diff suppressed because one or more lines are too long

View File

@ -7,7 +7,7 @@ const { SerialPort } = require('serialport')
const { ReadlineParser } = require('@serialport/parser-readline')
const exec = require('child_process').exec
const parser : any = new ReadlineParser({ })
const parser : any = new ReadlineParser({ delimiter: '\r\n' })
const newlineRe : RegExp = new RegExp('\n', 'g')
const returnRe : RegExp = new RegExp('\r', 'g')
@ -101,6 +101,7 @@ class Arduino {
**/
async sendAsync (device : string, cmd : string) {
return new Promise ((resolve, reject) => {
console.log(`${device} -> ${cmd}`)
this.queue[cmd] = (ms : number) => {
return resolve(ms)
}
@ -169,8 +170,9 @@ class Arduino {
this.log.info(`Device ${device} does not support state`)
return resolve(null)
}
}.bind(this), 100)
}.bind(this), 1000)
}
console.log(`${device} -> ${cmd}`)
return this.serial[device].write(cmd, (err : any, results : any) => {
if (err) {
//this.log.error(err)
@ -181,7 +183,7 @@ class Arduino {
}
async state (serial : string, confirm : boolean = false) : Promise<string>{
const device : any = this.alias[serial]
const device : string = confirm ? this.alias['connect'] : this.alias[serial]
let results : string
if (this.locks[serial]) {
@ -224,7 +226,7 @@ class Arduino {
const end : number = new Date().getTime()
const ms : number = end - this.timer
let complete : any
//this.log.info(`${serial} -> ${data}`)
this.log.info(`${serial} -> ${data}`)
if (this.queue[data] !== undefined) {
this.locks[serial] = false
complete = this.queue[data](ms) //execute callback
@ -260,7 +262,7 @@ class Arduino {
path : this.path[serial],
autoOpen : false,
baudRate: cfg.arduino.baud,
parser: parser
parser
})
this.locks[device] = false
try {
@ -283,19 +285,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])
})
}
confirmEnd (data : string) {
//this.log.info(data)
this.log.info(data)
if ( data === cfg.arduino.cmd.connect
|| data === cfg.arduino.cmd.projector_identifier
|| data === cfg.arduino.cmd.camera_identifier
@ -325,6 +321,9 @@ class Arduino {
this.confirmExec(null, data)
this.confirmExec = {}
} else if (data[0] === cfg.arduino.cmd.state) {
this.queue[cfg.arduino.cmd.state](0)
delete this.queue[cfg.arduino.cmd.state]
}
}

View File

@ -141,7 +141,16 @@ class Devices {
this.remember('arduino', device, type)
this.log.info(`Determined ${device} to be ${type}`, 'SERIAL', true, true)
await delay(100)
try {
await this.arduino.state(device.toString(), true)
} catch (err) {
this.log.error('Error checking state capability', err)
}
return type
}
/**

4
src/globals.d.ts vendored
View File

@ -26,8 +26,8 @@ interface Arduino {
verify () : any;
distinguish () : any;
fakeConnect ( id : string) : any;
stateAsync (device : string) : any;
state (serial: string) : number;
stateAsync (device : string, confirm: boolean) : any;
state (serial: string, confirm: boolean) : number;
close () : any;
aliasSerial ( id : string, device : Device) : any;
}