added admin panel

This commit is contained in:
Andy Killorin 2024-12-07 01:15:08 -05:00
parent 20fdd480dd
commit e2a28d43f0
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

@ -10,6 +10,7 @@ use tracing::{error, info, trace, Level};
use tracing_subscriber::{filter, layer::{Filter, SubscriberExt}, util::SubscriberInitExt, Layer}; use tracing_subscriber::{filter, layer::{Filter, SubscriberExt}, util::SubscriberInitExt, Layer};
const FISH: &'static str = "169.254.2.1:1234"; const FISH: &'static str = "169.254.2.1:1234";
const DOOR: &'static str = "169.254.1.1:1234";
#[derive(Clone)] #[derive(Clone)]
struct AppState { struct AppState {
@ -51,10 +52,13 @@ async fn main() -> Result<()>{
let app: Router = Router::new() let app: Router = Router::new()
.route("/", get(root)) .route("/names", get(root))
.route("/admin", get(control))
.route("/phil", get(phil)) .route("/phil", get(phil))
.route("/recent", get(recent_badge)) .route("/recent", get(recent_badge))
.route("/setLast", post(set_last)) .route("/setLast", post(set_last))
.route("/openDoor", post(open_door))
.route("/setVolume", post(set_volume))
//.route("/map", post(set_badge)) //.route("/map", post(set_badge))
.with_state(AppState { .with_state(AppState {
fish: fish.clone(), fish: fish.clone(),
@ -82,7 +86,7 @@ async fn main() -> Result<()>{
} }
}); });
{ /*{
let _permit = fish.acquire().await?; let _permit = fish.acquire().await?;
let mut conn = TcpStream::connect(FISH).await?; let mut conn = TcpStream::connect(FISH).await?;
@ -101,7 +105,7 @@ async fn main() -> Result<()>{
let req = Request::SetVolume(30); let req = Request::SetVolume(30);
conn.write_all(&to_allocvec(&req)?).await?; conn.write_all(&to_allocvec(&req)?).await?;
} }*/
loop { loop {
sleep(Duration::from_secs(2)).await; sleep(Duration::from_secs(2)).await;
@ -149,14 +153,78 @@ async fn root(state: State<AppState>) -> Html<String> {
"Zoey", "Zoey",
] { ] {
page.push_str(&format!(r#" page.push_str(&format!(r#"
<form action="setLast" method="post" id="myForm"> <form target="output" style="display:inline; margin-right:10px; " action="setLast" method="post" id="myForm">
<button style="font-size:40px;" type="submit" name="name", value="{name}">{name}</button> <button style="font-size:40px;" type="submit" name="name" value="{name}" target="output">{name}</button>
</form> </form>
"#)); "#));
} }
page.push_str(&format!(r#"
<form target="output" style="margin-top:10px;" action="setLast" method="post" id="myForm">
<input style="font-size:40px;" name="name" placeholder="other" "type="text"></input>
<button style="font-size:40px;" type="submit" target="output">other</button>
</form>
<iframe name="output"></iframe>
"#));
Html::from(page) Html::from(page)
} }
async fn control(state: State<AppState>) -> Html<String> {
let (last_badge, time) = *state.last_badge.read().await;
let time = time.elapsed();
let page = format!(r#"
<head><title>JOHNADMIN</title></head>
<h1>JOHN ADMIN PANEL</h1>
<form target="output" action="openDoor" method="post" id="myForm">
<button style="font-size:40px;" type="submit" name="name" value="open" target="output">Open noor</button>
</form>
<form target="output" action="setVolume" method="post" id="myForm">
<input type="range" min="0" max="30" value="0" name="volume" class="slider" id="volume">
<button style="font-size:30px;" type="submit" target="output">set volume</button>
</form>
<p>
most recent badge {last_badge:#0x}<br>
{time:?} ago
</p>
<iframe name="output"></iframe>
"#);
Html::from(page)
}
#[derive(serde::Deserialize)]
struct Volume {
volume: u8,
}
async fn open_door(state: State<AppState>) -> Html<&'static str> {
info!("opening door");
let mut conn = TcpStream::connect(DOOR).await.unwrap();
conn.write_all(b"B f7c31d1603000000 \r\n").await.unwrap();
conn.flush().await.unwrap();
Html::from("<p>opening the noor</p>")
}
async fn set_volume(
state: State<AppState>,
Form(input): Form<Volume>) -> Html<String> {
info!("setting volume to {}", input.volume);
let _fish = state.fish.acquire().await.unwrap();
let mut conn = TcpStream::connect(FISH).await.unwrap();
let req = Request::SetVolume(input.volume);
conn.write_all(&to_allocvec(&req).unwrap()).await.unwrap();
conn.flush().await.unwrap();
Html::from(format!("<p>volume is now {}</p></br><a href=\"/\">back</a>", input.volume))
}
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
struct NameForm { struct NameForm {
name: String, name: String,