uci-base/examples/web.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

import Base from '../src/base'
// const USOCKET = __dirname + '/sample.sock'
let dy = new Base({id:'websocket'})
dy.switch = {
on: function(packet){
return new Promise( async (resolve) => {
console.log(`turning switch on for id ${packet.id||packet.data}`)
// 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(`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
console.log('broadcast',res)
this.push(res)
res = { cmd:'ack'}
return resolve(res)
})
}
}
;
(async () => {
await dy.init()
await dy.addSocket('web','s','w')
await dy.addSocket('mqs','s','m')
dy.socket.mqs.subscribe(['switch/on','switch/off','switch/toggle'])
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
process.kill(process.pid, 'SIGTERM')
})