Fetch online states only, when panel is online
This commit is contained in:
parent
bf5418922f
commit
c3773ebecd
@ -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"
|
||||||
|
@ -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,30 +311,52 @@ 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);
|
||||||
let answer = openweathermap::update_forecast(&receiver);
|
// Only request, if the device is present
|
||||||
|
if device_online == true {
|
||||||
match answer {
|
let answer = openweathermap::update_forecast(&receiver);
|
||||||
Some(_) => {
|
match answer {
|
||||||
last_data = answer;
|
Some(_) => {
|
||||||
}
|
last_data = answer;
|
||||||
None => {
|
}
|
||||||
|
None => {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// request once a minute new data
|
|
||||||
if (strabaRes.request_time + 60) < seconds as i64 {
|
if (strabaRes.request_time + 60) < seconds as i64 {
|
||||||
strabaRes = straba::fetch_data(None);
|
// check once a minute if the panel is online
|
||||||
println!("Update {:?} {:?}s", strabaRes.outbound_station, strabaRes.outbound_diff);
|
let data: [u8; 24] = [0;24]; // ping data
|
||||||
println!("Update {:?} {:?}s", strabaRes.inbound_station , strabaRes.inbound_diff);
|
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
|
// all the other cases
|
||||||
|
Loading…
Reference in New Issue
Block a user