Move to new ant-plus library

Move to a new, still maintained, ant-plus library that solves the
connection dropout issue experienced with the original when using HRM as
well as that provides a cleaner interface to manage the ANT device.
This commit is contained in:
Abász 2022-12-18 23:50:06 +01:00
parent 17d6a74332
commit 3bb0229d3a
3 changed files with 33 additions and 50 deletions

View File

@ -10,42 +10,27 @@
- Garmin mini ANT+ (ID 0x1009) - Garmin mini ANT+ (ID 0x1009)
*/ */
import log from 'loglevel' import log from 'loglevel'
import Ant from 'ant-plus' import { AntDevice } from 'incyclist-ant-plus/lib/bindings/index.js'
export default class AntManager { export default class AntManager {
_isStickOpen = false _isStickOpen = false
_stick = new AntDevice({ startupTimeout: 2000 })
constructor () { async openAntStick () {
// it seems that we have to use two separate heart rate sensors to support both old and new if (this._isStickOpen) return
// ant sticks, since the library requires them to be bound before open is called if (!(await this._stick.open())) { throw (new Error('Error opening Ant Stick')) }
this._stick = new Ant.GarminStick3() // 0fcf:1009
if (!this._stick.is_present()) { log.info('ANT+ stick found')
this._stick = new Ant.GarminStick2() // 0fcf:1008 this._isStickOpen = true
}
} }
openAntStick () { async closeAntStick () {
return new Promise((resolve, reject) => { if (!this._isStickOpen) return
if (!this._stick.open()) {
reject(new Error('Error opening Ant Stick'))
}
this._stick.once('startup', () => {
log.info('ANT+ stick found')
this._isStickOpen = true
resolve(this._stick)
})
})
}
closeAntStick () { if (!(await this._stick.close())) { throw (new Error('Error closing Ant Stick')) }
return new Promise(resolve => {
this._stick.once('shutdown', () => { log.info('ANT+ stick is closed')
log.info('ANT+ stick is closed') this._isStickOpen = false
this._isStickOpen = false
resolve()
})
this._stick.close()
})
} }
isStickOpen () { isStickOpen () {

View File

@ -6,35 +6,33 @@
a Cycling Speed and Cadence Profile a Cycling Speed and Cadence Profile
*/ */
import EventEmitter from 'node:events' import EventEmitter from 'node:events'
import Ant from 'ant-plus' import log from 'loglevel'
import { HeartRateSensor } from 'incyclist-ant-plus'
function createAntHrmPeripheral (antManager) { function createAntHrmPeripheral (antManager) {
const emitter = new EventEmitter() const emitter = new EventEmitter()
const antStick = antManager.getAntStick() const antStick = antManager.getAntStick()
const heartRateSensor = new HeartRateSensor(0)
const heartRateSensor = new Ant.HeartRateSensor(antStick) async function attach () {
if (!antManager.isStickOpen()) { await antManager.openAntStick() }
this.channel = await antStick.getChannel()
heartRateSensor.on('hbData', (data) => { this.channel.on('data', (profile, deviceID, data) => {
emitter.emit('heartRateMeasurement', { heartrate: data.ComputedHeartRate, batteryLevel: data.BatteryLevel }) emitter.emit('heartRateMeasurement', { heartrate: data.ComputedHeartRate, batteryLevel: data.BatteryLevel })
})
function attach () {
return new Promise(resolve => {
heartRateSensor.once('attached', () => {
resolve()
})
heartRateSensor.attach(0, 0)
}) })
if (!(await this.channel.startSensor(heartRateSensor))) {
log.error('Could not start ANT+ heart rate sensor')
}
} }
function destroy () { async function destroy () {
return new Promise((resolve) => { if (!this.channel) {
heartRateSensor.once('detached', () => { log.debug('Ant Sensor does not seem to be running')
heartRateSensor.removeAllListeners() return
resolve() }
}) await this.channel.stopSensor(heartRateSensor)
heartRateSensor.detach()
})
} }
return Object.assign(emitter, { return Object.assign(emitter, {

View File

@ -33,9 +33,9 @@
"dependencies": { "dependencies": {
"@abandonware/bleno": "0.5.1-4", "@abandonware/bleno": "0.5.1-4",
"@abandonware/noble": "1.9.2-15", "@abandonware/noble": "1.9.2-15",
"ant-plus": "0.1.24",
"finalhandler": "1.1.2", "finalhandler": "1.1.2",
"form-data": "4.0.0", "form-data": "4.0.0",
"incyclist-ant-plus": "^0.1.15",
"lit": "2.1.3", "lit": "2.1.3",
"loglevel": "1.8.0", "loglevel": "1.8.0",
"nosleep.js": "0.12.0", "nosleep.js": "0.12.0",