#iot #sdk #astarte #iot-devices #api-bindings

astarte-device-sdk

A library that provides communication and pairing primitives to an Astarte Cluster

12 releases

0.7.2 Mar 21, 2024
0.7.0 Jan 22, 2024
0.6.2 Oct 19, 2023
0.6.0 Jul 5, 2023
0.5.1 Feb 6, 2023

#80 in Embedded development

Download history 65/week @ 2023-12-06 77/week @ 2023-12-13 158/week @ 2023-12-20 27/week @ 2024-01-03 21/week @ 2024-01-10 18/week @ 2024-01-17 30/week @ 2024-01-24 53/week @ 2024-01-31 161/week @ 2024-02-07 218/week @ 2024-02-14 179/week @ 2024-02-21 111/week @ 2024-02-28 379/week @ 2024-03-06 367/week @ 2024-03-13 864/week @ 2024-03-20

1,733 downloads per month
Used in astarte-message-hub

Apache-2.0

455KB
10K SLoC

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

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

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

    // 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?;

    // 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?;

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

    // Blocking call for the device event loop
    device.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

~48–65MB
~1M SLoC