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.

How it works: Clients can send JSON-formatted commands via WebSocket, specifying whether they want to simulate, create, or delete entities. The flow identifies the command and triggers the corresponding process.

Entity Simulation

Purpose: To simulate sensor data for entities and send this data to the Orion Context Broker.

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.

How it works: When the simulation command is triggered, the flow constructs a simulated entity by generating random values for each sensor attribute. This data is sent to Orion, and the result (success or error) is processed for display to the client.

Entity Creation

Purpose: To create a new entity in the Orion Context Broker based on client requests.

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.

How it works: When a create command is received, the flow constructs the entity’s body and sends it to the Orion Context Broker for creation. The client receives real-time feedback about whether the creation was successful.

Entity Deletion

Purpose: To delete an existing entity from the Orion Context Broker.

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.

How it works: When a delete command is received, the flow constructs a DELETE request to remove the specified entity from the Orion Context Broker. The outcome is logged and sent back to the client.

Entity Updates

Purpose: To periodically update the simulated sensor data for entities in the Orion Context Broker.

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.

How it works: The flow stores the entities requiring periodic updates and triggers the generation of new sensor values at regular intervals. The updated data is sent to the Orion Context Broker to keep the simulated sensor data fresh.

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.

How it works: All operations (create, simulate, delete) include error handling mechanisms to check the success of the operation. Clients receive instant feedback through WebSocket, ensuring real-time updates.