uci-base/examples/mqtt.js

46 lines
1.1 KiB
JavaScript

import Base from '../src/base'
// const USOCKET = __dirname + '/sample.sock'
const mqttfuncs = {
relays: {
on: async function(packet){
return new Promise( async (resolve) => {
console.log('ON')
console.dir(packet)
return resolve()
})
},
off: async function(packet){
return new Promise( async (resolve) => {
console.log('OFF')
console.dir(packet)
return resolve()
})
}
}
}
const process = async function (packet,topic) {
console.log('==============mqtt incoming packet/topic processor =========')
// console.log(packet, topic, this.topics[topic])
if (packet.cmd) await this.topics[topic][packet.cmd](packet)
else await this.topics[topic](packet)
console.log('===========================')
}
let mqtt = new Base({ mqtt:{ connect:{host:'localhost', port:1883}, id:'relays'} })
mqtt.topics = mqttfuncs
mqtt.socket.mqtt.registerPacketProcessor(process.bind(mqtt))
;
(async () => {
console.log(await mqtt.init())
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
process.kill(process.pid, 'SIGTERM')
})