0.2.12 refactored hook registration

master
David Kebler 2019-03-08 09:05:18 -08:00
parent be68921eac
commit c0964d1f1d
2 changed files with 37 additions and 43 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "@uci/interrupt", "name": "@uci/interrupt",
"main": "src", "main": "src",
"version": "0.2.11", "version": "0.2.12",
"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",

View File

@ -55,7 +55,7 @@ class Interrupt extends Base {
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._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 || opts.pushCmd || 'interrupt' this.packet.cmd = this.packet.cmd || opts.pushCmd || 'interrupt'
@ -68,7 +68,7 @@ class Interrupt extends Base {
// create the pigio pin_num // create the pigio pin_num
// TODO check for rpi and pigpio, if not available use onoff // TODO check for rpi and pigpio, if not available use onoff
// wrap all needed commands so can call module for pigpio or onoff // wrap all needed commands so can call module for pigpio or onoff
if (this.mock) bus = await import('pigpio-mock') if (process.env.UCI_MOCK==='true') bus = await import('pigpio-mock')
else bus = await import('pigpio') else bus = await import('pigpio')
this.pin = new bus.Gpio(this.pin_num, { this.pin = new bus.Gpio(this.pin_num, {
mode: bus.Gpio.INPUT, mode: bus.Gpio.INPUT,
@ -84,33 +84,22 @@ class Interrupt extends Base {
let cb = () => {} let cb = () => {}
if (this.wait === 0) { if (this.wait === 0) {
cb = this._interruptProcess.bind(this, this.packet) cb = this._interruptProcess.bind(this, this.packet)
console.log(`starting interrupt on pin ${this.pin_num} without debounce`) log.info({ packet: this.packet },`starting interrupt on pin ${this.pin_num} without debounce`)
log.info(
{ packet: this.packet },
`starting interrupt on pin ${this.pin_num} without debounce`
)
} else { } else {
cb = debounce( cb = debounce(
this._interruptProcess.bind(this, this.packet), this._interruptProcess.bind(this, this.packet),
this.wait, this.wait,
this.dbopts this.dbopts
) )
console.log( log.info({ packet: this.packet, wait: this.wait, options: this.dbopts },`starting interrupt on pin ${this.pin_num} with debounce wait:${this.wait}`)
`starting interrupt on pin ${this.pin_num} with debounce wait:${
this.wait
} options:${JSON.stringify(this.dbopts)}`
)
log.info(
{ packet: this.packet, wait: this.wait, options: this.dbopts },
`starting interrupt on pin ${this.pin_num} with debounce wait:${
this.wait
}`
)
} }
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 || bus.Gpio.RISING_EDGE) if (!this.mock) this.pin.enableInterrupt(this.edge || bus.Gpio.RISING_EDGE)
}
this.registerHook(defaultHook)
} // end init
// manual firing for testing // manual firing for testing
async fire() { async fire() {
@ -124,37 +113,42 @@ class Interrupt extends Base {
return Promise.reject('keyboard termination...terminating interrupts') return Promise.reject('keyboard termination...terminating interrupts')
} }
// default processor // use hook to do more processing
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 = await this.hook(packet) packet.id = this.id
if (this.hook) packet = await this._hookFunc.call(this,packet)
log.info({ packet: packet }, 'packet pushing to all clients') log.info({ packet: packet }, 'packet pushing to all clients')
this.push(packet) this.push(packet)
} }
//sample hook
async hook(packet) { registerHook(func) {
// return a promise if anything async happens in hook this._hookFunc = func
}
} // end Class
export default Interrupt
//default hook
async function defaultHook(packet) {
// return a promise or use await if anything async happens in hook
// new Promise((resolve) => { // new Promise((resolve) => {
console.log('=======================') console.log('==========default hook =============')
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(`pushing to all connected socket client with cmd:${packet.cmd}`) console.log(`pushing to all connected socket client with cmd:${packet.cmd}`)
console.dir(packet) console.dir('packet)
console.log( console.log('replace by a new function with .registerHook(function) to overwrite this')
'This hook allow you to modify the packet being pushed to connected clients' console.log('Must be async/promise returning if anything async happens in your hook')
) console.log('This hook allow you to modify/add to the packet being pushed to connected clients')
console.log( console.log('the function will be bound to the instance for complete access')
'add .hook method for your instance or extended class to overwrite this' console.log('if you pass a hash for .hook you can use it here as this.hook')
) console.log('the hook options contains', this.hook)
console.log( console.log('by default the instance id will be attached to the packet before this')
'Must be promise returning if anything async happens in your hook'
)
console.log('=======================')
return packet return packet
// resolve(packet) // resolve(packet)
// }) // })
} }
} // end Class
export default Interrupt