Skip to content

Commit

Permalink
improvement scanner/sensor start should not throw error on timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Guido Doumen committed Dec 12, 2023
1 parent 7bd8a63 commit e2b6be1
Showing 1 changed file with 102 additions and 61 deletions.
163 changes: 102 additions & 61 deletions src/ant-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Constants } from "./consts";
import EventEmitter from "events";
import { Messages } from "./messages";
import { ChannelProps, IAntDevice, IChannel, ISensor } from "./types";
import { start } from "repl";

export type MessageInfo = {
msgId: number;
Expand Down Expand Up @@ -59,23 +60,42 @@ export default class Channel extends EventEmitter implements IChannel {
if (this.isSensor)
await this.stopAllSensors()

let to;
try {
to = setTimeout( ()=>{ throw new Error('timeout')},START_TIMEOUT)
await this.sendMessage(Messages.assignChannel(this.channelNo, 'receive'));
await this.sendMessage(Messages.setDevice(this.channelNo, 0, 0, 0));
await this.sendMessage(Messages.setFrequency(this.channelNo, 57));
await this.sendMessage(Messages.setRxExt());
await this.sendMessage(Messages.libConfig(this.channelNo, 0xE0));
await this.sendMessage(Messages.openRxScan());
if (to) clearTimeout(to)
this.isScanner = true;
return this.isScanner
}
catch(err) {
if (to) clearTimeout(to)
return false;
}
return new Promise ( async done => {
let to;
let starting = true;

try {

const send = async (data,opts?) => {
if (!starting)
return;
return await this.sendMessage(data,opts);
}

to = setTimeout( ()=>{
done(false)
starting = false

},START_TIMEOUT)

await send(Messages.assignChannel(this.channelNo, 'receive'));
await send(Messages.setDevice(this.channelNo, 0, 0, 0));
await send(Messages.setFrequency(this.channelNo, 57));
await send(Messages.setRxExt());
await send(Messages.libConfig(this.channelNo, 0xE0));
await send(Messages.openRxScan());
if (to) clearTimeout(to)

this.isScanner = true;
done(true)
}
catch(err) {
if (to) clearTimeout(to)
done(false);
}

})

}

async stopScanner(): Promise<boolean> {
Expand All @@ -93,32 +113,36 @@ export default class Channel extends EventEmitter implements IChannel {

return new Promise ( async done => {
let to;
let emitter = new EventEmitter();


try {

let isStarting = true

to = setTimeout( ()=>{ emitter.emit('timeout')},START_TIMEOUT)
emitter.once('timeout',()=>{

const send = async (data,opts?) => {
if (!isStarting)
return;
return await this.sendMessage(data,opts);
}


to = setTimeout( ()=>{
if(isStarting) {
isStarting = false;
done(false)
return;
}
})
}
},START_TIMEOUT)

const {type,transmissionType,timeout,frequency,period} = sensor.getChannelConfiguration();
const deviceID = sensor.getDeviceID();
const deviceType = sensor.getDeviceType();

await this.sendMessage(Messages.assignChannel(this.channelNo, type));
await this.sendMessage(Messages.setDevice(this.channelNo, deviceID, deviceType, transmissionType));
await this.sendMessage(Messages.searchChannel(this.channelNo, timeout));
await this.sendMessage(Messages.setFrequency(this.channelNo, frequency));
await this.sendMessage(Messages.setPeriod(this.channelNo, period));
await this.sendMessage(Messages.libConfig(this.channelNo, 0xE0));
await this.sendMessage(Messages.openChannel(this.channelNo));
await send(Messages.assignChannel(this.channelNo, type));
await send(Messages.setDevice(this.channelNo, deviceID, deviceType, transmissionType));
await send(Messages.searchChannel(this.channelNo, timeout));
await send(Messages.setFrequency(this.channelNo, frequency));
await send(Messages.setPeriod(this.channelNo, period));
await send(Messages.libConfig(this.channelNo, 0xE0));
await send(Messages.openChannel(this.channelNo));

isStarting = false
if (to) clearTimeout(to)
Expand All @@ -127,9 +151,7 @@ export default class Channel extends EventEmitter implements IChannel {
this.isSensor = true;
done(this.isSensor)
}
catch(err) {
console.log(err)

catch(err) {
if (to) clearTimeout(to)
done(false);
}
Expand Down Expand Up @@ -161,34 +183,53 @@ export default class Channel extends EventEmitter implements IChannel {

await this.closeChannel({restart:true});
const sensor = this.attachedSensor;


return new Promise ( async done => {
let to;
let isStarting = true;
try {

const send = async (data,opts?) => {
if (!isStarting)
return;
return await this.sendMessage(data,opts);
}

to = setTimeout( ()=>{
if(isStarting) {
isStarting = false;
done(false)
}
},START_TIMEOUT)

const {type,transmissionType,timeout,frequency,period} = sensor.getChannelConfiguration();
const deviceID = sensor.getDeviceID();
const deviceType = sensor.getDeviceType();

await this.sendMessage(Messages.assignChannel(this.channelNo, type));
await this.sendMessage(Messages.setDevice(this.channelNo, deviceID, deviceType, transmissionType));
await this.sendMessage(Messages.searchChannel(this.channelNo, timeout));
await this.sendMessage(Messages.setFrequency(this.channelNo, frequency));
await this.sendMessage(Messages.setPeriod(this.channelNo, period));
await this.sendMessage(Messages.libConfig(this.channelNo, 0xE0));
await this.sendMessage(Messages.openChannel(this.channelNo));
isStarting = false;
if (to) clearTimeout(to)
this.attach(sensor)

return done(true)
}
catch(err) {

if (to) clearTimeout(to)

return done(false);
}

})


let to;
try {
to = setTimeout( ()=>{ throw new Error('timeout')},START_TIMEOUT)

const {type,transmissionType,timeout,frequency,period} = sensor.getChannelConfiguration();
const deviceID = sensor.getDeviceID();
const deviceType = sensor.getDeviceType();

await this.sendMessage(Messages.assignChannel(this.channelNo, type));
await this.sendMessage(Messages.setDevice(this.channelNo, deviceID, deviceType, transmissionType));
await this.sendMessage(Messages.searchChannel(this.channelNo, timeout));
await this.sendMessage(Messages.setFrequency(this.channelNo, frequency));
await this.sendMessage(Messages.setPeriod(this.channelNo, period));
await this.sendMessage(Messages.libConfig(this.channelNo, 0xE0));
await this.sendMessage(Messages.openChannel(this.channelNo));

if (to) clearTimeout(to)
this.attach(sensor)

return true
}
catch(err) {
console.log(err)
if (to) clearTimeout(to)
return false;
}
}

protected async closeChannel(props:{restart?:boolean}={}): Promise<void> {
Expand Down

0 comments on commit e2b6be1

Please sign in to comment.