55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
|
|
||
|
// 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 = 'relays'
|
||
|
|
||
|
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 num = Object.keys(packet.pins)[0]
|
||
|
packet.cmd = `${TOPIC}/status/${num}`
|
||
|
packet.payload = packet.pins[num] ? 'on' : 'off'
|
||
|
console.log('=============modified packet sent to broker================')
|
||
|
console.dir(packet)
|
||
|
console.log('================')
|
||
|
}
|
||
|
return packet
|
||
|
},
|
||
|
{name:name})
|
||
|
|
||
|
}
|
||
|
|
||
|
export default register
|