Start work on webui

This commit is contained in:
Austen Adler 2021-08-08 14:15:38 -04:00
parent 0b03474308
commit 09ac4e37fd
6 changed files with 63 additions and 8 deletions

View File

@ -1,17 +1,15 @@
[package]
name = "aw-lights"
version = "0.1.0"
authors = ["root"]
authors = ["Austen Adler <agadler@austenadler.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rppal = "0.7"
ws2818-rgb-led-spi-driver = { path = "lib-ws2818-rgb-led-spi-driver/" }
# ws2818-rgb-led-spi-driver = { path = "ws2818-rgb-led-spi-driver/" }
# ws2818-rgb-led-spi-driver = { path = "/home/pi/tt/" }
# ws2818-rgb-led-spi-driver = { path = "/home/pi/ws2818-rgb-led-spi-driver/" }
serde = {version = "1.0", features = ["derive"]}
actix-web = {version = "3", default_features = false}
rust-embed="6.0.0"
[target.armv7-unknown-linux-gnueabihf]
linker = "armv7-unknown-linux-gnueabihf"

View File

@ -2,7 +2,7 @@ CMD="$(cat <<'EOF'
set -euo pipefail
HEIGHT="$(($(tput lines) - 1))"
clear
for i in fmt build clippy; do
for i in check fmt build clippy; do
echo "+ cargo "${i}""
cargo --color=always "${i}" |& head -n "${HEIGHT}"
done

1
html/index.html Normal file
View File

@ -0,0 +1 @@
hi

View File

@ -31,7 +31,9 @@ mod errors;
mod pattern;
mod strip;
mod ui;
mod webui;
use errors::ProgramError;
use std::io;
use std::sync::mpsc::channel;
use std::thread;
use strip::{LedStrip, Message};
@ -56,5 +58,12 @@ fn main() -> Result<(), ProgramError> {
let _console_ui_handle =
thread::spawn(move || -> Result<(), ProgramError> { console_ui_loop(&tx) });
// I do not care if the web ui crashes
let _web_ui_handle = thread::spawn(move || -> Result<(), io::Error> {
webui::start()
// TODO: Do not join -- this is just because we are running on a computer with no spi env
})
.join()?;
strip_handle.join()?
}

View File

@ -144,7 +144,6 @@ impl LedStrip {
}
}
// TODO: Handle error properly
if self
.pattern
.step()

48
src/webui.rs Normal file
View File

@ -0,0 +1,48 @@
use actix_web::web::Form;
use actix_web::{get, post, web, App, HttpRequest, HttpServer, Responder};
use rust_embed::RustEmbed;
use serde::Deserialize;
// TODO: Pre-compute binary somehow? So do not have to unwrap
#[derive(RustEmbed)]
#[folder = "html/"]
struct Asset;
struct AppState {
app_name: String,
}
#[derive(Deserialize, Debug)]
struct ColorForm {
color: String,
}
#[post("/setcolor")]
async fn set_color(data: web::Data<AppState>, params: Form<ColorForm>) -> impl Responder {
format!("{:?}", params)
}
#[get("/")]
async fn test(data: web::Data<AppState>, req: HttpRequest) -> impl Responder {
// TODO: This is probably the ugliest possible way to do it. Make this better
String::from_utf8((&Asset::get("index.html").unwrap().data).to_vec()).unwrap()
}
#[actix_web::main]
pub async fn start() -> std::io::Result<()> {
println!("Starting webui");
HttpServer::new(|| {
App::new()
.data(AppState {
app_name: String::from("abawse"),
})
.service(test)
.service(set_color)
// App::new()
// .route("/", web::get().to(greet))
// .route("/{name}", web::get().to(greet))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}