Fetch online states only, when panel is online

This commit is contained in:
Ollo 2023-09-20 21:49:15 +02:00
parent bf5418922f
commit c3773ebecd
2 changed files with 38 additions and 14 deletions

View File

@ -21,3 +21,4 @@ serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
serde_json = "1.0" serde_json = "1.0"
# end of web stuff # end of web stuff
ping = "0.4.1"

View File

@ -1,4 +1,4 @@
use std::time::Duration;
use bit::BitIndex; use bit::BitIndex;
use chrono_tz::Europe::Berlin; use chrono_tz::Europe::Berlin;
use chrono::{DateTime, NaiveDateTime, Utc, Timelike}; use chrono::{DateTime, NaiveDateTime, Utc, Timelike};
@ -21,6 +21,7 @@ use std::{env, thread};
use openweathermap::forecast::Forecast; use openweathermap::forecast::Forecast;
use straba::NextDeparture; use straba::NextDeparture;
use ping;
// This declaration will look for a file named `straba.rs` and will // This declaration will look for a file named `straba.rs` and will
// insert its contents inside a module named `straba` under this scope // insert its contents inside a module named `straba` under this scope
mod straba; mod straba;
@ -310,13 +311,15 @@ fn main() {
// Render start // Render start
send_package(ip.to_string(), &last_data, &strabaRes); send_package(ip.to_string(), &last_data, &strabaRes);
let mut device_online = true;
loop { loop {
let st_now = SystemTime::now(); let st_now = SystemTime::now();
let seconds = st_now.duration_since(UNIX_EPOCH).unwrap().as_secs(); let seconds = st_now.duration_since(UNIX_EPOCH).unwrap().as_secs();
let delay = time::Duration::from_millis(10000); let delay = time::Duration::from_millis(10000);
thread::sleep(delay); thread::sleep(delay);
// Only request, if the device is present
if device_online == true {
let answer = openweathermap::update_forecast(&receiver); let answer = openweathermap::update_forecast(&receiver);
match answer { match answer {
Some(_) => { Some(_) => {
last_data = answer; last_data = answer;
@ -325,17 +328,37 @@ fn main() {
} }
} }
}
if (strabaRes.request_time + 60) < seconds as i64 {
// check once a minute if the panel is online
let data: [u8; 24] = [0;24]; // ping data
let result = ping::ping(ip.parse().unwrap(), Some(Duration::from_secs(1)),
Some(128), Some(0), Some(0), Some(&data));
match result {
Ok(_) =>{
device_online = true;
}
Err(e) => {
device_online = false;
println!("{} is offline, {:?}", &ip, e)
}
}
// request once a minute new data // request once a minute new data
if (strabaRes.request_time + 60) < seconds as i64 { if device_online == true {
strabaRes = straba::fetch_data(None); strabaRes = straba::fetch_data(None);
println!("Update {:?} {:?}s", strabaRes.outbound_station, strabaRes.outbound_diff); println!("Update {:?} {:?}s", strabaRes.outbound_station, strabaRes.outbound_diff);
println!("Update {:?} {:?}s", strabaRes.inbound_station , strabaRes.inbound_diff); println!("Update {:?} {:?}s", strabaRes.inbound_station , strabaRes.inbound_diff);
} }
}
if device_online == true {
// Render new image // Render new image
send_package(ip.to_string(), &last_data, &strabaRes); send_package(ip.to_string(), &last_data, &strabaRes);
} }
} }
}
// all the other cases // all the other cases
_ => { _ => {
// show a help message // show a help message