4 Commits

Author SHA1 Message Date
cc1a866233 Update changelog, version
All checks were successful
Node.js CI / build (18.x) (push) Successful in 12s
Node.js CI / build (20.x) (push) Successful in 11s
Node.js CI / build (22.x) (push) Successful in 11s
2025-01-15 22:45:01 -06:00
f8e2aa0f3e Get basic test up and passing
This replaces the 'ws' library with built-in websockets so that we can mock a server and successfully connect to it. A simple test verifies that Unit is handling a message request and response as we expect it to.
2025-01-13 13:06:58 -06:00
6c79e35e04 Fix VSCode launch config 2025-01-13 12:25:02 -06:00
331cea7007 Update README.md
All checks were successful
Node.js CI / build (18.x) (push) Successful in 11s
Node.js CI / build (20.x) (push) Successful in 11s
Node.js CI / build (22.x) (push) Successful in 11s
2025-01-09 16:58:19 -06:00
13 changed files with 193 additions and 122 deletions

4
.vscode/launch.json vendored
View File

@ -9,8 +9,8 @@
"request": "launch",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/example.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"program": "${workspaceFolder}/src/example.ts",
"preLaunchTask": "npm: build",
"outFiles": ["${workspaceFolder}/**/*.js"],
"env": {
"DEBUG": "ic:*"

View File

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v0.3.0] - 2025-01-15
### Changed
- Dropped the websocket library being used in favor of the default one for node. This allows tests to set up a mock server that the unit can connect to. Theoretically there's no consumer-visible behavior difference here, but it is a major change under the hood.
## [v0.2.2] - 2025-01-08
### Changed

View File

@ -8,7 +8,7 @@ The [Wiki](https://github.com/parnic/node-intellicenter/wiki) has development no
## Usage
See example.ts for an example of interfacing with the library. Broadly, import the library with
See [example.ts](./src/example.ts) for an example of interfacing with the library. Broadly, import the library with
```javascript
// ESM
@ -88,11 +88,11 @@ console.log(JSON.stringify(response, null, 2));
## Examples
See [the example script](./example.ts) (transpiled [CJS version](./cjs/example.js) | [ESM version](./esm/example.js)) for some code examples to get you going.
See [the example script](./src/example.ts) (transpiled [CJS version](./cjs/example.js) | [ESM version](./esm/example.js)) for some code examples to get you going.
## List-objects script
There is a [list-objects](./list-objects.ts) script available for easily identifying objects on your controller that you can use to get info from or set attributes on.
There is a [list-objects](./src/list-objects.ts) script available for easily identifying objects on your controller that you can use to get info from or set attributes on.
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:
@ -113,7 +113,7 @@ Contributions are welcomed! Please open a pull request with your changes and let
## API reference
See the list of [messages](./messages/messages.ts) for an up-to-date central listing of all available messages. These are essentially all helper methods for well-known messages, but you can construct your own by creating a new ICRequest via either the [GetRequest()](./messages/request.ts#L19) helper method or just constructing a `new ICRequest()` and filling it out (note: if you go the raw ICRequest route, you'll want some method of handling messageID that allows you to distinguish responses from each other).
See the list of [messages](./src/messages/messages.ts) for an up-to-date central listing of all available messages. These are essentially all helper methods for well-known messages, but you can construct your own by creating a new ICRequest via either the [GetRequest()](./src/messages/request.ts#L19) helper method or just constructing a `new ICRequest()` and filling it out (note: if you go the raw ICRequest route, you'll want some method of handling messageID that allows you to distinguish responses from each other).
IntelliCenter controllers can handle lots of messages being thrown at them at once and will respond when each request has been processed. This is why the `messageID` field on a request is important. If you use this library in async/await mode you'll only be dealing with one request in flight at a time, but that's not technically necessary.
@ -121,13 +121,13 @@ IntelliCenter controllers can handle lots of messages being thrown at them at on
Messages (requests and responses) are sent in JSON format. Generally a request is made with a `command`, an optional `condition`, and an `objectList` with details about what properties ("keys") and objects are being referred to. Some commands, such as `GetQuery`, require additional properties to be specified.
See [ICRequest](./messages/request.ts) and [ICResponse](./messages/response.ts) as well as [ICParam](./messages/param.ts) for all known fields.
See [ICRequest](./src/messages/request.ts) and [ICResponse](./src/messages/response.ts) as well as [ICParam](./src/messages/param.ts) for all known fields.
There are some redacted [connection logs](./connection-logs/) available for a raw look at some basic system communication. These were obtained by using [mitmproxy](https://mitmproxy.org/) as a websocket proxy and having the official Pentair Pool app connect to it while it was connected to the pool.
### Subscribing to updates
[SubscribeToUpdates](./messages/notify.ts) may be sent to be notified about changes to the given keys on the given object for the duration of the connection. It is unknown how/if you can unsubscribe after subscribing, but reconnecting to the unit will start fresh with no subscriptions registered.
[SubscribeToUpdates](./src/messages/notify.ts) may be sent to be notified about changes to the given keys on the given object for the duration of the connection. It is unknown how/if you can unsubscribe after subscribing, but reconnecting to the unit will start fresh with no subscriptions registered.
Example:

2
cjs/unit.d.ts vendored
View File

@ -30,6 +30,8 @@ export declare class Unit extends EventEmitter {
* Connects to the specified unit and maintains a connection to it until `close()` is called.
*/
connect(): Promise<void>;
private onOpen;
private onError;
/**
* Closes the connection to the unit.
*/

View File

@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Unit = void 0;
const events_1 = require("events");
const ws_1 = require("ws");
const debug_1 = __importDefault(require("debug"));
const debugUnit = (0, debug_1.default)("ic:unit");
/**
@ -47,33 +46,34 @@ class Unit extends events_1.EventEmitter {
throw new Error("can't open a client that is already open");
}
debugUnit(`connecting to ws://${this.endpoint}:${this.port.toString()}`);
this.client = new ws_1.WebSocket(`ws://${this.endpoint}:${this.port.toString()}`);
const { heartbeat, onClientMessage, socketCleanup } = this;
this.client.on("error", (evt) => {
// todo: emit event so we can reconnect? auto reconnect?
debugUnit("error in websocket: $o", evt);
this.emit("error", evt);
socketCleanup();
});
this.client.on("open", () => {
this.emit("open");
heartbeat();
});
this.client.on("ping", heartbeat);
this.client.on("pong", heartbeat);
this.client.on("close", socketCleanup);
this.client.on("message", onClientMessage);
this.client = new WebSocket(`ws://${this.endpoint}:${this.port.toString()}`);
const { onOpen, onError, onClientMessage, socketCleanup } = this;
this.client.addEventListener("error", onError);
this.client.addEventListener("open", onOpen);
this.client.addEventListener("close", socketCleanup);
this.client.addEventListener("message", onClientMessage);
this.pingTimer = setInterval(() => {
debugUnit("sending ping");
this.client?.ping();
// this isn't an actual command that is recognized by the system, we just want to make sure they're still there.
this.client?.send(JSON.stringify({ command: "ping" }));
}, this.pingInterval);
await new Promise((resolve, reject) => {
this.client?.once("error", reject);
this.client?.once("open", resolve);
this.client?.addEventListener("error", reject, true);
this.client?.addEventListener("open", resolve, true);
});
debugUnit("connected");
this.emit("connected");
}
onOpen = () => {
this.emit("open");
this.heartbeat();
};
onError = (evt) => {
// todo: emit event so we can reconnect? auto reconnect?
debugUnit("error in websocket: $o", evt);
this.emit("error", evt);
this.socketCleanup();
};
/**
* Closes the connection to the unit.
*/
@ -87,7 +87,10 @@ class Unit extends events_1.EventEmitter {
socketCleanup = () => {
debugUnit("socket cleanup");
this.emit("close");
this.client?.removeAllListeners();
this.client?.removeEventListener("error", this.onError);
this.client?.removeEventListener("open", this.onOpen);
this.client?.removeEventListener("close", this.socketCleanup);
this.client?.removeEventListener("message", this.onClientMessage);
this.client = undefined;
if (this.pingTimeout) {
clearTimeout(this.pingTimeout);
@ -104,13 +107,20 @@ class Unit extends events_1.EventEmitter {
this.pingTimeout = setTimeout(() => {
debugUnit("terminating connection due to heartbeat timeout");
this.emit("timeout");
this.client?.terminate();
try {
this.client?.close();
}
catch (ex) {
debugUnit("exception trying to close client from ping timeout: %o", ex);
}
this.socketCleanup();
}, this.pingInterval + 5000);
};
onClientMessage = (msg) => {
onClientMessage = (evt) => {
const msg = evt.data;
debugUnit("message received, length %d", msg.length);
const respObj = JSON.parse(msg.toString());
this.heartbeat();
const respObj = JSON.parse(msg);
if (respObj.command.toLowerCase() === "notifylist") {
debugUnit(" it's a subscription confirmation or update");
this.emit(`notify`, respObj);

View File

@ -1 +1 @@
{"version":3,"file":"unit.js","sourceRoot":"","sources":["../src/unit.ts"],"names":[],"mappings":";;;;;;AAAA,mCAAsC;AACtC,2BAA+B;AAC/B,kDAA0B;AAU1B,MAAM,SAAS,GAAG,IAAA,eAAK,EAAC,SAAS,CAAC,CAAC;AAEnC;;;;;;;;;;;;;;;;G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E,CAAC;YACnD,SAAS,CAAC,8CAA8C,CAAC,CAAC;YAC1D,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QAC/B,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,YAAY,OAAO,CAAC,SAAS,EAAE,EAAE,OAAO,CAAC,CAAC;IACtD,CAAC,CAAC;IAEF;;;;;OAKG;IACI,KAAK,CAAC,IAAI,CAAC,OAAkB;QAClC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,OAAO,MAAM,IAAI,OAAO,CAAC,GAAG,EAAE;gBAC5B,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;YAC1C,CAAC,CAAC,CAAC;QACL,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QACxC,SAAS,CACP,yCAAyC,EACzC,OAAO,CAAC,MAAM,EACd,OAAO,CAAC,SAAS,CAClB,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC1B,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;AA1JD,oBA0JC"}

2
esm/unit.d.ts vendored
View File

@ -30,6 +30,8 @@ export declare class Unit extends EventEmitter {
* Connects to the specified unit and maintains a connection to it until `close()` is called.
*/
connect(): Promise<void>;
private onOpen;
private onError;
/**
* Closes the connection to the unit.
*/

View File

@ -1,5 +1,4 @@
import { EventEmitter } from "events";
import { WebSocket } from "ws";
import debug from "debug";
const debugUnit = debug("ic:unit");
/**
@ -42,32 +41,33 @@ export class Unit extends EventEmitter {
}
debugUnit(`connecting to ws://${this.endpoint}:${this.port.toString()}`);
this.client = new WebSocket(`ws://${this.endpoint}:${this.port.toString()}`);
const { heartbeat, onClientMessage, socketCleanup } = this;
this.client.on("error", (evt) => {
// todo: emit event so we can reconnect? auto reconnect?
debugUnit("error in websocket: $o", evt);
this.emit("error", evt);
socketCleanup();
});
this.client.on("open", () => {
this.emit("open");
heartbeat();
});
this.client.on("ping", heartbeat);
this.client.on("pong", heartbeat);
this.client.on("close", socketCleanup);
this.client.on("message", onClientMessage);
const { onOpen, onError, onClientMessage, socketCleanup } = this;
this.client.addEventListener("error", onError);
this.client.addEventListener("open", onOpen);
this.client.addEventListener("close", socketCleanup);
this.client.addEventListener("message", onClientMessage);
this.pingTimer = setInterval(() => {
debugUnit("sending ping");
this.client?.ping();
// this isn't an actual command that is recognized by the system, we just want to make sure they're still there.
this.client?.send(JSON.stringify({ command: "ping" }));
}, this.pingInterval);
await new Promise((resolve, reject) => {
this.client?.once("error", reject);
this.client?.once("open", resolve);
this.client?.addEventListener("error", reject, true);
this.client?.addEventListener("open", resolve, true);
});
debugUnit("connected");
this.emit("connected");
}
onOpen = () => {
this.emit("open");
this.heartbeat();
};
onError = (evt) => {
// todo: emit event so we can reconnect? auto reconnect?
debugUnit("error in websocket: $o", evt);
this.emit("error", evt);
this.socketCleanup();
};
/**
* Closes the connection to the unit.
*/
@ -81,7 +81,10 @@ export class Unit extends EventEmitter {
socketCleanup = () => {
debugUnit("socket cleanup");
this.emit("close");
this.client?.removeAllListeners();
this.client?.removeEventListener("error", this.onError);
this.client?.removeEventListener("open", this.onOpen);
this.client?.removeEventListener("close", this.socketCleanup);
this.client?.removeEventListener("message", this.onClientMessage);
this.client = undefined;
if (this.pingTimeout) {
clearTimeout(this.pingTimeout);
@ -98,13 +101,20 @@ export class Unit extends EventEmitter {
this.pingTimeout = setTimeout(() => {
debugUnit("terminating connection due to heartbeat timeout");
this.emit("timeout");
this.client?.terminate();
try {
this.client?.close();
}
catch (ex) {
debugUnit("exception trying to close client from ping timeout: %o", ex);
}
this.socketCleanup();
}, this.pingInterval + 5000);
};
onClientMessage = (msg) => {
onClientMessage = (evt) => {
const msg = evt.data;
debugUnit("message received, length %d", msg.length);
const respObj = JSON.parse(msg.toString());
this.heartbeat();
const respObj = JSON.parse(msg);
if (respObj.command.toLowerCase() === "notifylist") {
debugUnit(" it's a subscription confirmation or update");
this.emit(`notify`, respObj);

View File

@ -1 +1 @@
{"version":3,"file":"unit.js","sourceRoot":"","sources":["../src/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;AAU1B,MAAM,SAAS,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC;AAEnC;;;;;;;;;;;;;;;;GAgBG;AACH,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,CAAQ;QAChB,SAAI,GAAJ,IAAI,CAAO;QAIlB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,OAAO;QAClB,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;QAC9D,CAAC;QAED,SAAS,CAAC,sBAAsB,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QAEzE,IAAI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AC,SAAS,CAClB,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC1B,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"}

61
package-lock.json generated
View File

@ -1,17 +1,16 @@
{
"name": "node-intellicenter",
"version": "0.2.2",
"version": "0.3.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "node-intellicenter",
"version": "0.2.2",
"version": "0.3.0",
"license": "MIT",
"dependencies": {
"debug": "^4.4.0",
"uuid": "^11.0.3",
"ws": "^8.18.0"
"uuid": "^11.0.3"
},
"devDependencies": {
"@eslint/js": "^9.17.0",
@ -19,9 +18,9 @@
"@types/jest": "^29.5.14",
"@types/minimist": "^1.2.5",
"@types/uuid": "^10.0.0",
"@types/ws": "^8.5.13",
"eslint": "^9.17.0",
"jest": "^29.7.0",
"jest-websocket-mock": "^2.5.0",
"markdownlint-cli2": "^0.17.1",
"minimist": "^1.2.8",
"ncp": "^2.0.0",
@ -1517,16 +1516,6 @@
"dev": true,
"license": "MIT"
},
"node_modules/@types/ws": {
"version": "8.5.13",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.13.tgz",
"integrity": "sha512-osM/gWBTPKgHV8XkTunnegTRIsvF6owmf5w+JtAfOw472dptdm0dlGv4xCt6GwQRcC2XVOvvRE/0bAoQcL2QkA==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/yargs": {
"version": "17.0.33",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.33.tgz",
@ -4059,6 +4048,17 @@
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
"node_modules/jest-websocket-mock": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/jest-websocket-mock/-/jest-websocket-mock-2.5.0.tgz",
"integrity": "sha512-a+UJGfowNIWvtIKIQBHoEWIUqRxxQHFx4CXT+R5KxxKBtEQ5rS3pPOV/5299sHzqbmeCzxxY5qE4+yfXePePig==",
"dev": true,
"license": "MIT",
"dependencies": {
"jest-diff": "^29.2.0",
"mock-socket": "^9.3.0"
}
},
"node_modules/jest-worker": {
"version": "29.7.0",
"resolved": "https://registry.npmjs.org/jest-worker/-/jest-worker-29.7.0.tgz",
@ -5009,6 +5009,16 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/mock-socket": {
"version": "9.3.1",
"resolved": "https://registry.npmjs.org/mock-socket/-/mock-socket-9.3.1.tgz",
"integrity": "sha512-qxBgB7Qa2sEQgHFjj0dSigq7fX4k6Saisd5Nelwp2q8mlbAFh5dHV9JTTlF8viYJLSSWgMCZFUom8PJcMNBoJw==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">= 8"
}
},
"node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
@ -6166,27 +6176,6 @@
"node": "^12.13.0 || ^14.15.0 || >=16.0.0"
}
},
"node_modules/ws": {
"version": "8.18.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz",
"integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==",
"license": "MIT",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
},
"node_modules/y18n": {
"version": "5.0.8",
"resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",

View File

@ -1,6 +1,6 @@
{
"name": "node-intellicenter",
"version": "0.2.2",
"version": "0.3.0",
"description": "NodeJS library for communicating with a Pentair IntelliCenter controller",
"keywords": [
"pentair",
@ -81,8 +81,7 @@
},
"dependencies": {
"debug": "^4.4.0",
"uuid": "^11.0.3",
"ws": "^8.18.0"
"uuid": "^11.0.3"
},
"devDependencies": {
"@eslint/js": "^9.17.0",
@ -90,9 +89,9 @@
"@types/jest": "^29.5.14",
"@types/minimist": "^1.2.5",
"@types/uuid": "^10.0.0",
"@types/ws": "^8.5.13",
"eslint": "^9.17.0",
"jest": "^29.7.0",
"jest-websocket-mock": "^2.5.0",
"markdownlint-cli2": "^0.17.1",
"minimist": "^1.2.8",
"ncp": "^2.0.0",

View File

@ -1,5 +1,4 @@
import { EventEmitter } from "events";
import { WebSocket } from "ws";
import debug from "debug";
import { ICRequest } from "./messages/request.js";
@ -59,36 +58,39 @@ export class Unit extends EventEmitter {
`ws://${this.endpoint}:${this.port.toString()}`,
);
const { heartbeat, onClientMessage, socketCleanup } = this;
this.client.on("error", (evt) => {
// todo: emit event so we can reconnect? auto reconnect?
debugUnit("error in websocket: $o", evt);
this.emit("error", evt);
socketCleanup();
});
this.client.on("open", () => {
this.emit("open");
heartbeat();
});
this.client.on("ping", heartbeat);
this.client.on("pong", heartbeat);
this.client.on("close", socketCleanup);
this.client.on("message", onClientMessage);
const { onOpen, onError, onClientMessage, socketCleanup } = this;
this.client.addEventListener("error", onError);
this.client.addEventListener("open", onOpen);
this.client.addEventListener("close", socketCleanup);
this.client.addEventListener("message", onClientMessage);
this.pingTimer = setInterval(() => {
debugUnit("sending ping");
this.client?.ping();
// this isn't an actual command that is recognized by the system, we just want to make sure they're still there.
this.client?.send(JSON.stringify({ command: "ping" }));
}, this.pingInterval);
await new Promise((resolve, reject) => {
this.client?.once("error", reject);
this.client?.once("open", resolve);
this.client?.addEventListener("error", reject, true);
this.client?.addEventListener("open", resolve, true);
});
debugUnit("connected");
this.emit("connected");
}
private onOpen = () => {
this.emit("open");
this.heartbeat();
};
private onError = (evt: Event) => {
// todo: emit event so we can reconnect? auto reconnect?
debugUnit("error in websocket: $o", evt);
this.emit("error", evt);
this.socketCleanup();
};
/**
* Closes the connection to the unit.
*/
@ -105,7 +107,10 @@ export class Unit extends EventEmitter {
debugUnit("socket cleanup");
this.emit("close");
this.client?.removeAllListeners();
this.client?.removeEventListener("error", this.onError);
this.client?.removeEventListener("open", this.onOpen);
this.client?.removeEventListener("close", this.socketCleanup);
this.client?.removeEventListener("message", this.onClientMessage);
this.client = undefined;
if (this.pingTimeout) {
@ -126,15 +131,21 @@ export class Unit extends EventEmitter {
this.pingTimeout = setTimeout(() => {
debugUnit("terminating connection due to heartbeat timeout");
this.emit("timeout");
this.client?.terminate();
try {
this.client?.close();
} catch (ex) {
debugUnit("exception trying to close client from ping timeout: %o", ex);
}
this.socketCleanup();
}, this.pingInterval + 5000);
};
private onClientMessage = (msg: Buffer) => {
private onClientMessage = (evt: MessageEvent) => {
const msg = evt.data as string;
debugUnit("message received, length %d", msg.length);
this.heartbeat();
const respObj = JSON.parse(msg.toString()) as ICResponse;
const respObj = JSON.parse(msg) as ICResponse;
if (respObj.command.toLowerCase() === "notifylist") {
debugUnit(" it's a subscription confirmation or update");
this.emit(`notify`, respObj);

View File

@ -1,3 +1,45 @@
describe("empty test", () => {
it("passes", () => {});
import { Unit } from "../src/index";
import * as messages from "../src/messages/messages";
import WS from "jest-websocket-mock";
function makeid(length: number) {
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
let result = "";
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
describe("basic message tests", () => {
let unit: Unit;
let mockServer: WS;
beforeEach(async () => {
mockServer = new WS("ws://localhost:6680");
unit = new Unit("localhost", 6680);
await unit.connect();
await mockServer.connected;
});
afterEach(async () => {
unit.close();
WS.clean();
});
it("can send a message and return its response", async () => {
const msg = messages.GetBodyStatus();
const sender = unit.send(msg);
await expect(mockServer).toReceiveMessage(JSON.stringify(msg));
const response = { messageID: msg.messageID, command: makeid(8) };
mockServer.send(JSON.stringify(response));
const clientResp = await sender;
expect(clientResp).toEqual(response);
});
});