0.1.25 Update all deps, add home assistant mqtt example
parent
f9503dcbf9
commit
21718d3998
|
@ -0,0 +1,108 @@
|
|||
import Base from '../src/base'
|
||||
|
||||
const BROKER = 'nas.kebler.net'
|
||||
const TOPICS = ['set/testing/+'] // listen for a set command
|
||||
|
||||
const commands = {
|
||||
on: function(packet){
|
||||
return new Promise( async (resolve) => {
|
||||
console.log(`turning switch on for id ${packet.id||packet.data}`)
|
||||
console.log('entire packet',packet)
|
||||
// call switch on here
|
||||
let res = {}
|
||||
res.cmd='status'
|
||||
res.state='on'
|
||||
res.id = packet.id
|
||||
this.push(packet)
|
||||
return resolve(res)
|
||||
})
|
||||
},
|
||||
off: function(packet){
|
||||
return new Promise( async (resolve) => {
|
||||
console.log(packet)
|
||||
console.log(`turning switch off for id ${packet.id||packet.data}`)
|
||||
// call switch on here
|
||||
let res = {}
|
||||
res.cmd='status'
|
||||
res.state='off'
|
||||
res.id = packet.id
|
||||
return resolve(res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let relays = new Base({id:'homeassistant-example'})
|
||||
|
||||
relays.commands = relays.bindFuncs(commands)
|
||||
|
||||
;
|
||||
(async () => {
|
||||
await relays.addSocket('mqs','s','m', {host:BROKER, topics:TOPICS})
|
||||
relays.addNamespace('commands','s')
|
||||
register.call(relays,'mqs')
|
||||
await relays.init()
|
||||
|
||||
})().catch(err => {
|
||||
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
||||
process.kill(process.pid, 'SIGTERM')
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
///***************** HOME ASSISTANT ************************
|
||||
// formats incoming and outgoing packets for HA convention
|
||||
|
||||
const STATUS_TOPIC = 'status/testing'
|
||||
|
||||
function register(name) {
|
||||
|
||||
this.beforeProcessHook(async (packet) => {
|
||||
console.log('incoming mqtt packet to modify')
|
||||
console.dir(packet)
|
||||
let modified = Object.assign({},packet)
|
||||
let cmd = packet.cmd.split('/')
|
||||
modified.cmd = `${packet.data.toLowerCase()}`
|
||||
modified.id = cmd[2]
|
||||
delete modified.data
|
||||
console.log('translated to uci packet')
|
||||
console.dir(modified)
|
||||
return modified
|
||||
// return packet
|
||||
},
|
||||
{name:name})
|
||||
|
||||
this.afterProcessHook(async (packet) => {
|
||||
console.log('processed packet available to modify again', packet)
|
||||
if (packet.error) {
|
||||
let npacket = {}
|
||||
npacket.cmd = 'error'
|
||||
npacket.payload = JSON.stringify(packet)
|
||||
return npacket
|
||||
}
|
||||
if (packet.cmd === 'status') {
|
||||
packet.cmd = `${STATUS_TOPIC}/${packet.id}`
|
||||
packet.payload = packet.state.toUpperCase()
|
||||
console.log('=============modified packet sent to broker================')
|
||||
console.dir(packet)
|
||||
console.log('================')
|
||||
}
|
||||
return packet
|
||||
},
|
||||
{name:name})
|
||||
|
||||
// this.beforeSendHook(async (packet) => {
|
||||
// console.log('beforeSendHook', packet)
|
||||
// if (packet.cmd === 'status') {
|
||||
// let num = Object.keys(packet.pins)[0]
|
||||
// packet.cmd = `${STATUS_TOPIC}/${num}`
|
||||
// packet.payload = packet.pins[num] ? 'ON' : 'OFF'
|
||||
// console.log('=============modified packet sent to broker================')
|
||||
// console.dir(packet)
|
||||
// console.log('================')
|
||||
// }
|
||||
// return packet
|
||||
// },
|
||||
// {name:name})
|
||||
|
||||
}
|
19
package.json
19
package.json
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@uci/base",
|
||||
"version": "0.1.24",
|
||||
"version": "0.1.25",
|
||||
"description": "Multi type and transport JSON packet communication base class. Used in UCI extended classes",
|
||||
"main": "src/base",
|
||||
"scripts": {
|
||||
|
@ -9,6 +9,7 @@
|
|||
"dy": "node -r esm examples/dynamic",
|
||||
"web": "UCI_DEV=true nodemon -r esm examples/web",
|
||||
"mqtt": "nodemon -r esm examples/mqtt",
|
||||
"ha-mqtt": "nodemon -r esm examples/ha-mqtt",
|
||||
"testw": "mocha -r esm test/*.test.mjs --watch --recurse --watch-extensions mjs",
|
||||
"test": "mocha -r esm test/*.test.mjs",
|
||||
"testci": "istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- -R spec --recursive && codecov || true"
|
||||
|
@ -31,16 +32,16 @@
|
|||
"homepage": "https://github.com/uCOMmandIt/message#readme",
|
||||
"devDependencies": {
|
||||
"chai": "^4.2.0",
|
||||
"esm": "^3.0.84",
|
||||
"mocha": "^5.2.0",
|
||||
"nodemon": "^1.18.6"
|
||||
"esm": "^3.2.25",
|
||||
"mocha": "^6.2.0",
|
||||
"nodemon": "^1.19.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@uci-utils/bind-funcs": "^0.2.3",
|
||||
"@uci-utils/logger": "^0.0.14",
|
||||
"@uci/mqtt": "^0.1.11",
|
||||
"@uci/socket": "^0.2.15",
|
||||
"@uci/websocket": "^0.3.7",
|
||||
"@uci-utils/bind-funcs": "^0.2.4",
|
||||
"@uci-utils/logger": "^0.0.15",
|
||||
"@uci/mqtt": "^0.1.12",
|
||||
"@uci/socket": "^0.2.17",
|
||||
"@uci/websocket": "^0.3.8",
|
||||
"await-to-js": "^2.1.1",
|
||||
"p-settle": "^3.1.0"
|
||||
}
|
||||
|
|
|
@ -402,8 +402,9 @@ class Base extends EventEmitter {
|
|||
if (packet.error) return packet // hook invalidated the packet abort further processing
|
||||
let processor = packet._processor || this._processors[socket_name] ? socket_name : '_default'
|
||||
let res = await this._processors[processor].call(this,packet,socket_name)
|
||||
log.debug({ socket:socket_name, response:res, msg:'processed packet ready for hook'})
|
||||
if (this._socket[socket_name].afterProcess) res = await this._socket[socket_name].afterProcess.call(this,res)
|
||||
log.debug({ socket:socket_name, response:res, msg:'processed packet ready for return'})
|
||||
log.debug({ socket:socket_name, response:res, msg:'packet after hook complete ready for return'})
|
||||
return res
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue