Add documentation

This commit is contained in:
2025-01-04 12:27:44 -06:00
parent ab4898c20b
commit 25acdd9481
8 changed files with 118 additions and 5 deletions

16
dist/finder.d.ts vendored
View File

@ -1,4 +1,7 @@
import { EventEmitter } from "events";
/**
* Contains connection information for an IntelliCenter controller.
*/
export declare class UnitInfo {
name: string;
hostname: string;
@ -7,6 +10,15 @@ export declare class UnitInfo {
get addressStr(): string;
constructor(_name: string, _hostname: string, _port: number, _address: number);
}
/**
* Broadcasts mDNS packets to the local network to identify any Pentair IntelliCenter controllers connected to it.
*
* Available events:
*
* * `"close"` - fired when the search socket has closed
* * `"error"` - fired when an unrecoverable error has occurred in the search socket
* * `"serverFound"` - fired immediately when an IntelliCenter unit has been located; receives a {@linkcode UnitInfo} argument
*/
export declare class FindUnits extends EventEmitter {
constructor();
private finder;
@ -15,12 +27,14 @@ export declare class FindUnits extends EventEmitter {
private units;
/**
* Begins a search and returns immediately. Must close the finder with close() when done with all searches.
* Subscribe to the `"serverFound"` event to receive connected unit information.
*/
search(): void;
/**
* Searches for the given amount of time. Must close the finder with close() when done with all searches.
*
* @param searchTimeMs the number of milliseconds to search before giving up and returning found results (default: 5000)
* @returns Promise resolving to a list of discovered units, if any.
* @returns Promise resolving to a list of discovered {@linkcode UnitInfo}, if any.
*/
searchAsync(searchTimeMs?: number): Promise<UnitInfo[]>;
private foundServer;

16
dist/finder.js vendored
View File

@ -3,6 +3,9 @@ import { EventEmitter } from "events";
import debug from "debug";
import { ARecord, GetDNSAnswer, GetDNSQuestion, ipToString, PtrRecord, SrvRecord, TypePtr, } from "./dns.js";
const debugFind = debug("ic:find");
/**
* Contains connection information for an IntelliCenter controller.
*/
export class UnitInfo {
name;
hostname;
@ -18,6 +21,15 @@ export class UnitInfo {
this.address = _address;
}
}
/**
* Broadcasts mDNS packets to the local network to identify any Pentair IntelliCenter controllers connected to it.
*
* Available events:
*
* * `"close"` - fired when the search socket has closed
* * `"error"` - fired when an unrecoverable error has occurred in the search socket
* * `"serverFound"` - fired immediately when an IntelliCenter unit has been located; receives a {@linkcode UnitInfo} argument
*/
export class FindUnits extends EventEmitter {
constructor() {
super();
@ -67,6 +79,7 @@ export class FindUnits extends EventEmitter {
units = [];
/**
* Begins a search and returns immediately. Must close the finder with close() when done with all searches.
* Subscribe to the `"serverFound"` event to receive connected unit information.
*/
search() {
if (!this.bound) {
@ -78,8 +91,9 @@ export class FindUnits extends EventEmitter {
}
/**
* Searches for the given amount of time. Must close the finder with close() when done with all searches.
*
* @param searchTimeMs the number of milliseconds to search before giving up and returning found results (default: 5000)
* @returns Promise resolving to a list of discovered units, if any.
* @returns Promise resolving to a list of discovered {@linkcode UnitInfo}, if any.
*/
async searchAsync(searchTimeMs) {
const p = new Promise((resolve) => {

2
dist/finder.js.map vendored

File diff suppressed because one or more lines are too long

22
dist/unit.d.ts vendored
View File

@ -1,6 +1,16 @@
import { EventEmitter } from "events";
import { ICRequest } from "./messages/request.js";
import { ICResponse } from "./messages/response.js";
/**
* Contains methods to connect to and communicate with an IntelliCenter controller.
*
* Call `connect` to connect to the unit.
*
* Available events:
*
* * `"response-{messageID}"` - fired once per message sent with `send()` where {messageID} is the ID specified in the {@linkcode ICRequest} given to `send()`
* * `"notify"` - fired when an update is available to a property previously subscribed to via a {@linkcode SubscribeToUpdates} request
*/
export declare class Unit extends EventEmitter {
endpoint: string;
port: number;
@ -9,10 +19,22 @@ export declare class Unit extends EventEmitter {
private pingTimer?;
private pingInterval;
constructor(endpoint: string, port?: number);
/**
* Connects to the specified unit and maintains a connection to it until `close()` is called.
*/
connect(): Promise<void>;
/**
* Closes the connection to the unit.
*/
close(): void;
private socketCleanup;
private heartbeat;
private onClientMessage;
/**
* Sends a request to the unit.
*
* @param request an message from {@linkcode messages} to send to the unit.
* @returns a promise that resolves into the {@linkcode ICResponse} with information about the request.
*/
send(request: ICRequest): Promise<ICResponse>;
}

22
dist/unit.js vendored
View File

@ -2,6 +2,16 @@ import { EventEmitter } from "events";
import { WebSocket } from "ws";
import debug from "debug";
const debugUnit = debug("ic:unit");
/**
* Contains methods to connect to and communicate with an IntelliCenter controller.
*
* Call `connect` to connect to the unit.
*
* Available events:
*
* * `"response-{messageID}"` - fired once per message sent with `send()` where {messageID} is the ID specified in the {@linkcode ICRequest} given to `send()`
* * `"notify"` - fired when an update is available to a property previously subscribed to via a {@linkcode SubscribeToUpdates} request
*/
export class Unit extends EventEmitter {
endpoint;
port;
@ -16,6 +26,9 @@ export class Unit extends EventEmitter {
this.endpoint = endpoint;
this.port = port;
}
/**
* Connects to the specified unit and maintains a connection to it until `close()` is called.
*/
async connect() {
if (this.client) {
throw new Error("can't open a client that is already open");
@ -43,6 +56,9 @@ export class Unit extends EventEmitter {
});
debugUnit("connected");
}
/**
* Closes the connection to the unit.
*/
close() {
debugUnit("closing connection by request");
this.client?.close();
@ -78,6 +94,12 @@ export class Unit extends EventEmitter {
}
this.emit(`response-${respObj.messageID}`, respObj);
};
/**
* Sends a request to the unit.
*
* @param request an message from {@linkcode messages} to send to the unit.
* @returns a promise that resolves into the {@linkcode ICResponse} with information about the request.
*/
async send(request) {
const payload = JSON.stringify(request);
debugUnit("sending message of length %d with id %s", payload.length, request.messageID);

2
dist/unit.js.map vendored
View File

@ -1 +1 @@
{"version":3,"file":"unit.js","sourceRoot":"","sources":["../unit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAC/B,OAAO,KAAK,MAAM,OAAO,CAAC;AAK1B,MAAM,SAAS,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC;AAEnC,MAAM,OAAO,IAAK,SAAQ,YAAY;IAO3B;IACA;IAPD,MAAM,CAAa;IACnB,WAAW,CAAiC;IAC5C,SAAS,CAAkC;IAC3C,YAAY,GAAG,KAAK,CAAC;IAE7B,YACS,QAAgB,EAChB,OAAO,IAAI;QAElB,KAAK,EAAE,CAAC;QAHD,aAAQ,GAAR,QAAQ,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AAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAC3B,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YACnC,IAAI,CAAC,IAAI,CAAC,YAAY,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,IAAgB,EAAE,EAAE;gBAC9D,SAAS,CAAC,oCAAoC,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC;gBACnE,OAAO,CAAC,IAAI,CAAC,CAAC;YAChB,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}

View File

@ -15,6 +15,9 @@ import {
const debugFind = debug("ic:find");
/**
* Contains connection information for an IntelliCenter controller.
*/
export class UnitInfo {
public name: string;
public hostname: string;
@ -38,6 +41,15 @@ export class UnitInfo {
}
}
/**
* Broadcasts mDNS packets to the local network to identify any Pentair IntelliCenter controllers connected to it.
*
* Available events:
*
* * `"close"` - fired when the search socket has closed
* * `"error"` - fired when an unrecoverable error has occurred in the search socket
* * `"serverFound"` - fired immediately when an IntelliCenter unit has been located; receives a {@linkcode UnitInfo} argument
*/
export class FindUnits extends EventEmitter {
constructor() {
super();
@ -92,6 +104,7 @@ export class FindUnits extends EventEmitter {
/**
* Begins a search and returns immediately. Must close the finder with close() when done with all searches.
* Subscribe to the `"serverFound"` event to receive connected unit information.
*/
public search() {
if (!this.bound) {
@ -103,8 +116,9 @@ export class FindUnits extends EventEmitter {
/**
* Searches for the given amount of time. Must close the finder with close() when done with all searches.
*
* @param searchTimeMs the number of milliseconds to search before giving up and returning found results (default: 5000)
* @returns Promise resolving to a list of discovered units, if any.
* @returns Promise resolving to a list of discovered {@linkcode UnitInfo}, if any.
*/
public async searchAsync(searchTimeMs?: number): Promise<UnitInfo[]> {
const p = new Promise<UnitInfo[]>((resolve) => {

27
unit.ts
View File

@ -4,9 +4,24 @@ import debug from "debug";
import { ICRequest } from "./messages/request.js";
import { ICResponse } from "./messages/response.js";
// needed for jsdoc
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { messages } from "./messages/messages.js";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { SubscribeToUpdates } from "./messages/notify.js";
const debugUnit = debug("ic:unit");
/**
* Contains methods to connect to and communicate with an IntelliCenter controller.
*
* Call `connect` to connect to the unit.
*
* Available events:
*
* * `"response-{messageID}"` - fired once per message sent with `send()` where {messageID} is the ID specified in the {@linkcode ICRequest} given to `send()`
* * `"notify"` - fired when an update is available to a property previously subscribed to via a {@linkcode SubscribeToUpdates} request
*/
export class Unit extends EventEmitter {
private client?: WebSocket;
private pingTimeout?: ReturnType<typeof setTimeout>;
@ -23,6 +38,9 @@ export class Unit extends EventEmitter {
this.port = port;
}
/**
* Connects to the specified unit and maintains a connection to it until `close()` is called.
*/
public async connect() {
if (this.client) {
throw new Error("can't open a client that is already open");
@ -59,6 +77,9 @@ export class Unit extends EventEmitter {
debugUnit("connected");
}
/**
* Closes the connection to the unit.
*/
public close() {
debugUnit("closing connection by request");
this.client?.close();
@ -104,6 +125,12 @@ export class Unit extends EventEmitter {
this.emit(`response-${respObj.messageID}`, respObj);
};
/**
* Sends a request to the unit.
*
* @param request an message from {@linkcode messages} to send to the unit.
* @returns a promise that resolves into the {@linkcode ICResponse} with information about the request.
*/
public async send(request: ICRequest): Promise<ICResponse> {
const payload = JSON.stringify(request);
debugUnit(