56 lines
1.7 KiB
Rust
56 lines
1.7 KiB
Rust
|
use crate::message::Message;
|
||
|
use crate::monitor::*;
|
||
|
use serde_derive::*;
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
pub struct WifiAvailability {
|
||
|
config: WifiAvailabilityConfig,
|
||
|
}
|
||
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||
|
struct WifiAvailabilityConfig {
|
||
|
iface: String,
|
||
|
ssid: String,
|
||
|
essid: Vec<String>,
|
||
|
ping_targets: Vec<String>,
|
||
|
}
|
||
|
|
||
|
impl Monitor for WifiAvailability {
|
||
|
fn new(config: &HashMap<String, serde_yaml::Value>) -> Self {
|
||
|
let config_node = config.get("config").expect("Missing `config` key").clone();
|
||
|
let config =
|
||
|
serde_yaml::from_value(config_node).expect("Invalid config for wifi_availability");
|
||
|
|
||
|
let wa = WifiAvailability { config: config };
|
||
|
// Activate iface
|
||
|
//ip link set wlan0 up
|
||
|
|
||
|
wa
|
||
|
}
|
||
|
fn run(&mut self, sender: &mpsc::Sender<Message>) {
|
||
|
// iw dev wlan0 link: get is connected
|
||
|
// iw dev wlan0 scan: scan all networks
|
||
|
|
||
|
for target in &self.config.ping_targets {
|
||
|
use std::process::{Command, Stdio};
|
||
|
let res = Command::new("ping")
|
||
|
.args(vec!["-c", "3", "-I", &self.config.iface, target])
|
||
|
.stdout(Stdio::null())
|
||
|
.status()
|
||
|
.expect("failed to execute process");
|
||
|
|
||
|
if !res.success() {
|
||
|
sender
|
||
|
.send(Message {
|
||
|
emitter: "wifi_availability".to_owned(),
|
||
|
msg_type: "ping.failed".to_owned(),
|
||
|
level: 10,
|
||
|
text: format!("Cannot ping {}", target),
|
||
|
})
|
||
|
.unwrap();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
std::thread::sleep(std::time::Duration::from_millis(2000))
|
||
|
}
|
||
|
}
|