// TODO make this an HA plugin to support HA convention --- ///***************** HOME ASSISTANT ************************ // uses main broker socket but formats incoming and outgoing packets for HA convention // TODO Make sure subscribed topics includes 'lighting/set/#' const TOPIC = 'relay' function register(name) { this.beforeProcessHook(async (packet) => { // console.log('incoming mqtt packet to modify') // console.dir(packet) let cmd = packet.cmd.split('/') packet.cmd = `${packet.data.toLowerCase()}` packet.pins = cmd[2] delete packet.data // console.log('translated to uci packet') // console.dir(packet) return packet }, {name:name}) this.afterProcessHook(async (packet) => { // console.log('processed packet available to modify again', packet) if (packet.error) { let npacket = {} npacket.cmd = 'error' npacket.payload = JSON.stringify(packet) // npacket.payload = packet.error return npacket } return packet }, {name:name}) this.beforeSendHook(async (packet) => { // console.log('beforeSendHook', packet) if (packet.cmd === 'status') { let pin = packet.pins || packet.pin || packet.gpios || packet.gpio || packet.ids || packet.id packet.cmd = `${TOPIC}/status/${pin}` packet.payload = (packet.state[pin]!=null ? packet.state[pin] : packet.state ) ? 'on' : 'off' // console.log('=============modified packet sent to broker================') // console.dir(packet) // console.log('================') } return packet }, {name:name}) } export default register