2019-02-14 07:21:22 +00:00
|
|
|
use crate::message::{Level, Message};
|
2019-01-31 16:13:58 +00:00
|
|
|
use crate::monitor::*;
|
2019-02-14 07:21:22 +00:00
|
|
|
use serde::Deserialize;
|
2019-01-31 16:13:58 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct WifiAvailability {
|
|
|
|
config: WifiAvailabilityConfig,
|
|
|
|
}
|
2019-02-14 07:21:22 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
2019-01-31 16:13:58 +00:00
|
|
|
struct WifiAvailabilityConfig {
|
|
|
|
iface: String,
|
|
|
|
ssid: String,
|
2019-02-14 07:21:22 +00:00
|
|
|
#[serde(default)]
|
2019-01-31 16:13:58 +00:00
|
|
|
essid: Vec<String>,
|
2019-02-14 07:21:22 +00:00
|
|
|
#[serde(default)]
|
2019-01-31 16:13:58 +00:00
|
|
|
ping_targets: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Monitor for WifiAvailability {
|
2019-02-23 17:45:43 +00:00
|
|
|
fn new(config: serde_yaml::Value) -> Result<Self, Box<dyn std::error::Error>> {
|
|
|
|
let config = serde_yaml::from_value(config).expect("Invalid config for wifi_availability");
|
2019-01-31 16:13:58 +00:00
|
|
|
|
|
|
|
let wa = WifiAvailability { config: config };
|
|
|
|
// Activate iface
|
|
|
|
//ip link set wlan0 up
|
|
|
|
|
2019-02-14 07:21:22 +00:00
|
|
|
Ok(wa)
|
2019-01-31 16:13:58 +00:00
|
|
|
}
|
|
|
|
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(),
|
2019-02-14 07:21:22 +00:00
|
|
|
level: Level::Issue,
|
2019-01-31 16:13:58 +00:00
|
|
|
text: format!("Cannot ping {}", target),
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(2000))
|
|
|
|
}
|
|
|
|
}
|