improved integration with telem viz
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use anyhow::bail;
|
||||
use api::client::Client;
|
||||
use api::client::command::CommandRegistry;
|
||||
use api::macros::IntoCommandDefinition;
|
||||
use chrono::TimeDelta;
|
||||
use itertools::Itertools;
|
||||
use chrono::{DateTime, Utc};
|
||||
use log::{error, trace};
|
||||
use nautilus_common::command::{Command, OwnedCommandHeader, SetPin, ValidPriorityCommand};
|
||||
use nautilus_common::udp::tokio::AsyncUdpSocketExt;
|
||||
@@ -15,6 +15,8 @@ use tokio::sync::RwLock;
|
||||
use tokio::sync::mpsc::{Sender, channel};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
const MAX_DATETIME: DateTime<Utc> = DateTime::from_timestamp_nanos(i64::MAX);
|
||||
|
||||
pub struct CommandHandler<'a> {
|
||||
cmd: CommandRegistry,
|
||||
flight_addr: &'a RwLock<SocketAddr>,
|
||||
@@ -24,6 +26,7 @@ pub struct CommandHandler<'a> {
|
||||
|
||||
#[derive(IntoCommandDefinition)]
|
||||
struct SetPinCommand {
|
||||
index: u8,
|
||||
state: bool,
|
||||
}
|
||||
|
||||
@@ -50,23 +53,27 @@ impl<'a> CommandHandler<'a> {
|
||||
runtime.block_on(async move {
|
||||
let (outgoing_commands_tx, mut outgoing_commands_rx) = channel::<OwnedCommandHeader>(16);
|
||||
|
||||
let commands = ["a", "b"].iter().cartesian_product(0..16)
|
||||
.map(|(bank, i)| {
|
||||
let mut commands = ["a", "b"].iter()
|
||||
.map(|bank| {
|
||||
let command_name = format!("/mcp23017{bank}/set");
|
||||
let outgoing_commands_tx = outgoing_commands_tx.clone();
|
||||
self.cmd.register_handler(
|
||||
format!("switch.{bank}.{i}.set"),
|
||||
move |header, cmd: SetPinCommand| -> anyhow::Result<_> {
|
||||
trace!("Setting Switch {bank} {i}");
|
||||
format!("switch.{bank}.set"),
|
||||
move |_, cmd: SetPinCommand| -> anyhow::Result<_> {
|
||||
trace!("Setting Switch {bank} {}", cmd.index);
|
||||
|
||||
if cmd.index >= 16 {
|
||||
bail!("Invalid Pin Number: {}", cmd.index)
|
||||
}
|
||||
|
||||
outgoing_commands_tx.try_send_command(
|
||||
&command_name,
|
||||
&ValidPriorityCommand {
|
||||
inner: SetPin {
|
||||
pin: i,
|
||||
pin: cmd.index,
|
||||
value: cmd.state,
|
||||
},
|
||||
valid_until: header.timestamp + TimeDelta::seconds(5),
|
||||
valid_until: MAX_DATETIME, // header.timestamp + TimeDelta::seconds(5),
|
||||
priority: 0,
|
||||
}
|
||||
)?;
|
||||
@@ -77,6 +84,14 @@ impl<'a> CommandHandler<'a> {
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
commands.push(self.cmd.register_handler("shutdown", move |_, ()| -> anyhow::Result<_> {
|
||||
trace!("Shutting Down Flight");
|
||||
|
||||
outgoing_commands_tx.try_send_command("/shutdown", &())?;
|
||||
|
||||
Ok("Command Executed Successfully".to_string())
|
||||
}));
|
||||
|
||||
let mut buffer = [0u8; 512];
|
||||
while !self.cancel.is_cancelled() {
|
||||
select! {
|
||||
@@ -86,7 +101,7 @@ impl<'a> CommandHandler<'a> {
|
||||
None => break,
|
||||
Some(outgoing) => {
|
||||
match self.udp.send_postcard(&outgoing, &mut buffer, *self.flight_addr.read().await).await {
|
||||
Ok(()) => {},
|
||||
Ok(_) => {},
|
||||
Err(err) => error!("Failed to Send Outgoing {err}"),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user