From c0964d1f1d95e51f8f2b014bfe5e3074706a25e7 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Fri, 8 Mar 2019 09:05:18 -0800 Subject: [PATCH] 0.2.12 refactored hook registration --- package.json | 2 +- src/interrupt.js | 78 ++++++++++++++++++++++-------------------------- 2 files changed, 37 insertions(+), 43 deletions(-) diff --git a/package.json b/package.json index b98bf4c..6ed1cbf 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/interrupt.js b/src/interrupt.js index 3f790e0..83ae75f 100644 --- a/src/interrupt.js +++ b/src/interrupt.js @@ -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 - // new Promise((resolve) => { - console.log('=======================') - 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('=======================') - return packet - // resolve(packet) - // }) + + 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('==========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('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) + // }) +}