increase command flexibility

This commit is contained in:
2025-11-30 08:02:39 -08:00
parent eefc3293b4
commit d53d78434c
17 changed files with 422 additions and 220 deletions

View File

@@ -4,7 +4,10 @@ version = "0.1.0"
edition = "2024"
[dependencies]
anyhow = "1.0.100"
anyhow = { workspace = true }
nautilus_common = { path = "../common" }
log = "0.4.28"
ciborium = { version = "0.2.2" }
log = { workspace = true }
chrono = { workspace = true }
postcard = { workspace = true }
hex = "0.4.3"
serde = { workspace = true }

View File

@@ -1,14 +1,11 @@
#![warn(
clippy::all,
clippy::pedantic,
)]
#![warn(clippy::all, clippy::pedantic)]
use anyhow::Result;
use chrono::{TimeDelta, Utc};
use log::{error, info};
use nautilus_common::add_ctrlc_handler;
use nautilus_common::command::Command;
use nautilus_common::command::{SetPin, ValidPriorityCommand};
use nautilus_common::telemetry::Telemetry;
use nautilus_common::udp::{UdpRecvCborError, UdpSocketExt};
use std::io::Cursor;
use nautilus_common::udp::{UdpRecvPostcardError, UdpSocketExt};
use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
@@ -33,14 +30,33 @@ pub fn run() -> Result<()> {
let udp = UdpSocket::bind(bind_addr)?;
udp.set_read_timeout(Some(Duration::from_millis(100)))?;
let mut buffer = Cursor::new([0u8; 512]);
let mut buffer = [0u8; 512];
let mut do_once = true;
while running.load(Ordering::Relaxed) {
match udp.recv_cbor::<Telemetry, _>(&mut buffer) {
match udp.recv_postcard::<Telemetry>(&mut buffer) {
Ok((tlm, addr)) => {
flight_addr = Some(addr);
info!("{tlm:?}");
if do_once {
do_once = false;
udp.send_command(
"/mcp23017a/set",
&ValidPriorityCommand {
inner: SetPin {
pin: 0,
value: true,
},
valid_until: Utc::now() + TimeDelta::seconds(5),
priority: 0,
},
addr,
)?;
}
}
Err(UdpRecvCborError::NoData) => {
Err(UdpRecvPostcardError::NoData) => {
// NoOp
}
Err(err) => {
@@ -50,8 +66,40 @@ pub fn run() -> Result<()> {
}
if let Some(flight_addr) = flight_addr {
let cmd = Command::Shutdown;
udp.send_cbor(&cmd, &mut buffer, flight_addr)?;
udp.send_command("/shutdown", &(), flight_addr)?;
// let cmd_data = CommandData::Shutdown;
// udp.send_postcard(&cmd_data, &mut buffer, flight_addr)?;
// let cmd_data = SetPin {
// pin: 4,
// value: true,
// valid_until: Utc::now(),
// priority: 120,
// };
// let cmd_data = postcard::to_allocvec(&cmd_data)?;
//
// let cmd = CommandHeader {
// name: "/shutdown",
// data: &cmd_data,
// };
//
// let encoded = postcard::to_allocvec(&cmd)?;
// println!("{}", hex::encode(&encoded));
//
// let decoded = postcard::from_bytes::<CommandHeader>(&encoded)?;
// println!("{decoded:?}");
//
// let (decoded, remainder) = postcard::take_from_bytes::<SetPin>(decoded.data)?;
// ensure!(remainder.is_empty(), "Not all command bytes consumed");
// println!("{decoded:?}");
// let mut another_buffer = Cursor::new([0u8; 512]);
// // ciborium::into_writer(&cmd, &mut another_buffer)?;
// let _ = Serializer::writer(&cmd, &mut another_buffer)?;
// let size_encoded = usize::try_from(another_buffer.position())?;
// let _ = test(&another_buffer.get_ref()[0..size_encoded])?;
}
Ok(())