Replace gRPC Backend (#10)

**Rationale:**

Having two separate servers and communication methods resulted in additional maintenance & the need to convert often between backend & frontend data types.
By moving the backend communication off of gRPC and to just use websockets it both gives more control & allows for simplification of the implementation.

#8

**Changes:**

- Replaces gRPC backend.
  - New implementation automatically handles reconnect logic
- Implements an api layer
- Migrates examples to the api layer
- Implements a proc macro to make command handling easier
- Implements unit tests for the api layer (90+% coverage)
- Implements integration tests for the proc macro (90+% coverage)

Reviewed-on: #10
Co-authored-by: Sergey Savelyev <sergeysav.nn@gmail.com>
Co-committed-by: Sergey Savelyev <sergeysav.nn@gmail.com>
This commit was merged in pull request #10.
This commit is contained in:
2026-01-01 10:11:53 -08:00
committed by sergeysav
parent f658b55586
commit 788dd10a91
68 changed files with 3934 additions and 1504 deletions

View File

@@ -1,7 +1,15 @@
use crate::core::{TelemetryDefinitionRequest, Uuid};
use crate::telemetry::data::TelemetryData;
use crate::telemetry::data_item::TelemetryDataItem;
use crate::telemetry::definition::TelemetryDefinition;
use crate::telemetry::history::{TelemetryHistory, TelemetryHistoryService};
use anyhow::bail;
use api::data_type::DataType;
use api::data_value::DataValue;
use api::messages::telemetry_definition::{
TelemetryDefinitionRequest, TelemetryDefinitionResponse,
};
use api::messages::telemetry_entry::TelemetryEntry;
use chrono::SecondsFormat;
use log::{error, info, warn};
use papaya::{HashMap, HashMapRef, LocalGuard};
use std::fs;
@@ -12,12 +20,13 @@ use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
use tokio::time::sleep;
use uuid::Uuid;
const RELEASED_ATTEMPTS: usize = 5;
pub struct TelemetryManagementService {
uuid_index: HashMap<String, String>,
tlm_data: HashMap<String, Arc<TelemetryHistory>>,
uuid_index: HashMap<String, Uuid>,
tlm_data: HashMap<Uuid, Arc<TelemetryHistory>>,
telemetry_history_service: Arc<TelemetryHistoryService>,
metadata_file: Arc<Mutex<File>>,
}
@@ -49,8 +58,8 @@ impl TelemetryManagementService {
// Skip invalid entries
match serde_json::from_str::<TelemetryDefinition>(line) {
Ok(tlm_def) => {
let _ = uuid_index.insert(tlm_def.name.clone(), tlm_def.uuid.clone());
let _ = tlm_data.insert(tlm_def.uuid.clone(), Arc::new(tlm_def.into()));
let _ = uuid_index.insert(tlm_def.name.clone(), tlm_def.uuid);
let _ = tlm_data.insert(tlm_def.uuid, Arc::new(tlm_def.into()));
}
Err(err) => {
error!("Failed to parse metadata entry {err}");
@@ -79,23 +88,20 @@ impl TelemetryManagementService {
pub fn register(
&self,
telemetry_definition_request: TelemetryDefinitionRequest,
) -> anyhow::Result<String> {
) -> anyhow::Result<TelemetryDefinitionResponse> {
let uuid_index = self.uuid_index.pin();
let tlm_data = self.tlm_data.pin();
let uuid = uuid_index
.get_or_insert_with(telemetry_definition_request.name.clone(), || {
Uuid::random().value
})
.clone();
let uuid =
*uuid_index.get_or_insert_with(telemetry_definition_request.name.clone(), Uuid::new_v4);
let inserted = tlm_data.try_insert(
uuid.clone(),
uuid,
Arc::new(
TelemetryDefinition {
uuid: uuid.clone(),
uuid,
name: telemetry_definition_request.name.clone(),
data_type: telemetry_definition_request.data_type(),
data_type: telemetry_definition_request.data_type,
}
.into(),
),
@@ -129,7 +135,38 @@ impl TelemetryManagementService {
});
}
Ok(uuid)
Ok(TelemetryDefinitionResponse { uuid })
}
pub fn add_tlm_item(&self, tlm_item: TelemetryEntry) -> anyhow::Result<()> {
let tlm_management_pin = self.pin();
let Some(tlm_data) = tlm_management_pin.get_by_uuid(&tlm_item.uuid) else {
bail!("Telemetry Item Not Found");
};
let expected_type = match &tlm_item.value {
DataValue::Float32(_) => DataType::Float32,
DataValue::Float64(_) => DataType::Float64,
DataValue::Boolean(_) => DataType::Boolean,
};
if expected_type != tlm_data.data.definition.data_type {
bail!("Data Type Mismatch");
};
let _ = tlm_data.data.data.send_replace(Some(TelemetryDataItem {
value: tlm_item.value,
timestamp: tlm_item
.timestamp
.to_rfc3339_opts(SecondsFormat::Millis, true),
}));
TelemetryHistory::insert_sync(
tlm_data.clone(),
self.history_service(),
tlm_item.value,
tlm_item.timestamp,
);
Ok(())
}
pub fn get_by_name(&self, name: &String) -> Option<TelemetryData> {
@@ -138,7 +175,7 @@ impl TelemetryManagementService {
self.get_by_uuid(uuid)
}
pub fn get_by_uuid(&self, uuid: &String) -> Option<TelemetryData> {
pub fn get_by_uuid(&self, uuid: &Uuid) -> Option<TelemetryData> {
let tlm_data = self.tlm_data.pin();
tlm_data
.get(uuid)
@@ -200,11 +237,11 @@ impl TelemetryManagementService {
}
pub struct TelemetryManagementServicePin<'a> {
tlm_data: HashMapRef<'a, String, Arc<TelemetryHistory>, RandomState, LocalGuard<'a>>,
tlm_data: HashMapRef<'a, Uuid, Arc<TelemetryHistory>, RandomState, LocalGuard<'a>>,
}
impl<'a> TelemetryManagementServicePin<'a> {
pub fn get_by_uuid(&'a self, uuid: &String) -> Option<&'a Arc<TelemetryHistory>> {
pub fn get_by_uuid(&'a self, uuid: &Uuid) -> Option<&'a Arc<TelemetryHistory>> {
self.tlm_data.get(uuid)
}
}