uci-interrupt/examples/single.js

43 lines
1.3 KiB
JavaScript

import Interrupt from '../src/interrupt'
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
const IPIN = process.env.IPIN || 4
const EDGE = process.env.EDGE || 'both'
// const REMOTE_HOST = process.env.REMOTE_HOST || 'sbc'
// const REMOTE_PORT = process.env.REMOTE_PORT || 9000
const PORT = process.env.PORT || 9000 + IPIN
let interrupt = new Interrupt(IPIN,{id:'test-interrupt', wait:0, edge:EDGE, resetInterval:1, reset:true})
interrupt.on('interrupt', packet => {
console.log('event: interrupt fired for',interrupt.pin_num)
console.log('count:', packet.count, ', state:',packet.state, ', cmd:',packet.cmd, ', data:',packet.data)
})
interrupt.on('interrupt.reset', packet => {
console.log('interrupt reset packet sent/emitted')
console.dir(packet)
})
;
(async () => {
interrupt.registerHook((packet) => {
packet.data='a custom property <data> added to packet via hook'
// console.log('custom hook data prop added:', packet.data)
return packet
})
await interrupt.init()
// interrupt.addSocket('inter','c','t',{host:REMOTE_HOST, port:REMOTE_PORT})
interrupt.addSocket('inters','s','t',{port:PORT})
console.log(await interrupt.socketsInit())
console.log('interrupt ready and waiting')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
process.kill(process.pid, 'SIGTERM')
})