From f782f1a810c4676796846bf4080d8ca50857bd1d Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 30 Oct 2021 18:34:56 +0200 Subject: [PATCH] adsb: emit Action::Ignored --- src/adsb.rs | 13 ++++++++++--- src/main.rs | 46 +++++++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/adsb.rs b/src/adsb.rs index 2650d33..36b8006 100644 --- a/src/adsb.rs +++ b/src/adsb.rs @@ -66,6 +66,7 @@ pub enum Action { Appeared, Disappeared, Moved, + Ignored, } struct State { @@ -154,11 +155,18 @@ pub fn run(url: &'static str, locations: Locations) -> Receiver { continue; } }; + let mut events = vec![]; for mut info in infos { if info.altitude > MAX_ALTITUDE { if !ignored.contains(&info.hex) { - states.remove(&info.hex); - ignored.insert(info.hex); + ignored.insert(info.hex.clone()); + if let Some(_) = states.remove(&info.hex) { + events.push(Event { + action: Action::Ignored, + info, + location: Arc::new("ignoriert".to_owned()), + }); + } } continue; } @@ -181,7 +189,6 @@ pub fn run(url: &'static str, locations: Locations) -> Receiver { } } - let mut events = vec![]; states.retain(|_, state| { if state.last + Duration::from_secs(STATE_TIMEOUT) < Instant::now() { events.push(Event { diff --git a/src/main.rs b/src/main.rs index fb1e309..2389694 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,30 +36,42 @@ async fn main() -> Result<(), Box> { while let Some(event) = events.recv().await { println!("event: {:?}", event); - let mut text; + let identifier; if let Some(aircraft) = aircrafts.find(&event.info.get_hex()) { println!("aircraft: {:?}", aircraft); - text = format!("{} {} ({} {})", - aircraft.owner, aircraft.registration, - aircraft.manufacturername, aircraft.model + // TODO: aircraft.model.starts_with(&aircraft.manufacturername) + identifier = format!( + "{} {} ({} {})", + aircraft.owner, aircraft.registration, + aircraft.manufacturername, aircraft.model ); } else if let Some(flight) = event.info.get_flight() { - text = format!("Flug {} [{}]", flight, event.info.get_hex()); + identifier = format!("Flug {} [{}]", flight, event.info.get_hex()); } else { - text = format!("[{}]", event.info.get_hex()); + identifier = format!("[{}]", event.info.get_hex()); } - text = format!("{} {}", text, match event.action { - adsb::Action::Appeared => "ist aufgetaucht", - adsb::Action::Moved => "fliegt jetzt", - adsb::Action::Disappeared => "ist abgetaucht.", - }); - if event.action != adsb::Action::Disappeared { - text = format!("{} {:.0}m über {}", text, - event.info.get_altitude_m(), - event.location - ); - } + let text = match event.action { + adsb::Action::Appeared => + format!("{} ist {:.0}m über {} aufgetaucht", + identifier, + event.info.get_altitude_m(), + event.location + ), + adsb::Action::Moved => + format!("{} ist fliegt jetzt {:.0}m über {}", + identifier, + event.info.get_altitude_m(), + event.location + ), + adsb::Action::Disappeared => + format!("{} ist abgetaucht", identifier), + adsb::Action::Ignored => + format!("{} wird ab {:.0}m ignoriert", + identifier, + event.info.get_altitude_m() + ), + }; println!(">> {}", text); jabber.send_message(text).await;