Agent Communication Flow With Node-RED

Overview

This flow enables communication between MQTT brokers, external APIs, and the Orion Context Broker for handling sensor data, user data retrieval, and entity management. It processes MQTT messages, API requests, and HTTP requests, allowing real-time data transmission and interaction with external services, including the Orion Context Broker for managing entities.

MQTT Data Handling

Purpose: To receive data from an MQTT broker, process it, and send it to another topic or use it for internal operations.

Nodes and Logic:

MQTT In Node: Subscribes to the MQTT topic /test with Quality of Service (QoS) 2, receiving data published on that topic.
Debug Node: Displays the received data from the MQTT broker in the Node-RED debug sidebar for real-time monitoring.
Function Node (Generate Sensor Data): Generates random sensor data and structures it into a payload with a list of sample user information. This data mimics sensor inputs, which are then sent to the MQTT output.
MQTT Out Node: Publishes the generated data back to the MQTT topic /test for other clients to receive.

How it works: This part of the flow listens to the MQTT topic /test, processes the incoming messages, and can generate and publish random sensor data to the same topic. This allows simulating a continuous sensor data stream.

HTTP Requests for Entity Management

Purpose: To interact with the Orion Context Broker by sending HTTP requests for creating, updating, and retrieving entities.

Nodes and Logic:

HTTP Request Node (GET entity): Sends a GET request to the Orion Context Broker at http://localhost:1026/v2/entities/device1 to retrieve the entity data of device1.
Debug Node: Logs the retrieved entity data for monitoring purposes.
Function Node (Log Response): This function logs the entity's data to the console. It also handles empty responses and prints a message if no entities are found.
HTTP Request Node (POST new entity): Sends a POST request to create a new entity in the Orion Context Broker at http://localhost:1026/v2/entities. The payload contains details about a room entity, including temperature and pressure attributes.
Function Node (Handle Update): This function checks if an entity already exists. If a conflict or duplication is detected (status code 422 or 409), the entity is updated instead of created, modifying only the attributes.

How it works: The flow can create and retrieve entities in the Orion Context Broker. It checks if an entity exists before sending update requests. This ensures seamless integration with the context broker by maintaining entities' data in real time.

External API Integration

Purpose: To retrieve and process data from external APIs, format it, and use it within the flow.

Nodes and Logic:

HTTP Request Node (GET user data): Sends a GET request to an external API (https://jsonplaceholder.typicode.com/users) to retrieve a list of user data.
Function Node (Process API data): Processes the retrieved user data by flattening nested objects and sanitizing inputs to avoid issues like HTML injection. The processed data is prepared in a format that is compatible with the Orion Context Broker.
Function Node (Send entities sequentially): This node sends each processed user entity to the Orion Context Broker sequentially, ensuring no overlap and controlling the timing between requests.
HTTP Request Node (POST entities): Sends the processed user data as new entities to the Orion Context Broker at http://localhost:1026/v2/entities.

How it works: The flow retrieves external API data, processes it, and sends the sanitized data as entities to the Orion Context Broker. It supports managing multiple user entities and handles sequential updates.

Sensor Data Simulation

Purpose: To simulate sensor data for devices and transmit the simulated data to the Orion Context Broker.

Nodes and Logic:

Function Node (Simulate Room Data): Creates a simulated entity with attributes like temperature and pressure for a room entity (Room10). The payload is structured to comply with the Orion Context Broker’s requirements.
HTTP Request Node (POST simulated data): Sends the simulated sensor data to the Orion Context Broker using a POST request at http://localhost:1026/v2/entities.

How it works: This section of the flow simulates data for sensor entities (e.g., a room with temperature and pressure attributes) and sends it to the Orion Context Broker for real-time monitoring and updates.

Error Handling and Debugging

Purpose: To provide error detection, handling, and debugging information for various operations within the flow.

Nodes and Logic:

Debug Nodes: Several debug nodes are used to display information about incoming messages, API responses, and HTTP requests to help with real-time monitoring and troubleshooting.
Function Nodes: These nodes handle the logic for error checking, ensuring operations like creating and updating entities are processed correctly and logging errors when they occur.

How it works: Throughout the flow, debug nodes and function nodes are used to provide feedback on the success or failure of various operations. These nodes help ensure that errors are caught early and logged for further analysis.