Split code into serveral modules

This commit is contained in:
Sebastian 2024-06-29 13:51:29 +02:00
parent ecb5399a42
commit b9437480e1
5 changed files with 91 additions and 72 deletions

0
output.log Normal file
View file

33
src/logger.rs Normal file
View file

@ -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(())
}

View file

@ -1,4 +1,6 @@
mod rotctl; mod logger;
mod rotctlprotocol;
mod rotor;
use anyhow::Result; use anyhow::Result;
use fern::colors::{Color, ColoredLevelConfig}; use fern::colors::{Color, ColoredLevelConfig};
@ -12,7 +14,10 @@ use tokio::{
time, time,
}; };
use rotctl::Command; use logger::setup_logger;
use rotor::control_rotor;
use rotctlprotocol::{parse_command, Command};
async fn process_socket( async fn process_socket(
socket: TcpStream, socket: TcpStream,
@ -31,7 +36,7 @@ async fn process_socket(
debug!("Received: {}", line.strip_suffix("\n").unwrap()); debug!("Received: {}", line.strip_suffix("\n").unwrap());
match rotctl::parse_command(&line) { match parse_command(&line) {
Ok(cmd) => match cmd { Ok(cmd) => match cmd {
Command::GetPos => { Command::GetPos => {
let (az, el) = pos_rx.borrow().clone(); let (az, el) = pos_rx.borrow().clone();
@ -67,74 +72,6 @@ async fn process_socket(
} }
} }
async fn control_rotor(mut rx_cmd: mpsc::Receiver<Command>, 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] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
setup_logger()?; setup_logger()?;
@ -147,7 +84,7 @@ async fn main() -> Result<()> {
control_rotor(cmd_rx, pos_tx).await; 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 { loop {
let (socket, _) = listener.accept().await?; let (socket, _) = listener.accept().await?;

49
src/rotor.rs Normal file
View file

@ -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<Command>, 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
};
}
}