-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-objects.ts
93 lines (78 loc) · 2.61 KB
/
list-objects.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import minimist from "minimist";
import { FindUnits, Unit } from "./index.js";
import { GetRequest, 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 = GetRequest();
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;
});