Added firmware

This commit is contained in:
Sebastian 2024-07-28 21:41:42 +02:00
parent f2fe686b0e
commit 4fb87dbae1
15 changed files with 2170 additions and 163 deletions

View file

@ -1,150 +0,0 @@
mod logger;
mod rotctlprotocol;
mod rotor;
use anyhow::Result;
use fern::colors::{Color, ColoredLevelConfig};
use log::{debug, error, info, warn, Level};
use serde_json::{json, Value};
use std::{borrow::Borrow, io};
use tokio::{
self,
io::{AsyncBufReadExt, AsyncWriteExt, BufStream},
net::{TcpListener, TcpStream},
sync::{mpsc, watch},
task::JoinSet,
};
use axum::{
extract::State,
http::StatusCode,
routing::{get, post},
Json, Router,
};
use tower_http::{
services::{ServeDir, ServeFile},
trace::TraceLayer,
};
use logger::setup_logger;
use rotor::control_rotor;
use rotctlprotocol::{parse_command, Command};
async fn process_socket(
socket: TcpStream,
cmd_tx: mpsc::Sender<Command>,
mut pos_rx: watch::Receiver<(f32, f32)>,
) {
let mut stream = BufStream::new(socket);
let mut line = String::new();
loop {
if let Ok(n) = stream.read_line(&mut line).await {
if n == 0 {
return;
}
debug!("Received: {}", line.strip_suffix("\n").unwrap());
match parse_command(&line) {
Ok(cmd) => match cmd {
Command::GetPos => {
let (az, el) = pos_rx.borrow().clone();
stream
.write_all(format!("{}\n{}\n", az, el).as_bytes())
.await
.unwrap();
stream.flush().await.unwrap();
}
Command::Exit => {
stream.write_all("RPRT 0\n".as_bytes()).await.unwrap();
stream.flush().await.unwrap();
return;
}
cmd => {
cmd_tx.send(cmd).await.unwrap();
stream.write_all("RPRT 0\n".as_bytes()).await.unwrap();
stream.flush().await.unwrap();
}
},
Err(msg) => {
error!("Unable to parse input:\n{}", msg);
stream.write_all("RPRT 6\n".as_bytes()).await.unwrap();
stream.flush().await.unwrap();
}
}
line.clear();
} else {
return;
}
}
}
#[derive(Clone)]
struct AxumAppState {
pos_rx: watch::Receiver<(f32, f32)>,
}
#[tokio::main]
async fn main() -> Result<()> {
setup_logger()?;
let (cmd_tx, cmd_rx) = mpsc::channel::<Command>(16);
let (pos_tx, pos_rx) = watch::channel::<(f32, f32)>((0.0, 0.0));
let mut tasks = JoinSet::new();
tasks.spawn(async move { control_rotor(cmd_rx, pos_tx).await });
let state = AxumAppState {
pos_rx: pos_rx.clone(),
};
tasks.spawn(async move {
let app = Router::new()
.route_service("/", ServeFile::new("assets/index.html"))
.route("/state", get(get_state))
.with_state(state)
.layer(TraceLayer::new_for_http());
let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await?;
axum::serve(listener, app).await?;
Ok(())
});
tasks.spawn(async move {
let listener = TcpListener::bind("127.0.0.1:1337").await?;
loop {
let (socket, _) = listener.accept().await?;
let cmd_tx = cmd_tx.clone();
let pos_rx = pos_rx.clone();
tokio::spawn(async move {
process_socket(socket, cmd_tx, pos_rx).await;
});
}
});
while let Some(res) = tasks.join_next().await {
res.unwrap();
}
Ok(())
}
async fn get_state(State(state): State<AxumAppState>) -> Json<Value> {
let (az, el) = state.pos_rx.borrow().clone();
Json(json!({
"position" : {
"az": az,
"el": el
}
}))
}