uci-interrupt/examples/remote.js

62 lines
2.2 KiB
JavaScript
Raw Normal View History

// a remove interface for an interrrupt
// starts a socket on 9000 by default to receive interrupt events
// start a consumer on 9004 to send commends to gpio interrupt
import Base from '@uci/base'
const INTER_HOST = process.env.INTER_HOST || 'sbc'
const INTER_PORT = process.env.INTER_PORT || 9004
const PORT = process.env.PORT || 9000
let processor = new Base({ id:'remote-interrupt-processor', useRootNS:true})
processor.interrupt = async function (packet) {
return new Promise(async (resolve) => {
console.log('=============interrupt occured =======================')
delete packet._header
console.dir({pin:packet.pin,state:packet.state})
let status = await this.send({cmd:'status'})
// console.log('check status', status)
console.log('confirmed state', packet.state, (packet.state=== status[packet.pin] || packet.state===status.state) )
console.log('======================================')
resolve({processed:true})
// console.log('confirm state', (await this.send({cmd:'status'})).state===packet.state)
})
}
processor.reply = async function (packet) {
return new Promise((resolve) => {
// console.log('reply processor from interrupt socket for request', packet._header.request)
// console.dir(packet)
resolve(packet)
})
}
processor.on('connection:socket', async ev => {
console.log(`============connection event to remote socket========== ${ev.socketName}: ${ev.state}`)
if (ev.state ==='connected') {
console.log('============ test by sending fire,status,reset commands ==========')
console.log(await processor.send({cmd:'reset'}))
console.log(await processor.send({cmd:'status'}))
console.log(await processor.send({cmd:'fire'}))
}
})
processor.on('connection:consumer', ev => {
console.log(`==consumer connection===:',${ev.name}:${ev.state}`)
if (ev.state ==='connected') {
console.log('====================ready to receive and process a remote interrupt==================')
}
})
;
(async () => {
processor.addSocket('inter','c','t',{host:INTER_HOST, port:INTER_PORT,initTimeout:60})
processor.addSocket('inters','s','t',{port:PORT})
await processor.socketsInit()
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})