Skip to main content

Client

Trait Client 

Source
pub trait Client: Clone {
    // Required methods
    fn send_individual(
        &mut self,
        interface_name: &str,
        mapping_path: &str,
        data: AstarteData,
    ) -> impl Future<Output = Result<(), Error>> + Send;
    fn send_individual_with_timestamp(
        &mut self,
        interface_name: &str,
        mapping_path: &str,
        data: AstarteData,
        timestamp: DateTime<Utc>,
    ) -> impl Future<Output = Result<(), Error>> + Send;
    fn send_object(
        &mut self,
        interface_name: &str,
        base_path: &str,
        data: AstarteObject,
    ) -> impl Future<Output = Result<(), Error>> + Send;
    fn send_object_with_timestamp(
        &mut self,
        interface_name: &str,
        base_path: &str,
        data: AstarteObject,
        timestamp: DateTime<Utc>,
    ) -> impl Future<Output = Result<(), Error>> + Send;
    fn set_property(
        &mut self,
        interface_name: &str,
        mapping_path: &str,
        data: AstarteData,
    ) -> impl Future<Output = Result<(), Error>> + Send;
    fn unset_property(
        &mut self,
        interface_name: &str,
        mapping_path: &str,
    ) -> impl Future<Output = Result<(), Error>> + Send;
    fn recv(
        &self,
    ) -> impl Future<Output = Result<DeviceEvent, RecvError>> + Send;
}
Expand description

A trait representing the behavior of an Astarte device client.

A device client is responsible for interacting with the Astarte platform by sending properties and datastreams, handling events, and managing device interfaces.

Required Methods§

Source

fn send_individual( &mut self, interface_name: &str, mapping_path: &str, data: AstarteData, ) -> impl Future<Output = Result<(), Error>> + Send

Send an individual datastream on an interface.

use astarte_device_sdk::{
    store::memory::MemoryStore, builder::DeviceBuilder,
    transport::mqtt::MqttConfig, types::AstarteData, prelude::*,
};
use chrono::{TimeZone, Utc};

#[tokio::main]
async fn main() {
    let mqtt_config = MqttConfig::with_credential_secret("realm_id", "device_id", "credential_secret", "pairing_url");

    let (mut client, connection) = DeviceBuilder::new().store(MemoryStore::new())
        .connection(mqtt_config).build().await.unwrap();

    let value: i32 = 42;
    client.send_individual("my.interface.name", "/endpoint/path", value.into())
        .await
        .unwrap();
}
Source

fn send_individual_with_timestamp( &mut self, interface_name: &str, mapping_path: &str, data: AstarteData, timestamp: DateTime<Utc>, ) -> impl Future<Output = Result<(), Error>> + Send

Send an individual datastream on an interface, with an explicit timestamp.

use astarte_device_sdk::{
    store::memory::MemoryStore, builder::DeviceBuilder,
    transport::mqtt::MqttConfig, types::AstarteData, prelude::*,
};
use chrono::{TimeZone, Utc};

#[tokio::main]
async fn main() {
    let mqtt_config = MqttConfig::with_credential_secret("realm_id", "device_id", "credential_secret", "pairing_url");

    let (mut client, connection) = DeviceBuilder::new().store(MemoryStore::new())
        .connection(mqtt_config).build().await.unwrap();

    let value: i32 = 42;
    let timestamp = Utc.timestamp_opt(1537449422, 0).unwrap();
    client.send_individual_with_timestamp("my.interface.name", "/endpoint/path", value.into(), timestamp)
        .await
        .unwrap();
}
Source

fn send_object( &mut self, interface_name: &str, base_path: &str, data: AstarteObject, ) -> impl Future<Output = Result<(), Error>> + Send

Send an object datastream on an interface.

The usage is the same of send_object_with_timestamp, without the timestamp.

Source

fn send_object_with_timestamp( &mut self, interface_name: &str, base_path: &str, data: AstarteObject, timestamp: DateTime<Utc>, ) -> impl Future<Output = Result<(), Error>> + Send

Send an object datastream on an interface, with an explicit timestamp.

use astarte_device_sdk::{
    store::memory::MemoryStore, builder::DeviceBuilder,
    transport::mqtt::MqttConfig, types::AstarteData, prelude::*,
};
use astarte_device_sdk::IntoAstarteObject;
use chrono::{TimeZone, Utc};

#[derive(IntoAstarteObject)]
struct TestObject {
    endpoint1: f64,
    endpoint2: bool,
}

#[tokio::main]
async fn main() {
    let mqtt_config = MqttConfig::with_credential_secret("realm_id", "device_id", "credential_secret", "pairing_url");

    let (mut client, connection) = DeviceBuilder::new().store(MemoryStore::new())
        .connection(mqtt_config).build().await.unwrap();

    let data = TestObject {
        endpoint1: 1.34,
        endpoint2: false
    };
    let timestamp = Utc.timestamp_opt(1537449422, 0).unwrap();
    client.send_object_with_timestamp("my.interface.name", "/endpoint/path", data.try_into().unwrap(), timestamp)
        .await
        .unwrap();
}
Source

fn set_property( &mut self, interface_name: &str, mapping_path: &str, data: AstarteData, ) -> impl Future<Output = Result<(), Error>> + Send

Send an individual datastream on an interface.

use astarte_device_sdk::{
    store::memory::MemoryStore, builder::DeviceBuilder,
    transport::mqtt::MqttConfig, types::AstarteData, prelude::*,
};
use chrono::{TimeZone, Utc};

#[tokio::main]
async fn main() {
    let mqtt_config = MqttConfig::with_credential_secret("realm_id", "device_id", "credential_secret", "pairing_url");

    let (mut client, connection) = DeviceBuilder::new().store(MemoryStore::new())
        .connection(mqtt_config).build().await.unwrap();

    let value: i32 = 42;
    client.set_property("my.interface.name", "/endpoint/path", value.into())
        .await
        .unwrap();
}
Source

fn unset_property( &mut self, interface_name: &str, mapping_path: &str, ) -> impl Future<Output = Result<(), Error>> + Send

Unset a device property.

use astarte_device_sdk::{
    store::memory::MemoryStore, builder::DeviceBuilder,
    transport::mqtt::MqttConfig, types::AstarteData, prelude::*,
};

#[tokio::main]
async fn main() {
    let mqtt_config = MqttConfig::with_credential_secret("realm_id", "device_id", "credential_secret", "pairing_url");

    let (mut device, _connection) = DeviceBuilder::new().store(MemoryStore::new())
        .connection(mqtt_config).build().await.unwrap();

    device
        .unset_property("my.interface.name", "/endpoint/path",)
        .await
        .unwrap();
}
Source

fn recv(&self) -> impl Future<Output = Result<DeviceEvent, RecvError>> + Send

Receives an event from Astarte.

§Note

An event can only be received once, so if the client is cloned only one of the clients instances will receive the message.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<C> Client for DeviceClient<C>
where C: Connection, <C as Connection>::Sender: Publish,