80 lines
2.1 KiB
JavaScript
80 lines
2.1 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 = {}
|
|
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 => {
|
|
pinopts[pin] = Object.assign({}, opts, pinopts[pin])
|
|
pinopts[pin].id = (opts.id || 'interrupt') + ':' + pin
|
|
pinopts[pin].conPacket = { cmd: opts.resetCmd, pin: pin }
|
|
log.info({ 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
|
|
fire(pin) {
|
|
if (pin) {
|
|
this.interrupt[pin].pin.emit('interrupt', 1)
|
|
console.log('manually firing interrupt for pin', pin)
|
|
} else {
|
|
console.log('manually firing interrupt for pins', this.pins)
|
|
|
|
this.pins.forEach(async pin => {
|
|
this.interrupt[pin].pin.emit('interrupt', 1)
|
|
})
|
|
}
|
|
}
|
|
|
|
setHook(func) {
|
|
this.pins.forEach(async pin => {
|
|
this.interrupt[pin].hook = func
|
|
})
|
|
}
|
|
// new test
|
|
push(packet) {
|
|
this.pins.forEach(async pin => {
|
|
console.log('all push', pin, packet)
|
|
this.interrupt[pin].push(packet)
|
|
})
|
|
}
|
|
} // end Class
|
|
|
|
export default Interrupts
|
|
|
|
// default hook
|
|
const hook = packet => {
|
|
console.log('======Common for all Pins Default Hook=================')
|
|
console.log(
|
|
`pin ${packet.pin} on sbc gpio bus has thrown ${packet.count}th interrupt`
|
|
)
|
|
console.log(
|
|
'sending to all connected consumers/clients with default cmd:"interrupt"'
|
|
)
|
|
console.dir(packet)
|
|
console.log('this is the default beforeHook')
|
|
console.log('add .hook for your instance or extended class')
|
|
console.log('=======================')
|
|
return packet
|
|
}
|