0.2.25 refactor makefunc in multiple to properly combine results from single interrupt calls

master
David Kebler 2020-01-23 22:53:14 -08:00
parent 48b6680585
commit 610aa53a93
3 changed files with 11 additions and 12 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "@uci/interrupt", "name": "@uci/interrupt",
"main": "src", "main": "src",
"version": "0.2.24", "version": "0.2.25",
"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": "node -r esm examples/single", "single": "node -r esm examples/single",

View File

@ -65,6 +65,8 @@ class Interrupt extends Base {
this.emit('log',{level:'info', msg:`new interrupt pin ${this.pin_num} created and watching`, state:await this.status(), edge:this.edge,debounce:this.wait}) this.emit('log',{level:'info', msg:`new interrupt pin ${this.pin_num} created and watching`, state:await this.status(), edge:this.edge,debounce:this.wait})
super.init()
} // end init } // end init
// manual firing for testing // manual firing for testing
@ -90,17 +92,13 @@ class Interrupt extends Base {
} }
async intervalReset(packet) { async intervalReset(packet) {
// console.log('intervalReset ---- passed argument',packet)
let interval = typeof packet === 'number'? packet : (packet || {}).interval let interval = typeof packet === 'number'? packet : (packet || {}).interval
console.log('----------intervalReset--------', this.pin_num, interval)
if (!interval || interval<=0) { if (!interval || interval<=0) {
clearInterval(this._intervalReset) clearInterval(this._intervalReset)
this._intervalReset = null this._intervalReset = null
console.log('---------disabled--------')
} }
else { else {
this._intervalReset = setInterval(this.reset.bind(this), interval*1000) this._intervalReset = setInterval(this.reset.bind(this), interval*1000)
console.log('----------enabled----------')
} }
return this._intervalReset ? true : false return this._intervalReset ? true : false
} }

View File

@ -3,7 +3,6 @@ import Base from '@uci/base'
import logger from '@uci-utils/logger' import logger from '@uci-utils/logger'
let log = {} let log = {}
// will more easily create a group of sbc pin interrupts // will more easily create a group of sbc pin interrupts
class Interrupts extends Base { class Interrupts extends Base {
@ -70,7 +69,7 @@ class Interrupts extends Base {
interrupt(pin) { return this._interrupts.get(Number(pin)) } // get a handle to single interrupt interrupt(pin) { return this._interrupts.get(Number(pin)) } // get a handle to single interrupt
async init() { async init() {
super.init()
return Promise.all( return Promise.all(
Array.from(this._interrupts).map(inter => { Array.from(this._interrupts).map(inter => {
return inter[1].init() return inter[1].init()
@ -78,7 +77,6 @@ class Interrupts extends Base {
) )
} }
// only adds consumer sockets to each interrupt to same socket/server // only adds consumer sockets to each interrupt to same socket/server
// alternatively use listen handler and single socket // alternatively use listen handler and single socket
async addInterSocket(name,type) { async addInterSocket(name,type) {
@ -99,14 +97,17 @@ class Interrupts extends Base {
export default Interrupts export default Interrupts
async function makefunc(fn, packet={}) { async function makefunc(fn, packet={}) {
if (!packet.pin || packet.pin==='all') { if (!packet.pin || packet.pin==='all') {
let res = {} let rpacket = {pins:[]}
for (let inter of this._interrupts.entries()) { for (let inter of this._interrupts.entries()) {
res[inter[0]] = await inter[1][fn](packet) let ipacket = Object.assign({},packet)
delete(ipacket.cmd);delete(ipacket._header)
let res = await inter[1][fn](ipacket)
res.pin = inter[0]
rpacket.pins.push(res)
} }
return Object.assign(packet,res) return rpacket
} }
let pin = isNaN(Number(packet)) ? packet.pin : packet let pin = isNaN(Number(packet)) ? packet.pin : packet
if (this._interrupts.has(Number(pin))) return await this.interrupt(packet.pin)[fn](packet) if (this._interrupts.has(Number(pin))) return await this.interrupt(packet.pin)[fn](packet)