From 39bacbc009ff4ba55b8fd7bb3df5012669bac553 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Tue, 5 Nov 2024 15:01:46 +0100 Subject: [PATCH] Update controller example to use events (#1362) --- .../examples/src/controller/ControllerNode.ts | 87 ++++++++++--------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/packages/examples/src/controller/ControllerNode.ts b/packages/examples/src/controller/ControllerNode.ts index 5771bdf71a..70409144d2 100644 --- a/packages/examples/src/controller/ControllerNode.ts +++ b/packages/examples/src/controller/ControllerNode.ts @@ -17,7 +17,7 @@ import { Ble, ClusterClientObj, ControllerCommissioningFlowOptions } from "@matt import { ManualPairingCodeCodec, NodeId } from "@matter/main/types"; import { NodeJsBle } from "@matter/nodejs-ble"; import { CommissioningController, NodeCommissioningOptions } from "@project-chip/matter.js"; -import { NodeStateInformation } from "@project-chip/matter.js/device"; +import { NodeStates } from "@project-chip/matter.js/device"; const logger = Logger.get("Controller"); @@ -181,50 +181,51 @@ class ControllerNode { throw new Error(`Node ${nodeId} not found in commissioned nodes`); } - const node = await commissioningController.connectNode(nodeId, { - 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 ${peerNodeId}: Node ${nodeId} connected`); - break; - case NodeStateInformation.Disconnected: - console.log(`stateInformationCallback ${peerNodeId}: Node ${nodeId} disconnected`); - break; - case NodeStateInformation.Reconnecting: - console.log(`stateInformationCallback ${peerNodeId}: Node ${nodeId} reconnecting`); - break; - case NodeStateInformation.WaitingForDeviceDiscovery: - console.log( - `stateInformationCallback ${peerNodeId}: Node ${nodeId} waiting for device discovery`, - ); - break; - case NodeStateInformation.StructureChanged: - console.log(`stateInformationCallback ${peerNodeId}: Node ${nodeId} structure changed`); - break; - case NodeStateInformation.Decommissioned: - console.log(`stateInformationCallback ${peerNodeId}: Node ${nodeId} decommissioned`); - break; - } - }, + // Trigger node connection. Returns once process started, events are there to wait for completion + // By default will subscript to all attributes and events + const node = await commissioningController.connectNode(nodeId); + + // React on generic events + node.events.attributeChanged.on(({ path: { nodeId, clusterId, endpointId, attributeName }, value }) => + console.log( + `attributeChangedCallback ${nodeId}: Attribute ${endpointId}/${clusterId}/${attributeName} changed to ${Logger.toJSON( + value, + )}`, + ), + ); + node.events.eventTriggered.on(({ path: { nodeId, clusterId, endpointId, eventName }, events }) => + console.log( + `eventTriggeredCallback ${nodeId}: Event ${endpointId}/${clusterId}/${eventName} triggered with ${Logger.toJSON( + events, + )}`, + ), + ); + node.events.stateChanged.on(info => { + switch (info) { + case NodeStates.Connected: + console.log(`state changed: Node ${nodeId} connected`); + break; + case NodeStates.Disconnected: + console.log(`state changed: Node ${nodeId} disconnected`); + break; + case NodeStates.Reconnecting: + console.log(`state changed: Node ${nodeId} reconnecting`); + break; + case NodeStates.WaitingForDeviceDiscovery: + console.log(`state changed: Node ${nodeId} waiting for device discovery`); + break; + } + }); + node.events.structureChanged.on(() => { + console.log(`Node ${nodeId} structure changed`); }); - // Important: This is a temporary API to proof the methods working and this will change soon and is NOT stable! - // It is provided to proof the concept + // Now wait till the structure of the node gor initialized (potentially with persisted data) + await node.events.initialized; + + // Or use this to wait for full remote initialization and reconnection. + // Will only return when node is connected! + // await node.events.initializedFromRemote; node.logStructure();