Add peripheral shutdown for app termination

In order to avoid errors with peripheral processes on the next startup
(e.g. ANT Stick getting stuck) graceful explicit shutdown of these is
necessary on app termination.
This commit is contained in:
Abász 2023-01-03 10:50:27 +01:00
parent 35303547e2
commit 3f129ceec2
2 changed files with 30 additions and 0 deletions

View File

@ -264,7 +264,21 @@ function createPeripheralManager () {
emitter.emit('control', event)
}
async function shutdownAllPeripherals () {
log.debug('shutting down all peripherals')
try {
await blePeripheral?.destroy()
await antPeripheral?.destroy()
await hrmPeripheral?.destroy()
await _antManager?.closeAntStick()
} catch (error) {
log.error('peripheral shutdown was unsuccessful, restart of Pi may required', error)
}
}
return Object.assign(emitter, {
shutdownAllPeripherals,
getBlePeripheral,
getBlePeripheralMode,
getAntPeripheral,

View File

@ -140,6 +140,22 @@ function resetWorkout () {
const gpioTimerService = child_process.fork('./app/gpio/GpioTimerService.js')
gpioTimerService.on('message', handleRotationImpulse)
process.once('SIGINT', async (signal) => {
log.debug(`${signal} signal was received, shutting down gracefully`)
await peripheralManager.shutdownAllPeripherals()
process.exit(0)
})
process.once('SIGTERM', async (signal) => {
log.debug(`${signal} signal was received, shutting down gracefully`)
await peripheralManager.shutdownAllPeripherals()
process.exit(0)
})
process.once('uncaughtException', async (error) => {
log.error('Uncaught Exception:', error)
await peripheralManager.shutdownAllPeripherals()
process.exit(1)
})
function handleRotationImpulse (dataPoint) {
workoutRecorder.recordRotationImpulse(dataPoint)
rowingStatistics.handleRotationImpulse(dataPoint)