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

View File

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

View File

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