usp-js
Helper library for easy usp communication using mqtt over tcp or ws.
Installation
npm install usp-js
Usage
To connect provide necessary info to the default export. (Values will differ depending on setup). Note: for browser usage make sure to import from usp-js/web.
const connect = require("usp-js").default;
const run = async () => {
// Connect
const usp = await connect({
host: "my.ip.here",
username: "username",
password: "password",
port: 9001,
protocol: "ws",
});
// Get property
await usp.get("Device.WiFi.").then(console.log);
// Disconnect
await usp.disconnect();
};
run();
API
- Connect
// options are based on https://github.com/mqttjs/MQTT.js#mqttconnecturl-options
const usp = await connect(options);
Additional connection options:
- fromId: Source ID
- toId: Destination ID
- closeOnDisconnect: Close connection if disconnected
- reconnectsBeforeClosing: Number of times to attempt reconnecting before closing connection
- connectTimeout: Timeout in milliseconds for connection
-
Get
- get object
await usp.get("Device.Time."); // => { // "CurrentLocalTime": "2020-12-15T12:33:19Z", // "Enable": true, // ... // }
- get property
await usp.get("Device.Time.CurrentLocalTime"); // => "2020-12-15T12:33:19Z"
- get multiple paths
await usp.get(["Device.WiFi.Radio.1.", "Device.WiFi.Radio.2."]); // => [ // { ... }, // { ... } // ]
- get using pattern
await usp.get('Device.Ethernet.Interface.[Alias=="WAN"].CurrentBitRate'); // => 0
- resolve references in get
await usp.get("Device.WiFi.Radio.1.").then(usp.resolve); // => { ... } // or if deeper resolution is needed (be careful when using level, going above 3 often causes an infinite reference loop) await usp .get("Device.WiFi.Radio.1.") .then((msg) => usp.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)
await usp.set("Device.WiFi.Radio.1.", { Name: "radio-1" }); // => void
- set object with allowPartial and required attributes
await usp.set("Device.WiFi.Radio.1.", { Name: { required: true, value: "radio-1" }, allowPartial: true, }); // => void
- set property
await usp.set("Device.WiFi.Radio.1.Name", "radio-1"); // => void
-
Operate
const [ping, cleanPing] = await usp.operate("Device.IP.Diagnostics.IPPing()"); const results = await ping({ Host: "iopsys.eu" }); await cleanPing(); // clears ping subscription (optional)
-
Add
- add with no arguments - adds a new default object
await usp.add("Device.NAT.PortMapping."); // => "Device.NAT.PortMapping.3."
- add with multiple responses
await usp.add("Device.IP.Interface.*.IPv4Address."); // => ['Device.IP.Interface.1.IPv4Address.2.', ... ]
- add with arguments - adds a new object with provided values
await usp.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."
- add with with allowPartial and required attributes
await usp.add("Device.NAT.PortMapping.", { allowPartial: true, Description: { required: true, value: "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
await usp.del("Device.NAT.PortMapping.4."); // => void
- Get Supported DM
await usp.supportedDM("Device.WiFi.");
- Get Supported Protocols
await usp.supportedProto("Device.WiFi.");
- Get Instances
await usp.instances("Device.WiFi.");
- Subscribe
const clearSub = await usp.subscribe(
{ id: "1234", notif: "ObjectCreation", reference: "Device.NAT.PortMapping." },
console.log
);
- optional second argument to callback gives access to full message.
await usp.subscribe(
{ id: "1234", notif: "ObjectCreation", reference: "Device.NAT.PortMapping." },
(_, fullMsg) => console.log(fullMsg)
);
-
On
Id can be a string or a regexp. Messages, generally, have their id in the form COMMAND@random_string (i.e. NOTIFY@12345). (Note: does not add subscription to USP model, instead allows for internal monitoring of received messages) Optional second argument to callback gives access to full message.
const clear = usp.on(/NOTIFY.*/, (data, msg) => console.log({ data, msg }));
-
Options
Options allows extending commands with additional functionality.
Props: timeout -> timeout after given ms (throws error) preCall -> called before each command, provides command name and arguments postCall -> called after each command, provides command name, arguments and result/error
await usp
.options({
timeout: 1000,
preCall: (name, args) => console.log(name, args),
postCall: (name, args, result) => console.log(name, args, result),
})
.get("Device.DeviceInfo.SerialNumber");