Example Flow With Node-Red

Metadata Query Flow

Purpose:

This flow retrieves and processes metadata from the Orion Context Broker. It focuses on fetching the version, entities, and subscription data from the broker.

Nodes and Logic:

  • Link In (queries_flow): Triggers the metadata request from another flow.

  • Change Node: Prepares the payload for the query based on the incoming message.

  • Switch Node: Routes the payload based on the type of metadata requested. It has three outputs:

    1. Output 1: If the payload equals version, it sends a GET request to http://orion:1026/version.

    2. Output 2: If the payload equals entities, it sends a GET request to http://orion:1026/v2/entities.

    3. Output 3: If the payload equals subscribers, it sends a GET request to http://orion:1026/v2/subscriptions.

  • HTTP Request Nodes: These nodes send the respective GET requests to the Orion broker.

    1. Output 1: The broker version is fetched.

    2. Output 2: A list of entities is retrieved.

    3. Output 3: Active subscriptions are fetched.

  • Change Nodes: Process the responses:

    1. They temporarily save the HTTP response (metadata, entities, or subscriptions) in msg.save_payload.

    2. The response is then formatted into a WebSocket-friendly message using flags (metadata-version, metadata-entities, metadata-subscriptions). WebSocket Out Node: Sends the formatted metadata to the WebSocket client.

How it works:

This flow listens for metadata requests through a Link In node and sends queries based on the type of metadata requested (version, entities, or subscriptions). It processes the Orion responses, organizes them into a structured format, and sends them to a WebSocket for real-time updates.

Entity Management Flow

Purpose:

This flow handles entity creation, simulation of sensor data, and deletion. It automates interaction with Orion for managing entities.

Nodes and Logic:

  • Entity Creation:

    1. Inject Node: Manually triggers entity creation for testing.

    2. Function Node (request body for entity create): Formats the message to create an entity. It generates random values for each entity attribute, such as the location or other attributes specified by the flow. It uses the rangeMax and rangeMin to calculate a random value within that range for attributes.

    3. HTTP Request (POST to /v2/entities): The generated entity is posted to Orion at http://orion:1026/v2/entities.

  • Entity Deletion:

    1. Function Node: Constructs the DELETE request to remove an entity by appending its ID to the URL.

    2. HTTP Request (DELETE to /v2/entities/): This node sends a DELETE request to Orion to remove the specified entity.

  • Sensor Simulation:

    1. Function Node (function 57): This node simulates sensor data for all active entities. It pulls a list of entities from the context storage (context.get('objects')) and updates their attributes with random values. Each entity’s attributes are updated by creating a new value, simulating real-time data.

    2. HTTP Request (POST to /v2/op/update): The updated sensor data is posted back to Orion, simulating a live update of entity attributes.

How it works:

Entities are created and managed dynamically by constructing proper request bodies for POST/DELETE operations using function nodes. A simulated sensor flow generates random data for entity attributes and posts them to Orion, mimicking a real-world scenario where sensors are continuously sending data to the broker.

Subscription Management Flow

Purpose:

This flow manages entity subscriptions in Orion. Subscriptions allow you to receive notifications whenever an entity’s attributes are updated.

Nodes and Logic:

  • Inject Node: Triggers the subscription creation process.

  • Function Node (function 63): Constructs the request body for the subscription, including:

    1. entities: Defines which entities the subscription applies to, using an idPattern (wildcards can be used).

    2. condition: Specifies the attributes to monitor.

    3. notification: Sets up the endpoint that will receive the notifications (in this case, a Node-RED HTTP endpoint or WebSocket).

  • HTTP Request (POST to /v2/subscriptions): Sends the subscription request to Orion. The Orion broker will start monitoring the specified entities and attributes, sending notifications when changes occur.

  • Debug Node: Shows the result of the subscription creation for debugging purposes.

How it works:

The flow listens for requests to create subscriptions and formats the request based on the specified entities, attributes, and the notification URL. The subscription is created using a POST request to Orion, and notifications are sent to the configured endpoint whenever the entity attributes change.

WebSocket Integration

Purpose:

Real-time communication between the flow and a client through WebSockets. The WebSocket listens for updates and sends data back to the connected clients.

Nodes:

WebSocket In/Out Nodes: Handle WebSocket communication between Node-RED and external clients. These nodes are used to push metadata updates (such as version, entities, or subscriptions) to the client in real time.

Key Operations in Flow:

  • Metadata Retrieval: Use GET requests to retrieve version, entity list, or subscription list from Orion.

  • Entity Creation: Constructs and sends POST requests to create new entities in Orion.

  • Sensor Simulation: Generates random values for attributes to simulate sensors.

  • Entity Deletion: Sends DELETE requests to remove entities from Orion.

  • Subscription Management: Sends POST requests to create subscriptions in Orion, triggering notifications when monitored attributes change.

Flow