Files
ring-mqtt/lib/main.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

64 lines
2.3 KiB
JavaScript

import exithandler from './exithandler.js'
import mqtt from './mqtt.js'
import config from './config.js'
import state from './state.js'
import ring from './ring.js'
import utils from './utils.js'
import tokenApp from './tokenapp.js'
import chalk from 'chalk'
import isOnline from 'is-online'
import debugModule from 'debug'
const debug = debugModule('ring-mqtt')
export default new class Main {
constructor() {
// Hack to suppress spurious message from push-receiver during startup
console.warn = (data) => {
if (data.includes('PHONE_REGISTRATION_ERROR') || data.match('^Retry...')) {
return
}
console.error(data)
};
// Start event listeners
utils.event.on('generated_token', (generatedToken) => {
this.init(generatedToken)
})
this.init()
}
async init(generatedToken) {
if (!state.valid) {
await state.init(config)
}
// Is there any usable token?
if (state.data.ring_token || generatedToken) {
// Wait for the network to be online and then attempt to connect to the Ring API using the token
while (!(await isOnline())) {
debug(chalk.yellow('Network is offline, waiting 10 seconds to check again...'))
await utils.sleep(10)
}
if (!await ring.init(state, generatedToken)) {
debug(chalk.red('Failed to connect to Ring API using saved token, generate a new token using the Web UI'))
debug(chalk.red('or wait 60 seconds to automatically retry authentication using the existing token'))
tokenApp.start()
await utils.sleep(60)
if (!ring.client) {
debug(chalk.yellow('Retrying authentication with existing saved token...'))
this.init()
}
}
} else {
if (process.env.RUNMODE === 'addon') {
debug(chalk.red('No refresh token was found in state file, generate a token using the addon Web UI'))
} else {
tokenApp.start()
debug(chalk.red('No refresh token was found in the state file, use the Web UI at http://<host_ip_address>:55123/ to generate a token.'))
}
}
}
}