uci-base/examples/mqtt.js

67 lines
1.6 KiB
JavaScript
Raw Normal View History

import Base from '../src/base'
let dy = new Base({id:'dynamic', useRootNS: true })
const HOST = 'nas.kebler.net'
let sensor = true
//dummy simulated push
dy.sensor = {
test: function(packet){
return new Promise( async (resolve) => {
console.log(`doing a dumming sensor push for id ${packet.id||packet.data}`)
console.log('entire packet',packet)
if (sensor) dy.push({cmd:'sensor/on', id:packet.id})
else dy.push({cmd:'sensor/off', id:packet.id})
let res = {}
dy.push({cmd:'sensor/on', id:packet.id})
res.cmd='sensor/test'
res.test=true
res.id = packet.id
return resolve(res)
})
}
}
dy.switch = {
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='switch/status'
res.status='on'
res.id = packet.id
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='switch/status'
res.status='off'
res.id = packet.id
return resolve(res)
})
}
}
;
(async () => {
await dy.init()
await dy.addSocket('mqs','s','m',{host:HOST})
dy.socket.mqs.subscribe(['lights/#'])
console.log('ready')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
process.kill(process.pid, 'SIGTERM')
})