mirror of
https://github.com/parnic/node-intellicenter.git
synced 2025-06-17 02:21:53 -05:00
No idea if this will be the best way to represent this stuff long-term, but it's working at the moment. I have some reservations about attempting to list all the possible ResponseParam keys, but I'm already this far in and it would be nice if it worked out...
61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
"use strict";
|
|
|
|
import { WebSocket } from "ws";
|
|
import { FindUnits } from "./finder.js";
|
|
import { GetSystemInfoRequest } from "./messages/system-info.js";
|
|
import { ICResponse } from "./messages/response.js";
|
|
|
|
console.log("searching...");
|
|
const f = new FindUnits();
|
|
const units = await f.searchAsync(1000);
|
|
f.close();
|
|
console.log("Discovered units:", units);
|
|
|
|
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)}`,
|
|
);
|
|
}
|
|
|
|
const endpoint = units[0].addressStr;
|
|
const port = units[0].port;
|
|
|
|
let pingTimeout: ReturnType<typeof setTimeout>;
|
|
|
|
console.log("connecting to intellicenter device at", endpoint, "port", port);
|
|
const client = new WebSocket(`ws://${endpoint}:${port.toString()}`);
|
|
|
|
const heartbeat = () => {
|
|
clearTimeout(pingTimeout);
|
|
|
|
pingTimeout = setTimeout(() => {
|
|
client.terminate();
|
|
}, 30000 + 1000);
|
|
};
|
|
|
|
client.on("error", console.error);
|
|
client.on("open", heartbeat);
|
|
client.on("ping", heartbeat);
|
|
client.on("close", () => {
|
|
clearTimeout(pingTimeout);
|
|
});
|
|
|
|
client.on("message", (msg: Buffer) => {
|
|
const respObj = JSON.parse(msg.toString()) as ICResponse;
|
|
console.log(JSON.stringify(respObj, null, 2));
|
|
});
|
|
await new Promise((resolve, reject) => {
|
|
client.once("error", reject);
|
|
client.once("open", resolve);
|
|
}).then(() => {
|
|
console.log("connected");
|
|
console.log("sending request...");
|
|
const req = GetSystemInfoRequest();
|
|
client.send(JSON.stringify(req));
|
|
client.close();
|
|
});
|