From 357437b36c0fa3859b56197e5a92d02a32879a4f Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Sat, 7 Dec 2024 14:03:16 -0500 Subject: [PATCH] show device status --- server/Cargo.lock | 260 ++++++++++++++++++++++++++++++++++++++++++--- server/Cargo.toml | 1 + server/src/main.rs | 128 ++++++++++++++++++---- 3 files changed, 351 insertions(+), 38 deletions(-) diff --git a/server/Cargo.lock b/server/Cargo.lock index f0ef608..06a9648 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -116,7 +116,7 @@ dependencies = [ "miniz_oxide", "object", "rustc-demangle", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -213,6 +213,21 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "futures" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.31" @@ -220,6 +235,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -228,6 +244,40 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" + +[[package]] +name = "futures-macro" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" + [[package]] name = "futures-task" version = "0.3.31" @@ -240,10 +290,16 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" dependencies = [ + "futures-channel", "futures-core", + "futures-io", + "futures-macro", + "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", + "slab", ] [[package]] @@ -417,6 +473,18 @@ dependencies = [ "adler2", ] +[[package]] +name = "mio" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys 0.48.0", +] + [[package]] name = "mio" version = "1.0.3" @@ -425,7 +493,7 @@ checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" dependencies = [ "libc", "wasi", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -485,9 +553,15 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets", + "windows-targets 0.52.6", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "percent-encoding" version = "2.3.1" @@ -506,6 +580,19 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "ping-rs" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d873f038f84371f9c7fa13f6afea4d5f1fbcd5070ba8eb7af2a6d41c768eff8b" +dependencies = [ + "futures", + "mio 0.8.11", + "paste", + "socket2 0.4.10", + "windows", +] + [[package]] name = "postcard" version = "1.1.1" @@ -652,6 +739,7 @@ dependencies = [ "anyhow", "axum", "common", + "ping-rs", "postcard", "serde", "tokio", @@ -678,12 +766,31 @@ dependencies = [ "libc", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + [[package]] name = "smallvec" version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" +[[package]] +name = "socket2" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "socket2" version = "0.5.8" @@ -691,7 +798,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -802,13 +909,13 @@ dependencies = [ "backtrace", "bytes", "libc", - "mio", + "mio 1.0.3", "parking_lot", "pin-project-lite", "signal-hook-registry", - "socket2", + "socket2 0.5.8", "tokio-macros", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -960,13 +1067,52 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.43.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04662ed0e3e5630dfa9b26e4cb823b817f1a9addda855d973a9458c236556244" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -975,28 +1121,64 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -1009,24 +1191,72 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" diff --git a/server/Cargo.toml b/server/Cargo.toml index acd12bd..14a5936 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" anyhow = "1.0.94" axum = "0.7.9" common = {path = "../common"} +ping-rs = "0.1.2" postcard = {version = "1.0.0", features = ["alloc"]} serde = "*" tokio = { version = "1.42.0", features = ["full"] } diff --git a/server/src/main.rs b/server/src/main.rs index b75b3f8..4c7c596 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,21 +1,33 @@ -use std::{sync::Arc, time::Duration}; +use std::{net::Ipv4Addr, sync::Arc, time::Duration}; use axum::{extract::State, http::StatusCode, response::Html, routing::{get, post}, serve, Form, Router}; use common::{Name, Request}; +use ping_rs::send_ping_async; use postcard::to_allocvec; -use tokio::{fs, io::{AsyncReadExt, AsyncWriteExt}, net::TcpStream, sync::{RwLock, Semaphore}, time::{self, sleep, Instant}}; +use tokio::{fs, io::{AsyncReadExt, AsyncWriteExt}, join, net::TcpStream, sync::{RwLock, Semaphore}, time::{self, sleep, Instant}}; use anyhow::{Ok, Result}; -use tracing::{error, info, trace, Level}; +use tracing::{error, info, trace, warn, Level}; use tracing_subscriber::{filter, layer::{Filter, SubscriberExt}, util::SubscriberInitExt, Layer}; -const FISH: &'static str = "169.254.2.1:1234"; -const DOOR: &'static str = "169.254.1.1:1234"; +const FISH: &'static str = "192.168.0.12:1234"; +const DOOR: &'static str = "192.168.0.10:1234"; + +#[derive(Default, Debug)] +struct NetStatus { + fish_reachable: bool, + fish_ping: Option, + door_reachable: bool, + door_ping: Option, + router_reachable: bool, + router_ping: Option, +} #[derive(Clone)] struct AppState { fish: Arc, last_badge: Arc>, + net_status: Arc>, } #[tokio::main] @@ -24,20 +36,25 @@ async fn main() -> Result<()>{ let (non_blocking, _guard) = tracing_appender::non_blocking(log.into_std().await); - let filter = filter::Targets::new() + let stdout_filter = filter::Targets::new() + .with_target("server", Level::TRACE) + .with_target("mio::poll", Level::WARN); + + let log_filter = stdout_filter.clone() .with_target("server", Level::INFO); let stdout = tracing_subscriber::fmt::layer() .compact() .with_file(true) - .with_target(true); + .with_target(true) + .with_filter(stdout_filter); let log = tracing_subscriber::fmt::layer() .compact() .with_file(false) .with_target(true) .with_writer(non_blocking) - .with_filter(filter); + .with_filter(log_filter); let reg = tracing_subscriber::registry() .with(stdout) @@ -47,6 +64,7 @@ async fn main() -> Result<()>{ let fish = Arc::new(Semaphore::new(1)); let last_badge = Arc::new(RwLock::new((0u64,Instant::now()))); + let network_status = Arc::new(RwLock::new(NetStatus::default())); info!("starting"); @@ -63,7 +81,8 @@ async fn main() -> Result<()>{ //.route("/map", post(set_badge)) .with_state(AppState { fish: fish.clone(), - last_badge: last_badge.clone() + last_badge: last_badge.clone(), + net_status: network_status.clone(), }); let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap(); @@ -71,7 +90,6 @@ async fn main() -> Result<()>{ tokio::spawn(async {serve(listener, app).await.unwrap()}); - let fish_ = fish.clone(); tokio::spawn(async move { loop { @@ -87,6 +105,44 @@ async fn main() -> Result<()>{ } }); + tokio::spawn(async move { + loop { + let fish = Ipv4Addr::new(192,168,0,12).into(); + let door = Ipv4Addr::new(192,168,0,10).into(); + let router = Ipv4Addr::new(192,168,0,50).into(); + let (fish,door,router) = join!( + send_ping_async(&fish, Duration::from_millis(1500), Arc::new(&[255,127]), None), + send_ping_async(&door, Duration::from_millis(1500), Arc::new(&[255,127]), None), + send_ping_async(&router, Duration::from_millis(1500), Arc::new(&[255,127]), None) + ); + + let status = NetStatus { + fish_reachable: fish.is_ok(), + door_reachable: door.is_ok(), + router_reachable: router.is_ok(), + fish_ping: fish.ok().map(|p| p.rtt), + door_ping: door.ok().map(|p| p.rtt), + router_ping: router.ok().map(|p| p.rtt), + }; + + let mut last = network_status.write().await; + + if last.fish_reachable != status.fish_reachable { + warn!("fish connected: {}", status.fish_reachable); + } + if last.door_reachable != status.door_reachable { + warn!("door connected: {}", status.door_reachable); + } + if last.router_reachable != status.router_reachable { + warn!("router connected: {}", status.router_reachable); + } + + *last = status; + drop(last); + sleep(Duration::from_secs(1)).await; + } + }); + /*{ let _permit = fish.acquire().await?; let mut conn = TcpStream::connect(FISH).await?; @@ -138,14 +194,10 @@ async fn root() -> Html { Html::from(page) } async fn names(state: State) -> Html { - let (last_badge, time) = *state.last_badge.read().await; - let time = time.elapsed(); + let last_badge_view = last_badge_view(&state.last_badge).await; let mut page = format!(r#" -

- most recent badge {last_badge:#0x}
- {time:?} ago -

+ {last_badge_view}

set last badge

"#); @@ -177,12 +229,26 @@ async fn names(state: State) -> Html { } async fn control(state: State) -> Html { - let (last_badge, time) = *state.last_badge.read().await; - let time = time.elapsed(); + let last_badge_view = last_badge_view(&state.last_badge).await; + + let netstat = state.net_status.read().await; + + let door_color = if netstat.door_reachable {"green"} else {"red"}; + let fish_color = if netstat.fish_reachable {"green"} else {"red"}; + let router_color = if netstat.router_reachable {"green"} else {"red"}; + let fish_ping = if let Some(ping) = netstat.fish_ping {&format!("ping: {ping}ms")} else {"disconnected"}; + let door_ping = if let Some(ping) = netstat.door_ping {&format!("ping: {ping}ms")} else {"disconnected"}; + let router_ping = if let Some(ping) = netstat.router_ping {&format!("ping: {ping}ms")} else {"offline"}; + let page = format!(r#" JOHNADMIN

JOHN ADMIN PANEL

+

+ lovesense {router_ping}
+ door {door_ping}
+ fish {fish_ping} +

@@ -190,22 +256,38 @@ async fn control(state: State) -> Html { -

- most recent badge {last_badge:#0x}
- {time:?} ago -

+ {last_badge_view} "#); Html::from(page) } +async fn last_badge_view(last_badge: &Arc>) -> String { + let (last_badge, time) = *last_badge.read().await; + let time = time.elapsed().as_secs(); + if last_badge == 0 { + format!(r#" +

+ no badges scanned, up {time}s +

+ "#) + } else { + format!(r#" +

+ most recent badge {last_badge:#0x}
+ {time}s ago +

+ "#) + } +} + #[derive(serde::Deserialize)] struct Volume { volume: u8, } -async fn open_door(state: State) -> Html<&'static str> { +async fn open_door() -> Html<&'static str> { info!("opening door"); let mut conn = TcpStream::connect(DOOR).await.unwrap();