Skip to content

Commit

Permalink
Merge branch 'master' into pr/xuexiaofei/7312
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Oct 24, 2024
2 parents fac53fc + 7334bd1 commit 06a54f4
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 75 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<!--
Add placeholder for next release with `wip` snippet
-->
## 13.10.0 (2024-10-24)
### Features
* `mock-server` now supports putting the simulated controller into add and remove mode (#7314)

## 13.9.1 (2024-10-17)
### Bugfixes
* Fixed an issue where preferred scales were not being found when set as a string (#7286)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zwave-js/repo",
"version": "13.9.1",
"version": "13.10.0",
"private": true,
"description": "Z-Wave driver written entirely in JavaScript/TypeScript",
"keywords": [],
Expand Down
2 changes: 1 addition & 1 deletion packages/flash/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zwave-js/flash",
"version": "13.9.1",
"version": "13.10.0",
"description": "zwave-js: firmware flash utility",
"keywords": [],
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion packages/testing/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zwave-js/testing",
"version": "13.9.1",
"version": "13.10.0",
"description": "zwave-js: testing utilities",
"keywords": [],
"publishConfig": {
Expand Down
2 changes: 2 additions & 0 deletions packages/testing/src/MockControllerCapabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export function getDefaultSupportedFunctionTypes(): FunctionType[] {
FunctionType.GetNodeProtocolInfo,
FunctionType.RequestNodeInfo,
FunctionType.AssignSUCReturnRoute,
FunctionType.AddNodeToNetwork,
FunctionType.RemoveNodeFromNetwork,
];
}

Expand Down
2 changes: 1 addition & 1 deletion packages/zwave-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zwave-js",
"version": "13.9.1",
"version": "13.10.0",
"description": "Z-Wave driver written entirely in JavaScript/TypeScript",
"keywords": [],
"type": "commonjs",
Expand Down
159 changes: 159 additions & 0 deletions packages/zwave-js/src/lib/controller/MockControllerBehaviors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ import {
GetControllerIdResponse,
} from "../serialapi/memory/GetControllerIdMessages";
import { SoftResetRequest } from "../serialapi/misc/SoftResetRequest";
import {
AddNodeStatus,
AddNodeToNetworkRequest,
AddNodeToNetworkRequestStatusReport,
AddNodeType,
} from "../serialapi/network-mgmt/AddNodeToNetworkRequest";
import {
AssignSUCReturnRouteRequest,
AssignSUCReturnRouteRequestTransmitReport,
Expand All @@ -68,6 +74,12 @@ import {
GetSUCNodeIdRequest,
GetSUCNodeIdResponse,
} from "../serialapi/network-mgmt/GetSUCNodeIdMessages";
import {
RemoveNodeFromNetworkRequest,
RemoveNodeFromNetworkRequestStatusReport,
RemoveNodeStatus,
RemoveNodeType,
} from "../serialapi/network-mgmt/RemoveNodeFromNetworkRequest";
import {
RequestNodeInfoRequest,
RequestNodeInfoResponse,
Expand All @@ -82,6 +94,7 @@ import {
} from "../serialapi/transport/SendDataMessages";
import {
MockControllerCommunicationState,
MockControllerInclusionState,
MockControllerStateKeys,
} from "./MockControllerState";
import { determineNIF } from "./NodeInformationFrame";
Expand Down Expand Up @@ -619,6 +632,150 @@ const handleAssignSUCReturnRoute: MockControllerBehavior = {
},
};

const handleAddNode: MockControllerBehavior = {
async onHostMessage(host, controller, msg) {
if (msg instanceof AddNodeToNetworkRequest) {
// Check if this command is legal right now
const state = controller.state.get(
MockControllerStateKeys.InclusionState,
) as MockControllerInclusionState | undefined;

const expectCallback = msg.callbackId !== 0;
let cb: AddNodeToNetworkRequestStatusReport | undefined;
if (
state === MockControllerInclusionState.AddingNode
) {
// While adding, only accept stop commands
if (msg.addNodeType === AddNodeType.Stop) {
controller.state.set(
MockControllerStateKeys.InclusionState,
MockControllerInclusionState.Idle,
);
cb = new AddNodeToNetworkRequestStatusReport(
host,
{
callbackId: msg.callbackId,
status: AddNodeStatus.Failed,
},
);
} else {
cb = new AddNodeToNetworkRequestStatusReport(
host,
{
callbackId: msg.callbackId,
status: AddNodeStatus.Failed,
},
);
}
} else if (state === MockControllerInclusionState.RemovingNode) {
// Cannot start adding nodes while removing one
cb = new AddNodeToNetworkRequestStatusReport(
host,
{
callbackId: msg.callbackId,
status: AddNodeStatus.Failed,
},
);
} else {
// Idle

// Set the controller into "adding node" state
// For now we don't actually do anything in that state
controller.state.set(
MockControllerStateKeys.InclusionState,
MockControllerInclusionState.AddingNode,
);

cb = new AddNodeToNetworkRequestStatusReport(
host,
{
callbackId: msg.callbackId,
status: AddNodeStatus.Ready,
},
);
}

if (expectCallback && cb) {
await controller.sendToHost(cb.serialize());
}

return true;
}
},
};

const handleRemoveNode: MockControllerBehavior = {
async onHostMessage(host, controller, msg) {
if (msg instanceof RemoveNodeFromNetworkRequest) {
// Check if this command is legal right now
const state = controller.state.get(
MockControllerStateKeys.InclusionState,
) as MockControllerInclusionState | undefined;

const expectCallback = msg.callbackId !== 0;
let cb: RemoveNodeFromNetworkRequestStatusReport | undefined;
if (
state === MockControllerInclusionState.RemovingNode
) {
// While removing, only accept stop commands
if (msg.removeNodeType === RemoveNodeType.Stop) {
controller.state.set(
MockControllerStateKeys.InclusionState,
MockControllerInclusionState.Idle,
);
cb = new RemoveNodeFromNetworkRequestStatusReport(
host,
{
callbackId: msg.callbackId,
status: RemoveNodeStatus.Failed,
},
);
} else {
cb = new RemoveNodeFromNetworkRequestStatusReport(
host,
{
callbackId: msg.callbackId,
status: RemoveNodeStatus.Failed,
},
);
}
} else if (state === MockControllerInclusionState.AddingNode) {
// Cannot start removing nodes while adding one
cb = new RemoveNodeFromNetworkRequestStatusReport(
host,
{
callbackId: msg.callbackId,
status: RemoveNodeStatus.Failed,
},
);
} else {
// Idle

// Set the controller into "removing node" state
// For now we don't actually do anything in that state
controller.state.set(
MockControllerStateKeys.InclusionState,
MockControllerInclusionState.RemovingNode,
);

cb = new RemoveNodeFromNetworkRequestStatusReport(
host,
{
callbackId: msg.callbackId,
status: RemoveNodeStatus.Ready,
},
);
}

if (expectCallback && cb) {
await controller.sendToHost(cb.serialize());
}

return true;
}
},
};

const forwardCommandClassesToHost: MockControllerBehavior = {
async onNodeFrame(host, controller, node, frame) {
if (
Expand Down Expand Up @@ -682,6 +839,8 @@ export function createDefaultBehaviors(): MockControllerBehavior[] {
handleSendDataMulticast,
handleRequestNodeInfo,
handleAssignSUCReturnRoute,
handleAddNode,
handleRemoveNode,
forwardCommandClassesToHost,
forwardUnsolicitedNIF,
];
Expand Down
7 changes: 7 additions & 0 deletions packages/zwave-js/src/lib/controller/MockControllerState.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
export enum MockControllerStateKeys {
CommunicationState = "communicationState",
InclusionState = "inclusionState",
}

export enum MockControllerCommunicationState {
Idle,
Sending,
WaitingForNode,
}

export enum MockControllerInclusionState {
Idle,
AddingNode,
RemovingNode,
}
4 changes: 3 additions & 1 deletion packages/zwave-js/src/lib/driver/Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,9 @@ export class Driver extends TypedEventEmitter<DriverEventCallbacks>
}
private set queueIdle(value: boolean) {
if (this._queueIdle !== value) {
this.driverLog.print(`all queues ${value ? "idle" : "busy"}`);
this.driverLog.print(
value ? "all queues idle" : "one or more queues busy",
);
this._queueIdle = value;
this.handleQueueIdleChange(value);
}
Expand Down
Loading

0 comments on commit 06a54f4

Please sign in to comment.