From b9437480e16e72788a28410c962711417ff26575 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 29 Jun 2024 13:51:29 +0200 Subject: [PATCH] Split code into serveral modules --- output.log | 0 src/logger.rs | 33 ++++++++++++ src/main.rs | 81 ++++------------------------ src/{rotctl.rs => rotctlprotocol.rs} | 0 src/rotor.rs | 49 +++++++++++++++++ 5 files changed, 91 insertions(+), 72 deletions(-) create mode 100644 output.log create mode 100644 src/logger.rs rename src/{rotctl.rs => rotctlprotocol.rs} (100%) create mode 100644 src/rotor.rs diff --git a/output.log b/output.log new file mode 100644 index 0000000..e69de29 diff --git a/src/logger.rs b/src/logger.rs new file mode 100644 index 0000000..204e75e --- /dev/null +++ b/src/logger.rs @@ -0,0 +1,33 @@ +use anyhow::Result; +use fern::colors::{Color, ColoredLevelConfig}; +use log::{debug, error, info, warn}; +use std::io; + +pub fn setup_logger() -> Result<()> { + let colors = ColoredLevelConfig::new() + .info(Color::Green) + .error(Color::Red) + .warn(Color::Yellow) + .debug(Color::Blue); + + fern::Dispatch::new() + // Perform allocation-free log formatting + .format(move |out, message, record| { + out.finish(format_args!( + "[{} {} {}] {}", + humantime::format_rfc3339_millis(std::time::SystemTime::now()), + colors.color(record.level()), + record.target(), + message + )) + }) + // Add blanket level filter - + .level(log::LevelFilter::Debug) + // - and per-module overrides + .chain(std::io::stdout()) + .chain(fern::log_file("output.log")?) + // Apply globally + .apply()?; + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 4b2e66a..68a06ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ -mod rotctl; +mod logger; +mod rotctlprotocol; +mod rotor; use anyhow::Result; use fern::colors::{Color, ColoredLevelConfig}; @@ -12,7 +14,10 @@ use tokio::{ time, }; -use rotctl::Command; +use logger::setup_logger; +use rotor::control_rotor; + +use rotctlprotocol::{parse_command, Command}; async fn process_socket( socket: TcpStream, @@ -31,7 +36,7 @@ async fn process_socket( debug!("Received: {}", line.strip_suffix("\n").unwrap()); - match rotctl::parse_command(&line) { + match parse_command(&line) { Ok(cmd) => match cmd { Command::GetPos => { let (az, el) = pos_rx.borrow().clone(); @@ -67,74 +72,6 @@ async fn process_socket( } } -async fn control_rotor(mut rx_cmd: mpsc::Receiver, pos_tx: watch::Sender<(f32, f32)>) { - let mut actual_az = 0.0; - let mut actual_el = 0.0; - - let mut target_az = 0.0; - let mut target_el = 0.0; - - loop { - tokio::select! { - Some(command) = rx_cmd.recv() => { - match command { - Command::SetPos(az, el) => { - info!("Received set pos {} {}", az, el); - target_az = az; - target_el = el; - } - _ => {} - } - }, - _ = time::sleep(time::Duration::from_millis(100)) => { - if target_az < actual_az { - actual_az -= 1.0; - } else if target_az > actual_az { - actual_az += 1.0; - } - - if target_el < actual_el { - actual_el -= 1.0; - } else if target_el > actual_el { - actual_el += 1.0; - } - - pos_tx.send((actual_az, actual_el)).unwrap(); - }, - else => return - }; - } -} - -fn setup_logger() -> Result<()> { - let colors = ColoredLevelConfig::new() - .info(Color::Green) - .error(Color::Red) - .warn(Color::Yellow) - .debug(Color::Blue); - - fern::Dispatch::new() - // Perform allocation-free log formatting - .format(move |out, message, record| { - out.finish(format_args!( - "[{} {} {}] {}", - humantime::format_rfc3339_millis(std::time::SystemTime::now()), - colors.color(record.level()), - record.target(), - message - )) - }) - // Add blanket level filter - - .level(log::LevelFilter::Debug) - // - and per-module overrides - .chain(std::io::stdout()) - .chain(fern::log_file("output.log")?) - // Apply globally - .apply()?; - - Ok(()) -} - #[tokio::main] async fn main() -> Result<()> { setup_logger()?; @@ -147,7 +84,7 @@ async fn main() -> Result<()> { control_rotor(cmd_rx, pos_tx).await; }); - let listener = TcpListener::bind("127.0.0.1:8080").await?; + let listener = TcpListener::bind("127.0.0.1:1337").await?; loop { let (socket, _) = listener.accept().await?; diff --git a/src/rotctl.rs b/src/rotctlprotocol.rs similarity index 100% rename from src/rotctl.rs rename to src/rotctlprotocol.rs diff --git a/src/rotor.rs b/src/rotor.rs new file mode 100644 index 0000000..e24adde --- /dev/null +++ b/src/rotor.rs @@ -0,0 +1,49 @@ +use log::{debug, error, info, warn}; +use tokio::{ + self, + io::{AsyncBufReadExt, AsyncWriteExt, BufStream}, + net::{TcpListener, TcpStream}, + sync::{self, mpsc, watch}, + time, +}; + +use crate::rotctlprotocol::{parse_command, Command}; + +pub async fn control_rotor(mut rx_cmd: mpsc::Receiver, pos_tx: watch::Sender<(f32, f32)>) { + let mut actual_az = 0.0; + let mut actual_el = 0.0; + + let mut target_az = 0.0; + let mut target_el = 0.0; + + loop { + tokio::select! { + Some(command) = rx_cmd.recv() => { + match command { + Command::SetPos(az, el) => { + info!("Received set pos {} {}", az, el); + target_az = az; + target_el = el; + } + _ => {} + } + }, + _ = time::sleep(time::Duration::from_millis(100)) => { + if target_az < actual_az { + actual_az -= 1.0; + } else if target_az > actual_az { + actual_az += 1.0; + } + + if target_el < actual_el { + actual_el -= 1.0; + } else if target_el > actual_el { + actual_el += 1.0; + } + + pos_tx.send((actual_az, actual_el)).unwrap(); + }, + else => return + }; + } +}