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_json = "1.0"
# end of web stuff
ping = "0.4.1"

View File

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