|
Sparkplug B C++ Library 1.0.0
Modern C++-23 implementation of Eclipse Sparkplug B 2.2 specification
|
Sparkplug B Host Application for SCADA/Primary Applications. More...
#include <host_application.hpp>
Classes | |
| struct | Config |
| Configuration parameters for the Sparkplug B Host Application. More... | |
| struct | DeviceState |
| Tracks the state of a device attached to an edge node. More... | |
| struct | NodeState |
| Tracks the state of an individual edge node. More... | |
| struct | TlsOptions |
| TLS/SSL configuration options for secure MQTT connections. More... | |
| struct | TransparentStringHash |
| Transparent hash for string keys to enable heterogeneous lookup. More... | |
Public Member Functions | |
| HostApplication (Config config) | |
| Constructs a HostApplication with the given configuration. | |
| ~HostApplication () | |
| Destroys the HostApplication and cleans up MQTT resources. | |
| HostApplication (const HostApplication &)=delete | |
| HostApplication & | operator= (const HostApplication &)=delete |
| HostApplication (HostApplication &&) noexcept | |
| HostApplication & | operator= (HostApplication &&) noexcept |
| void | set_credentials (std::optional< std::string > username, std::optional< std::string > password) |
| Sets MQTT username and password for authentication. | |
| void | set_tls (std::optional< TlsOptions > tls) |
| Configures TLS/SSL options for secure MQTT connections. | |
| void | set_message_callback (MessageCallback callback) |
| Sets the message callback for receiving Sparkplug messages. | |
| void | set_log_callback (LogCallback callback) |
| Sets the log callback for receiving library diagnostic messages. | |
| stdx::expected< void, std::string > | connect () |
| Connects to the MQTT broker. | |
| stdx::expected< void, std::string > | disconnect () |
| Gracefully disconnects from the MQTT broker. | |
| stdx::expected< void, std::string > | subscribe_all_groups () |
| Subscribes to all Sparkplug B messages across all groups. | |
| stdx::expected< void, std::string > | subscribe_group (std::string_view group_id) |
| Subscribes to messages from a specific group. | |
| stdx::expected< void, std::string > | subscribe_node (std::string_view group_id, std::string_view edge_node_id) |
| Subscribes to messages from a specific edge node in a group. | |
| stdx::expected< void, std::string > | subscribe_state (std::string_view host_id) |
| Subscribes to STATE messages from another primary application. | |
| std::optional< std::reference_wrapper< const NodeState > > | get_node_state (std::string_view group_id, std::string_view edge_node_id) const |
| Gets the current state of a specific edge node. | |
| std::optional< std::string_view > | get_metric_name (std::string_view group_id, std::string_view edge_node_id, std::string_view device_id, uint64_t alias) const |
| Resolves a metric alias to its name for a specific node or device. | |
| stdx::expected< void, std::string > | publish_state_birth (uint64_t timestamp) |
| Publishes a STATE birth message to indicate Host Application is online. | |
| stdx::expected< void, std::string > | publish_state_death (uint64_t timestamp) |
| Publishes a STATE death message to indicate Host Application is offline. | |
| stdx::expected< void, std::string > | publish_node_command (std::string_view group_id, std::string_view target_edge_node_id, PayloadBuilder &payload) |
| Publishes an NCMD (Node Command) message to an Edge Node. | |
| stdx::expected< void, std::string > | publish_device_command (std::string_view group_id, std::string_view target_edge_node_id, std::string_view target_device_id, PayloadBuilder &payload) |
| Publishes a DCMD (Device Command) message to a device on an Edge Node. | |
| void | log (LogLevel level, std::string_view message) const noexcept |
| Internal logging method accessible from C bindings. | |
Sparkplug B Host Application for SCADA/Primary Applications.
The HostApplication class implements the complete Sparkplug B protocol for Host Applications:
This is the authoritative consumer and command source in a Sparkplug B topology. A Host Application should use a single MQTT client that both receives data and sends commands.
Definition at line 95 of file host_application.hpp.
| sparkplug::HostApplication::HostApplication | ( | Config | config | ) |
Constructs a HostApplication with the given configuration.
| config | HostApplication configuration (moved) |
| stdx::expected< void, std::string > sparkplug::HostApplication::connect | ( | ) |
Connects to the MQTT broker.
Unlike Publisher::connect(), this does NOT automatically publish any messages. You should call publish_state_birth() after connecting to declare the Host App online.
| stdx::expected< void, std::string > sparkplug::HostApplication::disconnect | ( | ) |
Gracefully disconnects from the MQTT broker.
| std::optional< std::string_view > sparkplug::HostApplication::get_metric_name | ( | std::string_view | group_id, |
| std::string_view | edge_node_id, | ||
| std::string_view | device_id, | ||
| uint64_t | alias | ||
| ) | const |
Resolves a metric alias to its name for a specific node or device.
Looks up the metric name that corresponds to the given alias, based on the alias mappings captured from NBIRTH (node metrics) or DBIRTH (device metrics).
| group_id | The group ID |
| edge_node_id | The edge node ID |
| device_id | The device ID (empty string for node-level metrics) |
| alias | The metric alias to resolve |
| std::optional< std::reference_wrapper< const NodeState > > sparkplug::HostApplication::get_node_state | ( | std::string_view | group_id, |
| std::string_view | edge_node_id | ||
| ) | const |
Gets the current state of a specific edge node.
| group_id | The group ID |
| edge_node_id | The edge node ID to query |
|
noexcept |
Internal logging method accessible from C bindings.
| level | Log severity level |
| message | Log message content |
| stdx::expected< void, std::string > sparkplug::HostApplication::publish_device_command | ( | std::string_view | group_id, |
| std::string_view | target_edge_node_id, | ||
| std::string_view | target_device_id, | ||
| PayloadBuilder & | payload | ||
| ) |
Publishes a DCMD (Device Command) message to a device on an Edge Node.
DCMD messages are commands sent from Host Applications to devices attached to Edge Nodes.
| group_id | The Sparkplug group ID containing the target Edge Node |
| target_edge_node_id | The target Edge Node identifier |
| target_device_id | The target device identifier |
| payload | PayloadBuilder containing command metrics |
| stdx::expected< void, std::string > sparkplug::HostApplication::publish_node_command | ( | std::string_view | group_id, |
| std::string_view | target_edge_node_id, | ||
| PayloadBuilder & | payload | ||
| ) |
Publishes an NCMD (Node Command) message to an Edge Node.
NCMD messages are commands sent from Host Applications to Edge Nodes to request actions like rebirth, reboot, or custom operations.
| group_id | The Sparkplug group ID containing the target Edge Node |
| target_edge_node_id | The target Edge Node identifier |
| payload | PayloadBuilder containing command metrics (e.g., "Node Control/Rebirth") |
| stdx::expected< void, std::string > sparkplug::HostApplication::publish_state_birth | ( | uint64_t | timestamp | ) |
Publishes a STATE birth message to indicate Host Application is online.
STATE messages are used by Host Applications (SCADA/Primary Applications) to declare their online status to Edge Nodes. The birth message indicates the Host Application is online and ready to process data.
| timestamp | UTC milliseconds since epoch |
| stdx::expected< void, std::string > sparkplug::HostApplication::publish_state_death | ( | uint64_t | timestamp | ) |
Publishes a STATE death message to indicate Host Application is offline.
STATE messages are used by Host Applications (SCADA/Primary Applications) to declare their online status to Edge Nodes. The death message indicates the Host Application is going offline.
| timestamp | UTC milliseconds since epoch (typically matches birth timestamp) |
| void sparkplug::HostApplication::set_credentials | ( | std::optional< std::string > | username, |
| std::optional< std::string > | password | ||
| ) |
Sets MQTT username and password for authentication.
| username | MQTT username (empty string or std::nullopt to unset) |
| password | MQTT password (empty string or std::nullopt to unset) |
| void sparkplug::HostApplication::set_log_callback | ( | LogCallback | callback | ) |
Sets the log callback for receiving library diagnostic messages.
| callback | Callback function to invoke for log messages |
| void sparkplug::HostApplication::set_message_callback | ( | MessageCallback | callback | ) |
Sets the message callback for receiving Sparkplug messages.
| callback | Callback function to invoke for received messages |
| void sparkplug::HostApplication::set_tls | ( | std::optional< TlsOptions > | tls | ) |
Configures TLS/SSL options for secure MQTT connections.
| tls | TLS configuration options |
| stdx::expected< void, std::string > sparkplug::HostApplication::subscribe_all_groups | ( | ) |
Subscribes to all Sparkplug B messages across all groups.
Subscribes to the wildcard topic: spBv1.0/#
This receives all message types (NBIRTH, NDATA, NDEATH, DBIRTH, DDATA, DDEATH) from all edge nodes in all groups.
| stdx::expected< void, std::string > sparkplug::HostApplication::subscribe_group | ( | std::string_view | group_id | ) |
Subscribes to messages from a specific group.
Subscribes to: spBv1.0/{group_id}/#
| group_id | The group ID to subscribe to |
| stdx::expected< void, std::string > sparkplug::HostApplication::subscribe_node | ( | std::string_view | group_id, |
| std::string_view | edge_node_id | ||
| ) |
Subscribes to messages from a specific edge node in a group.
Subscribes to: spBv1.0/{group_id}/+/{edge_node_id}/#
| group_id | The group ID |
| edge_node_id | The edge node ID to subscribe to |
| stdx::expected< void, std::string > sparkplug::HostApplication::subscribe_state | ( | std::string_view | host_id | ) |
Subscribes to STATE messages from another primary application.
STATE messages indicate whether another SCADA/Primary Application is online. Subscribe to: spBv1.0/STATE/{host_id}
| host_id | The host application identifier |