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

Improved README

parent cf950b8d
No related branches found
Tags 0.0.4
No related merge requests found
Pipeline #8600 failed
......@@ -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
......@@ -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
......@@ -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>;
......
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