跳到主要内容

Node Store

The NodeStore is a reactive store that manages device (node) operations, provisioning, and node-related data. It provides a simplified interface for device management while automatically synchronizing with the backend.

Overview

The NodeStore provides:

  • Node list management and synchronization
  • Node operations and updates
  • Node sharing request management
  • Transport configuration
  • Automatic state synchronization
  • Reactive state management

Accessing the Store

Access the NodeStore through the CDF instance:

const nodeStore = espCDF.nodeStore;

Properties

PropertyTypeDescription
nodeListESPRMNode[] (computed)A computed array of all nodes in the store. This is automatically updated when nodes are added or removed.
nodesByID{ [key: string]: ESPRMNode }A dictionary of nodes indexed by their node ID for quick lookup.
issuedSharingRequests{ [key: string]: ESPNodeSharingRequest[] } (computed)Sharing requests that the current user has issued to other users, grouped by status.
receivedSharingRequests{ [key: string]: ESPNodeSharingRequest[] } (computed)Sharing requests that the current user has received from other users, grouped by status.
hasNextbooleanIndicates if there are more nodes to fetch (pagination).
hasNextIssuedSharingRequestsbooleanIndicates if there are more issued sharing requests to fetch.
hasNextReceivedSharingRequestsbooleanIndicates if there are more received sharing requests to fetch.

Example Usage

// Get all nodes
const nodes = nodeStore.nodeList;
console.log("Total nodes:", nodes.length);
nodes.forEach(node => {
console.log("Node:", node.name, node.id);
});

// Get node by ID
const node = nodeStore.nodesByID["node123"];
if (node) {
console.log("Node found:", node.name);
}

// Get sharing requests
const issuedRequests = nodeStore.issuedSharingRequests;
console.log("Pending requests:", issuedRequests.pending);
console.log("Accepted requests:", issuedRequests.accepted);

const receivedRequests = nodeStore.receivedSharingRequests;
console.log("Pending requests:", receivedRequests.pending);

// Check for pagination
if (nodeStore.hasNext) {
await nodeStore.fetchNext();
}

if (nodeStore.hasNextIssuedSharingRequests) {
await nodeStore.fetchNextIssuedSharingRequest();
}

if (nodeStore.hasNextReceivedSharingRequests) {
await nodeStore.fetchNextReceivedSharingRequest();
}

NodeStore Methods

Sync Node List

To fetch the list of nodes from the cloud and update the store, use the syncNodeList() method. This is automatically called when autoSync is enabled during login.

try {
await nodeStore.syncNodeList();
console.log("Nodes synced:", nodeStore.nodeList);
} catch (error) {
console.error("Error syncing nodes:", error);
}

Set Node List

To set the node list in the store, use the setNodeList() method. Each node is made observable and interceptors are set up.

/*
- nodeList: Array of nodes to set
*/

try {
const response = await userStore.user.getUserNodes();
nodeStore.setNodeList(response.nodes);
console.log("Node list set successfully");
} catch (error) {
console.error("Error setting node list:", error);
}

Add Node

To add a single node to the store and make it observable, use the addNode() method.

/*
- node: The node to add
*/

try {
const newNode = await userStore.user.getNodeDetails("node123");
const observableNode = nodeStore.addNode(newNode);
console.log("Node added:", observableNode);
} catch (error) {
console.error("Error adding node:", error);
}

Update Node

To update a specific node's properties based on the update type, use the updateNode() method.

/*
- nodeId: The ID of the node to update
- update: The update data
- type: The type of update (CONNECTIVITY_STATUS, DEVICE_PARAMS, NODE_CONFIG)
*/

// Update connectivity status
nodeStore.updateNode("node123", { isConnected: true }, NodeUpdateType.CONNECTIVITY_STATUS);

// Update device parameters
nodeStore.updateNode("node123", {
light: { power: true, brightness: 80 }
}, NodeUpdateType.DEVICE_PARAMS);

// Update node config
nodeStore.updateNode("node123", nodeConfig, NodeUpdateType.NODE_CONFIG);

Set Node by ID

To set a node in the store by its ID, use the setNodeByID() method.

/*
- id: The node ID
- node: The node object
*/

nodeStore.setNodeByID("node123", node);

Update Transport Order

To update the transport order for a specific node, use the updateTransportByID() method.

/*
- nodeId: The ID of the node
- order: Array of transport modes in priority order
*/

nodeStore.updateTransportByID("node123", ['local', 'cloud']);

Update Node Transport

To add or remove an available transport for a node, use the updateNodeTransport() method.

/*
- nodeId: The ID of the node
- transportDetails: Transport configuration
- operation: Operation to perform ("add" or "remove")
*/

// Add transport
nodeStore.updateNodeTransport("node123", {
type: ESPTransportMode.local,
host: "192.168.1.100",
port: 80
}, "add");

// Remove transport
nodeStore.updateNodeTransport("node123", {
type: ESPTransportMode.local
}, "remove");

Expand Nodes

To retrieve nodes by their IDs from the store, use the expand() method.

/*
- nodeIds: Array of node IDs
*/

const nodes = nodeStore.expand(["node1", "node2", "node3"]);
console.log("Expanded nodes:", nodes);

Update Multiple Nodes

To update multiple nodes in a single operation, use the updateMultipleNodes() method.

/*
- updates: Array of partial node updates
*/

nodeStore.updateMultipleNodes([
{ id: "node1", name: "Updated Name 1" },
{ id: "node2", name: "Updated Name 2" }
]);

Delete Nodes

To remove multiple nodes from the store by their IDs, use the deleteNodes() method.

/*
- ids: Array of node IDs to delete
*/

try {
await nodeStore.deleteNodes(["node1", "node2"]);
console.log("Nodes deleted successfully");
} catch (error) {
console.error("Error deleting nodes:", error);
}

Fetch Next Page

To fetch the next page of nodes when pagination is available, use the fetchNext() method.

if (nodeStore.hasNext) {
try {
const response = await nodeStore.fetchNext();
console.log("Next nodes:", response.nodes);
} catch (error) {
console.error("Error fetching next page:", error);
}
}

Fetch Next Issued Sharing Request

To fetch the next page of issued sharing requests, use the fetchNextIssuedSharingRequest() method.

if (nodeStore.hasNextIssuedSharingRequests) {
try {
const response = await nodeStore.fetchNextIssuedSharingRequest();
console.log("Next requests:", response.sharedRequests);
} catch (error) {
console.error("Error fetching issued requests:", error);
}
}

Fetch Next Received Sharing Request

To fetch the next page of received sharing requests, use the fetchNextReceivedSharingRequest() method.

if (nodeStore.hasNextReceivedSharingRequests) {
try {
const response = await nodeStore.fetchNextReceivedSharingRequest();
console.log("Next requests:", response.sharedRequests);
} catch (error) {
console.error("Error fetching received requests:", error);
}
}

Clear Store

To clear all nodes and sharing requests from the store, use the clear() method.

nodeStore.clear();

Add Custom Property

To dynamically add an observable property to the store with getter and setter methods, use the addProperty() method.

/*
- propertyName: Name of the property to add
- initialValue: Initial value for the property
*/

nodeStore.addProperty("customData", {});
// Creates: customData, getCustomData(), setCustomData(value)

SDK Methods Available via Node Instance

Each node in the NodeStore is an ESPRMNode instance. These methods are available directly on node objects. For detailed documentation, refer to the ESPRMNode API Reference.

Node Configuration Methods

Device Control Methods

Node Management Methods

Automation Methods

Device Provisioning

Device provisioning is handled through the base SDK's provisioning manager. The NodeStore automatically updates when devices are provisioned. For detailed provisioning documentation, see Device Provisioning.

Example: Provisioning Flow

// 1. Search for devices
const provManager = ESPRMBase.getProvisioningManager();
const devices = await provManager.searchESPDevices(devicePrefix, ESPTransport.ble);

// 2. Create ESP device
const device = await provManager.createESPDevice(name, transport, securityValue, pop);

// 3. Connect and provision
// ... provisioning steps ...

// 4. After provisioning, nodes are automatically synced if autoSync is enabled
// Or manually sync:
await nodeStore.syncNodeList();

Accessing Node Properties

Once you have a node from the store, you can access its properties:

const node = nodeStore.nodesByID["node123"];

// Node properties
console.log("Node ID:", node.id);
console.log("Node Name:", node.name);
console.log("Transport Order:", node.transportOrder);
console.log("Available Transports:", node.availableTransports);

// Node configuration
const config = await node.getNodeConfig();
console.log("Devices:", config.devices);

// Connectivity status
const status = await node.getConnectivityStatus();
console.log("Is Connected:", status.isConnected);

Reactive Updates

The NodeStore is reactive, so you can observe changes:

import { useEffect } from 'react';

useEffect(() => {
console.log("Node list updated:", nodeStore.nodeList);
}, [nodeStore.nodeList]);

useEffect(() => {
const node = nodeStore.nodesByID["node123"];
if (node) {
console.log("Node updated:", node);
}
}, [nodeStore.nodesByID["node123"]]);

Automatic Store Synchronization

When autoSync is enabled in CDF configuration, the NodeStore automatically synchronizes when a user logs in. This happens in the UserStore.setUserInstance() method.

Sharing Requests

The NodeStore manages node sharing requests. Sharing requests are automatically processed and made observable:

// Access sharing requests
const issuedRequests = nodeStore.issuedSharingRequests;
const receivedRequests = nodeStore.receivedSharingRequests;

// Accept a received request
const request = receivedRequests.pending[0];
if (request) {
await request.accept();
}

// Decline a received request
await request.decline();

// Remove an issued request
await request.remove();

For detailed information about node sharing, see Node Sharing documentation.

Additional Resources