Skip to content

Commit

Permalink
refactor: reorganize traits
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Oct 16, 2024
1 parent e933ef2 commit 8d89667
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 177 deletions.
38 changes: 0 additions & 38 deletions packages/core/src/abstractions/ICommandClass.ts

This file was deleted.

69 changes: 0 additions & 69 deletions packages/core/src/abstractions/IZWaveEndpoint.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/src/consts/Transmission.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { num2hex } from "@zwave-js/shared/safe";
import { isObject } from "alcalzone-shared/typeguards";
import type { CCId } from "../abstractions/ICommandClass";
import type { ProtocolDataRate } from "../capabilities/Protocols";
import { type SecurityClass } from "../security/SecurityClass";
import type { CCId } from "../traits/CommandClasses";
import { Duration } from "../values/Duration";

/** The priority of messages, sorted from high (0) to low (>0) */
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/* eslint-disable @typescript-eslint/consistent-type-exports */
export * from "./abstractions/ICommandClass";
export * from "./abstractions/IZWaveEndpoint";
export * from "./abstractions/IZWaveNode";
export * from "./capabilities/CommandClasses";
export * from "./capabilities/ControllerCapabilities";
export * from "./capabilities/LibraryTypes";
Expand Down Expand Up @@ -30,6 +27,10 @@ export * from "./security/crypto";
export * from "./security/ctr_drbg";
export * from "./security/shared_safe";
export * from "./test/assertZWaveError";
export * from "./traits/CommandClasses";
export * from "./traits/Endpoints";
export * from "./traits/Nodes";
export * from "./traits/SecurityManagers";
export * from "./util/_Types";
export * from "./util/config";
export * from "./util/crc";
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/index_safe.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/* eslint-disable @typescript-eslint/consistent-type-exports */
/* @forbiddenImports external */

export * from "./abstractions/ICommandClass";
export * from "./abstractions/IZWaveEndpoint";
export * from "./abstractions/IZWaveNode";
export * from "./capabilities/CommandClasses";
export * from "./capabilities/ControllerCapabilities";
export * from "./capabilities/LibraryTypes";
Expand All @@ -24,6 +21,11 @@ export * from "./registries/Sensors";
export * from "./security/DSK";
export * from "./security/SecurityClass";
export * from "./security/shared_safe";
export * from "./traits/CommandClasses";
export * from "./traits/Endpoints";
export * from "./traits/Endpoints";
export * from "./traits/Nodes";
export * from "./traits/SecurityManagers";
export * from "./util/_Types";
export * from "./util/config";
export * from "./util/crc";
Expand Down
56 changes: 56 additions & 0 deletions packages/core/src/traits/CommandClasses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type {
CommandClassInfo,
CommandClasses,
} from "../capabilities/CommandClasses";
import type {
MulticastDestination,
NODE_ID_BROADCAST,
NODE_ID_BROADCAST_LR,
} from "../consts";

/** A basic abstraction of a Z-Wave Command Class providing access to the relevant functionality */
export interface CCId {
nodeId: number | MulticastDestination;
endpointIndex?: number;
ccId: CommandClasses;
ccCommand?: number;
}

export type SinglecastCC<T extends CCId = CCId> = T & {
nodeId: number;
};

export type MulticastCC<T extends CCId = CCId> = T & {
nodeId: MulticastDestination;
};

export type BroadcastCC<T extends CCId = CCId> = T & {
nodeId: typeof NODE_ID_BROADCAST | typeof NODE_ID_BROADCAST_LR;
};

/** Allows querying if a CC is supported and in which version */
export interface SupportsCC {
supportsCC(cc: CommandClasses): boolean;
getCCVersion(cc: CommandClasses): number;
}

/** Allows querying if a CC is controlled */
export interface ControlsCC {
controlsCC(cc: CommandClasses): boolean;
}

/** Allows querying if a CC is supported or controlled only securely */
export interface IsCCSecure {
isCCSecure(cc: CommandClasses): boolean;
}

/** Allows querying all implemented CCs and their information */
export interface GetCCs {
getCCs(): Iterable<[ccId: CommandClasses, info: CommandClassInfo]>;
}

/** Allows modifying the list of supported/controlled CCs */
export interface ModifyCCs {
addCC(cc: CommandClasses, info: Partial<CommandClassInfo>): void;
removeCC(cc: CommandClasses): void;
}
21 changes: 21 additions & 0 deletions packages/core/src/traits/Endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { type MulticastDestination } from "../consts/Transmission";
import { type NodeId } from "./Nodes";

/** Identifies an endpoint */
export interface EndpointId {
readonly virtual: false;
readonly nodeId: number;
readonly index: number;
}

/** Identifies a virtual endpoint */
export interface VirtualEndpointId {
readonly virtual: true;
readonly nodeId: number | MulticastDestination;
readonly index: number;
}

/** Allows accessing the parent node of the endpoint, if it exists */
export interface GetEndpointNode<T extends NodeId> {
tryGetNode(): T | undefined;
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import type { FLiRS } from "../capabilities/NodeInfo";
import type { InterviewStage, NodeStatus } from "../consts";
import type {
QuerySecurityClasses,
SetSecurityClass,
} from "../security/SecurityClass";
import { type FLiRS } from "../capabilities/NodeInfo";
import { type NodeStatus } from "../consts/NodeStatus";
import { type MaybeNotKnown } from "../values/Primitive";
import type {
EndpointId,
IVirtualEndpoint,
IZWaveEndpoint,
VirtualEndpointId,
} from "./IZWaveEndpoint";
import { type EndpointId, type VirtualEndpointId } from "./Endpoints";

/** Identifies a node */
export interface NodeId extends EndpointId {
Expand All @@ -19,6 +10,10 @@ export interface NodeId extends EndpointId {
// readonly index: number;
}

export interface VirtualNodeId extends VirtualEndpointId {
readonly id: number | undefined;
}

/** Allows accessing a specific endpoint */
export interface GetEndpoint<T extends EndpointId | VirtualEndpointId> {
getEndpoint(index: 0): T;
Expand Down Expand Up @@ -51,35 +46,6 @@ export interface QueryNodeStatus {
readonly status: NodeStatus;
}

/** A basic abstraction of a Z-Wave node providing access to the relevant functionality */
export interface IZWaveNode
extends
IZWaveEndpoint,
NodeId,
ListenBehavior,
QueryNodeStatus,
GetEndpoint<IZWaveEndpoint>,
GetAllEndpoints<IZWaveEndpoint>,
QuerySecurityClasses,
SetSecurityClass
{
interviewStage: InterviewStage;
}

export interface VirtualNodeId extends VirtualEndpointId {
readonly id: number | undefined;
}

export interface PhysicalNodes<T extends NodeId> {
readonly physicalNodes: readonly T[];
}

/** A basic abstraction of a virtual node (multicast or broadcast) providing access to the relevant functionality */
export interface IVirtualNode
extends
IVirtualEndpoint,
VirtualNodeId,
GetEndpoint<IVirtualEndpoint>,
PhysicalNodes<IZWaveNode>
{
}
12 changes: 12 additions & 0 deletions packages/core/src/traits/SecurityManagers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { type SecurityManager } from "../security/Manager";
import { type SecurityManager2 } from "../security/Manager2";

/** Allows accessing the security manager instances */
export interface SecurityManagers {
/** Management of Security S0 keys and nonces */
securityManager: SecurityManager | undefined;
/** Management of Security S2 keys and nonces (Z-Wave Classic) */
securityManager2: SecurityManager2 | undefined;
/** Management of Security S2 keys and nonces (Z-Wave Long Range) */
securityManagerLR: SecurityManager2 | undefined;
}
Loading

0 comments on commit 8d89667

Please sign in to comment.