109 lines
2.9 KiB
JavaScript
109 lines
2.9 KiB
JavaScript
|
import Base from '../src/base'
|
||
|
|
||
|
const BROKER = 'nas.kebler.net'
|
||
|
const TOPICS = ['set/testing/+'] // listen for a set command
|
||
|
|
||
|
const commands = {
|
||
|
on: function(packet){
|
||
|
return new Promise( async (resolve) => {
|
||
|
console.log(`turning switch on for id ${packet.id||packet.data}`)
|
||
|
console.log('entire packet',packet)
|
||
|
// call switch on here
|
||
|
let res = {}
|
||
|
res.cmd='status'
|
||
|
res.state='on'
|
||
|
res.id = packet.id
|
||
|
this.push(packet)
|
||
|
return resolve(res)
|
||
|
})
|
||
|
},
|
||
|
off: function(packet){
|
||
|
return new Promise( async (resolve) => {
|
||
|
console.log(packet)
|
||
|
console.log(`turning switch off for id ${packet.id||packet.data}`)
|
||
|
// call switch on here
|
||
|
let res = {}
|
||
|
res.cmd='status'
|
||
|
res.state='off'
|
||
|
res.id = packet.id
|
||
|
return resolve(res)
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let relays = new Base({id:'homeassistant-example'})
|
||
|
|
||
|
relays.commands = relays.bindFuncs(commands)
|
||
|
|
||
|
;
|
||
|
(async () => {
|
||
|
await relays.addSocket('mqs','s','m', {host:BROKER, topics:TOPICS})
|
||
|
relays.addNamespace('commands','s')
|
||
|
register.call(relays,'mqs')
|
||
|
await relays.init()
|
||
|
|
||
|
})().catch(err => {
|
||
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
||
|
process.kill(process.pid, 'SIGTERM')
|
||
|
})
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
///***************** HOME ASSISTANT ************************
|
||
|
// formats incoming and outgoing packets for HA convention
|
||
|
|
||
|
const STATUS_TOPIC = 'status/testing'
|
||
|
|
||
|
function register(name) {
|
||
|
|
||
|
this.beforeProcessHook(async (packet) => {
|
||
|
console.log('incoming mqtt packet to modify')
|
||
|
console.dir(packet)
|
||
|
let modified = Object.assign({},packet)
|
||
|
let cmd = packet.cmd.split('/')
|
||
|
modified.cmd = `${packet.data.toLowerCase()}`
|
||
|
modified.id = cmd[2]
|
||
|
delete modified.data
|
||
|
console.log('translated to uci packet')
|
||
|
console.dir(modified)
|
||
|
return modified
|
||
|
// 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)
|
||
|
return npacket
|
||
|
}
|
||
|
if (packet.cmd === 'status') {
|
||
|
packet.cmd = `${STATUS_TOPIC}/${packet.id}`
|
||
|
packet.payload = packet.state.toUpperCase()
|
||
|
console.log('=============modified packet sent to broker================')
|
||
|
console.dir(packet)
|
||
|
console.log('================')
|
||
|
}
|
||
|
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 = `${STATUS_TOPIC}/${num}`
|
||
|
// packet.payload = packet.pins[num] ? 'ON' : 'OFF'
|
||
|
// console.log('=============modified packet sent to broker================')
|
||
|
// console.dir(packet)
|
||
|
// console.log('================')
|
||
|
// }
|
||
|
// return packet
|
||
|
// },
|
||
|
// {name:name})
|
||
|
|
||
|
}
|