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

astarte-device-sdk-derive

用于Astarte设备SDK的派生宏实现

21个版本

新版本 0.8.3 2024年8月22日
0.8.2 2024年5月29日
0.8.0 2024年4月29日
0.7.2 2024年3月21日
0.5.1 2023年2月6日

5#astarte

Download history 402/week @ 2024-05-02 314/week @ 2024-05-09 105/week @ 2024-05-16 469/week @ 2024-05-23 177/week @ 2024-05-30 122/week @ 2024-06-06 41/week @ 2024-06-13 8/week @ 2024-06-20 14/week @ 2024-06-27 61/week @ 2024-07-04 17/week @ 2024-07-11 94/week @ 2024-07-18 117/week @ 2024-07-25 19/week @ 2024-08-01 51/week @ 2024-08-08 40/week @ 2024-08-15

每月243次下载
用于 2 个crate(通过 astarte-device-sdk

Apache-2.0

29KB
494

Astarte设备SDK Rust  

Build Status Latest Version docs.rs Code coverage

警告:此SDK为实验性,目前不能保证正确性和API稳定性

Astarte设备SDK for Rust 是一个现成的库,它为Astarte集群提供了通信和配对原语。

有关Astarte和可用SDK的更多信息,请参阅Astarte文档

基本用法

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::from_uri("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::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();

    // 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(())
}

构建库

您可以使用以下命令构建库

cargo build

示例

查看如何使用以下示例开始使用SDK。

依赖项

~260–710KB
~17K SLoC