60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
import Interrupt from './interrupt'
|
|
|
|
import logger from '@uci-utils/logger'
|
|
let log = {}
|
|
|
|
class Interrupts {
|
|
constructor(pins, opts = {}) {
|
|
this.id = this.id || 'interrupts'
|
|
this.pins = pins
|
|
this.interrupt = {}
|
|
this.s = { fire:this.fire.bind(this)} // make fire available via consumer packet send
|
|
log = logger({ name: 'interrupts', id: this.id })
|
|
let pinopts = {}
|
|
pins.forEach(pin => {
|
|
// remove per pin opts and store
|
|
pinopts[pin] = Object.assign({}, opts[pin])
|
|
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
|
|
log.debug({ opts: pinopts[pin] }, `pin options for pin ${pin}`)
|
|
this.interrupt[pin] = new Interrupt(pin, pinopts[pin])
|
|
})
|
|
}
|
|
|
|
async init() {
|
|
return Promise.all(
|
|
this.pins.map(pin => {
|
|
return this.interrupt[pin].init()
|
|
})
|
|
)
|
|
}
|
|
|
|
// manual firing for testing
|
|
async fire(packet) {
|
|
if (packet.pin) return await this.interrupt[packet.pin].fire(packet)
|
|
for ( let pin of packet.pins) {
|
|
packet[pin] = await this.interrupt[pin].fire(packet)
|
|
} return packet
|
|
}
|
|
|
|
|
|
registerHook(func) {
|
|
this.pins.forEach(async pin => {
|
|
this.interrupt[pin].registerHook(func)
|
|
})
|
|
}
|
|
// used for testing
|
|
push(packet) {
|
|
this.pins.forEach(async pin => {
|
|
console.log('=======all push============', pin, packet)
|
|
this.interrupt[pin].push(packet)
|
|
})
|
|
}
|
|
} // end Class
|
|
|
|
export default Interrupts
|