From 943a65c4714d543698159e37515a9b7ab731e089 Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Thu, 24 Oct 2024 11:56:10 +0200 Subject: [PATCH] refactor: replace INodeQuery/isNodeQuery with hasNodeId --- docs/getting-started/migrating/v14.md | 1 + packages/serial/src/index.ts | 1 - packages/serial/src/message/INodeQuery.ts | 10 ---------- packages/serial/src/message/Message.test.ts | 5 ++--- packages/serial/src/message/Message.ts | 12 ++++++++++-- .../network-mgmt/AssignReturnRouteMessages.ts | 5 +---- .../network-mgmt/AssignSUCReturnRouteMessages.ts | 5 ++--- .../network-mgmt/DeleteReturnRouteMessages.ts | 5 +---- .../network-mgmt/DeleteSUCReturnRouteMessages.ts | 5 ++--- .../network-mgmt/RequestNodeInfoMessages.ts | 3 +-- packages/zwave-js/src/lib/driver/Driver.ts | 14 +++++++------- 11 files changed, 27 insertions(+), 39 deletions(-) delete mode 100644 packages/serial/src/message/INodeQuery.ts diff --git a/docs/getting-started/migrating/v14.md b/docs/getting-started/migrating/v14.md index d3ba743f9fbc..a07615b64fbe 100644 --- a/docs/getting-started/migrating/v14.md +++ b/docs/getting-started/migrating/v14.md @@ -192,3 +192,4 @@ Most of these should not affect any application: - The `Message.options` property was removed - The `ICommandClassContainer` interface was replaced with the new `ContainsCC` interface, which indicates that something contains a deserialized CC instance. - The `Driver.computeNetCCPayloadSize` method now requires that the passed message instance contains a deserialized CC instance. +- The `INodeQuery` interface and the `isNodeQuery` function were removed. To test whether a message references a node, use `hasNodeId(msg)` instead, which communicates the intent more clearly. diff --git a/packages/serial/src/index.ts b/packages/serial/src/index.ts index 420be1b62971..30ea53e36711 100644 --- a/packages/serial/src/index.ts +++ b/packages/serial/src/index.ts @@ -2,7 +2,6 @@ export { SerialLogger } from "./log/Logger"; export type { SerialLogContext } from "./log/Logger_safe"; export * from "./message/Constants"; -export * from "./message/INodeQuery"; export * from "./message/Message"; export * from "./message/MessageHeaders"; export * from "./message/SuccessIndicator"; diff --git a/packages/serial/src/message/INodeQuery.ts b/packages/serial/src/message/INodeQuery.ts deleted file mode 100644 index 9b93e7c572c3..000000000000 --- a/packages/serial/src/message/INodeQuery.ts +++ /dev/null @@ -1,10 +0,0 @@ -import type { Message } from "./Message"; - -export interface INodeQuery { - nodeId: number; -} - -/** Tests if the given message is for a node or references a node */ -export function isNodeQuery(msg: T): msg is T & INodeQuery { - return typeof (msg as any).nodeId === "number"; -} diff --git a/packages/serial/src/message/Message.test.ts b/packages/serial/src/message/Message.test.ts index 9e2fd13bee88..7d565a56b882 100644 --- a/packages/serial/src/message/Message.test.ts +++ b/packages/serial/src/message/Message.test.ts @@ -2,7 +2,6 @@ import { ZWaveErrorCodes, assertZWaveError } from "@zwave-js/core"; import { createTestingHost } from "@zwave-js/host"; import test from "ava"; import { FunctionType, MessageType } from "./Constants"; -import type { INodeQuery } from "./INodeQuery"; import { Message, messageTypes } from "./Message"; test("should deserialize and serialize correctly", (t) => { @@ -228,10 +227,10 @@ test("tryGetNode() returns the associated node otherwise", (t) => { }); // This node exists - (msg as any as INodeQuery).nodeId = 1; + (msg as any).nodeId = 1; t.is(msg.tryGetNode(host), host.getNode(1)); // This one does - (msg as any as INodeQuery).nodeId = 2; + (msg as any).nodeId = 2; t.is(msg.tryGetNode(host), undefined); }); diff --git a/packages/serial/src/message/Message.ts b/packages/serial/src/message/Message.ts index 1b8882f2f505..5e17c08701c5 100644 --- a/packages/serial/src/message/Message.ts +++ b/packages/serial/src/message/Message.ts @@ -21,7 +21,6 @@ import type { import type { JSONObject, TypedClassDecorator } from "@zwave-js/shared/safe"; import { num2hex, staticExtends } from "@zwave-js/shared/safe"; import { FunctionType, MessageType } from "./Constants"; -import { isNodeQuery } from "./INodeQuery"; import { MessageHeaders } from "./MessageHeaders"; export type MessageConstructor = typeof Message & { @@ -78,6 +77,15 @@ export interface MessageEncodingContext ): void; } +export interface HasNodeId { + nodeId: number; +} + +/** Tests if the given message is for a node or references a node */ +export function hasNodeId(msg: T): msg is T & HasNodeId { + return typeof (msg as any).nodeId === "number"; +} + /** Returns the number of bytes the first message in the buffer occupies */ function getMessageLength(data: Buffer): number { const remainingLength = data[1]; @@ -393,7 +401,7 @@ export class Message { /** Finds the ID of the target or source node in a message, if it contains that information */ public getNodeId(): number | undefined { - if (isNodeQuery(this)) return this.nodeId; + if (hasNodeId(this)) return this.nodeId; // Override this in subclasses if a different behavior is desired } diff --git a/packages/serial/src/serialapi/network-mgmt/AssignReturnRouteMessages.ts b/packages/serial/src/serialapi/network-mgmt/AssignReturnRouteMessages.ts index 8ce8a68edd0f..2c2e9d484b23 100644 --- a/packages/serial/src/serialapi/network-mgmt/AssignReturnRouteMessages.ts +++ b/packages/serial/src/serialapi/network-mgmt/AssignReturnRouteMessages.ts @@ -7,7 +7,6 @@ import { encodeNodeID, } from "@zwave-js/core"; import type { - INodeQuery, MessageEncodingContext, MessageParsingContext, MessageRaw, @@ -48,9 +47,7 @@ export interface AssignReturnRouteRequestOptions { @expectedResponse(FunctionType.AssignReturnRoute) @expectedCallback(FunctionType.AssignReturnRoute) -export class AssignReturnRouteRequest extends AssignReturnRouteRequestBase - implements INodeQuery -{ +export class AssignReturnRouteRequest extends AssignReturnRouteRequestBase { public constructor( options: AssignReturnRouteRequestOptions & MessageBaseOptions, ) { diff --git a/packages/serial/src/serialapi/network-mgmt/AssignSUCReturnRouteMessages.ts b/packages/serial/src/serialapi/network-mgmt/AssignSUCReturnRouteMessages.ts index 8234779d2807..b85a0edec06c 100644 --- a/packages/serial/src/serialapi/network-mgmt/AssignSUCReturnRouteMessages.ts +++ b/packages/serial/src/serialapi/network-mgmt/AssignSUCReturnRouteMessages.ts @@ -6,7 +6,6 @@ import { } from "@zwave-js/core"; import { FunctionType, - type INodeQuery, Message, type MessageBaseOptions, type MessageEncodingContext, @@ -55,8 +54,8 @@ function testAssignSUCReturnRouteCallback( @expectedResponse(FunctionType.AssignSUCReturnRoute) @expectedCallback(testAssignSUCReturnRouteCallback) -export class AssignSUCReturnRouteRequest extends AssignSUCReturnRouteRequestBase - implements INodeQuery +export class AssignSUCReturnRouteRequest + extends AssignSUCReturnRouteRequestBase { public constructor( options: AssignSUCReturnRouteRequestOptions & MessageBaseOptions, diff --git a/packages/serial/src/serialapi/network-mgmt/DeleteReturnRouteMessages.ts b/packages/serial/src/serialapi/network-mgmt/DeleteReturnRouteMessages.ts index 51eed2c05bb7..cf1eb46ecbe9 100644 --- a/packages/serial/src/serialapi/network-mgmt/DeleteReturnRouteMessages.ts +++ b/packages/serial/src/serialapi/network-mgmt/DeleteReturnRouteMessages.ts @@ -7,7 +7,6 @@ import { encodeNodeID, } from "@zwave-js/core"; import type { - INodeQuery, MessageEncodingContext, MessageParsingContext, MessageRaw, @@ -47,9 +46,7 @@ export interface DeleteReturnRouteRequestOptions { @expectedResponse(FunctionType.DeleteReturnRoute) @expectedCallback(FunctionType.DeleteReturnRoute) -export class DeleteReturnRouteRequest extends DeleteReturnRouteRequestBase - implements INodeQuery -{ +export class DeleteReturnRouteRequest extends DeleteReturnRouteRequestBase { public constructor( options: DeleteReturnRouteRequestOptions & MessageBaseOptions, ) { diff --git a/packages/serial/src/serialapi/network-mgmt/DeleteSUCReturnRouteMessages.ts b/packages/serial/src/serialapi/network-mgmt/DeleteSUCReturnRouteMessages.ts index 4f6dff91b8a8..be76caf364cc 100644 --- a/packages/serial/src/serialapi/network-mgmt/DeleteSUCReturnRouteMessages.ts +++ b/packages/serial/src/serialapi/network-mgmt/DeleteSUCReturnRouteMessages.ts @@ -5,7 +5,6 @@ import { encodeNodeID, } from "@zwave-js/core"; import type { - INodeQuery, MessageEncodingContext, MessageParsingContext, MessageRaw, @@ -57,8 +56,8 @@ function testDeleteSUCReturnRouteCallback( @expectedResponse(FunctionType.DeleteSUCReturnRoute) @expectedCallback(testDeleteSUCReturnRouteCallback) -export class DeleteSUCReturnRouteRequest extends DeleteSUCReturnRouteRequestBase - implements INodeQuery +export class DeleteSUCReturnRouteRequest + extends DeleteSUCReturnRouteRequestBase { public constructor( options: DeleteSUCReturnRouteRequestOptions & MessageBaseOptions, diff --git a/packages/serial/src/serialapi/network-mgmt/RequestNodeInfoMessages.ts b/packages/serial/src/serialapi/network-mgmt/RequestNodeInfoMessages.ts index 7e0d976641b6..a4a26b4b4b3f 100644 --- a/packages/serial/src/serialapi/network-mgmt/RequestNodeInfoMessages.ts +++ b/packages/serial/src/serialapi/network-mgmt/RequestNodeInfoMessages.ts @@ -6,7 +6,6 @@ import { } from "@zwave-js/core"; import { FunctionType, - type INodeQuery, Message, type MessageBaseOptions, type MessageEncodingContext, @@ -88,7 +87,7 @@ function testCallbackForRequestNodeInfoRequest( @expectedResponse(RequestNodeInfoResponse) @expectedCallback(testCallbackForRequestNodeInfoRequest) @priority(MessagePriority.NodeQuery) -export class RequestNodeInfoRequest extends Message implements INodeQuery { +export class RequestNodeInfoRequest extends Message { public constructor( options: RequestNodeInfoRequestOptions & MessageBaseOptions, ) { diff --git a/packages/zwave-js/src/lib/driver/Driver.ts b/packages/zwave-js/src/lib/driver/Driver.ts index f52149143475..83c67baed0ff 100644 --- a/packages/zwave-js/src/lib/driver/Driver.ts +++ b/packages/zwave-js/src/lib/driver/Driver.ts @@ -121,7 +121,7 @@ import { type BootloaderChunk, BootloaderChunkType, FunctionType, - type INodeQuery, + type HasNodeId, Message, type MessageEncodingContext, MessageHeaders, @@ -135,7 +135,7 @@ import { type ZWaveSerialPortImplementation, ZWaveSocket, getDefaultPriority, - isNodeQuery, + hasNodeId, isSuccessIndicator, isZWaveSerialPortImplementation, } from "@zwave-js/serial"; @@ -4046,7 +4046,7 @@ export class Driver extends TypedEventEmitter */ public handleMissingNodeACK( transaction: Transaction & { - message: INodeQuery; + message: HasNodeId; }, error: ZWaveError, ): boolean { @@ -5023,7 +5023,7 @@ ${handlers.length} left`, private async handleRequest(msg: Message): Promise { let handlers: RequestHandlerEntry[] | undefined; - if (isNodeQuery(msg) || containsCC(msg)) { + if (hasNodeId(msg) || containsCC(msg)) { const node = this.tryGetNode(msg); if (node) { // We have received an unsolicited message from a dead node, bring it back to life @@ -5465,7 +5465,7 @@ ${handlers.length} left`, // Update statistics const node = this.tryGetNode(msg); let success = true; - if (isSendData(msg) || isNodeQuery(msg)) { + if (isSendData(msg) || hasNodeId(msg)) { // This shouldn't happen, but just in case if (!node) return; @@ -5984,7 +5984,7 @@ ${handlers.length} left`, this.ensureReady(); let node: ZWaveNode | undefined; - if (isNodeQuery(msg) || containsCC(msg)) { + if (hasNodeId(msg) || containsCC(msg)) { node = this.tryGetNode(msg); } @@ -6111,7 +6111,7 @@ ${handlers.length} left`, } else { // For other messages to the node, just check for successful completion. If the callback is not OK, // we might not be able to communicate with the node. Sending another message is not a good idea. - maybeSendToSleep = isNodeQuery(msg) + maybeSendToSleep = hasNodeId(msg) && result && isSuccessIndicator(result) && result.isOK();