Astarte device API for C++ 0.8.1
Astarte device SDK for C++
Loading...
Searching...
No Matches
device_mqtt.hpp
Go to the documentation of this file.
1// (C) Copyright 2025, SECO Mind Srl
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#ifndef ASTARTE_DEVICE_SDK_DEVICE_MQTT_H
6#define ASTARTE_DEVICE_SDK_DEVICE_MQTT_H
7
15
16#include <chrono>
17#include <filesystem>
18#include <list>
19#include <memory>
20#include <optional>
21#include <string>
22#include <string_view>
23
33
35namespace astarte::device::mqtt {
36
44class DeviceMqtt : public Device {
45 public:
52 [[nodiscard]] static auto create(Config cfg) -> astarte_tl::expected<DeviceMqtt, Error>;
53
55 ~DeviceMqtt() override;
56
58 DeviceMqtt(DeviceMqtt& other) = delete;
59
61 auto operator=(DeviceMqtt& other) -> DeviceMqtt& = delete;
62
67 DeviceMqtt(DeviceMqtt&& other) = default;
68
74 auto operator=(DeviceMqtt&& other) -> DeviceMqtt& = default;
75
83 auto add_interface_from_file(const std::filesystem::path& json_file)
84 -> astarte_tl::expected<void, Error> override;
85
92 auto add_interface_from_str(std::string_view json) -> astarte_tl::expected<void, Error> override;
93
100 auto remove_interface(const std::string& interface_name)
101 -> astarte_tl::expected<void, Error> override;
102
111 auto connect() -> astarte_tl::expected<void, Error> override;
112
117 [[nodiscard]] auto is_connected() const -> bool override;
118
123 auto disconnect() -> astarte_tl::expected<void, Error> override;
124
134 auto send_individual(std::string_view interface_name, std::string_view path, const Data& data,
135 const std::chrono::system_clock::time_point* timestamp)
136 -> astarte_tl::expected<void, Error> override;
137
147 auto send_object(std::string_view interface_name, std::string_view path,
148 const DatastreamObject& object,
149 const std::chrono::system_clock::time_point* timestamp)
150 -> astarte_tl::expected<void, Error> override;
151
160 auto set_property(std::string_view interface_name, std::string_view path, const Data& data)
161 -> astarte_tl::expected<void, Error> override;
162
170 auto unset_property(std::string_view interface_name, std::string_view path)
171 -> astarte_tl::expected<void, Error> override;
172
183 auto poll_incoming(const std::chrono::milliseconds& timeout) -> std::optional<Message> override;
184
191 auto get_all_properties(const std::optional<Ownership>& ownership)
192 -> astarte_tl::expected<std::list<StoredProperty>, Error> override;
193
200 auto get_properties(std::string_view interface_name)
201 -> astarte_tl::expected<std::list<StoredProperty>, Error> override;
202
210 auto get_property(std::string_view interface_name, std::string_view path)
211 -> astarte_tl::expected<PropertyIndividual, Error> override;
212
213 private:
214 struct DeviceMqttImpl;
215 std::shared_ptr<DeviceMqttImpl> astarte_device_impl_;
216
221 explicit DeviceMqtt(std::shared_ptr<DeviceMqttImpl> impl);
222};
223
224} // namespace astarte::device::mqtt
225
226#endif // ASTARTE_DEVICE_SDK_DEVICE_MQTT_H
Represents a single Astarte data value.
Definition data.hpp:61
Astarte object class, representing the Astarte object datastream data.
Definition object.hpp:33
Device(const Device &other)=delete
Device is non-copyable.
Astarte message class, represents a full message for/from Astarte.
Definition msg.hpp:37
Representing the Astarte individual property data.
Definition property.hpp:22
Represents a stored property on the device.
Definition stored_property.hpp:33
Configuration for the Astarte MQTT connection.
Definition config.hpp:55
auto connect() -> astarte_tl::expected< void, Error > override
Connects the device to the Astarte platform.
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 disconnect() -> astarte_tl::expected< void, Error > override
Disconnects the device from Astarte.
~DeviceMqtt() override
Virtual destructor.
DeviceMqtt(DeviceMqtt &&other)=default
Move constructor.
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 poll_incoming(const std::chrono::milliseconds &timeout) -> std::optional< Message > override
Polls for incoming messages from Astarte.
DeviceMqtt(DeviceMqtt &other)=delete
Device is non-copyable.
auto operator=(DeviceMqtt &&other) -> DeviceMqtt &=default
Move assignment operator.
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.
static auto create(Config cfg) -> astarte_tl::expected< DeviceMqtt, Error >
Creates a new instance of the MQTT device.
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.
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 operator=(DeviceMqtt &other) -> DeviceMqtt &=delete
Device is non-copyable.
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 is_connected() const -> bool override
Checks connectivity status.
auto unset_property(std::string_view interface_name, std::string_view path) -> astarte_tl::expected< void, Error > override
Unsets (deletes) a device property.
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.
Configuration for the Astarte MQTT transport.
Astarte data class and its related methods.
Abstract Astarte device interface.
specific error definitions for the Astarte device SDK.
Astarte message class and its related methods.
Namespace for Astarte device functionality using the MQTT transport protocol.
Definition errors.hpp:57
Ownership
Possible Astarte ownership.
Definition ownership.hpp:23
std::variant< DataSerializationError, InternalError, FileOpenError, InvalidInputError, InterfaceValidationError, InvalidInterfaceVersionError, InvalidInterfaceTypeError, InvalidInterfaceOwnershipeError, InvalidInterfaceAggregationError, InvalidAstarteTypeError, InvalidReliabilityError, InvalidRetentionError, InvalidDatabaseRetentionPolicyError, OperationRefusedError, GrpcLibError, MsgHubError, mqtt::JsonParsingError, mqtt::DeviceRegistrationError, mqtt::PairingApiError, mqtt::MqttError, mqtt::InvalidUrlError, mqtt::RetrieveBrokerUrlError, mqtt::ReadCredentialError, mqtt::WriteCredentialError, mqtt::PairingConfigError, mqtt::CryptoError, mqtt::UuidError, mqtt::HttpError, mqtt::MqttConnectionError > Error
Variant type representing any error from the Astarte device library.
Definition errors.hpp:80
Astarte object class and its related methods.
Ownership definitions for communication with Astarte.
Astarte individual property class and its related methods.
Astarte stored property class.