From 7b8d5d7552a6a0ae00a0eb7afabe4237415af4c7 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Sun, 26 Jul 2020 17:29:24 -0700 Subject: [PATCH] 0.3.3 Bug fix - Make sure that the number key for the interrupts map is ALWAYS a number even if string is passed --- examples/multi.js.old | 38 ++++++++++++++++++++++++++++++++++++++ examples/multi.yaml | 20 ++++++++++++++++++++ nodemon.json | 5 +++++ package.json | 4 ++-- src/interrupts.js | 9 ++++++--- 5 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 examples/multi.js.old create mode 100644 examples/multi.yaml create mode 100644 nodemon.json diff --git a/examples/multi.js.old b/examples/multi.js.old new file mode 100644 index 0000000..7df3f64 --- /dev/null +++ b/examples/multi.js.old @@ -0,0 +1,38 @@ +import Interrupts from '../src/interrupts' + +const PINS = [process.env.PIN1 || 9 ,process.env.PIN2 || 10] + +const EDGE = process.env.EDGE || 'rising' // 'both' +// outbound consumer +const REMOTE_HOST = process.env.REMOTE_HOST || 'sbc' +const REMOTE_PORT = process.env.REMOTE_PORT || 9001 +const PATH = process.env.PATH || 'mcp' +const LISTEN_TCP = process.env.LISTEN_TCP || '8020' +const LISTEN_PATH = process.env.LISTEN_PATH || 'interrupt' + +let interrupts = new Interrupts(PINS, { id:'multi-interrupt', cmd:'interrupt.find', resetCmd:'interrupt.reset', resetInterval:0, edge:EDGE }) + +interrupts.amendConsumerCommands( {reply:() => {}} ) + +interrupts.on('log', ev => { + if (ev.level !== 'debug' && ev.level !== 'trace') console.log(`LOG:'--${ev.level}-- ${ev.msg}`) + // console.log(`LOG:'--${ev.level}-- ${ev.msg}`) +}) + +interrupts.on('connection:socket', ev => { + console.log(`remote socket connection event ${ev.socketName}: ${ev.state}`) +}) + +; +(async () => { + + // interrupts.registerSocket('interrupt','c','t',{host:REMOTE_HOST, port:REMOTE_PORT, name:'interrupt', data:{pins:PINS, type:'interrupt'}}) + interrupts.registerSocket('interrupt','c','n',{path:PATH, name:'interrupt', data:{name:'interrupt', pins:PINS, type:'interrupt'}}) + // interrupts.registerSocket('listen','s','t',{port:1777}) + await interrupts.init() + await interrupts.socketsInit() + +})().catch(err => { + console.error('FATAL: UNABLE TO START SYSTEM!\n',err) + // process.kill(process.pid, 'SIGTERM') +}) diff --git a/examples/multi.yaml b/examples/multi.yaml new file mode 100644 index 0000000..4b52005 --- /dev/null +++ b/examples/multi.yaml @@ -0,0 +1,20 @@ +name: interrupt +path: interrupt # make a named pipe socket (listnerer) +sockets: # make a consumer to the mcp device +- name: switches + type: c + transport: n + options: + data: + id: interrupt + name: interrupt # name of consumer + initTimeout: 0 + retryWait: 5 + path: switches # pipe at which to connect +pins: +- 24 +resetCmd: reset # command that will be pushed +resetInterval: 0 # will broadcast a generic reset request, 0 is disabled +interruptCmd: find # command that will be pushed on all consumers/sockets +edge: rising +pull: down diff --git a/nodemon.json b/nodemon.json new file mode 100644 index 0000000..119d10a --- /dev/null +++ b/nodemon.json @@ -0,0 +1,5 @@ +{ + "ignoreRoot": [".git"], + "watch": ["node_modules/@uci/","node_modules/@uci-utils/","src/","examples/"], + "ext":"js,json,yaml,yml" +} diff --git a/package.json b/package.json index 6f1758b..f952da3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@uci/interrupt", "main": "src", - "version": "0.3.2", + "version": "0.3.3", "description": "a class for adding interrupt processesing for gpio pins on Raspberry Pi and Similar SBCs", "scripts": { "single": "node -r esm examples/single", @@ -34,7 +34,7 @@ "homepage": "https://github.com/uCOMmandIt/uci-interrrupt#readme", "dependencies": { "@uci-utils/logger": "^0.0.18", - "@uci/base": "^0.5.2", + "@uci/base": "^0.5.4", "death": "^1.1.0", "onoff": "^6.0.0" }, diff --git a/src/interrupts.js b/src/interrupts.js index 19ae180..12fe5c8 100644 --- a/src/interrupts.js +++ b/src/interrupts.js @@ -22,13 +22,13 @@ class Interrupts extends Base { intervalReset:makefunc.bind(this,'intervalReset') }) let pinopts = {} - pins.forEach(pin => { + this.pins.forEach(pin => { // remove per pin opts and store pinopts[pin] = Object.assign({}, opts[pin]) delete opts[pin] pinopts[pin].multiple=true // each pin will only extend a simple emitter, uci-base }) - pins.forEach(pin => { + this.pins.forEach(pin => { pinopts[pin] = Object.assign({}, opts, pinopts[pin]) pinopts[pin].id = pinopts[pin].id || this.id + ':' + pin log.debug({ opts: pinopts[pin], method:'constructor', line:25, msg:`pin options for pin ${pin}`}) @@ -60,7 +60,10 @@ class Interrupts extends Base { } // end constructor - interrupt(pin) { return this._interrupts.get(Number(pin)) } // get a handle to single interrupt + interrupt(pin) { + if (isNaN(pin)) return null + return this._interrupts.get(Number(pin)) + } // pin type here same as when instantiated above. async init() { await super.init()