diff --git a/package.json b/package.json index d5cc96348455d43c5d47087edf4e5e0d98526e69..786ba827068d19aff248bc66670f8d18426430cd 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name": "usp-js", - "version": "0.0.3", + "version": "0.0.4", "description": "Helper library for easy usp communication using mqtt over tcp or ws.", "main": "build/index.js", "scripts": { "dev": "tsc -w --outDir build", "build": "tsc --outDir build", - "test": "npm run build && mocha tests/unit", - "test:device": "npm run build && mocha tests/integration", + "test": "mocha tests/unit", + "test:device": "mocha tests/integration", "prepublish": "npm run build" }, "keywords": [], diff --git a/src/protocol/index.ts b/src/protocol/index.ts index b3d72efa6461d68903fac1dc4054dcfcbb6869dc..5ce8e37f887635b4a6555f47b94ac3765cf9fdc3 100644 --- a/src/protocol/index.ts +++ b/src/protocol/index.ts @@ -139,6 +139,14 @@ const encode = ( } }; +const connect = async (opts: mqttAsync.IClientOptions) => + opts.protocol?.startsWith("ws") + ? mqttAsync.connectAsync( + `${opts.protocol}://${opts.host}:${opts.port}`, + opts + ) + : mqttAsync.connectAsync(opts); + const mqtt = ( opts: mqttAsync.IClientOptions, events: ProtocolEvents @@ -146,7 +154,7 @@ const mqtt = ( let client: any = null; const init = async (): Promise<InitResult> => { await messages.init(); - client = await mqttAsync.connectAsync(opts); + client = await connect(opts); await client.subscribe("/usp/controller/#"); client.on("message", (_topic: string, message: any) => { diff --git a/tests/integration/config.json b/tests/integration/config.json index d2fefdf06a17f7f663c8190010f8fb187d85a543..f7b6d9391b6f5fc188db4da0bd7997ffb9d5872e 100644 --- a/tests/integration/config.json +++ b/tests/integration/config.json @@ -1,5 +1,14 @@ { + "mqtt": { "host": "192.168.1.1", "username": "admin", "password": "admin" -} \ No newline at end of file + }, + "ws": { + "host": "192.168.1.1", + "username": "admin", + "password": "admin", + "port": 9001, + "protocol": "ws" + } +} diff --git a/tests/integration/index.test.js b/tests/integration/index.test.js index edf8d3d5740d9880c0ba37ecfb2c2b1d2e53992c..f965c8df735101270e8e51926a935b159ebfa0f8 100644 --- a/tests/integration/index.test.js +++ b/tests/integration/index.test.js @@ -1,12 +1,12 @@ const assert = require("assert"); const connect = require("../../build").default; -const config = require('./config.json') +const config = require("./config.json"); describe("Test general API", () => { let device = null; before(async () => { - device = await connect(config); + device = await connect(config.mqtt); }); // GET @@ -17,28 +17,35 @@ describe("Test general API", () => { }); it("get multiple paths resolves without error", async () => { - const resp = await device.get(["Device.DeviceInfo.Description", "Device.WiFi."]); + const resp = await device.get([ + "Device.DeviceInfo.Description", + "Device.WiFi.", + ]); assert.strictEqual(Array.isArray(resp), true); }); it("get incorrect path throws an error", async () => { - await device.get("Device.Not.A.Thing").catch(err => assert.strictEqual(err.type, "error")) + await device + .get("Device.Not.A.Thing") + .catch((err) => assert.strictEqual(err.type, "error")); }); // SET it("set with value", async () => { - const alias = await device.get("Device.WiFi.Radio.1.Alias") + const alias = await device.get("Device.WiFi.Radio.1.Alias"); await device.set("Device.WiFi.Radio.1.Alias", alias); }); it("set with object", async () => { - const Alias = await device.get("Device.WiFi.Radio.1.Alias") + const Alias = await device.get("Device.WiFi.Radio.1.Alias"); await device.set("Device.WiFi.Radio.1.", { Alias }); }); it("set throws an error on incorrect path", async () => { - await device.set("Device.Not.A.Path", { Cat: "cute" }).catch(err => assert.strictEqual(err.type, "error")); + await device + .set("Device.Not.A.Path", { Cat: "cute" }) + .catch((err) => assert.strictEqual(err.type, "error")); }); // OPERATE - WIP @@ -58,26 +65,44 @@ describe("Test general API", () => { Interface: "Device.IP.Interface.1", Enable: "true", InternalClient: "192.168.1.125", - InternalPort: "5000" + InternalPort: "5000", }); - const { Description } = await device.get(newPath) + const { Description } = await device.get(newPath); await device.del(newPath); - assert.strictEqual(Description, "webserver1-set") + assert.strictEqual(Description, "webserver1-set"); }); // RESOLVE it("resolves references in get call", async () => { - const msg = await device.get("Device.Bridging.Bridge.1.Port.1.").then(device.resolve); - assert.strictEqual(typeof msg.LowerLayers, "object") + const msg = await device + .get("Device.Bridging.Bridge.1.Port.1.") + .then(device.resolve); + assert.strictEqual(typeof msg.LowerLayers, "object"); }); it("resolves string reference in get call", async () => { - const msg = await device.get("Device.Bridging.Bridge.1.Port.1.LowerLayers").then(device.resolve); - assert.strictEqual(Array.isArray(msg), true) + const msg = await device + .get("Device.Bridging.Bridge.1.Port.1.LowerLayers") + .then(device.resolve); + assert.strictEqual(Array.isArray(msg), true); }); after(async () => { await device.disconnect(); }); }); + +describe("Test specific connection types", () => { + let usp = null; + + it("Connect over ws", async () => { + usp = await connect(config.ws); + const desc = await usp.get("Device.DeviceInfo.Description"); + assert.strictEqual(typeof desc, "string"); + }); + + after(async () => { + await usp.disconnect(); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 8ab52cc8cd69694fb1b5148825a14c5ea19771b6..aee4b7ac3293b2d62836b0905ad7fb2f5d8e89e6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,5 +19,9 @@ "sourceMap": true, "declaration": true }, - "compileOnSave": true + "compileOnSave": true, + "typedocOptions": { + "mode": "modules", + "out": "docs" + } }