Files
ring-mqtt/lib/exithandler.js
tsightler b8338e30de Release 5.1.0 (#537)
* Use MQTT for start-stream debug messages
* Fix ANSI colors
* Refactor event URL management
* Fix subscription detection
* Improve event URL expiry handling by parsing Amazon S3 expire time
* Convert to ESM/replace colors with chalk
* Force colors for chalk
* Migrate to ESM
* Fix stop of keepalive stream
* Add transcoded event selections
* Update event URL on raw/trancoded toggle
* Switch to per-camera livecall threads
* Customized WebRTC functions
Mostly copied from ring-client-api with port to pure Javascript, removal of unneeded features and additional debugging modified for use as worker thread with ring-mqtt.  Allows easier testing with updated Werift versions.
* Add nightlight enable/disable
* Include nightlight state as attribute
* Only pro versions have nightlight
* Tweak battery level reporting for dual battery cameras
* Release 5.1.0
2023-02-02 20:59:09 -05:00

58 lines
2.0 KiB
JavaScript

import chalk from 'chalk'
import utils from './utils.js'
import ring from './ring.js'
import debugModule from 'debug'
const debug = debugModule('ring-mqtt')
export default new class ExitHandler {
constructor() {
this.init()
}
init() {
// Setup Exit Handlers
process.on('exit', this.processExit.bind(null, 0))
process.on('SIGINT', this.processExit.bind(null, 0))
process.on('SIGTERM', this.processExit.bind(null, 0))
process.on('uncaughtException', (err) => {
debug(chalk.red('ERROR - Uncaught Exception'))
debug(chalk.red(err))
this.processExit(2)
})
process.on('unhandledRejection', (err) => {
switch(true) {
// For these strings suppress the stack trace and only print the message
case /token is not valid/.test(err.message):
case /https:\/\/github.com\/dgreif\/ring\/wiki\/Refresh-Tokens/.test(err.message):
case /error: access_denied/.test(err.message):
debug(chalk.yellow(err.message))
break;
default:
debug(chalk.yellow('WARNING - Unhandled Promise Rejection'))
debug(chalk.yellow(err))
break;
}
})
}
// Set offline status on exit
async processExit(exitCode) {
await utils.sleep(1)
debug('The ring-mqtt process is shutting down...')
await ring.go2rtcShutdown()
if (ring.devices.length > 0) {
debug('Setting all devices offline...')
await utils.sleep(1)
ring.devices.forEach(ringDevice => {
if (ringDevice.availabilityState === 'online') {
ringDevice.shutdown = true
ringDevice.offline()
}
})
}
await utils.sleep(2)
if (exitCode || exitCode === 0) debug(`Exit code: ${exitCode}`);
process.exit()
}
}