Helper library for easy usp communication using mqtt over tcp or ws.
npm install usp-js
const connect = require("usp-js").default
const run = async () => {
// Connect
const usp = await connect({
host: "192.168.1.1",
username: "admin",
password: "admin"
});
// Get property
await usp.get("Device.WiFi.").then(console.log);
// Disconnect
await usp.disconnect();
};
run();
Connect
// options are based on https://github.com/mqttjs/MQTT.js#mqttconnecturl-options
// they additionaly require fromId and toId, more info: url.here
const usp = await connect(options);
Get
get object - all object end with a dot
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 property
await usp.set("Device.WiFi.Radio.1.Name", "radio-1"); // => void
Operate - WIP (response message not working yet)
operate without no arguments
await usp.operate("Device.SelfTestDiagnostics()");
operate with arguments (for required args check USP Reference)
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 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."
Delete
await usp.del("Device.NAT.PortMapping.4."); // => void
Generated using TypeDoc