Skip to content
Snippets Groups Projects
Commit 6d172041 authored by Marin Karamihalev's avatar Marin Karamihalev
Browse files

Changed conenction handling in ws and wss cases

parent c6d6d0ae
No related branches found
Tags 0.0.4
No related merge requests found
Pipeline #8758 failed
{
"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": [],
......
......@@ -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) => {
......
{
"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"
}
}
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();
});
});
......@@ -19,5 +19,9 @@
"sourceMap": true,
"declaration": true
},
"compileOnSave": true
"compileOnSave": true,
"typedocOptions": {
"mode": "modules",
"out": "docs"
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment