uci-mcp/examples/switches.js

134 lines
4.3 KiB
JavaScript
Raw Normal View History

import { readFile as read } from 'fs-read-data'
import onDeath from 'ondeath'
import { MCP230XXi } from '../src'
let options = {}
;
(async () => {
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)
// 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
}
})
process.once('SIGUSR2', async () => {
console.log('shutting down nodemon')
await shutdown.call(switches)
process.kill(process.pid, 'SIGUSR2')
})
} // dev
else {
onDeath( async () => {
console.log('\nswitches plugin device, \nHe\'s dead Jim')
await shutdown.call(switches)
console.log('shutdown done')
})
}
// 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
// })
// 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
// })
if (options.interrupt) {
console.log('interrupt processor registered')
switches.registerInterruptProcessor(iprocessor) // register interrupt processor from below
}
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))
// 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
})().catch(err => {
console.log('FATAL: UNABLE TO START switches - KILLING FOR OS RESTART !\n',err, options)
process.exitCode = 1
process.kill(process.pid, 'SIGINT')
})
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()
}