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: add readonly rfRegion property to Controller class #5288

Merged
merged 2 commits into from
Jan 2, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/api/controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,15 @@ enum InclusionState {
}
```

### `rfRegion`

```ts
readonly rfRegion: RFRegion | undefined
```

Which RF region the controller is currently set to, or `undefined` if it could not be determined (yet).
This value is cached and updated automatically when using [`getRFRegion` or `setRFRegion`](#configure-rf-region).

## Controller events

The `Controller` class inherits from the Node.js [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter) and thus also supports its methods like `on`, `removeListener`, etc. The available events are available:
Expand Down
2 changes: 2 additions & 0 deletions packages/zwave-js/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,8 @@ export class ZWaveController extends TypedEventEmitter<ControllerEventCallbacks>
// Warning: (tsdoc-param-tag-missing-hyphen) The @param block should be followed by a parameter name and then a hyphen
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "zwave-js" does not have an export "restoreNVM"
restoreNVMRaw(nvmData: Buffer, onProgress?: (bytesWritten: number, total: number) => void): Promise<void>;
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "zwave-js" does not have an export "setRFRegion"
get rfRegion(): RFRegion_2 | undefined;
// (undocumented)
get sdkVersion(): string | undefined;
sdkVersionGt(version: SDKVersion): boolean | undefined;
Expand Down
31 changes: 31 additions & 0 deletions packages/zwave-js/src/lib/controller/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,12 @@ export class ZWaveController extends TypedEventEmitter<ControllerEventCallbacks>
this.driver.cacheSet(cacheKeys.controller.supportsSoftReset, value);
}

private _rfRegion: RFRegion | undefined;
/** Which RF region the controller is currently set to, or `undefined` if it could not be determined (yet). This value is cached and can be changed through {@link setRFRegion}. */
public get rfRegion(): RFRegion | undefined {
return this._rfRegion;
}

private _nodes: ThrowingMap<number, ZWaveNode>;
/** A dictionary of the nodes connected to this controller */
public get nodes(): ReadonlyThrowingMap<number, ZWaveNode> {
Expand Down Expand Up @@ -883,6 +889,29 @@ export class ZWaveController extends TypedEventEmitter<ControllerEventCallbacks>
);
}

// Also query the controller's current RF region if possible
if (
this.isSerialAPISetupCommandSupported(
SerialAPISetupCommand.GetRFRegion,
)
) {
this.driver.controllerLog.print(`Querying configured RF region...`);
const resp = await this.getRFRegion().catch(() => undefined);
if (resp != undefined) {
this.driver.controllerLog.print(
`The controller is using RF region ${getEnumMemberName(
RFRegion,
resp,
)}`,
);
} else {
this.driver.controllerLog.print(
`Querying the RF region failed!`,
"warn",
);
}
}

// find the SUC
this.driver.controllerLog.print(`finding SUC...`);
const suc = await this.driver.sendMessage<GetSUCNodeIdResponse>(
Expand Down Expand Up @@ -3698,6 +3727,7 @@ ${associatedNodes.join(", ")}`,
}

if (result.success) await this.driver.trySoftReset();
this._rfRegion = region;
return result.success;
}

Expand All @@ -3713,6 +3743,7 @@ ${associatedNodes.join(", ")}`,
ZWaveErrorCodes.Driver_NotSupported,
);
}
this._rfRegion = result.region;
return result.region;
}

Expand Down
41 changes: 2 additions & 39 deletions test/run.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { wait } from "alcalzone-shared/async";
import fs from "fs/promises";
import os from "os";
import path from "path";
import "reflect-metadata";
import { Driver, extractFirmware, guessFirmwareFileFormat } from "zwave-js";
import { Driver } from "zwave-js";

process.on("unhandledRejection", (_r) => {
debugger;
Expand Down Expand Up @@ -38,42 +36,7 @@ const driver = new Driver(port, {
})
.on("error", console.error)
.once("driver ready", async () => {
const node = driver.controller.nodes.getOrThrow(8);
node.once("ready", async () => {
await wait(2000);
const fwFilename =
"/home/dominic/Repositories/zwavejs2mqtt/MultiSensor_OTA_Security_ZW050x_EU_A_V1_13_hex__TargetZwave__.bin";
const fwData = await fs.readFile(fwFilename);
const format = guessFirmwareFileFormat(
path.basename(fwFilename),
fwData,
);
const fw = extractFirmware(fwData, format);

await node.beginFirmwareUpdate(fw.data, fw.firmwareTarget);
await wait(2000);
await node.abortFirmwareUpdate();
});
// setTimeout(
// () => driver.controller.nodes.getOrThrow(2).refreshInfo(),
// 2500,
// );
// Test code
// await wait(1000);
// const updates = await driver.controller.getAvailableFirmwareUpdates(10);
// console.log("Found updates:");
// console.dir(updates, { depth: Infinity });
// await wait(1000);
// try {
// console.log(`Installing update ${updates[0].version}...`);
// await wait(1000);
// await driver.controller.beginOTAFirmwareUpdate(
// 2,
// updates[0].files[0],
// );
// } catch (e) {
// console.error(e);
// }
// Test code goes here
});
void driver.start();
// driver.enableStatistics({
Expand Down