2018-01-27 23:20:33 -08:00
|
|
|
import Base from '../src/base'
|
|
|
|
|
2018-02-13 14:19:18 -08:00
|
|
|
// const USOCKET = __dirname + '/sample.sock'
|
2018-01-30 21:12:38 -08:00
|
|
|
|
2018-02-04 14:18:21 -08:00
|
|
|
const socketfuncs = {
|
2018-05-25 14:39:48 -07:00
|
|
|
switch: {
|
|
|
|
on: function(packet){
|
2018-02-05 22:05:38 -08:00
|
|
|
return new Promise( async (resolve) => {
|
2018-05-25 14:39:48 -07:00
|
|
|
packet.cmd='status/on'
|
|
|
|
this.push(packet) // push to all active socket servers
|
|
|
|
let res = { response:'status pushed on to all clients'}
|
2018-02-05 22:05:38 -08:00
|
|
|
return resolve(res)
|
|
|
|
})
|
|
|
|
},
|
2018-05-25 14:39:48 -07:00
|
|
|
off: function(packet){
|
2018-02-05 22:05:38 -08:00
|
|
|
return new Promise( async (resolve) => {
|
2018-05-25 14:39:48 -07:00
|
|
|
packet.cmd='status/off'
|
|
|
|
this.push(packet) // push to all active socket servers
|
|
|
|
let res = { response:'status pushed off to all clients'}
|
|
|
|
return resolve(res)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
toggle: function(packet){
|
|
|
|
return new Promise( async (resolve) => {
|
|
|
|
// would check status before deciding what to push
|
|
|
|
if (packet.status===null) packet.status = Math.random()>=.5 ? 'on' : 'off'
|
|
|
|
else packet.status = (packet.status==='on' ? 'off' : 'on')
|
|
|
|
packet.cmd ='status/'+ packet.status
|
|
|
|
this.push(packet) // push to all active socket servers
|
|
|
|
let res = { cmd:'reply', response:`command ${packet.cmd} with id:${packet.id} pushed to all consumers(clients)`}
|
2018-02-05 22:05:38 -08:00
|
|
|
return resolve(res)
|
|
|
|
})
|
|
|
|
}
|
2018-01-30 21:12:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 14:39:48 -07:00
|
|
|
const clientfuncs = {
|
|
|
|
reply: packet => {
|
|
|
|
return new Promise( async (resolve) => {
|
|
|
|
console.log('==============Reply Processor at Consumer==========')
|
|
|
|
console.dir(packet.response)
|
|
|
|
return resolve()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
status: {
|
|
|
|
on: packet => {
|
2018-02-06 18:30:00 -08:00
|
|
|
return new Promise( async (resolve) => {
|
2018-05-25 14:39:48 -07:00
|
|
|
console.log('==============Pushed to Consumer==========')
|
|
|
|
console.log(`Switch ${packet.id} is on`)
|
|
|
|
return resolve()
|
2018-02-06 18:30:00 -08:00
|
|
|
})
|
|
|
|
},
|
2018-05-25 14:39:48 -07:00
|
|
|
off: packet => {
|
2018-02-06 18:30:00 -08:00
|
|
|
return new Promise( async (resolve) => {
|
2018-05-25 14:39:48 -07:00
|
|
|
console.log('==============Pushed to Consumer==========')
|
|
|
|
console.log(`Switch ${packet.id} is off`)
|
|
|
|
return resolve()
|
2018-02-06 18:30:00 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 14:39:48 -07:00
|
|
|
let fio = new Base({sockets:'uc#c>n,us#s>n,tc#c>t,ts#s>t,mqtts#s>m,webs#s>w', tc:{port:8100}, ts:{port:8100}, webs:{ port:8090 }, mqtts:{ topics:['switch/#']}, id:'four-in-one'})
|
2018-01-27 23:20:33 -08:00
|
|
|
|
2018-05-25 14:39:48 -07:00
|
|
|
fio.s = socketfuncs
|
|
|
|
fio.c = clientfuncs
|
2018-01-30 21:12:38 -08:00
|
|
|
|
2018-02-06 18:30:00 -08:00
|
|
|
;
|
|
|
|
(async () => {
|
2018-01-29 21:51:13 -08:00
|
|
|
|
2018-05-20 15:44:31 -07:00
|
|
|
|
2018-05-25 14:39:48 -07:00
|
|
|
let res = await fio.init()
|
|
|
|
console.log('initialize errors',res)
|
|
|
|
console.log('waiting for packets')
|
2018-05-20 15:44:31 -07:00
|
|
|
|
|
|
|
|
|
|
|
let packet = {}
|
|
|
|
console.log('=============sending============')
|
2018-05-25 14:39:48 -07:00
|
|
|
packet = {cmd:'switch/on', id:'1'}
|
|
|
|
console.log('sending to socket uc',packet)
|
|
|
|
await fio.send('uc',packet)
|
2018-02-04 14:18:21 -08:00
|
|
|
|
2018-01-27 23:20:33 -08:00
|
|
|
|
|
|
|
})().catch(err => {
|
|
|
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
2018-01-29 21:51:13 -08:00
|
|
|
process.kill(process.pid, 'SIGTERM')
|
2018-01-27 23:20:33 -08:00
|
|
|
})
|