2020-03-24 14:28:43 -07:00
|
|
|
import { readFile as read } from 'fs-read-data'
|
|
|
|
import onDeath from 'ondeath'
|
2019-09-11 18:34:54 -07:00
|
|
|
|
2020-03-24 14:28:43 -07:00
|
|
|
|
|
|
|
import Bus from '../src/bus.js'
|
|
|
|
|
|
|
|
let options = {}
|
2019-09-11 18:34:54 -07:00
|
|
|
|
2018-02-04 14:19:44 -08:00
|
|
|
;
|
2018-01-31 18:42:11 -08:00
|
|
|
(async () => {
|
|
|
|
|
2020-03-24 14:28:43 -07:00
|
|
|
options = await read('./examples/bus.yaml')
|
|
|
|
|
|
|
|
options.id = options.id ||'i2c-bus'
|
|
|
|
|
|
|
|
// console.log('----------------switches start options--------\n',options,'\n---------------------------------')
|
|
|
|
// console.log('----------------switches interrupt options--------\n',options.interrupt,'\n---------------------------------')
|
|
|
|
|
|
|
|
let i2cbus = new Bus(options)
|
|
|
|
|
|
|
|
if ((process.env.UCI_ENV ||'').includes('dev')) {
|
|
|
|
i2cbus.on('log',ev => {
|
|
|
|
switch (ev.level) {
|
|
|
|
// case 'warning':
|
|
|
|
// case 'error':
|
|
|
|
// case 'testing':
|
|
|
|
case 'ready':
|
|
|
|
case 'state':
|
|
|
|
case 'i2c':
|
|
|
|
case 'fatal':
|
|
|
|
// case 'info':
|
|
|
|
console.log(ev.level.toUpperCase(),'\n',ev)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
process.once('SIGUSR2', async () => {
|
|
|
|
shutdown.call(i2cbus)
|
|
|
|
process.kill(process.pid, 'SIGUSR2')
|
|
|
|
})
|
|
|
|
|
|
|
|
} // dev
|
|
|
|
|
|
|
|
|
|
|
|
onDeath( async () => {
|
|
|
|
console.log('\nThe I2C Bus, \nHe\'s dead Jim')
|
|
|
|
shutdown.call(i2cbus)
|
|
|
|
console.log('shutdown done')
|
|
|
|
process.exit()
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
console.log('before init')
|
2018-01-31 18:42:11 -08:00
|
|
|
|
|
|
|
await i2cbus.init()
|
|
|
|
|
2020-03-24 14:28:43 -07:00
|
|
|
console.log('after init',i2cbus.ready.observers,i2cbus.ready.state)
|
|
|
|
|
|
|
|
i2cbus.ready.all.subscribe(
|
|
|
|
(ready) => {
|
|
|
|
if (!ready) {
|
|
|
|
console.log(options.name, 'YIKES! some observer is not reporting ready')
|
|
|
|
let failed = i2cbus.ready.state.filter(obs=> obs[1]===false).map(obs=>[obs[0],i2cbus.ready.getObserverDetails(obs)])
|
|
|
|
console.log('those that have failed\n',failed)
|
|
|
|
} else {
|
|
|
|
console.log(options.name, ': This I2C BUS is ONLINE!!!!')
|
|
|
|
console.log(i2cbus.ready.state)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-31 18:42:11 -08:00
|
|
|
})().catch(err => {
|
2020-03-24 14:28:43 -07:00
|
|
|
console.log('FATAL: UNABLE TO START IC2BUS - KILLING FOR OS RESTART !\n',err, options)
|
|
|
|
process.exitCode = 1
|
|
|
|
process.kill(process.pid, 'SIGINT')
|
2018-01-31 18:42:11 -08:00
|
|
|
})
|
2020-03-24 14:28:43 -07:00
|
|
|
|
|
|
|
|
|
|
|
async function shutdown (){
|
|
|
|
let names = Object.keys(this.getSocket())
|
|
|
|
console.log(names)
|
|
|
|
for (let name of names) {
|
|
|
|
console.log(name)
|
|
|
|
console.log(await this.removeSocket(name))
|
|
|
|
console.log('after remove', name)
|
|
|
|
}
|
|
|
|
this.removeAllListeners()
|
|
|
|
}
|