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

added options to options :), updated tests, added sendResponse options to operate command

parent ea3a81bb
No related branches found
No related tags found
No related merge requests found
Pipeline #20092 passed
......@@ -3,34 +3,36 @@ import { uniq } from "../util";
const operateSubscriptionPath = "Device.LocalAgent.Subscription.";
const make: MakeFn = (call): OperateRecipe => async (path, opts) => {
const Persistent = opts?.Persistent === undefined ? false : opts.Persistent;
const id = "NOTIFY@" + (opts?.ID || uniq(path));
const operateInput = {
Enable: true,
ID: id,
NotifType: "OperationComplete",
ReferenceList: path,
Persistent,
};
const newSubPath = await call("ADD", {
path: operateSubscriptionPath,
value: operateInput,
});
const make: MakeFn =
(call): OperateRecipe =>
async (path, opts) => {
const Persistent = opts?.Persistent === undefined ? false : opts.Persistent;
const id = "NOTIFY@" + (opts?.ID || uniq(path));
const operateInput = {
Enable: true,
ID: id,
NotifType: "OperationComplete",
ReferenceList: path,
Persistent,
};
const command: OperateFn = (input?: Record<string, any>) =>
call("OPERATE", {
path,
input,
id,
resp: true
const newSubPath = await call("ADD", {
path: operateSubscriptionPath,
value: operateInput,
});
const cleanup: OperateClearFn = () => call("DELETE", { paths: newSubPath });
const command: OperateFn = (input?: Record<string, any>) =>
call("OPERATE", {
path,
input,
id,
resp: opts?.sendResponse || false,
});
return [command, cleanup];
};
const cleanup: OperateClearFn = () => call("DELETE", { paths: newSubPath });
return [command, cleanup];
};
export default {
name: "operate",
......
......@@ -37,7 +37,7 @@ const fixId = (s: string) => s.split("+").join("%2B");
const wait = (ms: number) =>
new Promise((_, reject) => {
setTimeout(() => {
reject();
reject(`timeout of ${ms}ms reached`);
}, ms);
});
......@@ -59,14 +59,17 @@ const wrap = <T extends (...args: any[]) => any>(
});
};
const addOptions = (usp: Partial<USP>, opts: Options): USP =>
Object.entries(usp).reduce(
const addOptions = (usp: Partial<USP>, opts: Options): USP => {
const mainUSP = Object.entries(usp).reduce(
(acc: Partial<USP>, [k, fn]): Partial<USP> => ({
...acc,
[k]: wrap(opts, k as keyof USP, fn as Command),
}),
{} as any
) as USP;
mainUSP.options = wrap(opts, "options", (opts) => addOptions(mainUSP, opts));
return mainUSP;
};
/**
* Connect to device
......
......@@ -368,6 +368,7 @@ export type SubscriptionCallback = (
export interface OperateOptions {
ID?: string;
Persistent?: boolean;
sendResponse?: boolean;
}
export interface PbRequestHeader {
......
......@@ -10,5 +10,12 @@
"url": "ws://192.168.1.1:9001",
"username": "admin",
"password": "admin"
},
"bulut": {
"url": "wss://bulut.iopsys.eu:443",
"fromId": "proto::usp-cloud-controller",
"toId": "os::002207-+105318+2031000004",
"username": "admin",
"password": "admin"
}
}
......@@ -7,7 +7,7 @@ describe("Test general API", () => {
let device: any = null;
before(async () => {
device = await connect(config.ws as ConnectionOptions);
device = await connect(config.bulut as ConnectionOptions);
});
// GET
......@@ -172,7 +172,7 @@ describe("Test general API", () => {
await device
.options({ timeout: 0 })
.get("Device.CAT")
.then((msg) => assert.strictEqual(msg, null))
.then((msg) => assert.strictEqual(msg.startsWith("timeout of"), null))
.catch((err) => assert.strictEqual(typeof err, "object"));
});
......@@ -186,6 +186,17 @@ describe("Test general API", () => {
});
});
it("get with double options setup returns without error", async () => {
await device
.options({ timeout: -1 })
.options({ logMessage: () => null })
.get("Device.DeviceInfo.SerialNumber")
.then((msg) => assert.strictEqual(typeof msg, "string"))
.catch((err) => {
throw err;
});
});
after(async () => {
await device.disconnect();
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment