use std::time::Duration;
use emerwen_protocol::{
emerwen_protocol_client::EmerwenProtocolClient, target::Method, SetTargetStateRequest, Target,
TargetState,
};
use tonic::{service::interceptor::InterceptedService, transport::Channel};
use crate::AuthInterceptor;
pub struct TargetMonitor {
target: Target,
client: EmerwenProtocolClient<InterceptedService<Channel, AuthInterceptor>>,
last_state: TargetState,
}
impl TargetMonitor {
pub fn new(
target: Target,
client: EmerwenProtocolClient<InterceptedService<Channel, AuthInterceptor>>,
) -> TargetMonitor {
TargetMonitor {
target,
client,
last_state: TargetState::Unknown,
}
}
pub async fn run(&mut self) -> Result<(), Box<dyn std::error::Error>> {
// TODO: do some testing and figure out good logic for when we should
// return "Unknown"... Prolly check for specific errors in the monitor_ functions
let sleep_duration = Duration::from_millis(self.target.interval.into());
if let Some(method) = &self.target.method {
loop {
let state;
match method {
Method::Ping(_) => match self.monitor_ping().await {
Ok(up) => {
if up {
state = TargetState::Up;
} else {
state = TargetState::Down;
}
}
Err(_) => {
state = TargetState::Unknown;
}
},
Method::Get(params) => match self.monitor_get(¶ms.ok_codes).await {
Ok(up) => {
if up {
state = TargetState::Up;
} else {
state = TargetState::Down;
}
}
Err(_) => {
state = TargetState::Unknown;
}
},
}
if state != self.last_state {
self.last_state = state;
let _ = self
.client
.set_target_state(SetTargetStateRequest {
id: self.target.id,
state: state.into(),
})
.await;
}
tokio::time::sleep(sleep_duration).await;
}
} else {
// FIXME: lmao didn't have a method?
}
Ok(())
}
async fn monitor_ping(&self) -> Result<bool, Box<dyn std::error::Error>> {
match surge_ping::ping(self.target.addr.parse()?, &[0u8; 8]).await {
Ok((_packet, _duration)) => Ok(true),
Err(_) => Ok(false),
}
}
async fn monitor_get(&self, ok_codes: &[u32]) -> Result<bool, Box<dyn std::error::Error>> {
let response = reqwest::get(self.target.addr.clone()).await?;
Ok(ok_codes.contains(&response.status().as_u16().into()))
}
}