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