Skip to content

Commit 9334554

Browse files
committed
refactor(cfg): improve conditional compilation
Add the required features for each example. Signed-off-by: Joshua Chapman <joshua.chapman@secomind.com>
1 parent 640ff47 commit 9334554

File tree

5 files changed

+27
-29
lines changed

5 files changed

+27
-29
lines changed

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,13 @@ webpki-roots = "0.26.0"
181181
# Transitive dependencies
182182
litemap = "=0.7.4"
183183
zerofrom = "=0.1.5"
184+
185+
[[example]]
186+
name = "msghub_client"
187+
path = "examples/message_hub_client/main.rs"
188+
required-features = ["message-hub", "derive"]
189+
190+
[[example]]
191+
name = "object_datastream"
192+
path = "examples/object_datastream/main.rs"
193+
required-features = ["derive"]

docs/connect-to-the-astarte-message-hub.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The Astarte Device SDK supports two different connection types:
1515
Here we will go through, step by step, on how to connect and send data to the Message Hub.
1616

1717
> You can find the full code by going to the
18-
> [msghub-client example](https://github.com/astarte-platform/astarte-device-sdk-rust/tree/master/examples/msghub-client)
18+
> [message hub client example](https://github.com/astarte-platform/astarte-device-sdk-rust/tree/master/examples/message_hub_client)
1919
2020
## Before you begin
2121

@@ -37,9 +37,9 @@ Hub server, and all the keys necessary to register the Message Hub server as a n
3737
### Message Hub Server
3838

3939
To register the server you can follow the guide and examples in
40-
[the MessageHub repository](https://github.com/astarte-platform/astarte-message-hub). Following
41-
this, we assume the Server is listening on the same machine IP v4 address `127.0.0.1` and port
42-
`50051`.
40+
[the Astarte MessageHub repository](https://github.com/astarte-platform/astarte-message-hub).
41+
Following this, we assume the Server is listening on the same machine IP v4 address `127.0.0.1` and
42+
port `50051`.
4343

4444
#### Client Auth
4545

@@ -291,8 +291,8 @@ function that we declared previously.
291291
# const NODE_UUID: uuid::Uuid = uuid::uuid!("0444d8c3-f3f1-4b89-9e68-6ffb50ec1839");
292292
# const MESSAGE_HUB_URL: &str = "http://127.0.0.1:50051";
293293
# const STORE_DIRECTORY: &str = "./store-dir";
294-
295-
// NOTO: set the interface directory in your project, these are relative to this file
294+
#
295+
// NOTE: set the interface directory in your project, these are relative to this file
296296
const AGGREGATED_DEVICE: &str = include_str!("../../docs/interfaces/org.astarte-platform.rust.get-started.Aggregated.json");
297297
const INDIVIDUAL_DEVICE: &str = include_str!("../../docs/interfaces/org.astarte-platform.rust.get-started.IndividualDevice.json");
298298
const INDIVIDUAL_SERVER: &str = include_str!("../../docs/interfaces/org.astarte-platform.rust.get-started.IndividualServer.json");
@@ -337,8 +337,6 @@ We can now spawn a task to receive data from Astarte. Using the
337337
# transport::grpc::{tonic::transport::Endpoint, Grpc, GrpcConfig, store::GrpcStore},
338338
# DeviceClient, DeviceConnection,
339339
# };
340-
341-
342340
# #[cfg(not(feature = "derive"))]
343341
# use astarte_device_sdk_derive::FromEvent;
344342
# #[cfg(feature = "derive")]
@@ -398,8 +396,6 @@ async fn receive_data(client: DeviceClient<GrpcStore>) -> eyre::Result<()> {
398396
}
399397
}
400398
401-
402-
403399
#[tokio::main]
404400
async fn main() -> eyre::Result<()> {
405401
# let mut tasks = tokio::task::JoinSet::new();
@@ -448,7 +444,7 @@ convert the Rust struct in an Object Aggregate to send.
448444
# transport::grpc::{tonic::transport::Endpoint, Grpc, GrpcConfig, store::GrpcStore},
449445
# DeviceClient, DeviceConnection,
450446
# };
451-
447+
#
452448
use std::time::Duration;
453449
454450
# #[cfg(feature = "derive")]
File renamed without changes.
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@
1818

1919
//! Example on connecting to the Astarte MessageHub.
2020
21-
// Features required by the example
22-
#![cfg(all(feature = "message-hub", feature = "derive"))]
23-
2421
use std::{f64, time::Duration};
2522

2623
use astarte_device_sdk::{
2724
builder::DeviceBuilder,
2825
client::RecvError,
2926
prelude::*,
30-
store::SqliteStore,
31-
transport::grpc::{tonic::transport::Endpoint, Grpc, GrpcConfig},
27+
transport::grpc::{store::GrpcStore, tonic::transport::Endpoint, Grpc, GrpcConfig},
3228
DeviceClient, DeviceConnection,
3329
};
3430
use eyre::OptionExt;
@@ -69,8 +65,8 @@ enum ServerIndividual {
6965
}
7066

7167
async fn init() -> eyre::Result<(
72-
DeviceClient<SqliteStore>,
73-
DeviceConnection<SqliteStore, Grpc<SqliteStore>>,
68+
DeviceClient<GrpcStore>,
69+
DeviceConnection<GrpcStore, Grpc<GrpcStore>>,
7470
)> {
7571
tokio::fs::create_dir_all(&STORE_DIRECTORY).await?;
7672

@@ -84,15 +80,14 @@ async fn init() -> eyre::Result<(
8480
.interface_str(INDIVIDUAL_DEVICE)?
8581
.interface_str(INDIVIDUAL_SERVER)?
8682
.interface_str(PROPERTY_DEVICE)?
87-
.connect(grpc_config)
88-
.await?
83+
.connection(grpc_config)
8984
.build()
90-
.await;
85+
.await?;
9186

9287
Ok((client, connection))
9388
}
9489

95-
async fn receive_data(client: DeviceClient<SqliteStore>) -> eyre::Result<()> {
90+
async fn receive_data(client: DeviceClient<GrpcStore>) -> eyre::Result<()> {
9691
loop {
9792
let event = match client.recv().await {
9893
Ok(event) => event,
@@ -114,7 +109,7 @@ async fn receive_data(client: DeviceClient<SqliteStore>) -> eyre::Result<()> {
114109
.path
115110
.strip_prefix("/")
116111
.and_then(|s| s.strip_suffix("/data"))
117-
.ok_or_eyre("couldn't get endpoint id paramter")?
112+
.ok_or_eyre("couldn't get endpoint id parameter")?
118113
.to_string();
119114

120115
let ServerIndividual::Double(value) = ServerIndividual::from_event(event)?;
@@ -131,14 +126,14 @@ async fn receive_data(client: DeviceClient<SqliteStore>) -> eyre::Result<()> {
131126
}
132127

133128
/// Aggregated object
134-
#[derive(Debug, AstarteAggregate)]
129+
#[derive(Debug, IntoAstarteObject)]
135130
struct AggregatedDevice {
136131
double_endpoint: f64,
137132
string_endpoint: String,
138133
}
139134

140135
/// Send data after an interval to every interface
141-
async fn send_data(client: DeviceClient<SqliteStore>) -> eyre::Result<()> {
136+
async fn send_data(client: DeviceClient<GrpcStore>) -> eyre::Result<()> {
142137
// Every 2 seconds send the data
143138
let mut interval = tokio::time::interval(Duration::from_secs(2));
144139

@@ -160,7 +155,7 @@ async fn send_data(client: DeviceClient<SqliteStore>) -> eyre::Result<()> {
160155
.send_object(
161156
"org.astarte-platform.rust.get-started.Aggregated",
162157
"/group_data",
163-
obj_data,
158+
obj_data.try_into()?,
164159
)
165160
.await?;
166161
// Set the Property

examples/object_datastream/main.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ use std::time::Duration;
2020

2121
use serde::{Deserialize, Serialize};
2222

23-
#[cfg(feature = "derive")]
2423
use astarte_device_sdk::IntoAstarteObject;
2524
use astarte_device_sdk::{
2625
builder::DeviceBuilder, prelude::*, store::memory::MemoryStore, transport::mqtt::MqttConfig,
2726
};
28-
#[cfg(not(feature = "derive"))]
29-
use astarte_device_sdk_derive::IntoAstarteObject;
3027
use tokio::task::JoinSet;
3128
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
3229

0 commit comments

Comments
 (0)