Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed Nov 6, 2023
1 parent 041960f commit d3423c8
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 195 deletions.
15 changes: 8 additions & 7 deletions packages/matter-node-ble.js/src/ble/BleScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ type CommissionableDeviceData = CommissionableDevice & {
export class BleScanner implements Scanner {
private readonly recordWaiters = new Map<
string,
{ resolver: () => void; timer: Timer; resolveOnUpdatedRecords: boolean }
{
resolver: () => void;
timer: Timer;
resolveOnUpdatedRecords: boolean;
}
>();
private readonly discoveredMatterDevices = new Map<string, DiscoveredBleDevice>();

Expand Down Expand Up @@ -65,7 +69,7 @@ export class BleScanner implements Scanner {
resolveOnUpdatedRecords ? "" : " (not resolving on updated records)"
}`,
);
return { promise };
await promise;
}

/**
Expand Down Expand Up @@ -221,11 +225,9 @@ export class BleScanner implements Scanner {
let storedRecords = this.getCommissionableDevices(identifier);
if (storedRecords.length === 0) {
const queryKey = this.buildCommissionableQueryIdentifier(identifier);
const { promise } = await this.registerWaiterPromise(queryKey, timeoutSeconds);

await this.nobleClient.startScanning();

await promise;
await this.registerWaiterPromise(queryKey, timeoutSeconds);

storedRecords = this.getCommissionableDevices(identifier);
await this.nobleClient.stopScanning();
Expand Down Expand Up @@ -257,8 +259,7 @@ export class BleScanner implements Scanner {
if (remainingTime <= 0) {
break;
}
const { promise } = await this.registerWaiterPromise(queryKey, remainingTime, false);
await promise;
await this.registerWaiterPromise(queryKey, remainingTime, false);
}
await this.nobleClient.stopScanning();
return this.getCommissionableDevices(identifier).map(({ deviceData }) => deviceData);
Expand Down
50 changes: 2 additions & 48 deletions packages/matter-node-shell.js/src/shell/cmd_commission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import { NodeCommissioningOptions } from "@project-chip/matter-node.js";
import { BasicInformationCluster, DescriptorCluster, GeneralCommissioning } from "@project-chip/matter-node.js/cluster";
import { NodeId } from "@project-chip/matter-node.js/datatype";
import { NodeStateInformation } from "@project-chip/matter-node.js/device";
import { Logger } from "@project-chip/matter-node.js/log";
import { ManualPairingCodeCodec, QrCode } from "@project-chip/matter-node.js/schema";
import type { Argv } from "yargs";
import { MatterNode } from "../MatterNode";
import { createDiagnosticCallbacks } from "./cmd_nodes";

export default function commands(theNode: MatterNode) {
return {
Expand Down Expand Up @@ -92,53 +92,7 @@ export default function commands(theNode: MatterNode) {
},
},
passcode: setupPinCode,
attributeChangedCallback: (
peerNodeId,
{ path: { nodeId, clusterId, endpointId, attributeName }, value },
) =>
console.log(
`attributeChangedCallback ${peerNodeId} Attribute ${nodeId}/${endpointId}/${clusterId}/${attributeName} changed to ${Logger.toJSON(
value,
)}`,
),
eventTriggeredCallback: (
peerNodeId,
{ path: { nodeId, clusterId, endpointId, eventName }, events },
) =>
console.log(
`eventTriggeredCallback ${peerNodeId} Event ${nodeId}/${endpointId}/${clusterId}/${eventName} triggered with ${Logger.toJSON(
events,
)}`,
),
stateInformationCallback: (peerNodeId, info) => {
switch (info) {
case NodeStateInformation.Connected:
console.log(
`stateInformationCallback: Node ${peerNodeId} connected`,
);
break;
case NodeStateInformation.Disconnected:
console.log(
`stateInformationCallback: Node ${peerNodeId} disconnected`,
);
break;
case NodeStateInformation.Reconnecting:
console.log(
`stateInformationCallback: Node ${peerNodeId} reconnecting`,
);
break;
case NodeStateInformation.WaitingForDeviceDiscovery:
console.log(
`stateInformationCallback: Node ${peerNodeId} waiting that device gets discovered again`,
);
break;
case NodeStateInformation.StructureChanged:
console.log(
`stateInformationCallback: Node ${peerNodeId} structure changed`,
);
break;
}
},
...createDiagnosticCallbacks(),
} as NodeCommissioningOptions;

options.commissioning = {
Expand Down
82 changes: 40 additions & 42 deletions packages/matter-node-shell.js/src/shell/cmd_nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,49 @@
*/

import { NodeId } from "@project-chip/matter-node.js/datatype";
import { NodeStateInformation } from "@project-chip/matter-node.js/device";
import { CommissioningControllerNodeOptions, NodeStateInformation } from "@project-chip/matter-node.js/device";
import { Logger } from "@project-chip/matter-node.js/log";
import type { Argv } from "yargs";
import { MatterNode } from "../MatterNode";

export function createDiagnosticCallbacks(): Partial<CommissioningControllerNodeOptions> {
return {
attributeChangedCallback: (peerNodeId, { path: { nodeId, clusterId, endpointId, attributeName }, value }) =>
console.log(
`attributeChangedCallback ${peerNodeId}: Attribute ${nodeId}/${endpointId}/${clusterId}/${attributeName} changed to ${Logger.toJSON(
value,
)}`,
),
eventTriggeredCallback: (peerNodeId, { path: { nodeId, clusterId, endpointId, eventName }, events }) =>
console.log(
`eventTriggeredCallback ${peerNodeId}: Event ${nodeId}/${endpointId}/${clusterId}/${eventName} triggered with ${Logger.toJSON(
events,
)}`,
),
stateInformationCallback: (peerNodeId, info) => {
switch (info) {
case NodeStateInformation.Connected:
console.log(`stateInformationCallback Node ${peerNodeId} connected`);
break;
case NodeStateInformation.Disconnected:
console.log(`stateInformationCallback Node ${peerNodeId} disconnected`);
break;
case NodeStateInformation.Reconnecting:
console.log(`stateInformationCallback Node ${peerNodeId} reconnecting`);
break;
case NodeStateInformation.WaitingForDeviceDiscovery:
console.log(
`stateInformationCallback Node ${peerNodeId} waiting that device gets discovered again`,
);
break;
case NodeStateInformation.StructureChanged:
console.log(`stateInformationCallback Node ${peerNodeId} structure changed`);
break;
}
},
};
}

export default function commands(theNode: MatterNode) {
return {
command: ["nodes", "node"],
Expand Down Expand Up @@ -114,47 +152,7 @@ export default function commands(theNode: MatterNode) {
subscribeMaxIntervalCeilingSeconds: autoSubscribe
? maxSubscriptionInterval ?? 30
: undefined,
attributeChangedCallback: (
peerNodeId,
{ path: { nodeId, clusterId, endpointId, attributeName }, value },
) =>
console.log(
`attributeChangedCallback ${peerNodeId}: Attribute ${nodeId}/${endpointId}/${clusterId}/${attributeName} changed to ${Logger.toJSON(
value,
)}`,
),
eventTriggeredCallback: (
peerNodeId,
{ path: { nodeId, clusterId, endpointId, eventName }, events },
) =>
console.log(
`eventTriggeredCallback ${peerNodeId}: Event ${nodeId}/${endpointId}/${clusterId}/${eventName} triggered with ${Logger.toJSON(
events,
)}`,
),
stateInformationCallback: (peerNodeId, info) => {
switch (info) {
case NodeStateInformation.Connected:
console.log(`stateInformationCallback Node ${peerNodeId} connected`);
break;
case NodeStateInformation.Disconnected:
console.log(`stateInformationCallback Node ${peerNodeId} disconnected`);
break;
case NodeStateInformation.Reconnecting:
console.log(`stateInformationCallback Node ${peerNodeId} reconnecting`);
break;
case NodeStateInformation.WaitingForDeviceDiscovery:
console.log(
`stateInformationCallback Node ${peerNodeId} waiting that device gets discovered again`,
);
break;
case NodeStateInformation.StructureChanged:
console.log(
`stateInformationCallback Node ${peerNodeId} structure changed`,
);
break;
}
},
...createDiagnosticCallbacks(),
});
}
},
Expand Down
32 changes: 18 additions & 14 deletions packages/matter.js/src/CommissioningController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,23 @@ export type NodeCommissioningOptions = CommissioningControllerNodeOptions & {
commissioning?: CommissioningOptions;

/** Discovery related options. */
discovery: {
/**
* Device identifiers (Short or Long Discriminator, Product/Vendor-Ids, Device-type or a pre-discovered
* instance Id, or "nothing" to discover all commissionable matter devices) to use for discovery.
*/
identifierData: CommissionableDeviceIdentifiers;

/**
* Commissionable device object returned by a discovery run.
* If this property is provided then identifierData and knownAddress are ignored.
*/
commissionableDevice?: CommissionableDevice;

discovery: (
| {
/**
* Device identifiers (Short or Long Discriminator, Product/Vendor-Ids, Device-type or a pre-discovered
* instance Id, or "nothing" to discover all commissionable matter devices) to use for discovery.
* If the property commissionableDevice is provided this property is ignored.
*/
identifierData: CommissionableDeviceIdentifiers;
}
| {
/**
* Commissionable device object returned by a discovery run.
* If this property is provided then identifierData and knownAddress are ignored.
*/
commissionableDevice: CommissionableDevice;
}
) & {
/**
* Discovery capabilities to use for discovery. These are included in the QR code normally and defined if BLE
* is supported for initial commissioning.
Expand Down Expand Up @@ -248,7 +252,7 @@ export class CommissioningController extends MatterNode {

const existingNode = this.connectedNodes.get(nodeId);
if (existingNode !== undefined) {
if (!existingNode.isConnencted) {
if (!existingNode.isConnected) {
await existingNode.reconnect();
}
return existingNode;
Expand Down
Loading

0 comments on commit d3423c8

Please sign in to comment.