2018-01-27 23:20:33 -08:00
|
|
|
import Base from '../src/base'
|
2019-08-27 20:10:44 -07:00
|
|
|
import hahooks from './ha-mqtt'
|
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: {
|
2019-08-27 20:10:44 -07:00
|
|
|
status: 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'
|
2019-08-27 20:10:44 -07:00
|
|
|
packet.state=this.switches[packet.id-1]
|
2019-02-14 14:01:21 -08:00
|
|
|
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
|
|
|
})
|
|
|
|
},
|
2019-08-27 20:10:44 -07:00
|
|
|
on: function(packet){
|
|
|
|
return new Promise( async (resolve) => {
|
|
|
|
packet.cmd='switch/status'
|
|
|
|
packet.state='on'
|
|
|
|
this.switches[packet.id-1] = 'on'
|
|
|
|
packet.sender= packet.sender || (packet._header ? packet._header.sender.name : 'unknown')
|
|
|
|
this.push(packet) // push to all active socket servers
|
|
|
|
let res = { response:'status change - pushed on to all clients', id:packet.id}
|
|
|
|
return resolve(res) // this goes back to sender
|
|
|
|
})
|
|
|
|
},
|
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'
|
2019-08-27 20:10:44 -07:00
|
|
|
packet.state='off'
|
|
|
|
this.switches[packet.id-1] = 'off'
|
2019-02-14 14:01:21 -08:00
|
|
|
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-08-27 20:10:44 -07:00
|
|
|
let res = { response:'status change - 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) => {
|
2019-08-27 20:10:44 -07:00
|
|
|
// console.log('toggle current status of ', packet.current_status || 'unknown, so random set')
|
|
|
|
this.switches[packet.id-1] = this.switches[packet.id-1]==='on' ? 'off' : 'on'
|
|
|
|
packet.state = this.switches[packet.id-1]
|
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-08-27 20:10:44 -07:00
|
|
|
let res = { response:'status change - pushed toggled state 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-01-30 21:12:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 20:10:44 -07:00
|
|
|
// client processing function
|
|
|
|
function status(transport) {
|
2019-02-14 14:01:21 -08:00
|
|
|
return (packet) => {
|
2018-05-25 14:39:48 -07:00
|
|
|
return new Promise( async (resolve) => {
|
2019-08-27 20:10:44 -07:00
|
|
|
console.log('=== a by transport processor only function ====')
|
|
|
|
console.log(`Switch ${packet.id} is ${packet.state}, sent by ${packet.sender || packet.data.sender} to ${transport}`)
|
|
|
|
console.log('==================')
|
2018-05-25 14:39:48 -07:00
|
|
|
return resolve()
|
|
|
|
})
|
2018-02-06 18:30:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 20:10:44 -07:00
|
|
|
let fio = new Base({sockets:'uc#c>n,us#s>n,tc#c>t,ts#s>t', tc:{port:8100}, ts:{port:8100}})
|
2018-01-27 23:20:33 -08:00
|
|
|
|
2019-08-27 20:10:44 -07:00
|
|
|
fio.switches = ['off','off','off','off'] // database to hold switch states
|
|
|
|
fio.actions = socketfuncs
|
|
|
|
fio.addNamespace('actions')
|
|
|
|
fio.amendConsumerCommands({switch: {status:status('tcp only processor function which superceeds the all transports')}},'t')
|
|
|
|
fio.amendConsumerCommands({switch: {status:status('default all transports function')}})
|
2018-02-06 18:30:00 -08:00
|
|
|
;
|
|
|
|
(async () => {
|
2018-01-29 21:51:13 -08:00
|
|
|
|
2019-08-27 20:10:44 -07:00
|
|
|
await fio.addSocket('mqtt','s','m',{ host:'nas.kebler.net',topics:['switch/on','switch/off','switch/toggle']})
|
|
|
|
await fio.addSocket('ha','s','m',{ host:'nas.kebler.net',topics:['fio/switch/set/#']})
|
|
|
|
hahooks.call(fio,'ha')
|
|
|
|
await fio.addSocket('web','s','w',{ port:8090 })
|
2018-05-25 14:39:48 -07:00
|
|
|
let res = await fio.init()
|
|
|
|
console.log('initialize errors',res)
|
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)
|
2019-08-27 20:10:44 -07:00
|
|
|
console.log('this should get pushed to all listening consumers')
|
|
|
|
console.log('direct response back to uc with await: ',(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
|
|
|
})
|