edgehog_device_runtime/data/
mod.rs1use astarte_device_sdk::aggregate::AstarteObject;
22use astarte_device_sdk::chrono::{DateTime, Utc};
23use astarte_device_sdk::store::sqlite::SqliteError;
24use astarte_device_sdk::store::SqliteStore;
25use astarte_device_sdk::types::AstarteData;
26use futures::TryFutureExt;
27use std::path::Path;
28use tracing::{debug, error, info};
29
30use crate::Client;
31
32pub mod astarte_device_sdk_lib;
33#[cfg(feature = "message-hub")]
34pub mod astarte_message_hub_node;
35
36pub async fn connect_store<P>(store_dir: P) -> Result<SqliteStore, SqliteError>
38where
39 P: AsRef<Path>,
40{
41 let db_path = store_dir.as_ref().join("database.db");
42
43 debug!("connecting to store {}", db_path.display());
44 let store = SqliteStore::connect_db(&db_path).await?;
45
46 info!("connected to store {}", db_path.display());
47 Ok(store)
48}
49
50pub(crate) async fn send_object_with_timestamp<C, T>(
54 client: &mut C,
55 interface: &str,
56 path: &str,
57 data: T,
58 timestamp: DateTime<Utc>,
59) where
60 C: Client,
61 T: TryInto<AstarteObject, Error = astarte_device_sdk::Error>,
62{
63 let res = futures::future::ready(data.try_into())
64 .and_then(|data| client.send_object_with_timestamp(interface, path, data, timestamp))
65 .await;
66
67 if let Err(err) = res {
68 error!(
69 error = format!("{:#}", stable_eyre::Report::new(err)),
70 interface, path, "failed to publish",
71 )
72 }
73}
74
75pub(crate) async fn set_property<C>(
79 client: &mut C,
80 interface: &str,
81 path: &str,
82 data: impl Into<AstarteData>,
83) where
84 C: Client,
85{
86 if let Err(err) = client.set_property(interface, path, data.into()).await {
87 error!(
88 error = format!("{:#}", stable_eyre::Report::new(err)),
89 interface, path, "failed to set property",
90 )
91 }
92}
93
94#[cfg(test)]
95pub mod tests {
96 use super::*;
97
98 use tempdir::TempDir;
99
100 pub async fn create_tmp_store() -> (SqliteStore, TempDir) {
102 let tmp_dir = TempDir::new("edgehog-tmp-store").expect("failed to create tmp store dir");
103 let store = connect_store(tmp_dir.path())
104 .await
105 .expect("failed to connect store");
106
107 (store, tmp_dir)
108 }
109
110 #[tokio::test]
111 async fn test_connect_store() {
112 create_tmp_store().await;
113 }
114}