Refactored main-function

This commit is contained in:
Ollo 2024-01-07 16:25:01 +01:00
parent 256fdeee47
commit 4b6afda278

View File

@ -318,6 +318,8 @@ LEDboardClient <ip address>"
); );
println!("one argument necessary!"); println!("one argument necessary!");
println!("<ip address>"); println!("<ip address>");
println!("second argument is optional:");
println!("<ip of mqtt server>");
} }
fn check_connection(ipaddress: String) -> bool { fn check_connection(ipaddress: String) -> bool {
@ -348,23 +350,10 @@ fn check_connection(ipaddress: String) -> bool {
return device_online; return device_online;
} }
fn main() -> ExitCode { fn main_function(ipaddress: String) -> ExitCode {
let args: Vec<String> = env::args().collect(); let mut device_online = check_connection(ipaddress.clone());
match args.len() {
// no arguments passed
1 => {
// show a help message
help();
return ExitCode::SUCCESS;
}
// one argument passed
2 => {
let ip = &args[1];
let mut device_online = check_connection(ip.to_string());
if !device_online { if !device_online {
println!("{} not online", ip); println!("{:} not online", &ipaddress);
return ExitCode::FAILURE; return ExitCode::FAILURE;
} }
@ -382,7 +371,7 @@ fn main() -> ExitCode {
println!("{:?} {:?}s", straba_res.inbound_station , straba_res.inbound_diff); println!("{:?} {:?}s", straba_res.inbound_station , straba_res.inbound_diff);
// Render start // Render start
send_package(ip.to_string(), &last_data, &straba_res); send_package(ipaddress.clone(), &last_data, &straba_res);
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();
@ -402,7 +391,7 @@ fn main() -> ExitCode {
} }
if (straba_res.request_time + 50) < seconds as i64 { if (straba_res.request_time + 50) < seconds as i64 {
device_online = check_connection(ip.to_string()); device_online = check_connection(ipaddress.clone());
// request once a minute new data // request once a minute new data
if device_online == true { if device_online == true {
straba_res = straba::fetch_data(None); straba_res = straba::fetch_data(None);
@ -413,9 +402,24 @@ fn main() -> ExitCode {
if device_online == true { if device_online == true {
// Render new image // Render new image
send_package(ip.to_string(), &last_data, &straba_res); send_package(ipaddress.clone(), &last_data, &straba_res);
} }
} }
}
fn main() -> ExitCode {
let args: Vec<String> = env::args().collect();
match args.len() {
// no arguments passed
1 => {
// show a help message
help();
return ExitCode::SUCCESS;
}
// one argument passed
2 => {
let ip = &args[1];
return main_function(ip.to_string());
} }
// all the other cases // all the other cases
_ => { _ => {