add lcd-writer to server

This commit is contained in:
Simon 2023-12-20 10:50:54 +01:00
parent ceaeb97787
commit ca9c23b82b
2 changed files with 33 additions and 6 deletions

View File

@ -10,3 +10,4 @@ async-std = { version = "1.12.0", features = ["attributes"] }
local-ip-address = "0.5.6" local-ip-address = "0.5.6"
serde = { version = "1.0.193", features = ["derive"] } serde = { version = "1.0.193", features = ["derive"] }
tide = "0.16.0" tide = "0.16.0"
lcd-writer = { path = "../lcd-writer" }

View File

@ -1,16 +1,34 @@
use local_ip_address::local_ip; use local_ip_address::local_ip;
use std::sync::{Arc, Mutex};
use tide::prelude::*; use tide::prelude::*;
use tide::Request; use tide::Request;
use lcd_writer::lcd::Lcd;
#[derive(Clone)]
struct App {
values: Vec<i32>,
lcd: Arc<Mutex<Lcd>>,
}
impl App {
fn new(lcd: Lcd) -> Self {
App {
values: Vec::new(),
lcd: Arc::new(Mutex::new(lcd)),
}
}
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct Sample { struct Sample {
message: String,
value: i32, value: i32,
} }
#[async_std::main] #[async_std::main]
async fn main() -> tide::Result<()> { async fn main() -> tide::Result<()> {
let mut app = tide::new(); let lcd = Lcd::new()?;
let mut app = tide::with_state(App::new(lcd));
app.at("/sample").post(send_sample); app.at("/sample").post(send_sample);
if let Ok(ip) = local_ip() { if let Ok(ip) = local_ip() {
println!("Starting server at {ip}:8000!"); println!("Starting server at {ip}:8000!");
@ -21,8 +39,16 @@ async fn main() -> tide::Result<()> {
Ok(()) Ok(())
} }
async fn send_sample(mut req: Request<()>) -> tide::Result { async fn send_sample(mut req: Request<App>) -> tide::Result {
let Sample { message, value } = req.body_json().await?; let Sample { value } = req.body_json().await?;
println!("Ok, message: {}, value: {}", message, value);
Ok(format!("Ok, message: {}, value: {}", message, value).into()) let lcd = req.state().lcd.lock()?;
lcd.clear();
lcd.set_cursor(0, 0);
lcd.write(format!("Value: {}", value).as_str());
req.state().values.push(value);
println!("Ok, value: {}", value);
Ok(format!("Ok, value: {}", value).into())
} }