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; min-width: 200px;
width: 200px; width: 200px;
} }
#settings > div.right input[readonly] {
cursor: not-allowed;
}
#settings > div.right .spacer { #settings > div.right .spacer {
height: 62px; height: 62px;
} }

View File

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

View File

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

@ -142,6 +142,15 @@ class Devices {
this.remember('arduino', device, type) this.remember('arduino', device, type)
this.log.info(`Determined ${device} to be ${type}`, 'SERIAL', true, true) 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 return type
} }
/** /**

4
src/globals.d.ts vendored
View File

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