![Build Status] ![Latest Version]
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.
Initializing an instance of a device can be performed in three steps, as seen below. ```Rust use astartedevicesdk::{ database::AstarteSqliteDatabase, options::AstarteOptions, AstarteDeviceSdk};
// (optional) Initialize a database to store the properties let db = AstarteSqliteDatabase::new("sqlite::memory:").await.unwrap();
// Initialize device options let sdkoptions = AstarteOptions::new(&realm, &deviceid, &credentialssecret, &pairingurl) .interface_directory("./examples/interfaces") .unwrap() .database(db);
// 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.
```Rust
// Send individual datastream or set individual property
let data: i32 = 12;
device.send("interface.name", "/endpoint/path", data).await.unwrap();
// Send aggregated object datastream
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
.
Rust
loop {
match device.handle_events().await {
Ok(data) => (), // Handle data
Err(err) => (), // Handle errors
}
}
You can build the library using:
sh
cargo build
You can execute one of the examples using the following command (seen for the simple example).
sh
cargo run --example simple -- \
--credentials-secret <credentials-secret>
--device-id <device-id>
--pairing-url <pairing-url>
--realm <realm>