Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow passing custom serial port implementation in port param of Driver class #4885

Merged
merged 4 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/serial/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ export function isNodeQuery<T extends Message>(msg: T): msg is T & INodeQuery;
// @public (undocumented)
export function isSuccessIndicator<T extends Message>(msg: T): msg is T & SuccessIndicator;

// Warning: (ae-missing-release-tag) "isZWaveSerialPortImplementation" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export function isZWaveSerialPortImplementation(obj: unknown): obj is ZWaveSerialPortImplementation;

// Warning: (ae-missing-release-tag) "Message" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public
Expand Down
12 changes: 12 additions & 0 deletions packages/serial/src/ZWaveSerialPortBase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ZWaveLogContainer } from "@zwave-js/core";
import { Mixin } from "@zwave-js/shared";
import { isObject } from "alcalzone-shared/typeguards";
import { EventEmitter } from "events";
import { Duplex, PassThrough, Readable, Writable } from "stream";
import { SerialLogger } from "./Logger";
Expand Down Expand Up @@ -51,6 +52,17 @@ export interface ZWaveSerialPortBase {
): boolean;
}

export function isZWaveSerialPortImplementation(
obj: unknown,
): obj is ZWaveSerialPortImplementation {
return (
isObject(obj) &&
typeof obj.create === "function" &&
typeof obj.open === "function" &&
typeof obj.close === "function"
);
}

export interface ZWaveSerialPortImplementation {
create(): Duplex & EventEmitter;
open(
Expand Down
3 changes: 2 additions & 1 deletion packages/zwave-js/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ import type { ZWaveNotificationCallbackParams_NotificationCC } from '@zwave-js/c
import type { ZWaveNotificationCallbackParams_PowerlevelCC } from '@zwave-js/cc';
import { ZWavePlusNodeType } from '@zwave-js/cc';
import { ZWavePlusRoleType } from '@zwave-js/cc';
import { ZWaveSerialPortImplementation } from '@zwave-js/serial';

export { buffer2hex }

Expand Down Expand Up @@ -224,7 +225,7 @@ export class DeviceClass {
export class Driver extends TypedEventEmitter<DriverEventCallbacks> implements ZWaveApplicationHost {
// (undocumented)
[util.inspect.custom](): string;
constructor(port: string, options?: DeepPartial<ZWaveOptions>);
constructor(port: string | ZWaveSerialPortImplementation, options?: DeepPartial<ZWaveOptions>);
get allNodesReady(): boolean;
// (undocumented)
readonly cacheDir: string;
Expand Down
51 changes: 37 additions & 14 deletions packages/zwave-js/src/lib/driver/Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ import {
INodeQuery,
isNodeQuery,
isSuccessIndicator,
isZWaveSerialPortImplementation,
Message,
MessageHeaders,
MessageType,
ZWaveSerialPort,
ZWaveSerialPortBase,
ZWaveSerialPortImplementation,
ZWaveSocket,
} from "@zwave-js/serial";
import {
Expand Down Expand Up @@ -407,11 +409,22 @@ export class Driver
implements ZWaveApplicationHost
{
public constructor(
private port: string,
private port: string | ZWaveSerialPortImplementation,
options?: DeepPartial<ZWaveOptions>,
) {
super();

// Ensure the given serial port is valid
if (
typeof port !== "string" &&
!isZWaveSerialPortImplementation(port)
) {
throw new ZWaveError(
`The port must be a string or a valid custom serial port implementation!`,
ZWaveErrorCodes.Driver_InvalidOptions,
);
}

// merge given options with defaults
this.options = mergeDeep(options, defaultOptions) as ZWaveOptions;
// And make sure they contain valid values
Expand Down Expand Up @@ -808,22 +821,32 @@ export class Driver
this.sendThread.start();

// Open the serial port
if (this.port.startsWith("tcp://")) {
const url = new URL(this.port);
this.driverLog.print(`opening serial port ${this.port}`);
this.serial = new ZWaveSocket(
{
host: url.hostname,
port: parseInt(url.port),
},
this._logContainer,
);
if (typeof this.port === "string") {
if (this.port.startsWith("tcp://")) {
const url = new URL(this.port);
this.driverLog.print(`opening serial port ${this.port}`);
this.serial = new ZWaveSocket(
{
host: url.hostname,
port: parseInt(url.port),
},
this._logContainer,
);
} else {
this.driverLog.print(`opening serial port ${this.port}`);
this.serial = new ZWaveSerialPort(
this.port,
this._logContainer,
this.options.testingHooks?.serialPortBinding,
);
}
} else {
this.driverLog.print(`opening serial port ${this.port}`);
this.serial = new ZWaveSerialPort(
this.driverLog.print(
"opening serial port using the provided custom implementation",
);
this.serial = new ZWaveSerialPortBase(
this.port,
this._logContainer,
this.options.testingHooks?.serialPortBinding,
);
}
this.serial
Expand Down