Files
telemetry_visualization/examples/simple_command/src/main.rs

65 lines
2.5 KiB
Rust

use chrono::DateTime;
use server::core::command_service_client::CommandServiceClient;
use server::core::telemetry_value::Value;
use server::core::{CommandDefinitionRequest, CommandParameterDefinition, TelemetryDataType};
use std::error::Error;
use tokio::select;
use tokio_util::sync::CancellationToken;
use tonic::codegen::tokio_stream::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let cancellation_token = CancellationToken::new();
{
let cancellation_token = cancellation_token.clone();
tokio::spawn(async move {
let _ = tokio::signal::ctrl_c().await;
cancellation_token.cancel();
});
}
let mut client = CommandServiceClient::connect("http://[::1]:50051").await?;
let command_response = client
.new_command(CommandDefinitionRequest {
name: "simple_command/a".to_string(),
parameters: vec![
CommandParameterDefinition {
name: "a".to_string(),
data_type: TelemetryDataType::Float32.into(),
},
CommandParameterDefinition {
name: "b".to_string(),
data_type: TelemetryDataType::Float64.into(),
},
CommandParameterDefinition {
name: "c".to_string(),
data_type: TelemetryDataType::Boolean.into(),
},
],
})
.await?;
let mut command_stream = command_response.into_inner();
loop {
select! {
_ = cancellation_token.cancelled() => {
break;
},
Some(command) = command_stream.next() => {
let command = command?;
let timestamp = command.timestamp.expect("Missing Timestamp");
let timestamp = DateTime::from_timestamp(timestamp.secs, timestamp.nanos as u32).expect("Could not construct date time");
let Value::Float32(a) = command.parameters["a"].value.expect("Missing Value a") else { panic!("Wrong Type a"); };
let Value::Float64(b) = command.parameters["b"].value.expect("Missing Value b") else { panic!("Wrong Type b"); };
let Value::Boolean(c) = command.parameters["c"].value.expect("Missing Value c") else { panic!("Wrong Type c"); };
println!("Command Received:\n timestamp: {timestamp}\n a: {a}\n b: {b}\n c: {c}");
},
}
}
Ok(())
}