fixes formatting and lints

This commit is contained in:
2024-12-27 11:05:47 -05:00
parent 147d1abaf8
commit 12f27bad69
11 changed files with 281 additions and 210 deletions

View File

@@ -1,7 +1,10 @@
use chrono::DateTime;
use num_traits::float::FloatConst;
use server::core::telemetry_service_client::TelemetryServiceClient;
use server::core::telemetry_value::Value;
use server::core::{TelemetryDataType, TelemetryDefinitionRequest, TelemetryItem, TelemetryValue, Timestamp, Uuid};
use server::core::{
TelemetryDataType, TelemetryDefinitionRequest, TelemetryItem, TelemetryValue, Timestamp, Uuid,
};
use std::error::Error;
use std::time::Duration;
use tokio::sync::mpsc;
@@ -13,7 +16,6 @@ use tonic::codegen::tokio_stream::StreamExt;
use tonic::codegen::StdError;
use tonic::transport::Channel;
use tonic::Request;
use num_traits::float::FloatConst;
struct Telemetry {
client: TelemetryServiceClient<Channel>,
@@ -41,22 +43,22 @@ impl Telemetry {
tokio::spawn(async move {
while !cancel.is_cancelled() {
let (server_tx, server_rx) = mpsc::channel(1);
let response_stream = client.insert_telemetry(ReceiverStream::new(server_rx)).await;
let response_stream = client
.insert_telemetry(ReceiverStream::new(server_rx))
.await;
if let Ok(response_stream) = response_stream {
let mut response_stream = response_stream.into_inner();
while let Some(item) = local_rx.recv().await {
match server_tx.send(item).await {
Ok(_) => {},
Err(_) => {
break
},
Ok(_) => {}
Err(_) => break,
}
if let Some(response) = response_stream.next().await {
match response {
Ok(_) => {},
Ok(_) => {}
Err(_) => {
break;
},
}
}
} else {
break;
@@ -75,13 +77,23 @@ impl Telemetry {
})
}
pub async fn register(&mut self, name: String, data_type: TelemetryDataType) -> Result<TelemetryItemHandle, Box<dyn Error>> {
let response = self.client.new_telemetry(Request::new(TelemetryDefinitionRequest {
name,
data_type: data_type.into(),
})).await?.into_inner();
pub async fn register(
&mut self,
name: String,
data_type: TelemetryDataType,
) -> Result<TelemetryItemHandle, Box<dyn Error>> {
let response = self
.client
.new_telemetry(Request::new(TelemetryDefinitionRequest {
name,
data_type: data_type.into(),
}))
.await?
.into_inner();
let Some(uuid) = response.uuid else { return Err("UUID Missing".into()); };
let Some(uuid) = response.uuid else {
return Err("UUID Missing".into());
};
Ok(TelemetryItemHandle {
uuid: uuid.value,
@@ -97,20 +109,25 @@ impl Drop for Telemetry {
}
impl TelemetryItemHandle {
pub async fn publish(&self, value: Value, timestamp: DateTime<chrono::Utc>) -> Result<(), Box<dyn Error>> {
let offset_from_unix_epoch = timestamp - DateTime::from_timestamp(0, 0).expect("Could not get Unix epoch");
self.tx.send(TelemetryItem {
uuid: Some(Uuid {
value: self.uuid.clone()
}),
value: Some(TelemetryValue {
value: Some(value)
}),
timestamp: Some(Timestamp {
secs: offset_from_unix_epoch.num_seconds(),
nanos: offset_from_unix_epoch.subsec_nanos(),
}),
}).await?;
pub async fn publish(
&self,
value: Value,
timestamp: DateTime<chrono::Utc>,
) -> Result<(), Box<dyn Error>> {
let offset_from_unix_epoch =
timestamp - DateTime::from_timestamp(0, 0).expect("Could not get Unix epoch");
self.tx
.send(TelemetryItem {
uuid: Some(Uuid {
value: self.uuid.clone(),
}),
value: Some(TelemetryValue { value: Some(value) }),
timestamp: Some(Timestamp {
secs: offset_from_unix_epoch.num_seconds(),
nanos: offset_from_unix_epoch.subsec_nanos(),
}),
})
.await?;
Ok(())
}
}
@@ -119,23 +136,47 @@ impl TelemetryItemHandle {
async fn main() -> Result<(), Box<dyn Error>> {
let mut tlm = Telemetry::new("http://[::1]:50051").await?;
let sin_tlm_handle = tlm.register("simple_producer/sin".into(), TelemetryDataType::Float32).await?;
let cos_tlm_handle = tlm.register("simple_producer/cos".into(), TelemetryDataType::Float64).await?;
let sin_tlm_handle = tlm
.register("simple_producer/sin".into(), TelemetryDataType::Float32)
.await?;
let cos_tlm_handle = tlm
.register("simple_producer/cos".into(), TelemetryDataType::Float64)
.await?;
let sin2_tlm_handle = tlm.register("simple_producer/sin2".into(), TelemetryDataType::Float32).await?;
let cos2_tlm_handle = tlm.register("simple_producer/cos2".into(), TelemetryDataType::Float64).await?;
let sin2_tlm_handle = tlm
.register("simple_producer/sin2".into(), TelemetryDataType::Float32)
.await?;
let cos2_tlm_handle = tlm
.register("simple_producer/cos2".into(), TelemetryDataType::Float64)
.await?;
let sin3_tlm_handle = tlm.register("simple_producer/sin3".into(), TelemetryDataType::Float32).await?;
let cos3_tlm_handle = tlm.register("simple_producer/cos3".into(), TelemetryDataType::Float64).await?;
let sin3_tlm_handle = tlm
.register("simple_producer/sin3".into(), TelemetryDataType::Float32)
.await?;
let cos3_tlm_handle = tlm
.register("simple_producer/cos3".into(), TelemetryDataType::Float64)
.await?;
let sin4_tlm_handle = tlm.register("simple_producer/sin4".into(), TelemetryDataType::Float32).await?;
let cos4_tlm_handle = tlm.register("simple_producer/cos4".into(), TelemetryDataType::Float64).await?;
let sin4_tlm_handle = tlm
.register("simple_producer/sin4".into(), TelemetryDataType::Float32)
.await?;
let cos4_tlm_handle = tlm
.register("simple_producer/cos4".into(), TelemetryDataType::Float64)
.await?;
let sin5_tlm_handle = tlm.register("simple_producer/sin5".into(), TelemetryDataType::Float32).await?;
let cos5_tlm_handle = tlm.register("simple_producer/cos5".into(), TelemetryDataType::Float64).await?;
let sin5_tlm_handle = tlm
.register("simple_producer/sin5".into(), TelemetryDataType::Float32)
.await?;
let cos5_tlm_handle = tlm
.register("simple_producer/cos5".into(), TelemetryDataType::Float64)
.await?;
let sin6_tlm_handle = tlm.register("simple_producer/sin6".into(), TelemetryDataType::Float32).await?;
let cos6_tlm_handle = tlm.register("simple_producer/cos6".into(), TelemetryDataType::Float64).await?;
let sin6_tlm_handle = tlm
.register("simple_producer/sin6".into(), TelemetryDataType::Float32)
.await?;
let cos6_tlm_handle = tlm
.register("simple_producer/cos6".into(), TelemetryDataType::Float64)
.await?;
let cancellation_token = CancellationToken::new();
{
@@ -152,78 +193,78 @@ async fn main() -> Result<(), Box<dyn Error>> {
next_time += Duration::from_millis(100);
index += 10;
tokio::time::sleep_until(next_time).await;
sin_tlm_handle.publish(
Value::Float32(
(f32::TAU() * (index as f32) / (1000.0_f32)).sin()
),
chrono::Utc::now(),
).await?;
cos_tlm_handle.publish(
Value::Float64(
(f64::TAU() * (index as f64) / (1000.0_f64)).cos()
),
chrono::Utc::now(),
).await?;
sin2_tlm_handle.publish(
Value::Float32(
(f32::TAU() * (index as f32) / (500.0_f32)).sin()
),
chrono::Utc::now(),
).await?;
cos2_tlm_handle.publish(
Value::Float64(
(f64::TAU() * (index as f64) / (500.0_f64)).cos()
),
chrono::Utc::now(),
).await?;
sin3_tlm_handle.publish(
Value::Float32(
(f32::TAU() * (index as f32) / (333.0_f32)).sin()
),
chrono::Utc::now(),
).await?;
cos3_tlm_handle.publish(
Value::Float64(
(f64::TAU() * (index as f64) / (333.0_f64)).cos()
),
chrono::Utc::now(),
).await?;
sin4_tlm_handle.publish(
Value::Float32(
(f32::TAU() * (index as f32) / (250.0_f32)).sin()
),
chrono::Utc::now(),
).await?;
cos4_tlm_handle.publish(
Value::Float64(
(f64::TAU() * (index as f64) / (250.0_f64)).cos()
),
chrono::Utc::now(),
).await?;
sin5_tlm_handle.publish(
Value::Float32(
(f32::TAU() * (index as f32) / (200.0_f32)).sin()
),
chrono::Utc::now(),
).await?;
cos5_tlm_handle.publish(
Value::Float64(
(f64::TAU() * (index as f64) / (200.0_f64)).cos()
),
chrono::Utc::now(),
).await?;
sin6_tlm_handle.publish(
Value::Float32(
(f32::TAU() * (index as f32) / (166.0_f32)).sin()
),
chrono::Utc::now(),
).await?;
cos6_tlm_handle.publish(
Value::Float64(
(f64::TAU() * (index as f64) / (166.0_f64)).cos()
),
chrono::Utc::now(),
).await?;
sin_tlm_handle
.publish(
Value::Float32((f32::TAU() * (index as f32) / (1000.0_f32)).sin()),
chrono::Utc::now(),
)
.await?;
cos_tlm_handle
.publish(
Value::Float64((f64::TAU() * (index as f64) / (1000.0_f64)).cos()),
chrono::Utc::now(),
)
.await?;
sin2_tlm_handle
.publish(
Value::Float32((f32::TAU() * (index as f32) / (500.0_f32)).sin()),
chrono::Utc::now(),
)
.await?;
cos2_tlm_handle
.publish(
Value::Float64((f64::TAU() * (index as f64) / (500.0_f64)).cos()),
chrono::Utc::now(),
)
.await?;
sin3_tlm_handle
.publish(
Value::Float32((f32::TAU() * (index as f32) / (333.0_f32)).sin()),
chrono::Utc::now(),
)
.await?;
cos3_tlm_handle
.publish(
Value::Float64((f64::TAU() * (index as f64) / (333.0_f64)).cos()),
chrono::Utc::now(),
)
.await?;
sin4_tlm_handle
.publish(
Value::Float32((f32::TAU() * (index as f32) / (250.0_f32)).sin()),
chrono::Utc::now(),
)
.await?;
cos4_tlm_handle
.publish(
Value::Float64((f64::TAU() * (index as f64) / (250.0_f64)).cos()),
chrono::Utc::now(),
)
.await?;
sin5_tlm_handle
.publish(
Value::Float32((f32::TAU() * (index as f32) / (200.0_f32)).sin()),
chrono::Utc::now(),
)
.await?;
cos5_tlm_handle
.publish(
Value::Float64((f64::TAU() * (index as f64) / (200.0_f64)).cos()),
chrono::Utc::now(),
)
.await?;
sin6_tlm_handle
.publish(
Value::Float32((f32::TAU() * (index as f32) / (166.0_f32)).sin()),
chrono::Utc::now(),
)
.await?;
cos6_tlm_handle
.publish(
Value::Float64((f64::TAU() * (index as f64) / (166.0_f64)).cos()),
chrono::Utc::now(),
)
.await?;
}
Ok(())