Skip to content
Snippets Groups Projects

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)

const connect = require("usp-js").default;

const run = async () => {
  // Connect
  const usp = await connect({
    host: "my.ip.here",
    username: "username",
    password: "password",
    port: 90001,
    protocol: "ws",
    fromId: "from::id",
    toId: "to::id",
  });

  // 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
// 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 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 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 (WIP)

    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 }))