uci-interrupt/examples/multi.js

88 lines
2.4 KiB
JavaScript

import { readFile as read } from 'fs-read-data'
import onDeath from 'ondeath'
import { Interrupts } from '../src'
let options = {}
;
(async () => {
options = await read('./examples/multi.yaml')
options.id = options.id || options.name || 'switches'
console.log('----------------interrupt start options--------\n',options,'\n---------------------------------')
let interrupts = new Interrupts(options.pins,options)
// Uncomment to listen to local events
// interrupts.on('interrupt', packet => {
// console.log(packet.pin, 'has tripped \n interupt has been pushed \n but could take extra action here as well')
// })
// //
// interrupts.on('ready:24', ready => {
// console.log(ready, 'pin 24, ready:<pin number> is emitted every time status is checked')
// })
if ((process.env.UCI_ENV || '').includes('dev')) {
interrupts.on('log',ev => {
switch (ev.level) {
// case 'warning':
// case 'error':
// case 'info':
// case 'testing':
case 'interrupt':
case 'fatal':
console.log(ev.level.toUpperCase(),'\n',ev)
break
}
})
process.once('SIGUSR2', async () => {
shutdown.call(interrupts)
process.kill(process.pid, 'SIGUSR2')
})
} else {
onDeath( async () => {
console.log('\ninterrupt plugin device, \nHe\'s dead Jim')
shutdown.call(interrupts)
console.log('shutdown done')
})
}
await interrupts.init()
console.log('after init',interrupts.ready.observers,interrupts.ready.state)
interrupts.ready.all.subscribe(
(ready) => {
if (!ready) {
console.log(options.name, 'YIKES! some observer is still not reporting ready')
let failed = interrupts.ready.state.filter(obs=> obs[1]===false).map(obs=>[obs[0],interrupts.ready.getObserverDetails(obs)])
console.log('those that have failed\n',failed)
// notifiy here
} else {
console.log(options.name, ': This GPIO Interrupt Hardware is ONLINE!!!!')
interrupts.send({cmd:'interrupt.find', stuff:'some info'})
}
})
})().catch(err => {
console.log('FATAL: UNABLE TO START SYSTEM!\n',err)
process.kill(process.pid, 'SIGTERM')
})
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()
}