5 releases

0.6.2 Oct 19, 2023
0.6.1 Oct 2, 2023
0.6.0 Jul 5, 2023
0.5.1 Feb 6, 2023
0.1.0 Feb 1, 2023

#1484 in Procedural macros

Download history 175/week @ 2023-07-30 99/week @ 2023-08-06 58/week @ 2023-08-13 32/week @ 2023-08-20 31/week @ 2023-08-27 24/week @ 2023-09-03 132/week @ 2023-09-10 64/week @ 2023-09-17 99/week @ 2023-09-24 208/week @ 2023-10-01 54/week @ 2023-10-08 73/week @ 2023-10-15 122/week @ 2023-10-22 215/week @ 2023-10-29 217/week @ 2023-11-05 162/week @ 2023-11-12

716 downloads per month
Used in 2 crates (via astarte-device-sdk)

Apache-2.0

17KB
229 lines

Astarte Device SDK Rust  

Build Status Latest Version 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::{
    store::sqlite::SqliteStore,
    options::AstarteOptions,
    error::Error,
    AstarteDeviceSdk
};

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::new("sqlite::memory:").await?;

    // 2. Initialize device options (the ".database(db)" is not needed if 1 was skipped)
    let sdk_options = AstarteOptions::new(&realm, &device_id, &credentials_secret, &pairing_url)
        .interface_directory("./examples/interfaces")?
        .store(db);

    // 3. Create the device instance
    let mut device = AstarteDeviceSdk::new(sdk_options).await.unwrap();

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

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

    // 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};
    device.send_object("interface.name", "/common/endpoint/path", data).await.unwrap();

    // Polling for new data can be performed using the function handle_events.

    // Receive a server publish
    loop {
        match device.handle_events().await {
            Ok(data) => (), // Handle data
            Err(err) => (), // Handle errors
        }
    }
}

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

~1.5MB
~31K SLoC