2018-05-20 15:44:31 -07:00
|
|
|
import Base from '../src/base'
|
|
|
|
|
|
|
|
// const USOCKET = __dirname + '/sample.sock'
|
|
|
|
|
2019-01-01 16:39:08 -08:00
|
|
|
let wss = new Base({id:'websocket', useRootNS: true})
|
2018-05-20 15:44:31 -07:00
|
|
|
|
2019-01-01 16:39:08 -08:00
|
|
|
wss.switch = {
|
2018-05-20 15:44:31 -07:00
|
|
|
on: function(packet){
|
|
|
|
return new Promise( async (resolve) => {
|
2019-01-01 16:39:08 -08:00
|
|
|
console.log(`turning switch on for id ${packet.id||packet.payload.id}`)
|
2018-05-20 15:44:31 -07:00
|
|
|
// call switch on here
|
|
|
|
let res = {}
|
2019-01-01 16:39:08 -08:00
|
|
|
res.log = `turning switch on for id ${packet.id||packet.payload.id}`
|
2018-05-20 15:44:31 -07:00
|
|
|
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
|
2019-01-01 16:39:08 -08:00
|
|
|
return resolve(res)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
toggle: function(packet){
|
|
|
|
return new Promise( async (resolve) => {
|
|
|
|
console.log(`toggling switch for id ${packet.id||packet.data}`)
|
|
|
|
// call switch on here
|
|
|
|
let res = {}
|
|
|
|
res.cmd='switch/status'
|
|
|
|
res.status= (packet.status === 'on')?'off':'on'
|
|
|
|
res.id = packet.id
|
2018-05-25 14:39:48 -07:00
|
|
|
console.log('broadcast',res)
|
|
|
|
this.push(res)
|
2019-01-01 16:39:08 -08:00
|
|
|
// return only acknowledge to sender, packet went as push
|
2018-05-25 14:39:48 -07:00
|
|
|
res = { cmd:'ack'}
|
2018-05-20 15:44:31 -07:00
|
|
|
return resolve(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
(async () => {
|
|
|
|
|
2019-01-01 16:39:08 -08:00
|
|
|
await wss.init()
|
|
|
|
await wss.addSocket('web','s','w')
|
|
|
|
wss.push({server:'started'})
|
|
|
|
await wss.addSocket('mqs','s','m')
|
|
|
|
wss.socket.mqs.subscribe(['switch/on','switch/off','switch/toggle'])
|
2018-05-20 15:44:31 -07:00
|
|
|
|
|
|
|
})().catch(err => {
|
|
|
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
|
|
|
process.kill(process.pid, 'SIGTERM')
|
|
|
|
})
|