Astarte device API for C++ 0.8.1
Astarte device SDK for C++
Loading...
Searching...
No Matches
astarte::device::mqtt::DeviceMqtt Class Reference

MQTT implementation of an Astarte device. More...

#include <device_mqtt.hpp>

+ Inheritance diagram for astarte::device::mqtt::DeviceMqtt:

Public Member Functions

 ~DeviceMqtt () override
 Virtual destructor.
 
 DeviceMqtt (DeviceMqtt &other)=delete
 Device is non-copyable.
 
auto operator= (DeviceMqtt &other) -> DeviceMqtt &=delete
 Device is non-copyable.
 
 DeviceMqtt (DeviceMqtt &&other)=default
 Move constructor.
 
auto operator= (DeviceMqtt &&other) -> DeviceMqtt &=default
 Move assignment operator.
 
auto add_interface_from_file (const std::filesystem::path &json_file) -> astarte_tl::expected< void, Error > override
 Adds an interface definition to the device from a JSON file.
 
auto add_interface_from_str (std::string_view json) -> astarte_tl::expected< void, Error > override
 Adds an interface definition to the device from a JSON string.
 
auto remove_interface (const std::string &interface_name) -> astarte_tl::expected< void, Error > override
 Removes an installed interface from the device.
 
auto connect () -> astarte_tl::expected< void, Error > override
 Connects the device to the Astarte platform.
 
auto is_connected () const -> bool override
 Checks connectivity status.
 
auto disconnect () -> astarte_tl::expected< void, Error > override
 Disconnects the device from Astarte.
 
auto send_individual (std::string_view interface_name, std::string_view path, const Data &data, const std::chrono::system_clock::time_point *timestamp) -> astarte_tl::expected< void, Error > override
 Sends an individual data point to an Astarte Interface.
 
auto send_object (std::string_view interface_name, std::string_view path, const DatastreamObject &object, const std::chrono::system_clock::time_point *timestamp) -> astarte_tl::expected< void, Error > override
 Sends an aggregated object to an Astarte Interface.
 
auto set_property (std::string_view interface_name, std::string_view path, const Data &data) -> astarte_tl::expected< void, Error > override
 Updates a local device property and synchronize it with Astarte.
 
auto unset_property (std::string_view interface_name, std::string_view path) -> astarte_tl::expected< void, Error > override
 Unsets (deletes) a device property.
 
auto poll_incoming (const std::chrono::milliseconds &timeout) -> std::optional< Message > override
 Polls for incoming messages from Astarte.
 
auto get_all_properties (const std::optional< Ownership > &ownership) -> astarte_tl::expected< std::list< StoredProperty >, Error > override
 Retrieves all stored properties matching an ownership filter.
 
auto get_properties (std::string_view interface_name) -> astarte_tl::expected< std::list< StoredProperty >, Error > override
 Retrieves all stored properties belonging to a specific interface.
 
auto get_property (std::string_view interface_name, std::string_view path) -> astarte_tl::expected< PropertyIndividual, Error > override
 Retrieves a specific property value.
 
- Public Member Functions inherited from astarte::device::Device
virtual ~Device ()=default
 Virtual destructor.
 
 Device (const Device &other)=delete
 Device is non-copyable.
 
 Device (Device &&other)=default
 Move constructor.
 
auto operator= (const Device &other) -> Device &=delete
 Device is non-copyable.
 
auto operator= (Device &&other) -> Device &=default
 Move assignment operator.
 

Static Public Member Functions

static auto create (Config cfg) -> astarte_tl::expected< DeviceMqtt, Error >
 Creates a new instance of the MQTT device.
 

Detailed Description

MQTT implementation of an Astarte device.

This class implements the abstract Device interface using MQTT as the underlying transport protocol. It handles connection management, data serialization, and protocol-specific communication with the Astarte platform.

Constructor & Destructor Documentation

◆ ~DeviceMqtt()

astarte::device::mqtt::DeviceMqtt::~DeviceMqtt ( )
override

Virtual destructor.

◆ DeviceMqtt() [1/2]

astarte::device::mqtt::DeviceMqtt::DeviceMqtt ( DeviceMqtt & other)
delete

Device is non-copyable.

◆ DeviceMqtt() [2/2]

astarte::device::mqtt::DeviceMqtt::DeviceMqtt ( DeviceMqtt && other)
default

Move constructor.

Parameters
[in,out]otherThe device instance to move data from.

Member Function Documentation

◆ add_interface_from_file()

auto astarte::device::mqtt::DeviceMqtt::add_interface_from_file ( const std::filesystem::path & json_file) -> astarte_tl::expected< void, Error >
overridevirtual

Adds an interface definition to the device from a JSON file.

Parses the JSON file to validate and register a new Astarte Interface.

Parameters
[in]json_fileThe filesystem path to the .json interface definition.
Returns
An expected containing void on success or Error on failure.

Implements astarte::device::Device.

◆ add_interface_from_str()

auto astarte::device::mqtt::DeviceMqtt::add_interface_from_str ( std::string_view json) -> astarte_tl::expected< void, Error >
overridevirtual

Adds an interface definition to the device from a JSON string.

Parameters
[in]jsonThe interface definition as a JSON string view.
Returns
An expected containing void on success or Error on failure.

Implements astarte::device::Device.

◆ connect()

auto astarte::device::mqtt::DeviceMqtt::connect ( ) -> astarte_tl::expected< void, Error >
overridevirtual

Connects the device to the Astarte platform.

This is an asynchronous operation. It initializes the MQTT transport layer and starts a background routine to maintain connectivity.

Returns
An expected containing void on success or Error on failure.

Implements astarte::device::Device.

◆ create()

static auto astarte::device::mqtt::DeviceMqtt::create ( Config cfg) -> astarte_tl::expected< DeviceMqtt, Error >
staticnodiscard

Creates a new instance of the MQTT device.

Parameters
[in]cfgThe configuration options used to connect the device to Astarte.
Returns
An expected containing the DeviceMqtt instance on success or Error on failure.

◆ disconnect()

auto astarte::device::mqtt::DeviceMqtt::disconnect ( ) -> astarte_tl::expected< void, Error >
overridevirtual

Disconnects the device from Astarte.

Returns
An expected containing void on success or Error on failure.

Implements astarte::device::Device.

◆ get_all_properties()

auto astarte::device::mqtt::DeviceMqtt::get_all_properties ( const std::optional< Ownership > & ownership) -> astarte_tl::expected< std::list< StoredProperty >, Error >
overridevirtual

Retrieves all stored properties matching an ownership filter.

Parameters
[in]ownershipOptional filter, if std::nullopt, returns all properties.
Returns
An expected containing the list of properties on success or Error on failure.

Implements astarte::device::Device.

◆ get_properties()

auto astarte::device::mqtt::DeviceMqtt::get_properties ( std::string_view interface_name) -> astarte_tl::expected< std::list< StoredProperty >, Error >
overridevirtual

Retrieves all stored properties belonging to a specific interface.

Parameters
[in]interface_nameThe name of the interface to query.
Returns
An expected containing the list of properties on success or Error on failure.

Implements astarte::device::Device.

◆ get_property()

auto astarte::device::mqtt::DeviceMqtt::get_property ( std::string_view interface_name,
std::string_view path ) -> astarte_tl::expected< PropertyIndividual, Error >
overridevirtual

Retrieves a specific property value.

Parameters
[in]interface_nameThe name of the interface.
[in]pathThe exact path of the property.
Returns
An expected containing the property on success or Error on failure.

Implements astarte::device::Device.

◆ is_connected()

auto astarte::device::mqtt::DeviceMqtt::is_connected ( ) const -> bool
nodiscardoverridevirtual

Checks connectivity status.

Returns
True if the device is fully connected to Astarte, false otherwise.

Implements astarte::device::Device.

◆ operator=() [1/2]

auto astarte::device::mqtt::DeviceMqtt::operator= ( DeviceMqtt && other) -> DeviceMqtt &=default
default

Move assignment operator.

Parameters
[in,out]otherThe device instance to move data from.
Returns
A reference to this device.

◆ operator=() [2/2]

auto astarte::device::mqtt::DeviceMqtt::operator= ( DeviceMqtt & other) -> DeviceMqtt &=delete
delete

Device is non-copyable.

◆ poll_incoming()

auto astarte::device::mqtt::DeviceMqtt::poll_incoming ( const std::chrono::milliseconds & timeout) -> std::optional< Message >
overridevirtual

Polls for incoming messages from Astarte.

This method blocks the calling thread until a message is received or the timeout expires.

Parameters
[in]timeoutThe maximum duration to block waiting for a message.
Returns
std::optional containing the Message if received, or std::nullopt if the timeout was reached.

Implements astarte::device::Device.

◆ remove_interface()

auto astarte::device::mqtt::DeviceMqtt::remove_interface ( const std::string & interface_name) -> astarte_tl::expected< void, Error >
overridevirtual

Removes an installed interface from the device.

Parameters
[in]interface_nameThe name of the interface to remove.
Returns
An expected containing void on success or Error on failure.

Implements astarte::device::Device.

◆ send_individual()

auto astarte::device::mqtt::DeviceMqtt::send_individual ( std::string_view interface_name,
std::string_view path,
const Data & data,
const std::chrono::system_clock::time_point * timestamp ) -> astarte_tl::expected< void, Error >
overridevirtual

Sends an individual data point to an Astarte Interface.

Parameters
[in]interface_nameThe name of the target interface.
[in]pathThe specific mapping path within the interface (e.g., "/sensors/temp").
[in]dataThe value payload to transmit.
[in]timestampOptional timestamp. Should match the interface timestamp configuration.
Returns
An expected containing void on success or Error on failure.

Implements astarte::device::Device.

◆ send_object()

auto astarte::device::mqtt::DeviceMqtt::send_object ( std::string_view interface_name,
std::string_view path,
const DatastreamObject & object,
const std::chrono::system_clock::time_point * timestamp ) -> astarte_tl::expected< void, Error >
overridevirtual

Sends an aggregated object to an Astarte Interface.

Parameters
[in]interface_nameThe name of the target interface.
[in]pathThe common base path for the object aggregation.
[in]objectThe map of keys and values constituting the object.
[in]timestampOptional timestamp. Should match the interface timestamp configuration.
Returns
An expected containing void on success or Error on failure.

Implements astarte::device::Device.

◆ set_property()

auto astarte::device::mqtt::DeviceMqtt::set_property ( std::string_view interface_name,
std::string_view path,
const Data & data ) -> astarte_tl::expected< void, Error >
overridevirtual

Updates a local device property and synchronize it with Astarte.

Parameters
[in]interface_nameThe name of the interface containing the property.
[in]pathThe specific path of the property.
[in]dataThe new value for the property.
Returns
An expected containing void on success or Error on failure.

Implements astarte::device::Device.

◆ unset_property()

auto astarte::device::mqtt::DeviceMqtt::unset_property ( std::string_view interface_name,
std::string_view path ) -> astarte_tl::expected< void, Error >
overridevirtual

Unsets (deletes) a device property.

Parameters
[in]interface_nameThe name of the interface containing the property.
[in]pathThe specific path of the property to unset.
Returns
An expected containing void on success or Error on failure.

Implements astarte::device::Device.


The documentation for this class was generated from the following file: