From f5fde15007b5f1114b3bc8ff1ae8d3bfd04f9951 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Sun, 15 Mar 2020 15:56:48 -0700 Subject: [PATCH] 0.3.0 bump for 3-2020 deployment of light code adjust event names to match uci socket module catch no packet for send and push --- package.json | 6 +++--- src/mqtt.js | 15 ++++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 1fb95ae..f463da7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@uci/mqtt", - "version": "0.1.13", + "version": "0.3.0", "description": "mqtt client with UCI json packet payloads", "main": "src", "scripts": { @@ -40,7 +40,7 @@ "chai": "^4.2.0", "delay": "^4.3.0", "esm": "^3.2.25", - "mocha": "^6.2.2", - "nodemon": "^2.0.0" + "mocha": "^7.1.0", + "nodemon": "^2.0.2" } } diff --git a/src/mqtt.js b/src/mqtt.js index b0c45ee..49e22e4 100644 --- a/src/mqtt.js +++ b/src/mqtt.js @@ -42,6 +42,7 @@ class MQTTClient extends EventEmitter { this.mqtt = {} // orginal mqtt.js object when created by connect // self bindings this._connected = false + this.conPackets = opts.conPackets || [opts.conPacket] this.connect = this.connect.bind(this) this.publish = this.publish.bind(this) this._subscribe = this._subscribe.bind(this) @@ -77,14 +78,14 @@ class MQTTClient extends EventEmitter { this._connected=true this._subscribe(this.topics) // resubscribe this.emit('status',{level:'info', msg:'reconnected', id:this.id, opts:this.opts, connected:this._connected}) - this.emit('consumer-connection', {state:'reconnected', id:this.id}) + this.emit('connection:consumer', {state:'connected', id:this.id, mqtt:true}) if (this.opts.conPacket) this.send(this.conPacket) }) this.mqtt.on('error', err => { this.emit('status',{level:'fatal', method:'connect', err: err, msg:'connection error to broker'}) log.fatal({method:'connect', line:76, err: err, msg:'connection error to broker'}) - this.emit('consumer-connection', {state:'disconnected', id:this.id}) + this.emit('connection:consumer', {state:'disconnected', id:this.id, mqtt:true}) this._connected=false }) @@ -106,7 +107,7 @@ class MQTTClient extends EventEmitter { if (err) reject({error:'unable to subscribe to topics after connection', err:err}) else{ this.emit('status',{level:'info', msg:'connected', id:this.id, opts:this.opts, connected:this._connected}) - this.emit('consumer-connection', {state:'connected', id:this.id}) + this.emit('connection:consumer', {state:'connected', id:this.id, mqtt:true}) if (this.opts.conPacket) this.send(this.conPacket) resolve(`mqtt client connected to broker at ${this.mqtt.options.hostname}:${this.mqtt.options.port}`) } @@ -140,6 +141,8 @@ class MQTTClient extends EventEmitter { // if topic not passed then it will send to the topic given by the cmd async _send(ipacket, topic, options) { // packet is required + if (!ipacket || !Object.keys(ipacket).length) return {error:'send aborted, no packet passed'} + if (isPlainObject(topic)) { // if neither then assume none and what is passed is options options = topic || {} topic = '' @@ -184,7 +187,9 @@ class MQTTClient extends EventEmitter { }) // end promise } - async _push(packet={}, topic, options) { + async _push(packet, topic, options) { + + if (!packet || !Object.keys(packet).length) return {error:'push aborted, no packet passed', packet:packet, topic:topic} if (!this._connected) { log.error({method:'send', line:105, url:this.url, opts:this.opts, msg:'no active connection to broker'}) @@ -211,7 +216,7 @@ class MQTTClient extends EventEmitter { // this is internal UCI publish async _publish(packet,topic,options) { if(!topic) return {error: 'no topic to which to publish packet'} - let payload = packet.payload ? (typeof packet.payload ==='string' ? packet.payload : this._serialize(packet)) : this._serialize(packet) // payload is entire packet + let payload = packet.payload!=null ? ( (typeof packet.payload ==='string' ? packet.payload : this._serialize(packet.payload)) ) : this._serialize(packet) // payload is entire packet if (!payload) { log.error({method:'send', line:163, msg:'not able to serilaize packet', packet:packet}) return {error:'not able to serilaize packet', packet:packet}