93 lines
3.7 KiB
JavaScript
93 lines
3.7 KiB
JavaScript
import Base from '../src/base'
|
|
import hahooks from './ha-mqtt'
|
|
|
|
const socketfuncs = {
|
|
switch: {
|
|
status: function(packet){
|
|
return new Promise( async (resolve) => {
|
|
packet.cmd='switch/status'
|
|
packet.state=this.switches[packet.id-1]
|
|
packet.sender= packet.sender || (packet._header ? packet._header.sender.name : 'unknown')
|
|
this.push(packet) // push to all active socket servers
|
|
let res = { response:'status pushed on to all clients'}
|
|
return resolve(res) // this goes back to sender
|
|
})
|
|
},
|
|
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
|
|
})
|
|
},
|
|
off: function(packet){
|
|
return new Promise( async (resolve) => {
|
|
packet.cmd='switch/status'
|
|
packet.state='off'
|
|
this.switches[packet.id-1] = 'off'
|
|
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 off to all clients'}
|
|
return resolve(res) // this goes back to sender
|
|
})
|
|
},
|
|
toggle: function(packet){
|
|
return new Promise( async (resolve) => {
|
|
// 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]
|
|
packet.cmd ='switch/status'
|
|
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 toggled state to all clients'}
|
|
return resolve(res) // this goes back to sender
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// client processing function
|
|
function status(transport) {
|
|
return (packet) => {
|
|
return new Promise( async (resolve) => {
|
|
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('==================')
|
|
return resolve()
|
|
})
|
|
}
|
|
}
|
|
|
|
let fio = new Base({sockets:'uc#c>n,us#s>n,tc#c>t,ts#s>t', tc:{port:8100}, ts:{port:8100}})
|
|
|
|
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')}})
|
|
;
|
|
(async () => {
|
|
|
|
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 })
|
|
let res = await fio.init()
|
|
console.log('initialize errors',res)
|
|
|
|
let packet = {}
|
|
packet = {cmd:'switch/toggle', id:'1'}
|
|
console.log('=====sending to socket uc======',packet)
|
|
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)
|
|
|
|
|
|
})().catch(err => {
|
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
|
process.kill(process.pid, 'SIGTERM')
|
|
})
|