diff --git a/example/example.js b/example/example.js index cb891ff..3a4cd23 100644 --- a/example/example.js +++ b/example/example.js @@ -1,5 +1,5 @@ import Client from '../src/client' -import btc from 'better-try-catch' +// import btc from 'better-try-catch' // let mqtt = new Client({id:'example-mqtt-client', url:'tcp://trantor:1883', topics:'test1'}) let mqtt = new Client({id:'example-mqtt-client', topics:'lights'}) @@ -8,13 +8,11 @@ let mqtt = new Client({id:'example-mqtt-client', topics:'lights'}) (async () => { console.log(await mqtt.connect()) - await mqtt.send({cmd:'test', status:'sent to topic1 implicitly'}) - await mqtt.subscribe('topic2') - await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 explicitly'}) - await mqtt.unsubscribe('topic2') - await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 with subscription'}) - // await mqtt.subscribe('lights') - + await mqtt.send({cmd:'lights/on', status:'sent to topics lights'}) + // await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 explicitly but not listening'}) + // await mqtt.subscribe('topic2') + // await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 with subscription'}) + // now can send via some mqtt client })().catch(err => { console.error('FATAL: UNABLE TO START SYSTEM!\n',err) diff --git a/package.json b/package.json index 41685c3..b6ee871 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,13 @@ { "name": "@uci/mqtt", - "version": "0.1.2", + "version": "0.1.8", "description": "mqtt client with json payloads and mqtt custom broker", "main": "src", "scripts": { "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", "example": "node -r esm example/example", - "dev": "./node_modules/.bin/nodemon -r esm-e mjs example/example" + "dev": "./node_modules/.bin/nodemon -r esm example/example" }, "author": "David Kebler", "license": "MIT", @@ -26,8 +25,8 @@ }, "homepage": "https://github.com/uCOMmandIt/uci-changeme#readme", "dependencies": { - "@uci/logger": "0.0.6", - "async-mqtt": "^2.0.0", + "@uci-utils/logger": "0.0.13", + "async-mqtt": "^2.2.0", "better-try-catch": "^0.6.2", "is-plain-object": "^2.0.4", "lodash.merge": "^4.6.1", @@ -36,11 +35,8 @@ }, "devDependencies": { "chai": "^4.2.0", - "chai-as-promised": "^7.1.1", - "codecov": "^3.1.0", - "esm": "^3.0.84", - "istanbul": "^0.4.5", + "esm": "^3.2.5", "mocha": "^5.2.0", - "nodemon": "^1.18.6" + "nodemon": "^1.18.10" } } diff --git a/readme.md b/readme.md index 83b6323..64e4fc2 100644 --- a/readme.md +++ b/readme.md @@ -1,21 +1 @@ -# uCOMmandIt Template Package Repository - - -[![Build Status](https://img.shields.io/travis/uCOMmandIt/uci-pkg-template.svg?branch=master)](https://travis-ci.org/uCOMmandIt/uci-pkg-template) -[![Inline docs](http://inch-ci.org/github/uCOMmandIt/uci-pkg-template.svg?branch=master)](http://inch-ci.org/github/uCOMmandIt/uci-pkg-template) -[![Dependencies](https://img.shields.io/david/uCOMmandIt/uci-pkg-template.svg)](https://david-dm.org/uCOMmandIt/uci-pkg-template) -[![devDependencies](https://img.shields.io/david/dev/uCOMmandIt/uci-pkg-template.svg)](https://david-dm.org/uCOMmandIt/uci-pkg-template?type=dev) -[![codecov](https://img.shields.io/codecov/c/github/uCOMmandIt/uci-pkg-template/master.svg)](https://codecov.io/gh/uCOMmandIt/uci-pkg-template) - -Clone this to get a quick start on a new uci package. It has all the testing ready to go with Travis-CI and code coverage. All the readme badges are included as well - -Clone it for as a starting place for your own package! - -You'll need codecov and travis-ci accounts - -##Steps - -1. Clone repo -2. Edit package.json -3. Edit badge urls above changing to repo path -4. Start Coding +# uCOMmandIt MQTT diff --git a/src/client.js b/src/client.js index 4ca3c9b..ed0b62d 100644 --- a/src/client.js +++ b/src/client.js @@ -3,7 +3,7 @@ import merge from 'lodash.merge' import btc from 'better-try-catch' import union from 'lodash.union' import xor from 'lodash.xor' -import logger from '@uci/logger' +import logger from '@uci-utils/logger' import isPlainObject from 'is-plain-object' let log = {} @@ -53,14 +53,9 @@ class MQTTClient { this.opts ) this._extend(mqtt, 'subscribe,unsubscribe') // merge mqtt client into class extend with given functions - console.log('publish,', this.publish) let timeout = this.opts.connectTimeout || 5000 setTimeout(() => { - reject( - `ending mqtt connection attempt, no broker at ${ - this._client.options.hostname - }:${this._client.options.port}` - ) + reject({msg:'ending mqtt connection attempt, no broker', url:this.url, opts:this.opts}) }, timeout) this.once('connect', () => { @@ -74,7 +69,6 @@ class MQTTClient { this.on('error', err => { log.fatal({ err: err }, 'connection error to broker') - console.log('connection error', err.code) }) this.subscribe(this.topics) @@ -111,6 +105,10 @@ class MQTTClient { } async send(packet, topics, options) { + if (!this.hasOwnProperty('publish')) { + log.warn({url:this.url, opts:this.opts, msg:'connect method never called, initialization needed, aborting send'}) + return + } if (typeof topics !== 'string' && !Array.isArray(topics)) { options = topics topics = this.topics @@ -127,7 +125,6 @@ class MQTTClient { let pubs = [] topics.forEach(async topic => { log.info(`sending ${payload} to topic ${topic} with options ${options}`) - console.log('publish,', this.publish) pubs.push(this.publish(topic, payload, options)) }) return await Promise.all(pubs) @@ -188,7 +185,7 @@ class MQTTClient { log = logger({ file: 'src/client.js', class: 'Client', - name: 'mqtt', + package: '@uci/mqtt', id: this.id })