save data on backend

This commit is contained in:
2024-10-20 14:40:17 -07:00
parent e22245998f
commit 51af825b27
4 changed files with 178 additions and 96 deletions

View File

@@ -1,54 +1,16 @@
mod uuid;
mod grpc;
mod http;
mod telemetry;
pub mod core {
tonic::include_proto!("core");
}
use std::collections::HashMap;
use crate::core::TelemetryDataType;
use crate::telemetry::TelemetryManagementService;
use std::error::Error;
use std::fmt::Formatter;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::de::Visitor;
fn tlm_data_type_serialzier<S>(tlm_data_type: &TelemetryDataType, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
serializer.serialize_str(tlm_data_type.as_str_name())
}
struct TlmDataTypeVisitor;
impl<'de> Visitor<'de> for TlmDataTypeVisitor {
type Value = TelemetryDataType;
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
formatter.write_str("A &str")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
TelemetryDataType::from_str_name(v).ok_or(E::custom("Invalid TelemetryDataType"))
}
}
fn tlm_data_type_deserialzier<'de, D>(deserializer: D) -> Result<TelemetryDataType, D::Error> where D: Deserializer<'de> {
deserializer.deserialize_str(TlmDataTypeVisitor)
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct TelemetryDefinition {
uuid: String,
name: String,
#[serde(serialize_with = "tlm_data_type_serialzier")]
#[serde(deserialize_with = "tlm_data_type_deserialzier")]
data_type: TelemetryDataType,
}
pub async fn setup() -> Result<(), Box<dyn Error>> {
let cancellation_token = CancellationToken::new();
@@ -60,7 +22,7 @@ pub async fn setup() -> Result<(), Box<dyn Error>> {
});
}
let tlm = Arc::new(Mutex::new(HashMap::new()));
let tlm = Arc::new(TelemetryManagementService::new());
let grpc_server = grpc::setup(cancellation_token.clone(), tlm.clone())?;