pyrinas_server/
telemetry.rs

1use chrono::{DateTime, Utc};
2use influxdb::{InfluxDbWriteable, WriteQuery};
3use serde::{Deserialize, Serialize};
4
5// TODO: confirm the name works for each
6// Matches `pyrinas_cloud_telemetry_type_t` in `pyrinas_cloud.h`
7// Note: the enum indexes match the entry order below. i.e. the order counts!
8#[derive(Debug, Serialize, Deserialize, Clone)]
9pub struct TelemetryData {
10    version: Option<String>,
11    rsrp: Option<u32>,        // Won't always have rsrp (hub only)
12    rssi_hub: Option<i32>,    // Won't always have this guy either
13    rssi_client: Option<i32>, // Won't always have this guy either
14}
15
16#[derive(Debug, InfluxDbWriteable, Clone)]
17pub struct InfluxTelemetryData {
18    version: Option<String>,  // Wont always have this guy either.
19    rsrp: Option<u32>,        // Won't always have rsrp (hub only)
20    rssi_hub: Option<i32>,    // Won't always have this guy either
21    rssi_client: Option<i32>, // Won't always have this guy either
22    #[influxdb(tag)]
23    id: String, // Typically not sent as it's included in the MQTT topic
24    time: DateTime<Utc>,      // Only used for inserting data into Influx DB
25}
26
27impl TelemetryData {
28    pub fn to_influx_data(&self, uid: String) -> InfluxTelemetryData {
29        // Return new data structure that's friendly with Influx
30        InfluxTelemetryData {
31            version: self.version.clone(),
32            rsrp: self.rsrp,
33            rssi_hub: self.rssi_hub,
34            rssi_client: self.rssi_client,
35            id: uid,
36            time: Utc::now(),
37        }
38    }
39}
40
41impl InfluxTelemetryData {
42    pub fn to_influx_query(&self, category: String) -> WriteQuery {
43        // Create and return query
44        let data = self.clone();
45        data.into_query(category)
46    }
47}