add single push command for multiple interrupts
parent
03c3ac8747
commit
cc75908894
|
@ -1,6 +1,7 @@
|
|||
import Base from '@uci/base'
|
||||
|
||||
const HOST = 'localhost'
|
||||
// const HOST = 'localhost'
|
||||
const HOST = 'sbc'
|
||||
const PORT = 9024
|
||||
let processor = new Base({sockets:'inter#c>t', inter:{host:HOST, port:PORT}, id:'interrupt-processor', useRootNS:true})
|
||||
|
||||
|
@ -14,7 +15,8 @@ processor.interrupt = async function (packet) {
|
|||
|
||||
processor.reply = async function (packet) {
|
||||
return new Promise((resolve) => {
|
||||
console.log('interrupt fired')
|
||||
|
||||
console.log('reply from interrupt with packet')
|
||||
console.dir(packet)
|
||||
resolve({status: 'processed'})
|
||||
})
|
||||
|
@ -24,7 +26,9 @@ processor.reply = async function (packet) {
|
|||
(async () => {
|
||||
|
||||
await processor.init()
|
||||
console.log('====sending fire command to interrupt===')
|
||||
await processor.send({cmd: 'fire'})
|
||||
console.log('====sending fire command to interrupt===')
|
||||
await processor.send({cmd: 'fire'})
|
||||
// process.kill(process.pid, 'SIGTERM')
|
||||
|
||||
|
|
|
@ -2,9 +2,6 @@ import Interrupts from '../src/interrupts'
|
|||
import Base from '@uci/base'
|
||||
|
||||
const PINS = [9,10,24]
|
||||
const HOST = 'trantor'
|
||||
const PORT = 9000
|
||||
const PATH = 'interrupt'
|
||||
|
||||
let hook = (packet) =>
|
||||
{
|
||||
|
@ -13,23 +10,10 @@ let hook = (packet) =>
|
|||
return packet
|
||||
}
|
||||
|
||||
let interrupts = new Interrupts(PINS,{host:HOST, hook:true, 10:{wait:200}})
|
||||
// let listener = new Base({sockets:'inter#s>t', inter:{port:9000}, id:'listener'})
|
||||
let interrupts = new Interrupts(PINS,{hook:true, 10:{wait:200} })
|
||||
|
||||
interrupts.setHook(hook)
|
||||
|
||||
// let interrupts = new Interrupts(PINS,{host:HOST, port:9000, hook:true, path:PATH})
|
||||
// let listener = new Base({sockets:'intert#s>t,intern#s>n', intern:{path:PATH}, intert:{host:HOST, port:PORT}, id:'listener'})
|
||||
|
||||
// listener.interrupt = async function (packet) {
|
||||
// return new Promise((resolve) => {
|
||||
// console.log('interrupt received at listener')
|
||||
// console.dir(packet)
|
||||
// resolve({status: 'processed'})
|
||||
// })
|
||||
// }
|
||||
|
||||
|
||||
|
||||
;
|
||||
(async () => {
|
||||
|
|
|
@ -1,37 +1,27 @@
|
|||
import Interrupt from '../src/interrupt'
|
||||
// import Base from '@uci/base'
|
||||
|
||||
// let interrupt = new Interrupt(24,{id:'test-interrupt', wait:0})
|
||||
// let listener = new Base({sockets:'inter#s>n', inter:{path:'interrupt:24'}, id:'listener'})
|
||||
// const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
||||
|
||||
let interrupt = new Interrupt(24,{id:'test-interrupt', wait:0, hook:true, mock:true,useRootNS:true})
|
||||
// let processor = new Base({sockets:'inter#c>t', inter:{port:9024}, id:'listener'})
|
||||
|
||||
|
||||
// processor.interrupt = async function (packet) {
|
||||
// return new Promise((resolve) => {
|
||||
// console.log('interrupt socket listener got')
|
||||
// console.dir(packet)
|
||||
// resolve({status: 'processed'})
|
||||
// })
|
||||
//
|
||||
// }
|
||||
let interrupt = new Interrupt(24,{id:'test-interrupt', wait:0, hook:true, useRootNS:true})
|
||||
|
||||
;
|
||||
(async () => {
|
||||
|
||||
await interrupt.init()
|
||||
console.log('interrupt ready and waiting')
|
||||
// console.log((await processor.init())[0].err.opts)
|
||||
// interrupt.fire()
|
||||
// processor.send({cmd: 'fire'})
|
||||
// interrupt.fire()
|
||||
// interrupt.fire()
|
||||
// interrupt.fire()
|
||||
console.log('manual fire of interrupt via interrupt instance')
|
||||
interrupt.fire()
|
||||
console.log('manual fire of interrupt via interrupt instance after changing hook')
|
||||
interrupt.hook = (packet) => {
|
||||
packet.data='some hook added data'
|
||||
console.log('custom hook function modifies', packet)
|
||||
return packet
|
||||
}
|
||||
interrupt.fire()
|
||||
// await delay(3000)
|
||||
// process.kill(process.pid, 'SIGTERM')
|
||||
|
||||
})().catch(err => {
|
||||
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
||||
// process.kill(process.pid, 'SIGTERM')
|
||||
process.kill(process.pid, 'SIGTERM')
|
||||
})
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
{
|
||||
"name": "@uci/interrupt",
|
||||
"main": "src",
|
||||
"version": "0.1.5",
|
||||
"version": "0.1.9",
|
||||
"description": "a class for adding interrupt processesing for gpio pins on Raspberry Pi and Similar SBCs",
|
||||
"scripts": {
|
||||
"single": "sudo node -r esm examples/single",
|
||||
"singlem": "DEBUG=true node -r esm examples/single",
|
||||
"singlem": "MOCK=true DEBUG=true node -r esm examples/single",
|
||||
"client": "node -r esm examples/client",
|
||||
"singlelog": "UCI_LOG=true sudo node -r esm examples/single | pino-colada",
|
||||
"mmulti": "MOCK=true node --require esm examples/multi",
|
||||
"multi": "sudo node --require esm examples/multi",
|
||||
"multilog": "DEBUG=true sudo node --require esmexamples/multi"
|
||||
"multilog": "UCI_LOG=true sudo node --require esm examples/multi | pino-colada"
|
||||
},
|
||||
"author": "David Kebler",
|
||||
"license": "MIT",
|
||||
|
@ -44,6 +45,6 @@
|
|||
"istanbul": "^0.4.5",
|
||||
"mocha": "^5.0.1",
|
||||
"nodemon": "^1.14.3",
|
||||
"pigpio-mock": "0.0.1"
|
||||
"pigpio-mock": "uCOMmandIt/pigpio-mock#master"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
// import bus, { Gpio } from 'pigpio'
|
||||
// import bus, { Gpio } from 'pigpio-mock'
|
||||
// import btc from 'better-try-catch'
|
||||
let bus = {}
|
||||
import debounce from 'lodash.debounce'
|
||||
|
@ -34,29 +32,21 @@ export default class Interrupt extends Base {
|
|||
opts.itrt.port = opts.itrt.port || opts.port || 9000+pin
|
||||
opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + 'itrt#s>t'
|
||||
}
|
||||
console.log('sockets', opts.sockets)
|
||||
super(opts)
|
||||
console.dir(opts)
|
||||
this.id = (opts.id ||'interrupt') + ':' + pin
|
||||
log = logger({name:'interrupt',id:this.id})
|
||||
log.info({pins:pin, opts:opts},'created interrupt with these opts')
|
||||
this.pin_num = pin
|
||||
this.mock = opts.mock
|
||||
this.mock = opts.mock || process.env.MOCK
|
||||
this.wait = opts.wait || 0 // debounce is off by default
|
||||
this.dbopts = { maxWait:opts.maxwait || 500, leading: opts.leading || true, trailing:opts.trailing || false}
|
||||
// this.edge = opts.edge || Gpio.RISING_EDGE
|
||||
this.edge = opts.edge
|
||||
this.pull = opts.pull
|
||||
this.pin = {} //set at init
|
||||
// this.pin = new Gpio(
|
||||
// pin, {
|
||||
// mode: Gpio.INPUT,
|
||||
// pullUpDown: opts.pull || Gpio.PUD_DOWN
|
||||
// // do not! set edge here as it will start the emitter -- see pigio js
|
||||
// })
|
||||
this._hook = opts.hook
|
||||
this.packet = opts.packet || {}
|
||||
this.packet.pin = pin
|
||||
this.packet.cmd = this.packet.cmd || 'interrupt'
|
||||
this.packet.cmd = this.packet.cmd || opts.pushcmd || 'interrupt'
|
||||
this.packet.count = 0
|
||||
|
||||
|
||||
|
@ -67,9 +57,8 @@ export default class Interrupt extends Base {
|
|||
await super.init()
|
||||
// for cntrl-c exit of interrupt
|
||||
// create the pigio pin_num
|
||||
if (this.mock) bus = await import('../../pigpio-mock/src')
|
||||
if (this.mock) bus = await import('pigpio-mock')
|
||||
else bus = await import('pigpio')
|
||||
// console.log(bus.Gpio)
|
||||
this.pin = new bus.Gpio(
|
||||
this.pin_num, {
|
||||
mode: bus.Gpio.INPUT,
|
||||
|
@ -102,7 +91,7 @@ export default class Interrupt extends Base {
|
|||
}
|
||||
this.pin.on('interrupt',cb)
|
||||
// rock n roll!!, start the pigpio interrupt
|
||||
if(!this.mock) this.pin.enableInterrupt(this.edge)
|
||||
if(!this.mock) this.pin.enableInterrupt(this.edge || bus.Gpio.RISING_EDGE )
|
||||
}
|
||||
|
||||
|
||||
|
@ -122,20 +111,22 @@ export default class Interrupt extends Base {
|
|||
async _interruptProcess (packet) {
|
||||
packet.count += 1
|
||||
packet.time = new Date().getTime()
|
||||
if(this._hook) packet = this.hook(packet)
|
||||
// console.log('packet pushing to all clients',packet)
|
||||
if(this._hook) packet = await this.hook(packet)
|
||||
log.info({packet:packet},'packet pushing to all clients')
|
||||
this.push(packet)
|
||||
}
|
||||
|
||||
//sample hook
|
||||
hook (packet) {
|
||||
// return a promise if anything async happens in hook
|
||||
// new Promise((resolve) => {
|
||||
console.log('=======================')
|
||||
console.log(`pin ${packet.pin} on sbc gpio bus has thrown an interrupt`)
|
||||
console.log('sending to all connected sockets with default cmd:"interrupt"')
|
||||
console.log(`pushing to all connected socket client with cmd:${packet.cmd}`)
|
||||
console.dir(packet)
|
||||
console.log('this is the default beforeHook')
|
||||
console.log('add "beforeHook" for your instance or extended class')
|
||||
console.log('This hook allow you to modify the packet being pushed to connected clients')
|
||||
console.log('add .hook method for your instance or extended class to overwrite this')
|
||||
console.log('Must be promise returning if anything async happens in your hook')
|
||||
console.log('=======================')
|
||||
return packet
|
||||
// resolve(packet)
|
||||
|
|
|
@ -9,16 +9,18 @@ export default class Interrupts {
|
|||
this.pins = pins
|
||||
this.interrupt={}
|
||||
log = logger({name:'interrupts',id:this.id})
|
||||
pins.forEach (pin =>{
|
||||
opts[pin] = opts[pin] || {}
|
||||
opts[pin].id = (opts.id ||'interrupt') + ':' + pin;
|
||||
['host','port','path','hook','wait','maxwait','leading','mock','trailing','edge','pull'].forEach(prop =>{
|
||||
opts[pin][prop] = opts[pin][prop] || opts[prop]
|
||||
})
|
||||
this.interrupt[pin] = new Interrupt(pin,opts[pin])
|
||||
this.interrupt[pin].hook=hook
|
||||
this.interrupt[pin].reply = () =>{}
|
||||
let pinopts={}
|
||||
pins.forEach (pin =>{ // remove per pin opts and store
|
||||
pinopts[pin]=Object.assign({},opts[pin])
|
||||
delete(opts[pin])
|
||||
})
|
||||
pins.forEach (pin =>{
|
||||
pinopts[pin] = Object.assign({}, opts, pinopts[pin])
|
||||
pinopts[pin].id = (opts.id ||'interrupt') + ':' + pin
|
||||
log.info({opts:pinopts[pin]},`pin options for pin ${pin}`)
|
||||
this.interrupt[pin] = new Interrupt(pin,pinopts[pin])
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
async init() {
|
||||
|
@ -46,6 +48,13 @@ export default class Interrupts {
|
|||
this.interrupt[pin].hook=func
|
||||
})
|
||||
}
|
||||
// new test
|
||||
push(packet) {
|
||||
this.pins.forEach (async pin =>{
|
||||
console.log('all push', pin, packet)
|
||||
this.interrupt[pin].push(packet)
|
||||
})
|
||||
}
|
||||
|
||||
} // end Class
|
||||
|
||||
|
@ -53,7 +62,7 @@ export default class Interrupts {
|
|||
const hook = (packet) => {
|
||||
console.log('======Common for all Pins Default Hook=================')
|
||||
console.log(`pin ${packet.pin} on sbc gpio bus has thrown ${packet.count}th interrupt`)
|
||||
console.log('sending to all connected sockets with default cmd:"interrupt"')
|
||||
console.log('sending to all connected consumers/clients with default cmd:"interrupt"')
|
||||
console.dir(packet)
|
||||
console.log('this is the default beforeHook')
|
||||
console.log('add .hook for your instance or extended class')
|
||||
|
|
Loading…
Reference in New Issue