0.1.10 on send improved handle of creating payload. clean up logging, update deps

master
David Kebler 2019-04-26 10:41:46 -07:00
parent 4031ec9e02
commit 6eab513dc4
2 changed files with 35 additions and 41 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@uci/mqtt", "name": "@uci/mqtt",
"version": "0.1.9", "version": "0.1.10",
"description": "mqtt client with json payloads and mqtt custom broker", "description": "mqtt client with json payloads and mqtt custom broker",
"main": "src", "main": "src",
"scripts": { "scripts": {
@ -25,9 +25,9 @@
}, },
"homepage": "https://github.com/uCOMmandIt/uci-changeme#readme", "homepage": "https://github.com/uCOMmandIt/uci-changeme#readme",
"dependencies": { "dependencies": {
"@uci-utils/logger": "0.0.13", "@uci-utils/logger": "^0.0.14",
"async-mqtt": "^1.0", "async-mqtt": "1.0.1",
"better-try-catch": "^0.6.2", "better-try-catch": "0.6.2",
"is-plain-object": "^2.0.4", "is-plain-object": "^2.0.4",
"lodash.merge": "^4.6.1", "lodash.merge": "^4.6.1",
"lodash.union": "^4.6.0", "lodash.union": "^4.6.0",

View File

@ -25,12 +25,13 @@ class MQTTClient {
this.id = opts.id || opts.name || 'mqtt:' + new Date().getTime() this.id = opts.id || opts.name || 'mqtt:' + new Date().getTime()
log = logger({ log = logger({
file: 'src/client.js', file: 'src/client.js',
class: 'Client', package:'@uci/mqttjs',
class: 'MQTTClient',
name: 'mqtt', name: 'mqtt',
id: this.id id: this.id
}) })
this.url = opts.url || null this.url = opts.url || null
// subscription topics can be string of commna delimited or array of strings see object see mqtt.js docs // subscription topics can be string of commna delimited or array of strings. If none they by default topic is id
this.topics = Array.isArray(opts.topics) this.topics = Array.isArray(opts.topics)
? opts.topics ? opts.topics
: opts.topics : opts.topics
@ -67,26 +68,17 @@ class MQTTClient {
this._listen() this._listen()
this.on('reconnect', () => { this.on('reconnect', () => {
log.info('mqtt client reconnected to broker') log.debug({method:'connect', line:71, msg:'mqtt client reconnected to broker'})
this.subscribe(this.topics) this.subscribe(this.topics)
}) })
this.on('error', err => { this.on('error', err => {
log.fatal({ err: err }, 'connection error to broker') log.fatal({method:'connect', line:76, err: err, msg:'connection error to broker'})
}) })
this.subscribe(this.topics) this.subscribe(this.topics)
log.info( log.debug({method:'connect', line:80, options: this._client.options, msg:`mqtt client connected to broker at ${this._client.options.hostname}:${this._client.options.port}`})
{ options: this._client.options }, resolve(`mqtt client connected to broker at ${this._client.options.hostname}:${this._client.options.port}`)
`mqtt client connected to broker at ${
this._client.options.hostname
}:${this._client.options.port}`
)
resolve(
`mqtt client connected to broker at ${
this._client.options.hostname
}:${this._client.options.port}`
)
}) })
}) //end promise }) //end promise
} }
@ -96,7 +88,7 @@ class MQTTClient {
if (typeof topic === 'string') topic = topic.split(',') if (typeof topic === 'string') topic = topic.split(',')
this.topics = union(this.topics, topic) this.topics = union(this.topics, topic)
} }
log.info(`subscription for topic ${topic} added`) log.debug({method:'subscribe', line:91, msg:`subscription for topic ${topic} added`})
this._subscribe(topic, options) this._subscribe(topic, options)
} }
@ -110,7 +102,7 @@ class MQTTClient {
async send(packet, topics, options) { async send(packet, topics, options) {
if (!this.hasOwnProperty('publish')) { if (!this.hasOwnProperty('publish')) {
log.warn({url:this.url, opts:this.opts, msg:'connect method never called, initialization needed, aborting send'}) log.error({method:'send', line:105, url:this.url, opts:this.opts, msg:'connect method never called, initialization needed, aborting send'})
return return
} }
if (typeof topics !== 'string' && !Array.isArray(topics)) { if (typeof topics !== 'string' && !Array.isArray(topics)) {
@ -119,16 +111,20 @@ class MQTTClient {
} else { } else {
if (typeof topics === 'string') topics = topics.split(',') if (typeof topics === 'string') topics = topics.split(',')
} }
let payload = {}
let payload
if (typeof packet !== 'string') { if (typeof packet !== 'string') {
if (packet.payload) payload = packet.payload.toString()
else {
payload = this._serialize(packet) payload = this._serialize(packet)
if (!payload) if (!payload) payload = this._serialize({ err: 'could not serialze packet' })
payload = this._serialize({ err: 'could not serialze packet' }) }
topics = [packet.cmd] || topics // if payload has a cmd use that as topic topics = packet.cmd ? [packet.cmd.replace(/\./gi,'/') ] : topics // if payload has a cmd use that as topic, convert any . to slash
} else payload = this._serialize({ data: packet }) } else payload = packet // packet was just a string
let pubs = [] let pubs = []
topics.forEach(async topic => { topics.forEach(async topic => {
log.info(`sending ${payload} to topic ${topic} with options ${options}`) log.debug({method:'send', line:127, msg:'mqtt publishing', payload:payload, topic:topic})
pubs.push(this.publish(topic, payload, options)) pubs.push(this.publish(topic, payload, options))
}) })
return await Promise.all(pubs) return await Promise.all(pubs)
@ -141,35 +137,33 @@ class MQTTClient {
_serialize(json) { _serialize(json) {
let [err, payload] = btc(JSON.stringify)(json) let [err, payload] = btc(JSON.stringify)(json)
if (err) { if (err) {
log.warn(`error unable to stringify json:${json}`) log.warn({method:'_serialize', line:140, msg:`unable to stringify json for payload:${json}`})
return null return null
} }
return payload return payload
} }
_listen() { _listen() {
log.info( log.info({method:'_listen', line:147, msg:`listening for incoming mqtt packets from broker on topics ${this.topics}`})
`listening for incoming packets from broker on topics ${this.topics}`
)
this.on('message', messageProcess.bind(this)) this.on('message', messageProcess.bind(this))
async function messageProcess(topic, payload) { async function messageProcess(topic, payload) {
log.info('incoming message on topic', topic) log.debug({method:'_listen', line:153 ,msg:'incoming mqtt message to process', topic:topic, payload:payload})
let packet = this._handlePayload(payload) let packet = this._handlePayload(payload)
if (packet) packet.cmd = packet.cmd || topic if (packet) packet.cmd = packet.cmd || topic
// payload had no command that use topic as cmd // payload had no command that use topic as cmd
else packet = { cmd: topic } else packet = { cmd: topic }
let res = (await this._packetProcess(packet, topic)) || {} let res = (await this._packetProcess(packet, topic)) || {}
log.debug({method:'_listen', line:159, msg:'processed packet back to broker - cmd: should NOT be a subscribed topic or infinite loop!', res:res, topics:this.topics})
// TODO!!!!!!!!!! make sure cmd/topic is not the same as subscribed topics or can make an infinite loop
this.send(res) this.send(res)
} }
} }
_handlePayload(payload) { _handlePayload(payload) {
let [err, packet] = btc(JSON.parse)(payload.toString()) let [err, packet] = btc(JSON.parse)(payload.toString())
if (err) { if (err) log.debug({method:'_handlePayload', line:201, msg:'payload is not json returning as prop data:<payload>'})
log.info('payload not json returning as prop data:<payload>') if (!isPlainObject(packet)) return { data: payload.toString() }
}
if (!isPlainObject(packet)) packet = { data: payload.toString() }
return packet return packet
} }
@ -194,19 +188,19 @@ class MQTTClient {
}) })
this.on('close', () => { this.on('close', () => {
log.info('connection to broker was closed') log.debug({method:'_log', line:193, msg:'connection to broker was closed'})
}) })
this.on('offline', () => { this.on('offline', () => {
log.info('this client has gone offline from broker') log.warn({method:'_log', line:197, msg:'this client has gone offline from broker'})
}) })
this.on('packetsend', packet => { this.on('packetsend', packet => {
log.info({ packet: packet }, 'outgoing packet to mqtt broker') log.debug({method:'_log', line:201, packet: packet, msg:'outgoing packet to mqtt broker'})
}) })
this.on('packetreceive', packet => { this.on('packetreceive', packet => {
log.info({ packet: packet }, 'incoming packet from mqtt broker') log.debug({method:'_log', line:205, packet: packet, msg:'incoming packet from mqtt broker'})
}) })
} }