upgrade to esm from @std/esm
parent
5dda6b1e9e
commit
8ac552e9a8
|
@ -1,2 +1,3 @@
|
||||||
/node_modules/
|
/node_modules/
|
||||||
/coverage/
|
/coverage/
|
||||||
|
*.lock
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
import Client from '../src/client'
|
||||||
|
|
||||||
|
// let mqtt = new Client({id:'example-mqtt-client', url:'tcp://trantor:1883', topics:'test1'})
|
||||||
|
let mqtt = new Client({id:'example-mqtt-client', connect:{host:'localhost', port:1883}, topics:'topic1'})
|
||||||
|
|
||||||
|
;
|
||||||
|
(async () => {
|
||||||
|
|
||||||
|
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'})
|
||||||
|
|
||||||
|
|
||||||
|
})().catch(err => {
|
||||||
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
||||||
|
})
|
|
@ -1,19 +0,0 @@
|
||||||
import Client from '../src/client'
|
|
||||||
|
|
||||||
// let mqtt = new Client({id:'example-mqtt-client', url:'tcp://trantor:1883', topics:'test1'})
|
|
||||||
let mqtt = new Client({id:'example-mqtt-client', connect:{host:'localhost', port:1883}, topics:'test1'})
|
|
||||||
|
|
||||||
;
|
|
||||||
(async () => {
|
|
||||||
|
|
||||||
await mqtt.connect()
|
|
||||||
await mqtt.send({cmd:'test', status:'I\'m good'})
|
|
||||||
await mqtt.subscribe('test2',{pub:true})
|
|
||||||
await mqtt.send({cmd:'test', status:'I\'m good'})
|
|
||||||
await mqtt.unsubscribe('test2')
|
|
||||||
await mqtt.send({cmd:'test', status:'I\'m good'})
|
|
||||||
|
|
||||||
|
|
||||||
})().catch(err => {
|
|
||||||
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
|
||||||
})
|
|
19
package.json
19
package.json
|
@ -1,14 +1,14 @@
|
||||||
{
|
{
|
||||||
"name": "@uci/mqtt",
|
"name": "@uci/mqtt",
|
||||||
"version": "0.0.1",
|
"version": "0.0.5",
|
||||||
"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 @std/esm test/*.test.mjs --watch --recurse --watch-extensions mjs",
|
"testw": "mocha -r esm test/*.test.mjs --watch --recurse --watch-extensions mjs",
|
||||||
"test": "mocha -r @std/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",
|
"testci": "istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- -R spec --recursive && codecov || true",
|
||||||
"example": "node -r @std/esm example/example",
|
"example": "node -r esm example/example",
|
||||||
"dev": "./node_modules/.bin/nodemon -r @std/esm -e mjs example/example"
|
"dev": "./node_modules/.bin/nodemon -r esm-e mjs example/example"
|
||||||
},
|
},
|
||||||
"author": "David Kebler",
|
"author": "David Kebler",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -26,7 +26,7 @@
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/uCOMmandIt/uci-changeme#readme",
|
"homepage": "https://github.com/uCOMmandIt/uci-changeme#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@uci/logger": "0.0.1",
|
"@uci/logger": "^0.0.3",
|
||||||
"async-mqtt": "^1.0.1",
|
"async-mqtt": "^1.0.1",
|
||||||
"better-try-catch": "^0.6.2",
|
"better-try-catch": "^0.6.2",
|
||||||
"lodash.isarray": "^4.0.0",
|
"lodash.isarray": "^4.0.0",
|
||||||
|
@ -34,14 +34,13 @@
|
||||||
"lodash.union": "^4.6.0",
|
"lodash.union": "^4.6.0",
|
||||||
"lodash.xor": "^4.5.0"
|
"lodash.xor": "^4.5.0"
|
||||||
},
|
},
|
||||||
"@std/esm": "cjs",
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@std/esm": "^0.18.0",
|
|
||||||
"nodemon": "^1.14.3",
|
|
||||||
"chai": "^4.1.2",
|
"chai": "^4.1.2",
|
||||||
"chai-as-promised": "^7.1.1",
|
"chai-as-promised": "^7.1.1",
|
||||||
"codecov": "^3.0.0",
|
"codecov": "^3.0.0",
|
||||||
|
"esm": "^3.0.21",
|
||||||
"istanbul": "^0.4.5",
|
"istanbul": "^0.4.5",
|
||||||
"mocha": "^4.0.1"
|
"mocha": "^4.0.1",
|
||||||
|
"nodemon": "^1.14.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,14 +7,12 @@ import logger from '@uci/logger'
|
||||||
|
|
||||||
let log = {}
|
let log = {}
|
||||||
|
|
||||||
|
|
||||||
export default class Client {
|
export default class Client {
|
||||||
constructor (opts={}) {
|
constructor (opts={}) {
|
||||||
this.id = opts.id || opts.name || 'mqtt:'+ new Date().getTime()
|
this.id = opts.id || opts.name || 'mqtt:'+ new Date().getTime()
|
||||||
this.url = opts.url
|
this.url = opts.url
|
||||||
// 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 see object see mqtt.js docs
|
||||||
this.topics = Array.isArray(opts.topics) ? opts.topics : (opts.topics ? opts.topics.split(',') : [this.id])
|
this.topics = Array.isArray(opts.topics) ? opts.topics : (opts.topics ? opts.topics.split(',') : [this.id])
|
||||||
console.log(this.topics)
|
|
||||||
this.opts = opts.connect || {} // see options for new mqtt.Client
|
this.opts = opts.connect || {} // see options for new mqtt.Client
|
||||||
// self bindings
|
// self bindings
|
||||||
this.connect = this.connect.bind(this)
|
this.connect = this.connect.bind(this)
|
||||||
|
@ -70,11 +68,12 @@ export default class Client {
|
||||||
|
|
||||||
|
|
||||||
async send(topics,payload,options) {
|
async send(topics,payload,options) {
|
||||||
if (typeof topics !=='string'|| !Array.isArray(topics)) {
|
if (typeof topics !=='string' && !Array.isArray(topics)) {
|
||||||
payload = topics
|
payload = topics
|
||||||
topics = this.topics
|
topics = this.topics
|
||||||
}
|
}
|
||||||
if (typeof topics ==='string') topics = topics.split(',')
|
if (typeof topics ==='string') topics = topics.split(',')
|
||||||
|
// console.log('topics using', topics)
|
||||||
let serial = this._serialize(payload)
|
let serial = this._serialize(payload)
|
||||||
if (serial) {
|
if (serial) {
|
||||||
let pubs = []
|
let pubs = []
|
||||||
|
@ -106,7 +105,7 @@ export default class Client {
|
||||||
this.on('message',messageProcess.bind(this))
|
this.on('message',messageProcess.bind(this))
|
||||||
|
|
||||||
async function messageProcess (topic,payload) {
|
async function messageProcess (topic,payload) {
|
||||||
console.log('incoming messeage on topic', topic)
|
log.info('incoming messeage on topic', topic)
|
||||||
let packet = this._handlePayload(payload)
|
let packet = this._handlePayload(payload)
|
||||||
if (packet) await this._packetProcess (packet,topic)
|
if (packet) await this._packetProcess (packet,topic)
|
||||||
|
|
||||||
|
@ -117,7 +116,7 @@ export default class Client {
|
||||||
let [err,packet] = btc(JSON.parse)(payload.toString())
|
let [err,packet] = btc(JSON.parse)(payload.toString())
|
||||||
if (err) {
|
if (err) {
|
||||||
log.fatal({payload:payload},'Could not parse JSON of payload')
|
log.fatal({payload:payload},'Could not parse JSON of payload')
|
||||||
console.log('Could not parse JSON of payload', payload.toString())
|
// console.log('Could not parse JSON of payload', payload.toString())
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
return packet
|
return packet
|
||||||
|
@ -136,19 +135,7 @@ export default class Client {
|
||||||
|
|
||||||
async _log() {
|
async _log() {
|
||||||
|
|
||||||
const LOG_OPTS = {
|
log = logger({file:'src/client.js',class:'Client',name:'mqtt',id:this.id})
|
||||||
repo:'uci-mqtt',
|
|
||||||
npm:'@uci/mqtt',
|
|
||||||
file:'src/client.mjs',
|
|
||||||
class:'Client',
|
|
||||||
id:this.id,
|
|
||||||
instance_created:new Date().getTime(),
|
|
||||||
mqtt: this._client.options // await mqtt.unsubscribe('test2')
|
|
||||||
// await mqtt.send({cmd:'test', status:'I\'m good'})
|
|
||||||
// console.log('+++++++++++++++')
|
|
||||||
}
|
|
||||||
|
|
||||||
log = logger.child(LOG_OPTS)
|
|
||||||
|
|
||||||
this.on('close', () => {
|
this.on('close', () => {
|
||||||
log.info('connection to broker was closed')
|
log.info('connection to broker was closed')
|
Loading…
Reference in New Issue