canon_ble #82
|
@ -97,7 +97,7 @@ class Arduino {
|
||||||
};
|
};
|
||||||
return this.serial[device].write(cmd, (err, results) => {
|
return this.serial[device].write(cmd, (err, results) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
//console.error(err)
|
//this.log.error(err)
|
||||||
return reject(err);
|
return reject(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -106,7 +106,7 @@ class Arduino {
|
||||||
async send(serial, cmd) {
|
async send(serial, cmd) {
|
||||||
const device = this.alias[serial];
|
const device = this.alias[serial];
|
||||||
let results;
|
let results;
|
||||||
//console.log(`${cmd} -> ${serial}`)
|
//this.log.info(`${cmd} -> ${serial}`)
|
||||||
if (this.locks[serial]) {
|
if (this.locks[serial]) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -117,13 +117,13 @@ class Arduino {
|
||||||
results = await this.sendAsync(device, cmd);
|
results = await this.sendAsync(device, cmd);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
return console.error(e);
|
return this.log.error(e);
|
||||||
}
|
}
|
||||||
this.locks[serial] = false;
|
this.locks[serial] = false;
|
||||||
await eventEmitter.emit('arduino_send', cmd);
|
await eventEmitter.emit('arduino_send', cmd);
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
async string(serial, str) {
|
async sendString(serial, str) {
|
||||||
const device = this.alias[serial];
|
const device = this.alias[serial];
|
||||||
let writeSuccess;
|
let writeSuccess;
|
||||||
await delay_1.delay(cfg.arduino.serialDelay);
|
await delay_1.delay(cfg.arduino.serialDelay);
|
||||||
|
@ -136,11 +136,44 @@ class Arduino {
|
||||||
writeSuccess = await this.writeAsync(device, str);
|
writeSuccess = await this.writeAsync(device, str);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
return console.error(e);
|
return this.log.error(e);
|
||||||
}
|
}
|
||||||
return writeSuccess;
|
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
|
* Send a string to an Arduino using async/await
|
||||||
*
|
*
|
||||||
|
@ -163,25 +196,28 @@ 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;
|
||||||
//console.log(`${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
|
||||||
eventEmitter.emit('arduino_end', data);
|
eventEmitter.emit('arduino_end', data);
|
||||||
delete this.queue[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
|
//error state
|
||||||
//stop sequence
|
//stop sequence
|
||||||
//throw error in ui
|
//throw error in ui
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
//console.log('Received stray "' + data + '"'); //silent to user
|
//this.log.info('Received stray "' + data + '"') //silent to user
|
||||||
}
|
}
|
||||||
return ms;
|
return ms;
|
||||||
}
|
}
|
||||||
aliasSerial(serial, device) {
|
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;
|
this.alias[serial] = device;
|
||||||
}
|
}
|
||||||
async connect(serial, device, confirm) {
|
async connect(serial, device, confirm) {
|
||||||
|
@ -200,10 +236,10 @@ class Arduino {
|
||||||
connectSuccess = await this.openArduino(device);
|
connectSuccess = await this.openArduino(device);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.error('failed to open: ' + e);
|
this.log.error('failed to open: ' + e);
|
||||||
return reject(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) {
|
if (!confirm) {
|
||||||
this.serial[device].on('data', async (data) => {
|
this.serial[device].on('data', async (data) => {
|
||||||
let d = data.toString('utf8');
|
let d = data.toString('utf8');
|
||||||
|
@ -222,7 +258,7 @@ class Arduino {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
confirmEnd(data) {
|
confirmEnd(data) {
|
||||||
//console.dir(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
|
||||||
|
@ -352,7 +388,6 @@ class Arduino {
|
||||||
}
|
}
|
||||||
return closeSuccess;
|
return closeSuccess;
|
||||||
}
|
}
|
||||||
;
|
|
||||||
async fakeConnect(serial) {
|
async fakeConnect(serial) {
|
||||||
const device = '/dev/fake';
|
const device = '/dev/fake';
|
||||||
this.alias[serial] = device;
|
this.alias[serial] = device;
|
||||||
|
@ -378,7 +413,7 @@ class Arduino {
|
||||||
},
|
},
|
||||||
fake: 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;
|
return true;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -64,7 +64,7 @@ class Light {
|
||||||
}
|
}
|
||||||
await delay_1.delay(1);
|
await delay_1.delay(1);
|
||||||
try {
|
try {
|
||||||
this.arduino.string(this.id, str);
|
this.arduino.sendString(this.id, str);
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
this.log.error('Error sending light string', err);
|
this.log.error('Error sending light string', err);
|
||||||
|
|
|
@ -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,OAAiB,EAAE,GAAS,EAAE,EAAQ;QAd5C,UAAK,GAAS,EAAE,KAAK,EAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAA;QAOlC,YAAO,GAAa,IAAI,CAAC;QAEzB,OAAE,GAAY,OAAO,CAAC;QAM7B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAA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,CAAC,GAAG,CAAC,KAAK,CAAC,4BAA4B,EAAE,GAAG,CAAC,CAAC;SAClD;QACD,MAAM,aAAK,CAAC,CAAC,CAAC,CAAC;QACf,MAAM,EAAE,CAAC;QACT,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IACpC,CAAC;IAED;;QAEI;IACI,KAAK,CAAC,GAAG,CAAE,GAAc,EAAE,EAAW,EAAE,EAAW;QAC1D,IAAI,GAAG,CAAC;QACR,iBAAiB;QACjB,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,gBAAgB,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QACpE,IAAI;YACH,8BAA8B;YAC9B,GAAG,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;SACnD;QAAC,OAAO,GAAG,EAAE;YACb,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACnB,MAAM,GAAG,CAAA;SACT;QACD,OAAO,GAAG,CAAC;IACZ,CAAC;CACD;AAED,MAAM,CAAC,OAAO,GAAG,UAAU,OAAiB,EAAE,GAAS,EAAE,EAAQ;IAChE,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;AACpC,CAAC,CAAA"}
|
|
@ -105,7 +105,7 @@ class Arduino {
|
||||||
}
|
}
|
||||||
return this.serial[device].write(cmd, (err : any, results : any) => {
|
return this.serial[device].write(cmd, (err : any, results : any) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
//console.error(err)
|
//this.log.error(err)
|
||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -115,7 +115,7 @@ class Arduino {
|
||||||
async send (serial : string, cmd : string) {
|
async send (serial : string, cmd : string) {
|
||||||
const device : any = this.alias[serial]
|
const device : any = this.alias[serial]
|
||||||
let results : any
|
let results : any
|
||||||
//console.log(`${cmd} -> ${serial}`)
|
//this.log.info(`${cmd} -> ${serial}`)
|
||||||
if (this.locks[serial]) {
|
if (this.locks[serial]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ class Arduino {
|
||||||
try {
|
try {
|
||||||
results = await this.sendAsync(device, cmd)
|
results = await this.sendAsync(device, cmd)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return console.error(e)
|
return this.log.error(e)
|
||||||
}
|
}
|
||||||
this.locks[serial] = false
|
this.locks[serial] = false
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ class Arduino {
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
async string (serial : string, str : string) {
|
async sendString (serial : string, str : string) : Promise<any> {
|
||||||
const device : any = this.alias[serial]
|
const device : any = this.alias[serial]
|
||||||
let writeSuccess : any
|
let writeSuccess : any
|
||||||
await delay(cfg.arduino.serialDelay)
|
await delay(cfg.arduino.serialDelay)
|
||||||
|
@ -144,12 +144,48 @@ class Arduino {
|
||||||
try {
|
try {
|
||||||
writeSuccess = await this.writeAsync(device, str)
|
writeSuccess = await this.writeAsync(device, str)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return console.error(e)
|
return this.log.error(e)
|
||||||
}
|
}
|
||||||
return writeSuccess
|
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
|
* Send a string to an Arduino using async/await
|
||||||
*
|
*
|
||||||
|
@ -158,7 +194,7 @@ class Arduino {
|
||||||
*
|
*
|
||||||
* @returns {Promise} Resolves after sending
|
* @returns {Promise} Resolves after sending
|
||||||
**/
|
**/
|
||||||
async writeAsync (device : string, str : string) {
|
async writeAsync (device : string, str : string) : Promise<any> {
|
||||||
return new Promise ((resolve, reject) => {
|
return new Promise ((resolve, reject) => {
|
||||||
this.serial[device].write(str, function (err : any, results : any) {
|
this.serial[device].write(str, function (err : any, results : any) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -169,32 +205,34 @@ class Arduino {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
end (serial : string, data : string) {
|
end (serial : string, data : string) : number {
|
||||||
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
|
||||||
//console.log(`${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
|
||||||
eventEmitter.emit('arduino_end', data);
|
eventEmitter.emit('arduino_end', data)
|
||||||
delete this.queue[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
|
//error state
|
||||||
//stop sequence
|
//stop sequence
|
||||||
//throw error in ui
|
//throw error in ui
|
||||||
} else {
|
} 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) {
|
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;
|
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) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let connectSuccess : any
|
let connectSuccess : any
|
||||||
this.path[serial] = device
|
this.path[serial] = device
|
||||||
|
@ -209,10 +247,10 @@ class Arduino {
|
||||||
try {
|
try {
|
||||||
connectSuccess = await this.openArduino(device)
|
connectSuccess = await this.openArduino(device)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('failed to open: ' + e)
|
this.log.error('failed to open: ' + e)
|
||||||
return reject(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) {
|
if (!confirm) {
|
||||||
this.serial[device].on('data', async (data : Buffer) => {
|
this.serial[device].on('data', async (data : Buffer) => {
|
||||||
let d = data.toString('utf8')
|
let d = data.toString('utf8')
|
||||||
|
@ -231,7 +269,7 @@ class Arduino {
|
||||||
}
|
}
|
||||||
|
|
||||||
confirmEnd (data : string) {
|
confirmEnd (data : string) {
|
||||||
//console.dir(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
|
||||||
|
@ -259,8 +297,8 @@ class Arduino {
|
||||||
|| data === cfg.arduino.cmd.camera_capper_projector_identifier
|
|| data === cfg.arduino.cmd.camera_capper_projector_identifier
|
||||||
|| data === cfg.arduino.cmd.camera_capper_projectors_identifier) {
|
|| data === cfg.arduino.cmd.camera_capper_projectors_identifier) {
|
||||||
|
|
||||||
this.confirmExec(null, data);
|
this.confirmExec(null, data)
|
||||||
this.confirmExec = {};
|
this.confirmExec = {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -348,10 +386,10 @@ class Arduino {
|
||||||
try {
|
try {
|
||||||
closeSuccess = await this.closeArduino(device)
|
closeSuccess = await this.closeArduino(device)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw e;
|
throw e
|
||||||
}
|
}
|
||||||
return closeSuccess
|
return closeSuccess
|
||||||
};
|
}
|
||||||
|
|
||||||
async fakeConnect (serial : string) {
|
async fakeConnect (serial : string) {
|
||||||
const device : string = '/dev/fake'
|
const device : string = '/dev/fake'
|
||||||
|
@ -379,8 +417,8 @@ class Arduino {
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
fake : 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
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,12 +20,14 @@ interface Device {
|
||||||
|
|
||||||
interface Arduino {
|
interface Arduino {
|
||||||
send (id : string, cmd : string) : number;
|
send (id : string, cmd : string) : number;
|
||||||
string (id : string, str : string) : any;
|
sendString (id : string, str : string) : any;
|
||||||
enumerate () : any;
|
enumerate () : any;
|
||||||
connect (id : string, device : Device, state : boolean) : any;
|
connect (id : string, device : Device, state : boolean) : any;
|
||||||
verify () : any;
|
verify () : any;
|
||||||
distinguish () : any;
|
distinguish () : any;
|
||||||
fakeConnect ( id : string) : any;
|
fakeConnect ( id : string) : any;
|
||||||
|
stateAsync (device : string) : any;
|
||||||
|
state (serial: string) : number;
|
||||||
close () : any;
|
close () : any;
|
||||||
aliasSerial ( id : string, device : Device) : any;
|
aliasSerial ( id : string, device : Device) : any;
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,7 +75,7 @@ class Light {
|
||||||
}
|
}
|
||||||
await delay(1);
|
await delay(1);
|
||||||
try {
|
try {
|
||||||
this.arduino.string(this.id, str);
|
this.arduino.sendString(this.id, str);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.log.error('Error sending light string', err);
|
this.log.error('Error sending light string', err);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue