0.2.11 fix issue where pin is string, cast to number

master
David Kebler 2019-03-07 07:35:08 -08:00
parent d656af443e
commit be68921eac
3 changed files with 11 additions and 9 deletions

View File

@ -1,7 +1,7 @@
{
"name": "@uci/interrupt",
"main": "src",
"version": "0.2.10",
"version": "0.2.11",
"description": "a class for adding interrupt processesing for gpio pins on Raspberry Pi and Similar SBCs",
"scripts": {
"single": "sudo node -r esm examples/single",

View File

@ -7,6 +7,7 @@ let log = {}
// a pin makes a socket (server/listner) for each pin to which a consumer can be connected
class Interrupt extends Base {
constructor(pin, opts = {}) {
if (typeof pin !=='number') pin = parseInt(pin) // make sure pin is a number!
opts.conPacket = (opts.resetCmd && !opts.conPacket) ? opts.conPacket : { cmd: opts.resetCmd, pin: pin } // will use either option
// if opts .port/.path/.topic/.wport are base number/name to which pin number is added/appended (default being 9000,9100,'interrupt')
// for specific port number pass itrt.port,itrn.path,itrm.topic,itrw.port which override it if present
@ -27,15 +28,15 @@ class Interrupt extends Base {
if (opts.itrw || opts.wport || opts.wport===0 ) {
opts.itrw = opts.itrw || {}
if (opts.wport) opts.wport = opts.wport + pin
opts.itrw.port = opts.itrw.port || opts.wport || 9100 + pin
if (opts.wport) opts.wport = opts.wport + +pin
opts.itrw.port = opts.itrw.port || opts.wport || 9100 + +pin
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'itrw#s>w'
}
// default is a tcp socket server at 9000+pin
if (opts.itrt || opts.port || opts.port===0 || !opts.sockets ) {
opts.itrt = opts.itrt || {}
if (opts.port) opts.port = opts.port + pin
opts.itrt.port = opts.itrt.port || opts.port || 9000 + pin
if (opts.port) opts.port = opts.port + +pin
opts.itrt.port = opts.itrt.port || opts.port || 9000 + +pin
opts.itrt.conPacket = opts.conPacket
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'itrt#s>t'
}
@ -43,7 +44,7 @@ class Interrupt extends Base {
this.id = (opts.id || 'interrupt') + ':' + pin
log = logger({ name: 'interrupt', id: this.id })
log.info({ pins: pin, opts: opts }, 'created interrupt with these opts')
this.pin_num = pin
this.pin_num = +pin
this.mock = opts.mock || process.env.MOCK
this.wait = opts.wait || 0 // debounce is off by default
this.dbopts = {
@ -57,7 +58,7 @@ class Interrupt extends Base {
this._hook = opts.hook
this.packet = opts.packet || {}
this.packet.pin = pin
this.packet.cmd = this.packet.cmd || opts.pushcmd || 'interrupt'
this.packet.cmd = this.packet.cmd || opts.pushCmd || 'interrupt'
this.packet.count = 0
} // end constructor
@ -133,7 +134,7 @@ class Interrupt extends Base {
}
//sample hook
hook(packet) {
async hook(packet) {
// return a promise if anything async happens in hook
// new Promise((resolve) => {
console.log('=======================')

View File

@ -16,6 +16,7 @@ class Interrupts {
delete opts[pin]
})
pins.forEach(pin => {
if (typeof pin !=='number') pin = parseInt(pin)
pinopts[pin] = Object.assign({}, opts, pinopts[pin])
pinopts[pin].id = (opts.id || 'interrupt') + ':' + pin
pinopts[pin].conPacket = { cmd: opts.resetCmd, pin: pin }
@ -54,7 +55,7 @@ class Interrupts {
// new test
push(packet) {
this.pins.forEach(async pin => {
console.log('all push', pin, packet)
console.log('=======all push============', pin, packet)
this.interrupt[pin].push(packet)
})
}