Skip to content

Commit

Permalink
Update controller example to use events (#1362)
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 authored Nov 5, 2024
1 parent 2450340 commit 39bacbc
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions packages/examples/src/controller/ControllerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 39bacbc

Please sign in to comment.