show device status

This commit is contained in:
Andy Killorin 2024-12-07 14:03:16 -05:00
parent 4288d979fc
commit 357437b36c
Signed by: ank
GPG key ID: B6241CA3B552BCA4
3 changed files with 351 additions and 38 deletions

260
server/Cargo.lock generated
View file

@ -116,7 +116,7 @@ dependencies = [
"miniz_oxide", "miniz_oxide",
"object", "object",
"rustc-demangle", "rustc-demangle",
"windows-targets", "windows-targets 0.52.6",
] ]
[[package]] [[package]]
@ -213,6 +213,21 @@ dependencies = [
"percent-encoding", "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]] [[package]]
name = "futures-channel" name = "futures-channel"
version = "0.3.31" version = "0.3.31"
@ -220,6 +235,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-sink",
] ]
[[package]] [[package]]
@ -228,6 +244,40 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" 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]] [[package]]
name = "futures-task" name = "futures-task"
version = "0.3.31" version = "0.3.31"
@ -240,10 +290,16 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [ dependencies = [
"futures-channel",
"futures-core", "futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task", "futures-task",
"memchr",
"pin-project-lite", "pin-project-lite",
"pin-utils", "pin-utils",
"slab",
] ]
[[package]] [[package]]
@ -417,6 +473,18 @@ dependencies = [
"adler2", "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]] [[package]]
name = "mio" name = "mio"
version = "1.0.3" version = "1.0.3"
@ -425,7 +493,7 @@ checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd"
dependencies = [ dependencies = [
"libc", "libc",
"wasi", "wasi",
"windows-sys", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -485,9 +553,15 @@ dependencies = [
"libc", "libc",
"redox_syscall", "redox_syscall",
"smallvec", "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]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.3.1" version = "2.3.1"
@ -506,6 +580,19 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" 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]] [[package]]
name = "postcard" name = "postcard"
version = "1.1.1" version = "1.1.1"
@ -652,6 +739,7 @@ dependencies = [
"anyhow", "anyhow",
"axum", "axum",
"common", "common",
"ping-rs",
"postcard", "postcard",
"serde", "serde",
"tokio", "tokio",
@ -678,12 +766,31 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "slab"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.13.2" version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" 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]] [[package]]
name = "socket2" name = "socket2"
version = "0.5.8" version = "0.5.8"
@ -691,7 +798,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -802,13 +909,13 @@ dependencies = [
"backtrace", "backtrace",
"bytes", "bytes",
"libc", "libc",
"mio", "mio 1.0.3",
"parking_lot", "parking_lot",
"pin-project-lite", "pin-project-lite",
"signal-hook-registry", "signal-hook-registry",
"socket2", "socket2 0.5.8",
"tokio-macros", "tokio-macros",
"windows-sys", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -960,13 +1067,52 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 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]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.52.0" version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [ 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]] [[package]]
@ -975,28 +1121,64 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm 0.52.6",
"windows_aarch64_msvc", "windows_aarch64_msvc 0.52.6",
"windows_i686_gnu", "windows_i686_gnu 0.52.6",
"windows_i686_gnullvm", "windows_i686_gnullvm",
"windows_i686_msvc", "windows_i686_msvc 0.52.6",
"windows_x86_64_gnu", "windows_x86_64_gnu 0.52.6",
"windows_x86_64_gnullvm", "windows_x86_64_gnullvm 0.52.6",
"windows_x86_64_msvc", "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]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" 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]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" 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]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.52.6" version = "0.52.6"
@ -1009,24 +1191,72 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" 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]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" 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]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" 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]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" 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]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.52.6" version = "0.52.6"

View file

@ -7,6 +7,7 @@ edition = "2021"
anyhow = "1.0.94" anyhow = "1.0.94"
axum = "0.7.9" axum = "0.7.9"
common = {path = "../common"} common = {path = "../common"}
ping-rs = "0.1.2"
postcard = {version = "1.0.0", features = ["alloc"]} postcard = {version = "1.0.0", features = ["alloc"]}
serde = "*" serde = "*"
tokio = { version = "1.42.0", features = ["full"] } tokio = { version = "1.42.0", features = ["full"] }

View file

@ -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 axum::{extract::State, http::StatusCode, response::Html, routing::{get, post}, serve, Form, Router};
use common::{Name, Request}; use common::{Name, Request};
use ping_rs::send_ping_async;
use postcard::to_allocvec; 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 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}; use tracing_subscriber::{filter, layer::{Filter, SubscriberExt}, util::SubscriberInitExt, Layer};
const FISH: &'static str = "169.254.2.1:1234"; const FISH: &'static str = "192.168.0.12:1234";
const DOOR: &'static str = "169.254.1.1:1234"; const DOOR: &'static str = "192.168.0.10:1234";
#[derive(Default, Debug)]
struct NetStatus {
fish_reachable: bool,
fish_ping: Option<u32>,
door_reachable: bool,
door_ping: Option<u32>,
router_reachable: bool,
router_ping: Option<u32>,
}
#[derive(Clone)] #[derive(Clone)]
struct AppState { struct AppState {
fish: Arc<Semaphore>, fish: Arc<Semaphore>,
last_badge: Arc<RwLock<(u64,Instant)>>, last_badge: Arc<RwLock<(u64,Instant)>>,
net_status: Arc<RwLock<NetStatus>>,
} }
#[tokio::main] #[tokio::main]
@ -24,20 +36,25 @@ async fn main() -> Result<()>{
let (non_blocking, _guard) = tracing_appender::non_blocking(log.into_std().await); 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); .with_target("server", Level::INFO);
let stdout = tracing_subscriber::fmt::layer() let stdout = tracing_subscriber::fmt::layer()
.compact() .compact()
.with_file(true) .with_file(true)
.with_target(true); .with_target(true)
.with_filter(stdout_filter);
let log = tracing_subscriber::fmt::layer() let log = tracing_subscriber::fmt::layer()
.compact() .compact()
.with_file(false) .with_file(false)
.with_target(true) .with_target(true)
.with_writer(non_blocking) .with_writer(non_blocking)
.with_filter(filter); .with_filter(log_filter);
let reg = tracing_subscriber::registry() let reg = tracing_subscriber::registry()
.with(stdout) .with(stdout)
@ -47,6 +64,7 @@ async fn main() -> Result<()>{
let fish = Arc::new(Semaphore::new(1)); let fish = Arc::new(Semaphore::new(1));
let last_badge = Arc::new(RwLock::new((0u64,Instant::now()))); let last_badge = Arc::new(RwLock::new((0u64,Instant::now())));
let network_status = Arc::new(RwLock::new(NetStatus::default()));
info!("starting"); info!("starting");
@ -63,7 +81,8 @@ async fn main() -> Result<()>{
//.route("/map", post(set_badge)) //.route("/map", post(set_badge))
.with_state(AppState { .with_state(AppState {
fish: fish.clone(), 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(); 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()}); tokio::spawn(async {serve(listener, app).await.unwrap()});
let fish_ = fish.clone(); let fish_ = fish.clone();
tokio::spawn(async move { tokio::spawn(async move {
loop { 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 _permit = fish.acquire().await?;
let mut conn = TcpStream::connect(FISH).await?; let mut conn = TcpStream::connect(FISH).await?;
@ -138,14 +194,10 @@ async fn root() -> Html<String> {
Html::from(page) Html::from(page)
} }
async fn names(state: State<AppState>) -> Html<String> { async fn names(state: State<AppState>) -> Html<String> {
let (last_badge, time) = *state.last_badge.read().await; let last_badge_view = last_badge_view(&state.last_badge).await;
let time = time.elapsed();
let mut page = format!(r#" let mut page = format!(r#"
<p> {last_badge_view}
most recent badge {last_badge:#0x}<br>
{time:?} ago
</p>
<h1>set last badge</h1> <h1>set last badge</h1>
"#); "#);
@ -177,12 +229,26 @@ async fn names(state: State<AppState>) -> Html<String> {
} }
async fn control(state: State<AppState>) -> Html<String> { async fn control(state: State<AppState>) -> Html<String> {
let (last_badge, time) = *state.last_badge.read().await; let last_badge_view = last_badge_view(&state.last_badge).await;
let time = time.elapsed();
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#" let page = format!(r#"
<head><title>JOHNADMIN</title></head> <head><title>JOHNADMIN</title></head>
<h1>JOHN ADMIN PANEL</h1> <h1>JOHN ADMIN PANEL</h1>
<p>
<span style="background-color:{router_color};">lovesense</span> {router_ping} <br>
<span style="background-color:{door_color};">door</span> {door_ping} <br>
<span style="background-color:{fish_color};">fish</span> {fish_ping}
</p>
<form target="output" action="openDoor" method="post" id="myForm"> <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> <button style="font-size:40px;" type="submit" name="name" value="open" target="output">Open noor</button>
</form> </form>
@ -190,22 +256,38 @@ async fn control(state: State<AppState>) -> Html<String> {
<input type="range" min="0" max="30" value="0" name="volume" class="slider" id="volume"> <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> <button style="font-size:30px;" type="submit" target="output">set volume</button>
</form> </form>
<p> {last_badge_view}
most recent badge {last_badge:#0x}<br>
{time:?} ago
</p>
<iframe name="output"></iframe> <iframe name="output"></iframe>
"#); "#);
Html::from(page) Html::from(page)
} }
async fn last_badge_view(last_badge: &Arc<RwLock<(u64, Instant)>>) -> String {
let (last_badge, time) = *last_badge.read().await;
let time = time.elapsed().as_secs();
if last_badge == 0 {
format!(r#"
<p>
no badges scanned, up {time}s
</p>
"#)
} else {
format!(r#"
<p>
most recent badge {last_badge:#0x}<br>
{time}s ago
</p>
"#)
}
}
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
struct Volume { struct Volume {
volume: u8, volume: u8,
} }
async fn open_door(state: State<AppState>) -> Html<&'static str> { async fn open_door() -> Html<&'static str> {
info!("opening door"); info!("opening door");
let mut conn = TcpStream::connect(DOOR).await.unwrap(); let mut conn = TcpStream::connect(DOOR).await.unwrap();