Skip to content

Commit

Permalink
bugfix: fixes #146 ANT-Device not launching
Browse files Browse the repository at this point in the history
  • Loading branch information
Guido Doumen committed Nov 19, 2024
1 parent 2e0bd93 commit 9868a9c
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 7 deletions.
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(ts|js)?$',
testRegex: '^[^\\.]+(\\.)?(unit){0,1}\\.(test|spec)\\.(ts|js)?$',
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
collectCoverageFrom: [
"src/**/*.{js,ts}",
Expand All @@ -13,6 +13,7 @@ module.exports = {
"!src/**/types.ts",
"!src/types/**",
"!src/**/consts.ts",
"!src/**/*.e2e.{test,tests}.{js,ts}",
"!src/**/*.unit.{test,tests}.{js,ts}",
"!src/**/*.test.util.{js,ts}"
]
Expand Down
8 changes: 6 additions & 2 deletions src/antv2/base/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,15 @@ export default class AntAdapter<TDeviceData extends BaseDeviceData> extends Incy
async stop(): Promise<boolean> {
let stopped;

this.logger.logEvent( {message:'stopping device', device:this.getName()})

// in case there was a start ongoing, enforce stop of waiting for data and interrup start
this.internalEmitter.emit('stop')

if (this.stopped)
return

this.logger.logEvent( {message:'stopping device', device:this.getName()})


this.promiseWaitForData = null;
if (this.startStatus) {
this.startStatus.interrupted = true
Expand Down
42 changes: 39 additions & 3 deletions src/antv2/base/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IncyclistInterface } from "../../types";
import AntDeviceBinding from "./binding";
import SensorFactory from "../factories/sensor-factory";
import { isTrue, runWithTimeout, sleep, waitWithTimeout } from "../../utils/utils";
import { AdapterFactory } from "incyclist-devices";

Check failure on line 9 in src/antv2/base/interface.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find module 'incyclist-devices' or its corresponding type declarations.

Check failure on line 9 in src/antv2/base/interface.ts

View workflow job for this annotation

GitHub Actions / build (21.x)

Cannot find module 'incyclist-devices' or its corresponding type declarations.

type ChannelUsage = 'scan'|'sensor'
interface ChannelInfo {
Expand Down Expand Up @@ -314,6 +315,9 @@ export default class AntInterface extends EventEmitter implements IncyclistInt
this.activeScan.emitter.removeAllListeners()

this.emit('stop-scan')
await this.stopDevices(detected)
await this.stopAllSensors(sensors)

const stopped = await this.activeScan.channel.stopScanner()
this.logEvent({message:'scan stopped'})

Expand Down Expand Up @@ -364,6 +368,38 @@ export default class AntInterface extends EventEmitter implements IncyclistInt
return this.scanPromise!==undefined && this.scanPromise !== null
}

protected async stopAllSensors(sensors:Array<ISensor>):Promise<void> {
this.logger.logEvent({message:'stopping all sensors '})
let promises = []

sensors.forEach( (sensor)=>{
promises.push(this.stopSensor(sensor).catch(err => {
this.logger.logEvent({message:'could not stop sensor', error:err.message,channel:sensor.getChannel()?.getChannelNo(), stack:err.stack})
}))
})
if (promises.length>0) {
await Promise.allSettled(promises)
}
this.logger.logEvent({message:'sensors stopped'})
}

protected async stopDevices(detected:AntDeviceSettings[]):Promise<void> {
this.logger.logEvent({message:'stopping devices'})
let promises = []

detected.forEach( (settings)=>{
const adapter = AdapterFactory.create(settings)
promises.push(adapter.stop().catch(err => {
this.logger.logEvent({message:'could not stop device', error:err.message,deviceID:settings.deviceID, stack:err.stack})
}))
})

if (promises.length>0) {
await Promise.allSettled(promises)
}
this.logger.logEvent({message:'devices stopped'})

}

async stopScan():Promise<boolean> {
this.logEvent({message:'stopping scan ..'})
Expand Down Expand Up @@ -450,7 +486,7 @@ export default class AntInterface extends EventEmitter implements IncyclistInt
}

const channel = sensor.getChannel() as Channel
if (channel) {
if (channel!==undefined) {
try {

// old versions of ant-plus library did not have a flush functionn
Expand All @@ -477,8 +513,8 @@ export default class AntInterface extends EventEmitter implements IncyclistInt
}
}
else {
this.logEvent( {message:'could not stop sensor', deviceID:sensor.getDeviceID(), error:'no channel attached'})
return false;
//this.logEvent( {message:'could not stop sensor', deviceID:sensor.getDeviceID(), error:'no channel attached'})
return true;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/antv2/base/interface.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ describe('Ant Interface',()=>{
test('no channel',async ()=>{
sensor.getChannel = jest.fn().mockReturnValue(undefined)
const res = await i.stopSensor(sensor)
expect(res).toBe(false)
expect(res).toBe(true)
})

test('channel flush does not exist',async ()=>{
Expand Down
81 changes: 81 additions & 0 deletions src/antv2/scan.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { EventLogger,ConsoleAdapter } from "gd-eventlog"
import { AdapterFactory, InterfaceFactory, SerialScannerProps } from "../index"
import { sleep } from "../utils/utils";
import AntAdapter from "./base/adapter";
import { AntInterface } from "incyclist-devices";

const {AntDevice} = require('incyclist-ant-plus/lib/bindings');

EventLogger.registerAdapter(new ConsoleAdapter())

describe('Ant+ Scan', () => {



test('Simulate scan and pair', async () => {
const logger = new EventLogger('AntSample')

const ant = InterfaceFactory.create('ant',{logger, log:true, binding:AntDevice}) as unknown as AntInterface


const scan = ():Promise<AntAdapter<any>|null> =>{
logger.logEvent({message:'scanning ....'})

let found = false

return new Promise(async (resolve,reject)=>{
const onData = (...args) =>{
console.log(...args)
}
const onDevice = async (settings)=> {
console.log(settings)
found = true
const adapter = AdapterFactory.create(settings)

await sleep(4000)

ant.off('device',onDevice)
ant.off('data',onData)
adapter.pause()
const stopped = await ant.stopScan()
console.log(stopped)
adapter.removeAllListeners('data')
resolve(adapter)
}

ant.on('device',onDevice)
ant.on('data',onData)

const devices = await ant.scan( { timeout: 10000} )
logger.logEvent({message:'devices found', devices})
if (!found)
resolve(null)

})
}



const connected = await ant.connect()
if (connected) {
const device = await scan()
if (device) {
//found.start()
await device.stop()

device.on('data',console.log)
await device.start()

await sleep(4000)
await device.stop()

}

await ant.disconnect()
}
else (
console.log('~~~ no ANT+ stick connected')
)

},40000)
})

0 comments on commit 9868a9c

Please sign in to comment.