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

54 lines
1.4 KiB
Rust

use api::client::command::CommandRegistry;
use api::client::Client;
use api::macros::IntoCommandDefinition;
use api::messages::command::CommandHeader;
use log::info;
use std::error::Error;
use std::sync::Arc;
use tokio_util::sync::CancellationToken;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
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 client = Arc::new(Client::connect("ws://localhost:8080/backend")?);
let cmd = CommandRegistry::new(client);
let handle = cmd.register_handler("simple_command/a", handle_command);
cancellation_token.cancelled().await;
// This will automatically drop when we return
drop(handle);
Ok(())
}
#[derive(IntoCommandDefinition)]
struct SimpleCommandA {
a: f32,
b: f64,
c: bool,
}
fn handle_command(header: CommandHeader, command: SimpleCommandA) -> anyhow::Result<String> {
let timestamp = header.timestamp;
let SimpleCommandA { a, b, c } = command;
info!("Command Received:\n timestamp: {timestamp}\n a: {a}\n b: {b}\n c: {c}");
// This gets sent back to the source of the command
Ok(format!(
"Successfully Received Command! timestamp: {timestamp} a: {a} b: {b} c: {c}"
))
}