From 03c3ac87474f9496a6ac0fc488bf185d95e33860 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Sun, 1 Jul 2018 15:37:59 -0700 Subject: [PATCH] add client example for interrupt sockect server example. Bump version. --- examples/client.js | 33 ++++++++++++ examples/single.js | 31 ++++++------ package.json | 10 ++-- src/interrupt-c.js | 123 --------------------------------------------- src/interrupt.js | 16 +++--- 5 files changed, 62 insertions(+), 151 deletions(-) create mode 100644 examples/client.js delete mode 100644 src/interrupt-c.js diff --git a/examples/client.js b/examples/client.js new file mode 100644 index 0000000..dc313a3 --- /dev/null +++ b/examples/client.js @@ -0,0 +1,33 @@ +import Base from '@uci/base' + +const HOST = 'localhost' +const PORT = 9024 +let processor = new Base({sockets:'inter#c>t', inter:{host:HOST, port:PORT}, id:'interrupt-processor', useRootNS:true}) + +processor.interrupt = async function (packet) { + return new Promise((resolve) => { + console.log('interrupt occured') + console.dir(packet) + resolve({status: 'processed'}) + }) +} + +processor.reply = async function (packet) { + return new Promise((resolve) => { + console.log('interrupt fired') + console.dir(packet) + resolve({status: 'processed'}) + }) +} + +; +(async () => { + + await processor.init() + await processor.send({cmd: 'fire'}) + await processor.send({cmd: 'fire'}) + // process.kill(process.pid, 'SIGTERM') + +})().catch(err => { + console.error('FATAL: UNABLE TO START SYSTEM!\n',err) +}) diff --git a/examples/single.js b/examples/single.js index 9a3bbad..35eec82 100644 --- a/examples/single.js +++ b/examples/single.js @@ -1,29 +1,30 @@ import Interrupt from '../src/interrupt' -import Base from '@uci/base' +// 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'}) -let interrupt = new Interrupt(24,{id:'test-interrupt', wait:0, hook:true, mock:true}) -let processor = new Base({sockets:'inter#c>t', inter:{port:9024}, id:'listener'}) +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'}) - }) - -} +// processor.interrupt = async function (packet) { +// return new Promise((resolve) => { +// console.log('interrupt socket listener got') +// console.dir(packet) +// resolve({status: 'processed'}) +// }) +// +// } ; (async () => { - console.log(await interrupt.init()) - console.log(await processor.init()) - interrupt.fire() - processor.send({cmd: 'fire'}) + 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() diff --git a/package.json b/package.json index bd81f5d..079067b 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,12 @@ { "name": "@uci/interrupt", "main": "src", - "version": "0.1.4", + "version": "0.1.5", "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", + "client": "node -r esm examples/client", "singlelog": "UCI_LOG=true sudo node -r esm examples/single | pino-colada", "multi": "sudo node --require esm examples/multi", "multilog": "DEBUG=true sudo node --require esmexamples/multi" @@ -30,15 +32,15 @@ "pigpio": "^0.x" }, "dependencies": { - "@uci/base": "^0.1.1", - "@uci/logger": "^0.0.2", + "@uci/base": "^0.1.7", + "@uci/logger": "0.0.3", "lodash.debounce": "^4.0.8" }, "devDependencies": { "chai": "^4.1.2", "chai-as-promised": "^7.1.1", "codecov": "^3.0.0", - "esm": "^3.0.21", + "esm": "^3.0.58", "istanbul": "^0.4.5", "mocha": "^5.0.1", "nodemon": "^1.14.3", diff --git a/src/interrupt-c.js b/src/interrupt-c.js deleted file mode 100644 index b091efb..0000000 --- a/src/interrupt-c.js +++ /dev/null @@ -1,123 +0,0 @@ -import bus, { Gpio } from 'pigpio' -// import bus, { Gpio } from 'pigpio-mock' -// import btc from 'better-try-catch' -import debounce from 'lodash.debounce' -// import throttle from 'lodash.throttle' -// import debounce from 'debounce-fn' -import Base from '@uci/base' - -import logger from '@uci/logger' -let log = {} - -export default class Interrupt extends Base { - constructor(pin,opts={}) { - - opts.itrt = opts.itrt || {} - if (opts.itrt || opts.host) { - opts.itrt.host = opts.host || opts.itrt.host - if (opts.itrt.host) { - opts.itrt.port = opts.itrt.port || opts.port || 9000+pin - opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + 'itrt#c>t' - } - } - if (opts.path || opts.itrn || !opts.itrt.host) { - opts.itrn = opts.itrn || {} - opts.itrn.path = opts.path || opts.itrn.path || 'interrupt:'+ pin - opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + 'itrn#c>n' - } - if (opts.sockets==='') throw ('must have at least one socket client') - super(opts) - console.dir(opts) - 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.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.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.count = 0 - - - } // end constructor - - - async init(){ - await super.init() - // for cntrl-c exit of interrupt - process.on('SIGINT', () => { - this.exit().then((resp) => console.log('\n', resp)) // unexport on cntrl-c - .catch(err => console.log('error:', err)) - }) - // const pinDebounce = function (processor, wait, options, packet) { - // console.log(processor,wait,options,packet) - // return debounce.bind(this,processor.bind(this,packet),wait, options) - // return debounce.bind(processor.bind(this,packet),wait) - // return debounce.bind(null,processor,{ wait:wait}) - // } - // else cb = pinDebounce(this.interruptProcess,this.wait,this.dbopts,this.packet) - - 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`) - } - 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}` ) - } - this.pin.on('interrupt',cb) - // rock n roll!!, start the pigpio interrupt - if(!this.mock) this.pin.enableInterrupt(this.edge) - } - - - // manual firing for testing - fire() { - console.log('manually firing interrupt for pin', this.pin_num) - this.pin.emit('interrupt',1) - } - - exit() { - bus.terminate() - return Promise.reject('keyboard termination...terminating interrupts') - } - - // default processor - async _interruptProcess (packet) { - packet.count += 1 - packet.time = new Date().getTime() - if(this._hook) packet = this.hook(packet) - // console.log('packet sending',packet) - this.send(packet) - } - - //sample hook - hook (packet) { - // 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.dir(packet) - console.log('this is the default beforeHook') - console.log('add "beforeHook" for your instance or extended class') - console.log('=======================') - return packet - // resolve(packet) - // }) - - } - - -} // end Class diff --git a/src/interrupt.js b/src/interrupt.js index 823c5fa..c74cbf9 100644 --- a/src/interrupt.js +++ b/src/interrupt.js @@ -29,12 +29,12 @@ export default class Interrupt extends Base { opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + 'itrw#s>w' } // default is a tcp socket server at 9000+pin - if (opts.itrt || opts.port || opts.sockets==='') { + if (opts.itrt || opts.port || !opts.sockets) { opts.itrt = opts.itrt || {} 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) log = logger({name:'interrupt',id:this.id}) @@ -67,8 +67,9 @@ export default class Interrupt extends Base { await super.init() // for cntrl-c exit of interrupt // create the pigio pin_num - bus = await import('../../pigpio-mock/src') - console.log(bus.Gpio) + if (this.mock) bus = await import('../../pigpio-mock/src') + else bus = await import('pigpio') + // console.log(bus.Gpio) this.pin = new bus.Gpio( this.pin_num, { mode: bus.Gpio.INPUT, @@ -76,10 +77,6 @@ export default class Interrupt extends Base { // do not! set edge here as it will start the emitter -- see pigio js }) - - - - process.on('SIGINT', () => { this.exit().then((resp) => console.log('\n', resp)) // unexport on cntrl-c .catch(err => console.log('error:', err)) @@ -110,9 +107,10 @@ export default class Interrupt extends Base { // manual firing for testing - fire() { + async fire() { console.log('manually firing interrupt for pin', this.pin_num) this.pin.emit('interrupt',1) + return Promise.resolve({status:'fired'}) } exit() {