#sdk #derive #astarte #iot #api-bindings

macro astarte-device-sdk-derive

Derive macros implementation used by Astarte Device SDK

25 releases

new 0.9.2 Nov 4, 2024
0.8.4 Sep 11, 2024
0.8.2 May 29, 2024
0.7.2 Mar 21, 2024
0.5.1 Feb 6, 2023

#5 in #astarte

Download history 22/week @ 2024-07-14 89/week @ 2024-07-21 124/week @ 2024-07-28 16/week @ 2024-08-04 51/week @ 2024-08-11 156/week @ 2024-08-18 161/week @ 2024-08-25 56/week @ 2024-09-01 390/week @ 2024-09-08 192/week @ 2024-09-15 524/week @ 2024-09-22 667/week @ 2024-09-29 182/week @ 2024-10-06 540/week @ 2024-10-13 360/week @ 2024-10-20 274/week @ 2024-10-27

1,356 downloads per month
Used in 2 crates (via astarte-device-sdk)

Apache-2.0

38KB
689 lines

Astarte Device SDK Rust  

Build Status Latest Version docs.rs Code coverage

Warning: this SDK is experimental, correctness and API stability are currently not guaranteed

The Astarte Device SDK for Rust is a ready to use library that provides communication and pairing primitives to an Astarte Cluster.

See the Astarte documentation for more information regarding Astarte and the available SDKs.

Basic usage

use std::error::Error as StdError;

use astarte_device_sdk::{
    builder::DeviceBuilder,
    transport::mqtt::MqttConfig,
    error::Error,
    prelude::*,
    store::sqlite::SqliteStore,
};

async fn run_astarte_device() -> Result<(), Box<dyn StdError>> {

    let realm = "realm_name";
    let device_id = "device_id";
    let credentials_secret = "device_credentials_secret";
    let pairing_url = "astarte_cluster_pairing_url";

    // Initializing an instance of a device can be performed as shown in the following three steps.

    // 1. (optional) Initialize a database to store the properties
    let db = SqliteStore::connect_db("/var/lib/astarte/store.db").await?;

    // 2. Initialize device options and mqtt config (the ".database(db)" is not needed if 1 was skipped)
    let mut mqtt_config = MqttConfig::with_credential_secret(realm, device_id, credentials_secret, pairing_url);
    mqtt_config.ignore_ssl_errors();

    // 3. Create the device instance
    let (mut client, mut connection) = DeviceBuilder::new()
        .interface_directory("./examples/interfaces")?
        .store(db)
        .connect(mqtt_config).await?
        .build().await;

    // Publishing new values can be performed using the send and send_object functions.

    // Send individual datastream or set individual property
    let data: i32 = 12;
    client.send("interface.name", "/endpoint/path", data).await?;

    // Send aggregated object datastream
    use astarte_device_sdk::AstarteAggregate;
    // If the derive feature is not enabled
    #[cfg(not(feature = "derive"))]
    use astarte_device_sdk_derive::AstarteAggregate;

    #[derive(Debug, AstarteAggregate)]
    struct MyAggObj {
        endpoint1: f64,
        endpoint2: i32
    }

    let data = MyAggObj {endpoint1: 1.34, endpoint2: 22};
    client.send_object("interface.name", "/common/endpoint/path", data).await?;

    // Receive a server publish from the event channel
    tokio::spawn(async move {
        loop {
          match client.recv().await {
              Ok(data) => (), // Handle data
              Err(err) => (), // Handle errors
          }
        }
    });

    // Blocking call for the device event loop
    connection.handle_events().await?;

    Ok(())
}

Building the library

You can build the library using:

cargo build

Examples

Check out how to start with the SDK using one of the included examples.

Dependencies

~255–710KB
~17K SLoC