diff --git a/README.md b/README.md index 99f1a55c1e259e48b07d77e16af3458bc93b6ec1..e8982004dd00d349b2278a92b9356c74244adef0 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,13 @@ Helper library for easy usp communication using mqtt over tcp or ws. [USP Reference](https://usp-data-models.broadband-forum.org/tr-181-2-13-0-usp.html) +# Installation +`npm install usp-js` + # Usage ```javascript -const connect = require("usp-js"); +const connect = require("usp-js").default; const options = { host: "192.168.1.1", @@ -30,3 +33,102 @@ const run = async () => { run(); ``` + +# API + + +- Connect +```javascript +// options are based on https://github.com/mqttjs/MQTT.js#mqttconnecturl-options +const device = await connect(options); +``` + +- Get + - get object - all object end with a dot + ```javascript + await device.get("Device.Time."); + // => { + // "CurrentLocalTime": "2020-12-15T12:33:19Z", + // "Enable": true, + // ... + // } + ``` + + - get property + ```javascript + await device.get("Device.Time.CurrentLocalTime"); // => "2020-12-15T12:33:19Z" + ``` + + - get multiple paths + ```javascript + await device.get(["Device.WiFi.Radio.1.", "Device.WiFi.Radio.2."]); + // => [ + // { ... }, + // { ... } + // ] + ``` + + - get using pattern + ```javascript + await device.get('Device.Ethernet.Interface.[Alias=="WAN"].CurrentBitRate'); // => 0 + ``` + + - resolve references in get + ```javascript + await device.get("Device.WiFi.Radio.1.").then(device.resolve); // => { ... } + // or if deeper resolution is needed (be careful when using level, going above 3 often causes an infinite reference loop) + await device.get("Device.WiFi.Radio.1.").then(msg => device.resolve(msg, 3 /* level - defaults to 1 */)); // => { ... } + ``` + + +- Set + + - set object - does not need to have all attributes, but some may be required (check USP Reference) + ```javascript + await device.set("Device.WiFi.Radio.1.", { Name: "radio-1" }); // => void + ``` + + - set property + ```javascript + await device.set("Device.WiFi.Radio.1.Name", "radio-1"); // => void + ``` + +- Operate - WIP (response message not working yet) + + - operate without no arguments + ```javascript + await device.operate("Device.SelfTestDiagnostics()"); + ``` + + - operate with arguments (for required args check USP Reference) + ```javascript + await device.operate("Device.IP.Diagnostics.IPPing()", { Host: "iopsys.eu" }); + ``` + + +- Add + + - add with no arguments - adds a new default object + <br> + ```javascript + await device.add("Device.NAT.PortMapping."); // => "Device.NAT.PortMapping.3." + ``` + + - add with arguments - adds a new object with provided values + <br> + ```javascript + await device.add("Device.NAT.PortMapping.", { + Description: "webserver1-set", + ExternalPort: "80", + Protocol: "TCP", + Interface: "Device.IP.Interface.1", + Enable: "true", + InternalClient: "192.168.1.125", + InternalPort: "5000", + }); // => "Device.NAT.PortMapping.4." + ``` + +- Delete +```javascript +await device.del("Device.NAT.PortMapping.4."); // => void +``` \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index bbe68dba820a9165aca6af271bda191f8bccf0ab..7d132094e7b4d3021c25aab06e0666192dd7cd40 100644 --- a/src/index.ts +++ b/src/index.ts @@ -80,7 +80,7 @@ const makeSet = (send: SendFn) => (path: string, value: JSType) => path ) as Promise<void>; -const makeAdd = (send: SendFn) => (path: string, input: JSObject) => +const makeAdd = (send: SendFn) => (path: string, input?: JSObject) => handleSend(send("add", { path, input }), path) as Promise<string>; const makeDel = (send: SendFn) => (path: string | string[]) => @@ -175,4 +175,4 @@ const connect = async ( }; }; -export default connect; +export default connect; \ No newline at end of file diff --git a/src/types.d.ts b/src/types.d.ts index 0230998bcb9916b4885d8a8d95534629827cf3f0..260e65500c819effec12a9d804ca74abdc0c6ab7 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -31,7 +31,7 @@ interface Device { get: (path: string | string[]) => Promise<JSType>; set: (path: string, value: JSType) => Promise<void>; operate: (path: string, input?: JSObject) => Promise<JSType>; - add: (path: string, values: JSObject) => Promise<string>; + add: (path: string, values?: JSObject) => Promise<string>; del: (path: string | string[]) => Promise<void>; resolve: (msg: JSType, level: number = 1) => Promise<JSType>;