diff --git a/server/Cargo.toml b/server/Cargo.toml index 60b097f..6c1cd6e 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -10,3 +10,4 @@ async-std = { version = "1.12.0", features = ["attributes"] } local-ip-address = "0.5.6" serde = { version = "1.0.193", features = ["derive"] } tide = "0.16.0" +lcd-writer = { path = "../lcd-writer" } diff --git a/server/src/main.rs b/server/src/main.rs index deef898..b5bbe3c 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,16 +1,34 @@ use local_ip_address::local_ip; +use std::sync::{Arc, Mutex}; use tide::prelude::*; use tide::Request; +use lcd_writer::lcd::Lcd; + +#[derive(Clone)] +struct App { + values: Vec, + lcd: Arc>, +} + +impl App { + fn new(lcd: Lcd) -> Self { + App { + values: Vec::new(), + lcd: Arc::new(Mutex::new(lcd)), + } + } +} + #[derive(Debug, Deserialize)] struct Sample { - message: String, value: i32, } #[async_std::main] 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); if let Ok(ip) = local_ip() { println!("Starting server at {ip}:8000!"); @@ -21,8 +39,16 @@ async fn main() -> tide::Result<()> { Ok(()) } -async fn send_sample(mut req: Request<()>) -> tide::Result { - let Sample { message, value } = req.body_json().await?; - println!("Ok, message: {}, value: {}", message, value); - Ok(format!("Ok, message: {}, value: {}", message, value).into()) +async fn send_sample(mut req: Request) -> tide::Result { + let Sample { value } = req.body_json().await?; + + 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()) }