Skip to main content

edgehog_device_runtime/data/
mod.rs

1/*
2 * This file is part of Edgehog.
3 *
4 * Copyright 2022 SECO Mind Srl
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * SPDX-License-Identifier: Apache-2.0
19 */
20
21use 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
36/// Connect to the store.
37pub 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
50/// Publishes the value to Astarte and logs if an error happens.
51///
52/// This is used to send telemetry data without returning an error.
53pub(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
75/// Sets the property and logs if an error happens.
76///
77/// This is used to send telemetry data without returning an error.
78pub(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    /// Create tmp store and store dir.
101    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}