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,10 +1,10 @@
use crate::core::TelemetryDataType;
use crate::serialization::file_ext::{ReadExt, WriteExt};
use crate::telemetry::data::TelemetryData;
use crate::telemetry::data_item::TelemetryDataItem;
use crate::telemetry::data_value::TelemetryDataValue;
use crate::telemetry::definition::TelemetryDefinition;
use anyhow::{anyhow, ensure, Context};
use api::data_type::DataType;
use api::data_value::DataValue;
use chrono::{DateTime, DurationRound, SecondsFormat, TimeDelta, Utc};
use log::{error, info};
use std::cmp::min;
@@ -44,7 +44,7 @@ fn update_next_from(
}
struct SegmentData {
values: Vec<TelemetryDataValue>,
values: Vec<DataValue>,
timestamps: Vec<DateTime<Utc>>,
}
@@ -66,7 +66,7 @@ impl HistorySegmentRam {
}
}
fn insert(&self, value: TelemetryDataValue, timestamp: DateTime<Utc>) {
fn insert(&self, value: DataValue, timestamp: DateTime<Utc>) {
if timestamp < self.start || timestamp >= self.end {
return;
}
@@ -121,7 +121,7 @@ impl HistorySegmentRam {
next_from,
);
result.push(TelemetryDataItem {
value: data.values[i].clone(),
value: data.values[i],
timestamp: t.to_rfc3339_opts(SecondsFormat::Millis, true),
});
}
@@ -196,9 +196,9 @@ impl HistorySegmentFile {
// Write all the values
for value in &data.values {
match value {
TelemetryDataValue::Float32(value) => file.write_data::<f32>(*value)?,
TelemetryDataValue::Float64(value) => file.write_data::<f64>(*value)?,
TelemetryDataValue::Boolean(value) => file.write_data::<bool>(*value)?,
DataValue::Float32(value) => file.write_data::<f32>(*value)?,
DataValue::Float64(value) => file.write_data::<f64>(*value)?,
DataValue::Boolean(value) => file.write_data::<bool>(*value)?,
}
}
@@ -215,10 +215,7 @@ impl HistorySegmentFile {
})
}
fn load_to_ram(
mut self,
telemetry_data_type: TelemetryDataType,
) -> anyhow::Result<HistorySegmentRam> {
fn load_to_ram(mut self, telemetry_data_type: DataType) -> anyhow::Result<HistorySegmentRam> {
let mut segment_data = SegmentData {
values: Vec::with_capacity(self.length as usize),
timestamps: Vec::with_capacity(self.length as usize),
@@ -281,7 +278,7 @@ impl HistorySegmentFile {
from: DateTime<Utc>,
to: DateTime<Utc>,
maximum_resolution: TimeDelta,
telemetry_data_type: TelemetryDataType,
telemetry_data_type: DataType,
) -> anyhow::Result<(DateTime<Utc>, Vec<TelemetryDataItem>)> {
self.file_position = 0;
self.file.seek(SeekFrom::Start(0))?;
@@ -334,22 +331,19 @@ impl HistorySegmentFile {
self.read_date_time()
}
fn read_telemetry_item(
&mut self,
telemetry_data_type: TelemetryDataType,
) -> anyhow::Result<TelemetryDataValue> {
fn read_telemetry_item(&mut self, telemetry_data_type: DataType) -> anyhow::Result<DataValue> {
match telemetry_data_type {
TelemetryDataType::Float32 => {
DataType::Float32 => {
self.file_position += 4;
Ok(TelemetryDataValue::Float32(self.file.read_data::<f32>()?))
Ok(DataValue::Float32(self.file.read_data::<f32>()?))
}
TelemetryDataType::Float64 => {
DataType::Float64 => {
self.file_position += 8;
Ok(TelemetryDataValue::Float64(self.file.read_data::<f64>()?))
Ok(DataValue::Float64(self.file.read_data::<f64>()?))
}
TelemetryDataType::Boolean => {
DataType::Boolean => {
self.file_position += 1;
Ok(TelemetryDataValue::Boolean(self.file.read_data::<bool>()?))
Ok(DataValue::Boolean(self.file.read_data::<bool>()?))
}
}
}
@@ -357,12 +351,12 @@ impl HistorySegmentFile {
fn get_telemetry_item(
&mut self,
index: u64,
telemetry_data_type: TelemetryDataType,
) -> anyhow::Result<TelemetryDataValue> {
telemetry_data_type: DataType,
) -> anyhow::Result<DataValue> {
let item_length = match telemetry_data_type {
TelemetryDataType::Float32 => 4,
TelemetryDataType::Float64 => 8,
TelemetryDataType::Boolean => 1,
DataType::Float32 => 4,
DataType::Float64 => 8,
DataType::Boolean => 1,
};
let desired_position =
Self::HEADER_LENGTH + self.length * Self::TIMESTAMP_LENGTH + index * item_length;
@@ -429,7 +423,7 @@ impl TelemetryHistory {
history_segment_ram: HistorySegmentRam,
) -> JoinHandle<()> {
let mut path = service.data_root_folder.clone();
path.push(&self.data.definition.uuid);
path.push(self.data.definition.uuid.as_hyphenated().to_string());
spawn_blocking(move || {
match HistorySegmentFile::save_to_disk(path, history_segment_ram) {
// Immediately drop the segment - now that we've saved it to disk we don't need to keep it in memory
@@ -450,7 +444,7 @@ impl TelemetryHistory {
start: DateTime<Utc>,
) -> JoinHandle<anyhow::Result<HistorySegmentFile>> {
let mut path = service.data_root_folder.clone();
path.push(&self.data.definition.uuid);
path.push(self.data.definition.uuid.as_hyphenated().to_string());
spawn_blocking(move || HistorySegmentFile::open(path, start))
}
@@ -458,7 +452,7 @@ impl TelemetryHistory {
&self,
start: DateTime<Utc>,
service: &TelemetryHistoryService,
telemetry_data_type: TelemetryDataType,
telemetry_data_type: DataType,
) -> HistorySegmentRam {
let ram = self
.get_disk_segment(service, start)
@@ -480,7 +474,7 @@ impl TelemetryHistory {
pub async fn insert(
&self,
service: &TelemetryHistoryService,
value: TelemetryDataValue,
value: DataValue,
timestamp: DateTime<Utc>,
) {
let segments = self.segments.read().await;
@@ -531,7 +525,7 @@ impl TelemetryHistory {
pub fn insert_sync(
history: Arc<Self>,
service: Arc<TelemetryHistoryService>,
value: TelemetryDataValue,
value: DataValue,
timestamp: DateTime<Utc>,
) {
tokio::spawn(async move {
@@ -579,7 +573,7 @@ impl TelemetryHistory {
.unwrap();
let mut path = telemetry_history_service.data_root_folder.clone();
path.push(&self.data.definition.uuid);
path.push(self.data.definition.uuid.as_hyphenated().to_string());
let mut start = start;
while start < end {