|
Sparkplug B C++ Library 1.0.0
Modern C++-23 implementation of Eclipse Sparkplug B 2.2 specification
|
Sparkplug B Edge Node implementing the complete message lifecycle. More...
#include <edge_node.hpp>
Classes | |
| struct | Config |
| Configuration parameters for the Sparkplug B Edge Node. More... | |
| struct | TlsOptions |
| TLS/SSL configuration options for secure MQTT connections. More... | |
Public Member Functions | |
| EdgeNode (Config config) | |
| Constructs an EdgeNode with the given configuration. | |
| ~EdgeNode () | |
| Destroys the EdgeNode and cleans up MQTT resources. | |
| EdgeNode (const EdgeNode &)=delete | |
| EdgeNode & | operator= (const EdgeNode &)=delete |
| EdgeNode (EdgeNode &&) noexcept | |
| EdgeNode & | operator= (EdgeNode &&) 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_log_callback (std::optional< LogCallback > callback) |
| stdx::expected< void, std::string > | connect () |
| Connects to the MQTT broker and establishes a Sparkplug B session. | |
| stdx::expected< void, std::string > | disconnect () |
| Gracefully disconnects from the MQTT broker. | |
| stdx::expected< void, std::string > | publish_birth (PayloadBuilder &payload) |
| Publishes an NBIRTH (Node Birth) message. | |
| stdx::expected< void, std::string > | publish_data (PayloadBuilder &payload) |
| Publishes an NDATA (Node Data) message. | |
| stdx::expected< void, std::string > | publish_death () |
| Publishes an NDEATH (Node Death) message. | |
| stdx::expected< void, std::string > | rebirth () |
| Triggers a rebirth by publishing a new NBIRTH with incremented bdSeq. | |
| uint64_t | get_seq () const |
| Gets the current message sequence number. | |
| uint64_t | get_bd_seq () const |
| Gets the current birth/death sequence number. | |
| bool | is_primary_host_online () const |
| Checks if the primary host application is online. | |
| stdx::expected< void, std::string > | publish_device_birth (std::string_view device_id, PayloadBuilder &payload) |
| Publishes a DBIRTH (Device Birth) message. | |
| stdx::expected< void, std::string > | publish_device_data (std::string_view device_id, PayloadBuilder &payload) |
| Publishes a DDATA (Device Data) message. | |
| stdx::expected< void, std::string > | publish_device_death (std::string_view device_id) |
| Publishes a DDEATH (Device Death) message. | |
| stdx::expected< void, std::string > | publish_node_command (std::string_view target_edge_node_id, PayloadBuilder &payload) |
| Publishes an NCMD (Node Command) message to another edge node. | |
| stdx::expected< void, std::string > | publish_device_command (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 another edge node. | |
| void | log (LogLevel level, std::string_view message) const noexcept |
Sparkplug B Edge Node implementing the complete message lifecycle.
The EdgeNode class manages the full Sparkplug B protocol for an edge node:
Definition at line 96 of file edge_node.hpp.
| sparkplug::EdgeNode::EdgeNode | ( | Config | config | ) |
| stdx::expected< void, std::string > sparkplug::EdgeNode::connect | ( | ) |
Connects to the MQTT broker and establishes a Sparkplug B session.
Sets the NDEATH message as the MQTT Last Will Testament before connecting. The NDEATH will be sent automatically if the connection is lost unexpectedly.
| stdx::expected< void, std::string > sparkplug::EdgeNode::disconnect | ( | ) |
Gracefully disconnects from the MQTT broker.
Sends NDEATH via MQTT Last Will Testament and closes the connection.
|
inline |
Gets the current birth/death sequence number.
Definition at line 298 of file edge_node.hpp.
|
inline |
Gets the current message sequence number.
Definition at line 286 of file edge_node.hpp.
|
inline |
Checks if the primary host application is online.
Definition at line 312 of file edge_node.hpp.
| stdx::expected< void, std::string > sparkplug::EdgeNode::publish_birth | ( | PayloadBuilder & | payload | ) |
Publishes an NBIRTH (Node Birth) message.
The NBIRTH message must be the first message published after connect(). It establishes the session and declares all available metrics with their aliases.
| payload | PayloadBuilder containing metrics with names and aliases |
| stdx::expected< void, std::string > sparkplug::EdgeNode::publish_data | ( | PayloadBuilder & | payload | ) |
Publishes an NDATA (Node Data) message.
NDATA messages are used for Report by Exception updates. Your application is responsible for determining WHEN to call this method based on whether metrics have changed according to your domain-specific criteria (e.g., deadband thresholds, exact equality, percentage change). The payload should use aliases for bandwidth efficiency (60-80% reduction vs. full names).
| payload | PayloadBuilder containing changed metrics (by alias only) |
| stdx::expected< void, std::string > sparkplug::EdgeNode::publish_death | ( | ) |
Publishes an NDEATH (Node Death) message.
Explicitly sends the NDEATH message. Usually not needed as NDEATH is sent automatically via MQTT Last Will Testament on disconnect or connection loss.
| stdx::expected< void, std::string > sparkplug::EdgeNode::publish_device_birth | ( | std::string_view | device_id, |
| PayloadBuilder & | payload | ||
| ) |
Publishes a DBIRTH (Device Birth) message.
The DBIRTH message declares a device attached to this edge node. It must be published after NBIRTH and declares all device metrics with aliases.
| device_id | The device identifier (e.g., "Sensor01", "Motor02") |
| payload | PayloadBuilder containing device metrics with names and aliases |
| stdx::expected< void, std::string > sparkplug::EdgeNode::publish_device_command | ( | 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 another edge node.
DCMD messages are commands sent to devices attached to edge nodes.
| 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::EdgeNode::publish_device_data | ( | std::string_view | device_id, |
| PayloadBuilder & | payload | ||
| ) |
Publishes a DDATA (Device Data) message.
DDATA messages are used for Report by Exception updates. Your application is responsible for determining WHEN to call this method based on whether device metrics have changed according to your domain-specific criteria. The payload should use aliases for bandwidth efficiency.
| device_id | The device identifier |
| payload | PayloadBuilder containing changed metrics (by alias only) |
| stdx::expected< void, std::string > sparkplug::EdgeNode::publish_device_death | ( | std::string_view | device_id | ) |
Publishes a DDEATH (Device Death) message.
Explicitly sends a device death message to indicate device disconnection.
| device_id | The device identifier |
| stdx::expected< void, std::string > sparkplug::EdgeNode::publish_node_command | ( | std::string_view | target_edge_node_id, |
| PayloadBuilder & | payload | ||
| ) |
Publishes an NCMD (Node Command) message to another edge node.
NCMD messages are commands sent from SCADA/Primary Applications or other edge nodes to request actions like rebirth, reboot, or custom operations.
| 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::EdgeNode::rebirth | ( | ) |
Triggers a rebirth by publishing a new NBIRTH with incremented bdSeq.
Rebirth is used when:
| void sparkplug::EdgeNode::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::EdgeNode::set_tls | ( | std::optional< TlsOptions > | tls | ) |
Configures TLS/SSL options for secure MQTT connections.
| tls | TLS configuration options |