2020-03-24 14:50:50 -07:00
|
|
|
import { readFile as read } from 'fs-read-data'
|
|
|
|
import onDeath from 'ondeath'
|
2020-01-09 20:17:32 -08:00
|
|
|
|
2020-03-24 14:50:50 -07:00
|
|
|
import { MCP230XXi } from '../src'
|
2020-01-09 20:17:32 -08:00
|
|
|
|
2020-03-24 14:50:50 -07:00
|
|
|
let options = {}
|
2020-01-09 20:17:32 -08:00
|
|
|
|
2020-03-24 14:50:50 -07:00
|
|
|
;
|
2020-01-09 20:17:32 -08:00
|
|
|
(async () => {
|
|
|
|
|
2020-03-24 14:50:50 -07:00
|
|
|
options = await read('./examples/switches.yaml')
|
|
|
|
|
|
|
|
options.id = options.id || options.name || 'switches'
|
|
|
|
|
|
|
|
// console.log('----------------switches start options--------\n',options,'\n---------------------------------')
|
|
|
|
// console.log('----------------switches interrupt options--------\n',options.interrupt,'\n---------------------------------')
|
|
|
|
|
|
|
|
let switches = new MCP230XXi(options)
|
2020-01-09 20:17:32 -08:00
|
|
|
|
2020-03-24 14:50:50 -07:00
|
|
|
// TODO make this part of UCI logger
|
|
|
|
if ((process.env.UCI_ENV || '').includes('dev')) {
|
|
|
|
|
|
|
|
switches.on('log',ev => {
|
|
|
|
switch (ev.level) {
|
|
|
|
// case 'warning':
|
|
|
|
// case 'error':
|
|
|
|
// case 'testing':
|
|
|
|
// case 'ready':
|
|
|
|
// case 'state':
|
|
|
|
case 'fatal':
|
|
|
|
// case 'mcp':
|
|
|
|
// case 'info':
|
|
|
|
// case 'debug':
|
|
|
|
// case 'interrupt':
|
|
|
|
console.log(ev.level.toUpperCase(),'\n',ev)
|
|
|
|
break
|
2020-01-09 20:17:32 -08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-03-24 14:50:50 -07:00
|
|
|
process.once('SIGUSR2', async () => {
|
|
|
|
console.log('shutting down nodemon')
|
|
|
|
await shutdown.call(switches)
|
|
|
|
process.kill(process.pid, 'SIGUSR2')
|
2020-01-09 20:17:32 -08:00
|
|
|
})
|
|
|
|
|
2020-03-24 14:50:50 -07:00
|
|
|
} // dev
|
2020-01-09 20:17:32 -08:00
|
|
|
|
2020-03-24 14:50:50 -07:00
|
|
|
else {
|
|
|
|
onDeath( async () => {
|
|
|
|
console.log('\nswitches plugin device, \nHe\'s dead Jim')
|
|
|
|
await shutdown.call(switches)
|
|
|
|
console.log('shutdown done')
|
|
|
|
})
|
2020-01-09 20:17:32 -08:00
|
|
|
}
|
|
|
|
|
2020-03-24 14:50:50 -07:00
|
|
|
// uncomment to see connection events
|
|
|
|
// switches.on('connection:socket', async ev => {
|
|
|
|
// console.log('connection event: outbound > ', ev.state,'to socket',ev.socketName)// if (ev.state ==='connected') switches.duplex = true
|
|
|
|
// })
|
2020-01-09 20:17:32 -08:00
|
|
|
|
2020-03-24 14:50:50 -07:00
|
|
|
// switches.on('connection:consumer', async ev => {
|
|
|
|
// console.log('connection event: inbound >', ev.state,'from consumer',ev.name, ev.data)// if (ev.state ==='connected') switches.duplex = true
|
|
|
|
// })
|
2020-01-09 20:17:32 -08:00
|
|
|
|
|
|
|
|
2020-03-24 14:50:50 -07:00
|
|
|
if (options.interrupt) {
|
|
|
|
console.log('interrupt processor registered')
|
|
|
|
switches.registerInterruptProcessor(iprocessor) // register interrupt processor from below
|
2020-01-09 20:17:32 -08:00
|
|
|
|
2020-03-24 14:50:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
switches.amendConsumerCommands(
|
|
|
|
{
|
|
|
|
reply: async function (packet) { // silence end point send command to bus
|
|
|
|
if (!packet.error) {
|
|
|
|
// console.log('reply processor', packet)
|
|
|
|
// if (packet._header.path.includes('i2c-bus')) {
|
|
|
|
// if (packet.args.cmd ===24 || packet.args.cmd===8) console.log('=== interrupt reset at i2c bus===')
|
|
|
|
// } else console.log('reply back from master socket', packet)
|
|
|
|
} else console.log('processing error at socket', packet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
await switches.init()
|
|
|
|
|
|
|
|
console.log('after init',switches.ready.observers,switches.ready.state)
|
|
|
|
|
|
|
|
switches.ready.all.subscribe(
|
|
|
|
function (ready) {
|
|
|
|
if (!ready) {
|
|
|
|
console.log(options.name, 'YIKES! some observer is still not reporting ready')
|
|
|
|
let failed = this.ready.state.filter(obs=> obs[1]===false).map(obs=>[obs[0],this.ready.getObserverDetails(obs)])
|
|
|
|
console.log('those that have failed\n',failed)
|
|
|
|
// notifiy here
|
|
|
|
} else {
|
|
|
|
console.log(options.name, ': This switches Hardware is...ONLINE!!!!')
|
|
|
|
// console.log('state\n,',this.ready.state)
|
|
|
|
// here is where to continue with startup /circuits reset
|
|
|
|
}
|
|
|
|
}.bind(switches))
|
2020-01-09 20:17:32 -08:00
|
|
|
|
2020-03-24 14:50:50 -07:00
|
|
|
// setInterval(function () {
|
|
|
|
// let failed = this.ready.state.filter(obs=> obs[1]===false).map(obs=>[obs[0],this.ready.getObserverDetails(obs)])
|
|
|
|
// console.log('those that have failed\n',failed)
|
|
|
|
// }.bind(switches),5000)
|
|
|
|
|
|
|
|
// pins are outputs by default or all interrupt pulldown if interrupt(s) being used.
|
|
|
|
// set customized pin configurations here or later via master controller and database
|
2020-01-09 20:17:32 -08:00
|
|
|
|
|
|
|
})().catch(err => {
|
2020-03-24 14:50:50 -07:00
|
|
|
console.log('FATAL: UNABLE TO START switches - KILLING FOR OS RESTART !\n',err, options)
|
|
|
|
process.exitCode = 1
|
|
|
|
process.kill(process.pid, 'SIGINT')
|
2020-01-09 20:17:32 -08:00
|
|
|
})
|
2020-03-24 14:50:50 -07:00
|
|
|
|
|
|
|
function iprocessor (packet) {
|
|
|
|
packet.hardware = options.id // id needs to be the _id of the switches device in the database
|
|
|
|
packet.cmd = 'switches.triggered'
|
|
|
|
console.log('interrupt processor, could send on to another controller process')
|
|
|
|
console.dir(packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|