import Interrupt from './interrupt' import logger from '@uci/logger' let log = {} const LOG_OPTS = (id) => { return { repo:'uci-interrupt', npm:'@uci/interrupt', file:'src/interrupts.mjs', class:'Interrupts', id:id, instance_created:new Date().getTime() }} export default class Interrupts { constructor(pins,opts={}) { this.id = this.id || 'interrupts' this.pins = pins this.interrupt={} pins.forEach (pin =>{ opts[pin] = opts[pin] || {} opts[pin].id = (opts.id ||'interrupt') + ':' + pin; ['host','port','path','hook','wait','maxwait','leading','mock','trailing','edge','pull'].forEach(prop =>{ opts[pin][prop] = opts[pin][prop] || opts[prop] }) this.interrupt[pin] = new Interrupt(pin,opts[pin]) this.interrupt[pin].hook=hook this.interrupt[pin].reply = () =>{} }) log = logger.child(LOG_OPTS(this.id)) } 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 }) } } // end Class // 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 sockets 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 }