add telemetry producer to api
This commit is contained in:
@@ -1,276 +1,215 @@
|
||||
use api::client::error::MessageError;
|
||||
use api::client::telemetry::Telemetry;
|
||||
use api::client::Client;
|
||||
use api::data_type::DataType;
|
||||
use api::data_value::DataValue;
|
||||
use api::messages::telemetry_definition::TelemetryDefinitionRequest;
|
||||
use api::messages::telemetry_entry::TelemetryEntry;
|
||||
use chrono::{DateTime, TimeDelta, Utc};
|
||||
use chrono::{TimeDelta, Utc};
|
||||
use futures_util::future::join_all;
|
||||
use num_traits::FloatConst;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::{oneshot, RwLock};
|
||||
use tokio::time::{sleep_until, Instant};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use uuid::Uuid;
|
||||
|
||||
struct Telemetry {
|
||||
client: Arc<Client>,
|
||||
}
|
||||
|
||||
struct TelemetryItemHandle {
|
||||
cancellation_token: CancellationToken,
|
||||
uuid: Arc<RwLock<Uuid>>,
|
||||
client: Arc<Client>,
|
||||
}
|
||||
|
||||
impl Telemetry {
|
||||
pub fn new(client: Arc<Client>) -> Self {
|
||||
Self { client }
|
||||
}
|
||||
|
||||
pub async fn register(
|
||||
&self,
|
||||
name: String,
|
||||
data_type: DataType,
|
||||
) -> anyhow::Result<TelemetryItemHandle> {
|
||||
let cancellation_token = CancellationToken::new();
|
||||
let cancel_token = cancellation_token.clone();
|
||||
let client = self.client.clone();
|
||||
|
||||
let response_uuid = Arc::new(RwLock::new(Uuid::nil()));
|
||||
|
||||
let response_uuid_inner = response_uuid.clone();
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let mut write_lock = Some(response_uuid_inner.write().await);
|
||||
let _ = tx.send(());
|
||||
while !cancel_token.is_cancelled() {
|
||||
if let Ok(response) = client
|
||||
.send_request(TelemetryDefinitionRequest {
|
||||
name: name.clone(),
|
||||
data_type,
|
||||
})
|
||||
.await
|
||||
{
|
||||
let mut lock = match write_lock {
|
||||
None => response_uuid_inner.write().await,
|
||||
Some(lock) => lock,
|
||||
};
|
||||
// Update the value in the lock
|
||||
*lock = response.uuid;
|
||||
// Set this value so the loop works
|
||||
write_lock = None;
|
||||
}
|
||||
|
||||
client.wait_disconnected().await;
|
||||
}
|
||||
});
|
||||
|
||||
// Wait until the write lock is acquired
|
||||
let _ = rx.await;
|
||||
// Wait until the write lock is released for the first time
|
||||
drop(response_uuid.read().await);
|
||||
|
||||
Ok(TelemetryItemHandle {
|
||||
cancellation_token,
|
||||
uuid: response_uuid,
|
||||
client: self.client.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl TelemetryItemHandle {
|
||||
pub async fn publish(&self, value: DataValue, timestamp: DateTime<Utc>) -> anyhow::Result<()> {
|
||||
let Ok(lock) = self.uuid.try_read() else {
|
||||
return Ok(());
|
||||
};
|
||||
let uuid = *lock;
|
||||
drop(lock);
|
||||
|
||||
self.client
|
||||
.send_message_if_connected(TelemetryEntry {
|
||||
uuid,
|
||||
value,
|
||||
timestamp,
|
||||
})
|
||||
.await
|
||||
.or_else(|e| match e {
|
||||
MessageError::TokioLockError(_) => Ok(()),
|
||||
e => Err(e),
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TelemetryItemHandle {
|
||||
fn drop(&mut self) {
|
||||
self.cancellation_token.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
env_logger::init();
|
||||
|
||||
let client = Arc::new(Client::connect("ws://[::1]:8080/backend")?);
|
||||
let tlm = Telemetry::new(client);
|
||||
|
||||
let time_offset = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/time_offset".into(),
|
||||
DataType::Float64,
|
||||
)
|
||||
.await;
|
||||
|
||||
let publish_offset = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/publish_offset".into(),
|
||||
DataType::Float64,
|
||||
)
|
||||
.await;
|
||||
|
||||
let await_offset = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/await_offset".into(),
|
||||
DataType::Float64,
|
||||
)
|
||||
.await;
|
||||
|
||||
let sin_tlm_handle = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/sin".into(),
|
||||
DataType::Float32,
|
||||
)
|
||||
.await;
|
||||
let cos_tlm_handle = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/cos".into(),
|
||||
DataType::Float64,
|
||||
)
|
||||
.await;
|
||||
let bool_tlm_handle = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/bool".into(),
|
||||
DataType::Boolean,
|
||||
)
|
||||
.await;
|
||||
|
||||
let sin2_tlm_handle = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/sin2".into(),
|
||||
DataType::Float32,
|
||||
)
|
||||
.await;
|
||||
let cos2_tlm_handle = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/cos2".into(),
|
||||
DataType::Float64,
|
||||
)
|
||||
.await;
|
||||
|
||||
let sin3_tlm_handle = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/sin3".into(),
|
||||
DataType::Float32,
|
||||
)
|
||||
.await;
|
||||
let cos3_tlm_handle = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/cos3".into(),
|
||||
DataType::Float64,
|
||||
)
|
||||
.await;
|
||||
|
||||
let sin4_tlm_handle = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/sin4".into(),
|
||||
DataType::Float32,
|
||||
)
|
||||
.await;
|
||||
let cos4_tlm_handle = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/cos4".into(),
|
||||
DataType::Float64,
|
||||
)
|
||||
.await;
|
||||
|
||||
let sin5_tlm_handle = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/sin5".into(),
|
||||
DataType::Float32,
|
||||
)
|
||||
.await;
|
||||
let cos5_tlm_handle = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/cos5".into(),
|
||||
DataType::Float64,
|
||||
)
|
||||
.await;
|
||||
|
||||
let sin6_tlm_handle = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/sin6".into(),
|
||||
DataType::Float32,
|
||||
)
|
||||
.await;
|
||||
let cos6_tlm_handle = Telemetry::register(
|
||||
client.clone(),
|
||||
"simple_producer/cos6".into(),
|
||||
DataType::Float64,
|
||||
)
|
||||
.await;
|
||||
|
||||
let cancellation_token = CancellationToken::new();
|
||||
{
|
||||
let time_offset = tlm
|
||||
.register("simple_producer/time_offset".into(), DataType::Float64)
|
||||
.await?;
|
||||
|
||||
let publish_offset = tlm
|
||||
.register("simple_producer/publish_offset".into(), DataType::Float64)
|
||||
.await?;
|
||||
|
||||
let await_offset = tlm
|
||||
.register("simple_producer/await_offset".into(), DataType::Float64)
|
||||
.await?;
|
||||
|
||||
let sin_tlm_handle = tlm
|
||||
.register("simple_producer/sin".into(), DataType::Float32)
|
||||
.await?;
|
||||
let cos_tlm_handle = tlm
|
||||
.register("simple_producer/cos".into(), DataType::Float64)
|
||||
.await?;
|
||||
let bool_tlm_handle = tlm
|
||||
.register("simple_producer/bool".into(), DataType::Boolean)
|
||||
.await?;
|
||||
|
||||
let sin2_tlm_handle = tlm
|
||||
.register("simple_producer/sin2".into(), DataType::Float32)
|
||||
.await?;
|
||||
let cos2_tlm_handle = tlm
|
||||
.register("simple_producer/cos2".into(), DataType::Float64)
|
||||
.await?;
|
||||
|
||||
let sin3_tlm_handle = tlm
|
||||
.register("simple_producer/sin3".into(), DataType::Float32)
|
||||
.await?;
|
||||
let cos3_tlm_handle = tlm
|
||||
.register("simple_producer/cos3".into(), DataType::Float64)
|
||||
.await?;
|
||||
|
||||
let sin4_tlm_handle = tlm
|
||||
.register("simple_producer/sin4".into(), DataType::Float32)
|
||||
.await?;
|
||||
let cos4_tlm_handle = tlm
|
||||
.register("simple_producer/cos4".into(), DataType::Float64)
|
||||
.await?;
|
||||
|
||||
let sin5_tlm_handle = tlm
|
||||
.register("simple_producer/sin5".into(), DataType::Float32)
|
||||
.await?;
|
||||
let cos5_tlm_handle = tlm
|
||||
.register("simple_producer/cos5".into(), DataType::Float64)
|
||||
.await?;
|
||||
|
||||
let sin6_tlm_handle = tlm
|
||||
.register("simple_producer/sin6".into(), DataType::Float32)
|
||||
.await?;
|
||||
let cos6_tlm_handle = tlm
|
||||
.register("simple_producer/cos6".into(), DataType::Float64)
|
||||
.await?;
|
||||
|
||||
let cancellation_token = CancellationToken::new();
|
||||
{
|
||||
let cancellation_token = cancellation_token.clone();
|
||||
tokio::spawn(async move {
|
||||
let _ = tokio::signal::ctrl_c().await;
|
||||
cancellation_token.cancel();
|
||||
println!("Cancellation Token Cancelled");
|
||||
});
|
||||
}
|
||||
|
||||
let start_time = Utc::now();
|
||||
let start_instant = Instant::now();
|
||||
let mut next_time = start_instant;
|
||||
let mut index = 0;
|
||||
let mut tasks = vec![];
|
||||
while !cancellation_token.is_cancelled() {
|
||||
next_time += Duration::from_millis(1000);
|
||||
index += 1;
|
||||
sleep_until(next_time).await;
|
||||
let publish_time = start_time + TimeDelta::from_std(next_time - start_instant).unwrap();
|
||||
let actual_time = Instant::now();
|
||||
tasks.push(time_offset.publish(
|
||||
DataValue::Float64((actual_time - next_time).as_secs_f64()),
|
||||
Utc::now(),
|
||||
));
|
||||
tasks.push(sin_tlm_handle.publish(
|
||||
DataValue::Float32((f32::TAU() * (index as f32) / (1000.0_f32)).sin()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(cos_tlm_handle.publish(
|
||||
DataValue::Float64((f64::TAU() * (index as f64) / (1000.0_f64)).cos()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(
|
||||
bool_tlm_handle.publish(DataValue::Boolean(index % 1000 > 500), publish_time),
|
||||
);
|
||||
tasks.push(sin2_tlm_handle.publish(
|
||||
DataValue::Float32((f32::TAU() * (index as f32) / (500.0_f32)).sin()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(cos2_tlm_handle.publish(
|
||||
DataValue::Float64((f64::TAU() * (index as f64) / (500.0_f64)).cos()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(sin3_tlm_handle.publish(
|
||||
DataValue::Float32((f32::TAU() * (index as f32) / (333.0_f32)).sin()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(cos3_tlm_handle.publish(
|
||||
DataValue::Float64((f64::TAU() * (index as f64) / (333.0_f64)).cos()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(sin4_tlm_handle.publish(
|
||||
DataValue::Float32((f32::TAU() * (index as f32) / (250.0_f32)).sin()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(cos4_tlm_handle.publish(
|
||||
DataValue::Float64((f64::TAU() * (index as f64) / (250.0_f64)).cos()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(sin5_tlm_handle.publish(
|
||||
DataValue::Float32((f32::TAU() * (index as f32) / (200.0_f32)).sin()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(cos5_tlm_handle.publish(
|
||||
DataValue::Float64((f64::TAU() * (index as f64) / (200.0_f64)).cos()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(sin6_tlm_handle.publish(
|
||||
DataValue::Float32((f32::TAU() * (index as f32) / (166.0_f32)).sin()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(cos6_tlm_handle.publish(
|
||||
DataValue::Float64((f64::TAU() * (index as f64) / (166.0_f64)).cos()),
|
||||
publish_time,
|
||||
));
|
||||
|
||||
tasks.push(publish_offset.publish(
|
||||
DataValue::Float64((Instant::now() - actual_time).as_secs_f64()),
|
||||
Utc::now(),
|
||||
));
|
||||
|
||||
// Join the tasks so they all run in parallel
|
||||
for task in join_all(tasks.drain(..)).await {
|
||||
task?;
|
||||
}
|
||||
|
||||
tasks.push(await_offset.publish(
|
||||
DataValue::Float64((Instant::now() - actual_time).as_secs_f64()),
|
||||
Utc::now(),
|
||||
));
|
||||
}
|
||||
println!("Exiting Loop");
|
||||
let cancellation_token = cancellation_token.clone();
|
||||
tokio::spawn(async move {
|
||||
let _ = tokio::signal::ctrl_c().await;
|
||||
cancellation_token.cancel();
|
||||
});
|
||||
}
|
||||
|
||||
let start_time = Utc::now();
|
||||
let start_instant = Instant::now();
|
||||
let mut next_time = start_instant;
|
||||
let mut index = 0;
|
||||
let mut tasks = vec![];
|
||||
while !cancellation_token.is_cancelled() {
|
||||
next_time += Duration::from_millis(10);
|
||||
index += 1;
|
||||
sleep_until(next_time).await;
|
||||
let publish_time = start_time + TimeDelta::from_std(next_time - start_instant).unwrap();
|
||||
let actual_time = Instant::now();
|
||||
tasks.push(time_offset.publish(
|
||||
DataValue::Float64((actual_time - next_time).as_secs_f64()),
|
||||
Utc::now(),
|
||||
));
|
||||
tasks.push(sin_tlm_handle.publish(
|
||||
DataValue::Float32((f32::TAU() * (index as f32) / (1000.0_f32)).sin()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(cos_tlm_handle.publish(
|
||||
DataValue::Float64((f64::TAU() * (index as f64) / (1000.0_f64)).cos()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(bool_tlm_handle.publish(DataValue::Boolean(index % 1000 > 500), publish_time));
|
||||
tasks.push(sin2_tlm_handle.publish(
|
||||
DataValue::Float32((f32::TAU() * (index as f32) / (500.0_f32)).sin()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(cos2_tlm_handle.publish(
|
||||
DataValue::Float64((f64::TAU() * (index as f64) / (500.0_f64)).cos()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(sin3_tlm_handle.publish(
|
||||
DataValue::Float32((f32::TAU() * (index as f32) / (333.0_f32)).sin()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(cos3_tlm_handle.publish(
|
||||
DataValue::Float64((f64::TAU() * (index as f64) / (333.0_f64)).cos()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(sin4_tlm_handle.publish(
|
||||
DataValue::Float32((f32::TAU() * (index as f32) / (250.0_f32)).sin()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(cos4_tlm_handle.publish(
|
||||
DataValue::Float64((f64::TAU() * (index as f64) / (250.0_f64)).cos()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(sin5_tlm_handle.publish(
|
||||
DataValue::Float32((f32::TAU() * (index as f32) / (200.0_f32)).sin()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(cos5_tlm_handle.publish(
|
||||
DataValue::Float64((f64::TAU() * (index as f64) / (200.0_f64)).cos()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(sin6_tlm_handle.publish(
|
||||
DataValue::Float32((f32::TAU() * (index as f32) / (166.0_f32)).sin()),
|
||||
publish_time,
|
||||
));
|
||||
tasks.push(cos6_tlm_handle.publish(
|
||||
DataValue::Float64((f64::TAU() * (index as f64) / (166.0_f64)).cos()),
|
||||
publish_time,
|
||||
));
|
||||
|
||||
tasks.push(publish_offset.publish(
|
||||
DataValue::Float64((Instant::now() - actual_time).as_secs_f64()),
|
||||
Utc::now(),
|
||||
));
|
||||
|
||||
// Join the tasks so they all run in parallel
|
||||
for task in join_all(tasks.drain(..)).await {
|
||||
task?;
|
||||
}
|
||||
|
||||
tasks.push(await_offset.publish(
|
||||
DataValue::Float64((Instant::now() - actual_time).as_secs_f64()),
|
||||
Utc::now(),
|
||||
));
|
||||
}
|
||||
drop(tlm);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user