0.2.12 refactored hook registration
parent
be68921eac
commit
c0964d1f1d
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "@uci/interrupt",
|
||||
"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",
|
||||
"scripts": {
|
||||
"single": "sudo node -r esm examples/single",
|
||||
|
|
|
@ -55,7 +55,7 @@ class Interrupt extends Base {
|
|||
this.edge = opts.edge
|
||||
this.pull = opts.pull
|
||||
this.pin = {} //set at init
|
||||
this._hook = opts.hook
|
||||
this.hook = opts.hook
|
||||
this.packet = opts.packet || {}
|
||||
this.packet.pin = pin
|
||||
this.packet.cmd = this.packet.cmd || opts.pushCmd || 'interrupt'
|
||||
|
@ -68,7 +68,7 @@ class Interrupt extends Base {
|
|||
// create the pigio pin_num
|
||||
// TODO check for rpi and pigpio, if not available use 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')
|
||||
this.pin = new bus.Gpio(this.pin_num, {
|
||||
mode: bus.Gpio.INPUT,
|
||||
|
@ -84,33 +84,22 @@ class Interrupt extends Base {
|
|||
let cb = () => {}
|
||||
if (this.wait === 0) {
|
||||
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 {
|
||||
cb = debounce(
|
||||
this._interruptProcess.bind(this, this.packet),
|
||||
this.wait,
|
||||
this.dbopts
|
||||
)
|
||||
console.log(
|
||||
`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
|
||||
}`
|
||||
)
|
||||
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)
|
||||
// rock n roll!!, start the pigpio interrupt
|
||||
if (!this.mock) this.pin.enableInterrupt(this.edge || bus.Gpio.RISING_EDGE)
|
||||
}
|
||||
|
||||
this.registerHook(defaultHook)
|
||||
|
||||
} // end init
|
||||
|
||||
// manual firing for testing
|
||||
async fire() {
|
||||
|
@ -124,37 +113,42 @@ class Interrupt extends Base {
|
|||
return Promise.reject('keyboard termination...terminating interrupts')
|
||||
}
|
||||
|
||||
// default processor
|
||||
// use hook to do more processing
|
||||
async _interruptProcess(packet) {
|
||||
packet.count += 1
|
||||
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')
|
||||
this.push(packet)
|
||||
}
|
||||
|
||||
//sample hook
|
||||
async hook(packet) {
|
||||
// return a promise if anything async happens in hook
|
||||
|
||||
registerHook(func) {
|
||||
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) => {
|
||||
console.log('=======================')
|
||||
console.log('==========default hook =============')
|
||||
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.dir(packet)
|
||||
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('=======================')
|
||||
console.dir('packet)
|
||||
console.log('replace by a new function with .registerHook(function) to overwrite this')
|
||||
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('the function will be bound to the instance for complete access')
|
||||
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('by default the instance id will be attached to the packet before this')
|
||||
return packet
|
||||
// resolve(packet)
|
||||
// })
|
||||
}
|
||||
} // end Class
|
||||
|
||||
export default Interrupt
|
||||
|
|
Loading…
Reference in New Issue