Sensor Simulation Flow With Node-RED
Overview
This flow simulates entities and handles the creation, deletion, and update of entity data in a context broker using the Orion API. It interacts with WebSocket clients, allowing for real-time updates of entity simulations. The flow includes the logic to simulate sensor data and send the corresponding entities to the Orion Context Broker, while also facilitating entity creation, updates, and deletions based on client requests.
WebSocket Input
Purpose: To handle real-time WebSocket communication from clients. The clients send commands (simulate, create, delete) for managing entities, and the flow processes these commands accordingly.
Nodes and Logic:
WebSocket In Node: Receives WebSocket messages from clients through the endpoint /sensor-sim.
JSON Node: Converts the WebSocket input into a JSON format for easier processing.
Switch Node: This node directs the message flow based on the flag in the WebSocket message (simulate, create, or delete). It routes the messages to different paths depending on the command received: Simulate: Processes entity simulation. Create: Handles new entity creation. Delete: Manages entity deletion requests.
Entity Simulation
Nodes and Logic:
Function Node (construct body for request with simulation): This node constructs the simulated entity by processing the input data, generating random values for sensor attributes, and formatting it as required for the Orion Context Broker. It iterates through entity attributes, generating values based on rangeMin and rangeMax provided in the input.
HTTP Request Node: Sends the simulated entity data to the Orion Context Broker through a POST request at http://localhost:1026/v2/entities.
Switch Node: Verifies the response from the Orion Context Broker. If the payload is empty, the simulation was successful; otherwise, an error occurred.
Link Nodes: These nodes manage the flow’s internal logic for handling simulation success and error scenarios.
Entity Creation
Nodes and Logic:
Function Node (construct body for request without simulation): Prepares the entity's JSON body for creation by removing the flag property and keeping other attributes.
HTTP Request Node: Sends the entity creation request to the Orion Context Broker using the POST method at http://localhost:1026/v2/entities.
Link Nodes: Manage the flow after the entity is created. These nodes handle error checking and success messages.
WebSocket Out Node: Sends feedback (success or error) to the WebSocket client based on the outcome of the entity creation.
Entity Deletion
Nodes and Logic:
HTTP Request Node: Sends a DELETE request to the Orion Context Broker at http://localhost:1026/v2/entities/a to delete the specified entity.
Link Nodes: Manage the error and success cases after attempting to delete an entity.
Debug Node: Logs the result of the deletion attempt for monitoring purposes.
Entity Updates
Nodes and Logic:
Function Node (save and periodically update entities): Manages a list of entities that need periodic updates. It updates each entity's sensor data by generating new random values for attributes with a defined range.
HTTP Request Node: Sends an update request to the Orion Context Broker at http://localhost:1026/v2/op/update with the new sensor values for all entities.
Inject Node: Triggers the periodic update of entities, ensuring the simulated sensor data is refreshed at regular intervals.
Debug Node: Logs the update actions for monitoring.
Error Handling and Real-Time Feedback
Purpose: To provide real-time feedback to the client, indicating the success or failure of entity operations.
Nodes and Logic:
Function Node (check for successful operation): Checks if the operation (create, simulate, delete) was successful and prepares the corresponding success message.
Link Nodes: Handle the flow of success and error messages across different parts of the flow.
WebSocket Out Node: Sends real-time feedback to the WebSocket client, informing them about the success or failure of the requested operation.