dimo_rust_sdk/graphql/
telemetry.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use crate::utils::request::{make_auth_request, AuthRequestParams};
use serde_json::Value;
use std::collections::HashMap;
use std::error::Error;

pub struct Telemetry {
    base_url: String,
}

impl Telemetry {
    pub fn new(base_url: &str) -> Self {
        Self {
            base_url: base_url.to_string(),
        }
    }

    pub async fn query(&self, query: &str) -> Result<Value, Box<dyn Error>> {
        let mut body = HashMap::new();
        body.insert("query".to_string(), Value::String(query.to_string()));

        let params = AuthRequestParams {
            method: reqwest::Method::POST,
            base_url: self.base_url.clone(),
            path: "/telemetry".to_string(),
            query_params: None,
            body: Some(body),
            headers: None,
            token_type: "privilege".to_string(),
        };

        make_auth_request(params).await
    }

    pub async fn get_latest_signals(&self, token_id: &str) -> Result<Value, Box<dyn Error>> {
        let query = format!(
            r#"
            query {{
                SignalsLatest(tokenID: "{}") {{
                    powertrainTransmissionTravelledDistance {{
                        timestamp
                        value
                    }}
                    exteriorAirTemperature {{
                        timestamp
                        value
                    }}
                    speed {{
                        timestamp
                        value
                    }}
                    powertrainType {{
                        timestamp
                        value
                    }}
                }}
            }}
            "#,
            token_id
        );

        self.query(&query).await
    }
}