add lcd-writer to server
This commit is contained in:
parent
ceaeb97787
commit
ca9c23b82b
@ -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" }
|
||||
|
@ -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<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)]
|
||||
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<App>) -> 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())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user