Add OFF mode to BLE peripheral and refactoring

Add OFF mode to be able to turn BLE advertisement off. Rename
switchPeripheralMode function to include BLE in order to be more
descriptive.
This commit is contained in:
Abász 2022-12-17 21:35:22 +01:00
parent 7ef338d856
commit a3cd6e6f39
5 changed files with 49 additions and 43 deletions

View File

@ -65,7 +65,7 @@ export class DashboardActions extends AppElement {
return html`
<button @click=${this.reset}>${icon_undo}</button>
${this.renderOptionalButtons()}
<button @click=${this.switchPeripheralMode}>${icon_bluetooth}</button>
<button @click=${this.switchBlePeripheralMode}>${icon_bluetooth}</button>
<div class="peripheral-mode">${this.peripheralMode()}</div>
${this.dialog ? this.dialog : ''}
`
@ -116,7 +116,7 @@ export class DashboardActions extends AppElement {
case 'FTMS':
return 'FTMS Rower'
default:
return ''
return 'Off'
}
}
@ -135,8 +135,8 @@ export class DashboardActions extends AppElement {
this.sendEvent('triggerAction', { command: 'reset' })
}
switchPeripheralMode () {
this.sendEvent('triggerAction', { command: 'switchPeripheralMode' })
switchBlePeripheralMode () {
this.sendEvent('triggerAction', { command: 'switchBlePeripheralMode' })
}
uploadTraining () {

View File

@ -130,8 +130,8 @@ export function createApp (app) {
function handleAction (action) {
switch (action.command) {
case 'switchPeripheralMode': {
if (socket)socket.send(JSON.stringify({ command: 'switchPeripheralMode' }))
case 'switchBlePeripheralMode': {
if (socket)socket.send(JSON.stringify({ command: 'switchBlePeripheralMode' }))
break
}
case 'reset': {

View File

@ -16,83 +16,88 @@ import child_process from 'child_process'
import AntManager from './ant/AntManager.js'
import { createAntHrmPeripheral } from './ant/HrmPeripheral.js'
const modes = ['FTMS', 'FTMSBIKE', 'PM5', 'CSC', 'CPS']
const bleModes = ['FTMS', 'FTMSBIKE', 'PM5', 'CSC', 'CPS', 'OFF']
function createPeripheralManager () {
const emitter = new EventEmitter()
let peripheral
let mode
let blePeripheral
let bleMode
createPeripheral(config.bluetoothMode)
createBlePeripheral(config.bluetoothMode)
function getPeripheral () {
return peripheral
function getBlePeripheral () {
return blePeripheral
}
function getPeripheralMode () {
return mode
function getBlePeripheralMode () {
return bleMode
}
function switchPeripheralMode (newMode) {
function switchBlePeripheralMode (newMode) {
// if now mode was passed, select the next one from the list
if (newMode === undefined) {
newMode = modes[(modes.indexOf(mode) + 1) % modes.length]
newMode = bleModes[(bleModes.indexOf(bleMode) + 1) % bleModes.length]
}
createPeripheral(newMode)
createBlePeripheral(newMode)
}
function notifyMetrics (type, metrics) {
peripheral.notifyData(type, metrics)
blePeripheral.notifyData(type, metrics)
}
function notifyStatus (status) {
peripheral.notifyStatus(status)
blePeripheral.notifyStatus(status)
}
async function createPeripheral (newMode) {
if (peripheral) {
await peripheral.destroy()
async function createBlePeripheral (newMode) {
if (blePeripheral) {
await blePeripheral.destroy()
}
switch (newMode) {
case 'PM5':
log.info('bluetooth profile: Concept2 PM5')
peripheral = createPm5Peripheral(controlCallback)
mode = 'PM5'
blePeripheral = createPm5Peripheral(controlCallback)
bleMode = 'PM5'
break
case 'FTMSBIKE':
log.info('bluetooth profile: FTMS Indoor Bike')
peripheral = createFtmsPeripheral(controlCallback, {
blePeripheral = createFtmsPeripheral(controlCallback, {
simulateIndoorBike: true
})
mode = 'FTMSBIKE'
bleMode = 'FTMSBIKE'
break
case 'CSC':
log.info('bluetooth profile: Cycling Speed and Cadence')
peripheral = createCscPeripheral()
mode = 'CSC'
blePeripheral = createCscPeripheral()
bleMode = 'CSC'
break
case 'CPS':
log.info('bluetooth profile: Cycling Power Meter')
peripheral = createCpsPeripheral()
mode = 'CPS'
blePeripheral = createCpsPeripheral()
bleMode = 'CPS'
break
case 'FTMS':
default:
log.info('bluetooth profile: FTMS Rower')
peripheral = createFtmsPeripheral(controlCallback, {
blePeripheral = createFtmsPeripheral(controlCallback, {
simulateIndoorBike: false
})
mode = 'FTMS'
bleMode = 'FTMS'
break
default:
log.info('bluetooth profile: Off')
bleMode = 'OFF'
}
peripheral.triggerAdvertising()
if (bleMode.toLocaleLowerCase() !== 'OFF'.toLocaleLowerCase()) { blePeripheral.triggerAdvertising() }
emitter.emit('control', {
req: {
name: 'peripheralMode',
peripheralMode: mode
name: 'blePeripheralMode',
peripheralMode: bleMode
}
})
}
@ -123,9 +128,9 @@ function createPeripheralManager () {
return Object.assign(emitter, {
startAntHeartRateService,
startBleHeartRateService,
getPeripheral,
getPeripheralMode,
switchPeripheralMode,
getBlePeripheral,
getBlePeripheralMode,
switchBlePeripheralMode,
notifyMetrics,
notifyStatus
})

View File

@ -98,7 +98,7 @@ peripheralManager.on('control', (event) => {
peripheralManager.notifyStatus({ name: 'startedOrResumedByUser' })
event.res = true
break
case 'peripheralMode':
case 'blePeripheralMode':
webServer.notifyClients('config', getConfig())
event.res = true
break
@ -216,8 +216,8 @@ workoutUploader.on('resetWorkout', () => {
const webServer = createWebServer()
webServer.on('messageReceived', async (message, client) => {
switch (message.command) {
case 'switchPeripheralMode':
peripheralManager.switchPeripheralMode()
case 'switchBlePeripheralMode':
peripheralManager.switchBlePeripheralMode()
break
case 'reset':
resetWorkout()
@ -243,7 +243,7 @@ webServer.on('clientConnected', (client) => {
// todo: extract this into some kind of state manager
function getConfig () {
return {
peripheralMode: peripheralManager.getPeripheralMode(),
peripheralMode: peripheralManager.getBlePeripheralMode(),
stravaUploadEnabled: !!config.stravaClientId && !!config.stravaClientSecret,
shutdownEnabled: !!config.shutdownCommand
}

View File

@ -78,6 +78,7 @@ export default {
// - FTMSBIKE: The FTMS profile is used by Smart Bike Trainers (please note: the speed and power are still aimed for rowing, NOT for a bike!)
// - CPS: The BLE Cycling Power Profile simulates a bike for more modern Garmin watches
// - CSC: The BLE Cycling Speed and Cadence Profile simulates a bike for older Garmin watches
// - OFF: Turns Bluetooth advertisement off
bluetoothMode: 'FTMS',
// Turn this on if you want support for Bluetooth Low Energy heart rate monitors