mirror of
https://github.com/parnic/node-intellicenter.git
synced 2025-06-16 18:20:14 -05:00
This can be used to find all the controllable circuits in your system. When invoked with `npm run list-objects` or `node esm/list-objects.js` it will search for units, then request and format+display the objects to the user. There are a few arguments available: * --controllerAddr=1.2.3.4 * Specifies the IntelliCenter controller's address directly which skips the searching phase. * --controllerPort=1234 * Specifies the port to use when connecting to the controller (probably should never use this; does nothing if --controllerAddr is not specified) * --multicastAddr=1.2.3.4 * Specifies the address of the network interface to send the multicast search packet on (useful if you have multiple adapters/interfaces and the system is picking the wrong one; does nothing if --controllerAddr is specified) * --onlyToggleable * Only displays objects which can be toggled on or off Note that if you are invoking this with `npm run list-objects` then the arguments must be specified after an empty `--` so that they are given to the script rather than npm itself. Example: `npm run list-objects -- --controllerAddr=10.0.0.41 --onlyToggleable`
94 lines
2.6 KiB
TypeScript
94 lines
2.6 KiB
TypeScript
import minimist from "minimist";
|
|
import { FindUnits, Unit } from "./index.js";
|
|
import { ICRequest, ICRequestObj } from "./messages/request.js";
|
|
|
|
const argv = minimist(process.argv.slice(2));
|
|
|
|
let endpoint = "";
|
|
let port = 6680;
|
|
if (typeof argv.controllerAddr === "string") {
|
|
endpoint = argv.controllerAddr;
|
|
}
|
|
if (typeof argv.controllerPort === "number") {
|
|
port = argv.controllerPort;
|
|
}
|
|
|
|
const example = async () => {
|
|
if (!endpoint) {
|
|
console.log("searching for units...");
|
|
|
|
let multicastAddr: string | undefined;
|
|
if (typeof argv.multicastAddr === "string") {
|
|
multicastAddr = argv.multicastAddr;
|
|
}
|
|
const finder = new FindUnits(multicastAddr);
|
|
const units = await finder.searchAsync(3000);
|
|
finder.close();
|
|
|
|
if (units.length === 0) {
|
|
throw new Error("no IntelliCenter units found, exiting.");
|
|
}
|
|
|
|
if (units.length > 1) {
|
|
throw new Error(
|
|
`found more than one IntelliCenter unit, unsure which one to use. ${JSON.stringify(units)}`,
|
|
);
|
|
}
|
|
|
|
endpoint = units[0].addressStr;
|
|
port = units[0].port;
|
|
console.log(`found unit at ${endpoint}"${port.toString()}`);
|
|
}
|
|
|
|
if (!endpoint) {
|
|
throw new Error("invalid controller address");
|
|
}
|
|
|
|
console.log(`connecting to unit ${endpoint}:${port.toString()}...`);
|
|
const unit = new Unit(endpoint, port);
|
|
await unit.connect();
|
|
console.log("...connected.");
|
|
|
|
const customRequest = new ICRequest();
|
|
customRequest.command = "GetParamList";
|
|
customRequest.condition = "";
|
|
const reqObj = new ICRequestObj();
|
|
reqObj.objnam = "ALL";
|
|
reqObj.keys = ["OBJTYP", "SUBTYP", "SNAME", "STATUS"];
|
|
customRequest.objectList = [reqObj];
|
|
|
|
console.log("requesting object info...");
|
|
const response = await unit.send(customRequest);
|
|
console.log("...received.");
|
|
console.log();
|
|
|
|
let objects = response.objectList;
|
|
if (argv.onlyToggleable) {
|
|
objects = response.objectList?.filter(
|
|
(obj) => obj.params?.STATUS === "ON" || obj.params?.STATUS === "OFF",
|
|
);
|
|
}
|
|
|
|
if (!objects) {
|
|
console.error("no objects found");
|
|
} else {
|
|
console.log("Found the following objects:");
|
|
console.log(`[OBJNM][Type(/Subtype)] "Friendly Name": CURRENT_STATUS`);
|
|
console.log("---------------------------------------");
|
|
|
|
for (const obj of objects
|
|
.filter((obj) => obj.params?.SNAME !== "SNAME")
|
|
.map(
|
|
(obj) =>
|
|
`[${obj.objnam}][${obj.params?.OBJTYP ?? ""}${obj.params?.SUBTYP !== "SUBTYP" ? `/${obj.params?.SUBTYP ?? ""}` : ""}] "${obj.params?.SNAME ?? ""}": ${obj.params?.STATUS ?? ""}`,
|
|
)) {
|
|
console.log(obj);
|
|
}
|
|
}
|
|
|
|
unit.close();
|
|
};
|
|
example().catch((e: unknown) => {
|
|
throw e;
|
|
});
|