update deps, update to new logger location
parent
4a63a53a3d
commit
3788a4c097
|
@ -1,5 +1,5 @@
|
||||||
import Client from '../src/client'
|
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', url:'tcp://trantor:1883', topics:'test1'})
|
||||||
let mqtt = new Client({id:'example-mqtt-client', topics:'lights'})
|
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 () => {
|
(async () => {
|
||||||
|
|
||||||
console.log(await mqtt.connect())
|
console.log(await mqtt.connect())
|
||||||
await mqtt.send({cmd:'test', status:'sent to topic1 implicitly'})
|
await mqtt.send({cmd:'lights/on', status:'sent to topics lights'})
|
||||||
await mqtt.subscribe('topic2')
|
// await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 explicitly but not listening'})
|
||||||
await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 explicitly'})
|
// await mqtt.subscribe('topic2')
|
||||||
await mqtt.unsubscribe('topic2')
|
// await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 with subscription'})
|
||||||
await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 with subscription'})
|
// now can send via some mqtt client
|
||||||
// await mqtt.subscribe('lights')
|
|
||||||
|
|
||||||
|
|
||||||
})().catch(err => {
|
})().catch(err => {
|
||||||
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
||||||
|
|
16
package.json
16
package.json
|
@ -1,14 +1,13 @@
|
||||||
{
|
{
|
||||||
"name": "@uci/mqtt",
|
"name": "@uci/mqtt",
|
||||||
"version": "0.1.2",
|
"version": "0.1.8",
|
||||||
"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": {
|
||||||
"testw": "mocha -r esm test/*.test.mjs --watch --recurse --watch-extensions mjs",
|
"testw": "mocha -r esm test/*.test.mjs --watch --recurse --watch-extensions mjs",
|
||||||
"test": "mocha -r esm test/*.test.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",
|
"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",
|
"author": "David Kebler",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -26,8 +25,8 @@
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/uCOMmandIt/uci-changeme#readme",
|
"homepage": "https://github.com/uCOMmandIt/uci-changeme#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@uci/logger": "0.0.6",
|
"@uci-utils/logger": "0.0.13",
|
||||||
"async-mqtt": "^2.0.0",
|
"async-mqtt": "^2.2.0",
|
||||||
"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",
|
||||||
|
@ -36,11 +35,8 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"chai": "^4.2.0",
|
"chai": "^4.2.0",
|
||||||
"chai-as-promised": "^7.1.1",
|
"esm": "^3.2.5",
|
||||||
"codecov": "^3.1.0",
|
|
||||||
"esm": "^3.0.84",
|
|
||||||
"istanbul": "^0.4.5",
|
|
||||||
"mocha": "^5.2.0",
|
"mocha": "^5.2.0",
|
||||||
"nodemon": "^1.18.6"
|
"nodemon": "^1.18.10"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
22
readme.md
22
readme.md
|
@ -1,21 +1 @@
|
||||||
# uCOMmandIt Template Package Repository
|
# uCOMmandIt MQTT
|
||||||
|
|
||||||
<!-- find and replace the package name to match -->
|
|
||||||
[![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
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import merge from 'lodash.merge'
|
||||||
import btc from 'better-try-catch'
|
import btc from 'better-try-catch'
|
||||||
import union from 'lodash.union'
|
import union from 'lodash.union'
|
||||||
import xor from 'lodash.xor'
|
import xor from 'lodash.xor'
|
||||||
import logger from '@uci/logger'
|
import logger from '@uci-utils/logger'
|
||||||
import isPlainObject from 'is-plain-object'
|
import isPlainObject from 'is-plain-object'
|
||||||
|
|
||||||
let log = {}
|
let log = {}
|
||||||
|
@ -53,14 +53,9 @@ class MQTTClient {
|
||||||
this.opts
|
this.opts
|
||||||
)
|
)
|
||||||
this._extend(mqtt, 'subscribe,unsubscribe') // merge mqtt client into class extend with given functions
|
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
|
let timeout = this.opts.connectTimeout || 5000
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
reject(
|
reject({msg:'ending mqtt connection attempt, no broker', url:this.url, opts:this.opts})
|
||||||
`ending mqtt connection attempt, no broker at ${
|
|
||||||
this._client.options.hostname
|
|
||||||
}:${this._client.options.port}`
|
|
||||||
)
|
|
||||||
}, timeout)
|
}, timeout)
|
||||||
|
|
||||||
this.once('connect', () => {
|
this.once('connect', () => {
|
||||||
|
@ -74,7 +69,6 @@ class MQTTClient {
|
||||||
|
|
||||||
this.on('error', err => {
|
this.on('error', err => {
|
||||||
log.fatal({ err: err }, 'connection error to broker')
|
log.fatal({ err: err }, 'connection error to broker')
|
||||||
console.log('connection error', err.code)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
this.subscribe(this.topics)
|
this.subscribe(this.topics)
|
||||||
|
@ -111,6 +105,10 @@ class MQTTClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
async send(packet, topics, options) {
|
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)) {
|
if (typeof topics !== 'string' && !Array.isArray(topics)) {
|
||||||
options = topics
|
options = topics
|
||||||
topics = this.topics
|
topics = this.topics
|
||||||
|
@ -127,7 +125,6 @@ class MQTTClient {
|
||||||
let pubs = []
|
let pubs = []
|
||||||
topics.forEach(async topic => {
|
topics.forEach(async topic => {
|
||||||
log.info(`sending ${payload} to topic ${topic} with options ${options}`)
|
log.info(`sending ${payload} to topic ${topic} with options ${options}`)
|
||||||
console.log('publish,', this.publish)
|
|
||||||
pubs.push(this.publish(topic, payload, options))
|
pubs.push(this.publish(topic, payload, options))
|
||||||
})
|
})
|
||||||
return await Promise.all(pubs)
|
return await Promise.all(pubs)
|
||||||
|
@ -188,7 +185,7 @@ class MQTTClient {
|
||||||
log = logger({
|
log = logger({
|
||||||
file: 'src/client.js',
|
file: 'src/client.js',
|
||||||
class: 'Client',
|
class: 'Client',
|
||||||
name: 'mqtt',
|
package: '@uci/mqtt',
|
||||||
id: this.id
|
id: this.id
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue