handle array of topcis

master
David Kebler 2018-04-05 15:50:39 -07:00
parent c8071ed66e
commit 5dda6b1e9e
4 changed files with 40 additions and 42 deletions

View File

@ -2,7 +2,7 @@
"name": "@uci/mqtt", "name": "@uci/mqtt",
"version": "0.0.1", "version": "0.0.1",
"description": "mqtt client with json payloads and mqtt custom broker", "description": "mqtt client with json payloads and mqtt custom broker",
"main": "src/index.js", "main": "src",
"scripts": { "scripts": {
"testw": "mocha -r @std/esm test/*.test.mjs --watch --recurse --watch-extensions mjs", "testw": "mocha -r @std/esm test/*.test.mjs --watch --recurse --watch-extensions mjs",
"test": "mocha -r @std/esm test/*.test.mjs", "test": "mocha -r @std/esm test/*.test.mjs",

View File

@ -1,7 +1,6 @@
import { connect } from 'async-mqtt' import { connect } from 'async-mqtt'
import merge from 'lodash.merge' import merge from 'lodash.merge'
import btc from 'better-try-catch' import btc from 'better-try-catch'
import isArray from 'lodash.isarray'
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/logger'
@ -14,7 +13,8 @@ export default class Client {
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 = isArray(opts.topics) ? opts.topic : (opts.topics.split(',') || ['default']) 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,7 +70,7 @@ export default class Client {
async send(topics,payload,options) { async send(topics,payload,options) {
if (typeof topics !=='string'|| !isArray(topics)) { if (typeof topics !=='string'|| !Array.isArray(topics)) {
payload = topics payload = topics
topics = this.topics topics = this.topics
} }
@ -106,7 +106,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) console.log('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)

View File

@ -1,51 +1,49 @@
'use strict' import aClass from '../src'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
const chai.use(chaiAsPromised)
Changeme = require('../src/changeme'), const expect = chai.expect
expect = require('chai').expect,
pause = require('@uci/utils').pPause
describe( describe(
`Testing `, 'Testing ',
function () { function () {
hooks() hooks()
sometests() sometests()
someothertests() someothertests()
}) })
//****************** TESTS ********************** //****************** TESTS **********************
function sometests() { function sometests() {
it('==> test something', async function () { it('==> test something', async function () {
let result = await someasyncfunction() let result = await someasyncfunction()
expect(result, `test failed`).to.equal('expectedresult') expect(result, 'test failed').to.equal('expectedresult')
await pause(1000)
}) })
} }
function someothertests() { function someothertests() {
it('==> test something', async function () { it('==> test something', async function () {
let result = await someasyncfunction() let result = await someasyncfunction()
expect(result, `test failed`).to.equal('expectedresult') expect(result, 'test failed').to.equal('expectedresult')
await pause(1000)
}) })
} }
function hooks() { function hooks() {
before(async() => { before(async() => {
await someasyncfunctiontodobefore() await someasyncfunctiontodobefore()
}) })
beforeEach(async() => { beforeEach(async() => {
await someasyncfunctiontodobeforeeachtest() await someasyncfunctiontodobeforeeachtest()
}) })
after(async() => { after(async() => {
await someasyncfunctiontodoaftereeachtest() await someasyncfunctiontodoaftereeachtest()
}) })
} }

View File

@ -1,9 +1,9 @@
'use strict' import changeme from '../src/changeme'
import path from 'path'
const chai = require('chai'), import chai from 'chai'
chaiAsPromised = require("chai-as-promised"), import chaiAsPromised from 'chai-as-promised'
lib = require('../')
const { assert } = chai;
chai.use(chaiAsPromised); chai.use(chaiAsPromised);
const expect = chai.expect const expect = chai.expect
@ -12,8 +12,8 @@ describe('Promise Stuff - ', function () {
it('Can test a promise', function () { it('Can test a promise', function () {
return expect(lib.apromise).to.eventually.equal('some promise hey') return expect(changeme.apromise).to.eventually.equal('some promise hey')
return Promise.resolve().then(() => expect(lib.apromise).to.eventually.equal('some promise hey')) return Promise.resolve().then(() => expect(changeme.apromise).to.eventually.equal('some promise hey'))
}) })
}) })