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) => {
|
2019-02-14 14:01:21 -08:00
|
|
|
packet.cmd='switch/status'
|
|
|
|
packet.status='on'
|
|
|
|
packet.sender= packet.sender || (packet._header ? packet._header.sender.name : 'unknown')
|
2018-05-25 14:39:48 -07:00
|
|
|
this.push(packet) // push to all active socket servers
|
|
|
|
let res = { response:'status pushed on to all clients'}
|
2019-02-14 14:01:21 -08:00
|
|
|
return resolve(res) // this goes back to sender
|
2018-02-05 22:05:38 -08:00
|
|
|
})
|
|
|
|
},
|
2018-05-25 14:39:48 -07:00
|
|
|
off: function(packet){
|
2018-02-05 22:05:38 -08:00
|
|
|
return new Promise( async (resolve) => {
|
2019-02-14 14:01:21 -08:00
|
|
|
packet.cmd='switch/status'
|
|
|
|
packet.status='off'
|
|
|
|
packet.sender= packet.sender || (packet._header ? packet._header.sender.name : 'unknown')
|
2018-05-25 14:39:48 -07:00
|
|
|
this.push(packet) // push to all active socket servers
|
|
|
|
let res = { response:'status pushed off to all clients'}
|
2019-02-14 14:01:21 -08:00
|
|
|
return resolve(res) // this goes back to sender
|
2018-05-25 14:39:48 -07:00
|
|
|
})
|
|
|
|
},
|
|
|
|
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')
|
2019-02-14 14:01:21 -08:00
|
|
|
packet.cmd ='switch/status'
|
|
|
|
packet.sender= packet.sender || (packet._header ? packet._header.sender.name : 'unknown')
|
2018-05-25 14:39:48 -07:00
|
|
|
this.push(packet) // push to all active socket servers
|
2019-02-14 14:01:21 -08:00
|
|
|
let res = { response:`command ${packet.cmd} with id:${packet.id} pushed to all consumers(clients)`}
|
|
|
|
return resolve(res) // this goes back to sender
|
2018-02-05 22:05:38 -08:00
|
|
|
})
|
|
|
|
}
|
2018-01-30 21:12:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 14:01:21 -08:00
|
|
|
|
|
|
|
function status(state,consumer) {
|
|
|
|
return (packet) => {
|
2018-05-25 14:39:48 -07:00
|
|
|
return new Promise( async (resolve) => {
|
2019-02-14 14:01:21 -08:00
|
|
|
// console.log('complete packet', packet, consumer)
|
|
|
|
console.log(`Switch ${packet.id} is ${packet.status}, sent by ${packet.sender || packet.data.sender} to ${consumer}`)
|
2018-05-25 14:39:48 -07:00
|
|
|
return resolve()
|
|
|
|
})
|
2018-02-06 18:30:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 14:01:21 -08:00
|
|
|
let fio = new Base({sockets:'uc#c>n,us#s>n,tc#c>t,ts#s>t,mqtts#s>m,mqtt#c>m,webs#s>w', tc:{port:8100}, ts:{port:8100}, webs:{ port:8090 }, mqtts:{ topics:['switch/on','switch/off','switch/toggle']}, mqtt:{ topics:['switch/status']}})
|
2018-01-27 23:20:33 -08:00
|
|
|
|
2018-05-25 14:39:48 -07:00
|
|
|
fio.s = socketfuncs
|
2019-02-14 14:01:21 -08:00
|
|
|
fio.c = { reply: () => {return Promise.resolve()} }
|
|
|
|
fio.cn = {switch:{status:status('on','named')}}
|
|
|
|
fio.ct = {switch:{status:status('on','tcp')}}
|
|
|
|
fio.cm = {switch:{status:status('on','mqtt')}}
|
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 = {}
|
2019-02-14 14:01:21 -08:00
|
|
|
packet = {cmd:'switch/toggle', id:'1'}
|
|
|
|
console.log('=====sending to socket uc======',packet)
|
|
|
|
console.log('this should get pushed to all listening')
|
|
|
|
console.log('response back to uc: ',(await fio.send('uc',packet)).response)
|
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
|
|
|
})
|