Refactored function: always use only two parameter
This commit is contained in:
parent
4473fa7f8b
commit
922c023216
@ -26,6 +26,7 @@ use std::process::ExitCode;
|
||||
use openweathermap::forecast::Forecast;
|
||||
use straba::NextDeparture;
|
||||
use ini::Ini;
|
||||
use std::path::Path;
|
||||
// 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;
|
||||
@ -303,6 +304,7 @@ type UserTopics = RwLock<Vec<String>>;
|
||||
struct Config {
|
||||
mqttPrefix: String,
|
||||
mqttIPAddress: String,
|
||||
panelIPAddress: String,
|
||||
}
|
||||
|
||||
|
||||
@ -310,7 +312,8 @@ struct Config {
|
||||
fn read_ini_file(filename: String) -> Result<Config, std::io::Error> {
|
||||
|
||||
let mut config = Config { mqttPrefix: "room/ledboard".to_string(),
|
||||
mqttIPAddress: String::new() };
|
||||
mqttIPAddress: String::new(),
|
||||
panelIPAddress: String::new() };
|
||||
let i = Ini::load_from_file(filename).unwrap();
|
||||
for (sec, prop) in i.iter() {
|
||||
|
||||
@ -328,6 +331,13 @@ fn read_ini_file(filename: String) -> Result<Config, std::io::Error> {
|
||||
config.mqttIPAddress = v.trim().to_string();
|
||||
}
|
||||
}
|
||||
else if (sec.is_some()) && (sec.unwrap() == "panel")
|
||||
{
|
||||
if k == "ip"
|
||||
{
|
||||
config.panelIPAddress = v.trim().to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(config)
|
||||
@ -402,7 +412,7 @@ LEDboardClient <ip address>"
|
||||
println!("<ip of mqtt server>");
|
||||
println!("");
|
||||
println!("Config mode");
|
||||
println!("--mode config <file.ini>");
|
||||
println!("--config <file.ini>");
|
||||
}
|
||||
|
||||
fn check_connection(ipaddress: String) -> bool {
|
||||
@ -439,37 +449,61 @@ struct Message {
|
||||
|
||||
// Lazy static to load the config file content
|
||||
lazy_static! {
|
||||
static ref GlobalConfiguration: Config = Config { mqttIPAddress : "".to_string(), mqttPrefix : "test/ledboard".to_string() };
|
||||
static ref GlobalConfiguration: Config = Config {
|
||||
mqttIPAddress : "".to_string(),
|
||||
panelIPAddress : "".to_string(),
|
||||
mqttPrefix : "test/ledboard".to_string() };
|
||||
}
|
||||
|
||||
fn main_function(ipaddress: String, mqtt: Option<String>, inifile: Option<String>) -> ExitCode {
|
||||
fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
|
||||
|
||||
// Read configuration file
|
||||
if (inifile.is_some()) && (mqtt.is_some()) && (ipaddress == "--mode")
|
||||
if (parameter1 == "--config") && (parameter2.is_some())
|
||||
{
|
||||
read_ini_file(inifile.unwrap()).expect("Failed to load config file");
|
||||
println!("Config parsed");
|
||||
let configOrMqttAddress: String = parameter2.unwrap();
|
||||
if Path::new(&configOrMqttAddress).exists()
|
||||
{
|
||||
read_ini_file(configOrMqttAddress).expect("Failed to load config file");
|
||||
println!("Config parsed");
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Panel and MQTT Configured*/
|
||||
let mut config = Config { mqttPrefix: "room/ledboard".to_string(),
|
||||
mqttIPAddress: configOrMqttAddress.clone(),
|
||||
panelIPAddress: parameter1.clone() };
|
||||
//FIXME how is th global config updated ?
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
let mut device_online = check_connection(ipaddress.clone());
|
||||
if !device_online {
|
||||
println!("{:} not online", &ipaddress);
|
||||
return ExitCode::FAILURE;
|
||||
let mut device_online = false;
|
||||
if GlobalConfiguration.panelIPAddress.len() > 0
|
||||
{
|
||||
device_online = check_connection(GlobalConfiguration.panelIPAddress.clone());
|
||||
if !device_online {
|
||||
println!("{:} not online", &GlobalConfiguration.panelIPAddress);
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
let mut mqtt_client: Option<paho_mqtt::AsyncClient> = None;
|
||||
let mut mqtt_message: Arc<Mutex<Message>> = Arc::new(Mutex::new(Message{ string: None }));
|
||||
if mqtt.is_some() {
|
||||
let mqtt_ip: String = mqtt.clone().unwrap();
|
||||
if GlobalConfiguration.mqttIPAddress.len() > 0
|
||||
{
|
||||
// Define the set of options for the create.
|
||||
// Use an ID for a persistent session.
|
||||
let create_opts = paho_mqtt::CreateOptionsBuilder::new()
|
||||
.server_uri(mqtt_ip.clone())
|
||||
.server_uri(GlobalConfiguration.mqttIPAddress.clone())
|
||||
.client_id("ledboard")
|
||||
.finalize();
|
||||
// Create a client.
|
||||
let local_mqtt = paho_mqtt::AsyncClient::new(create_opts).unwrap();
|
||||
|
||||
println!("MQTT | Connecting to {:} MQTT server...", mqtt_ip);
|
||||
println!("MQTT | Connecting to {:} MQTT server...", GlobalConfiguration.mqttIPAddress);
|
||||
|
||||
// Define the set of options for the connection.
|
||||
let conn_opts = paho_mqtt::ConnectOptionsBuilder::new()
|
||||
@ -536,8 +570,11 @@ fn main_function(ipaddress: String, mqtt: Option<String>, inifile: Option<String
|
||||
println!("{:?} {:?}s", straba_res.outbound_station, straba_res.outbound_diff);
|
||||
println!("{:?} {:?}s", straba_res.inbound_station , straba_res.inbound_diff);
|
||||
|
||||
// Render start
|
||||
send_package(ipaddress.clone(), &last_data, &straba_res, Some("MQTT: room/ledboard".to_string()));
|
||||
if GlobalConfiguration.panelIPAddress.len() > 0
|
||||
{
|
||||
// Render start
|
||||
send_package(GlobalConfiguration.panelIPAddress.clone(), &last_data, &straba_res, Some("MQTT: room/ledboard".to_string()));
|
||||
}
|
||||
|
||||
loop {
|
||||
let st_now = SystemTime::now();
|
||||
@ -558,7 +595,11 @@ fn main_function(ipaddress: String, mqtt: Option<String>, inifile: Option<String
|
||||
}
|
||||
|
||||
if (straba_res.request_time + 50) < seconds as i64 {
|
||||
device_online = check_connection(ipaddress.clone());
|
||||
if GlobalConfiguration.mqttIPAddress.len() > 0
|
||||
{
|
||||
device_online = check_connection(GlobalConfiguration.mqttIPAddress.clone());
|
||||
}
|
||||
|
||||
// request once a minute new data
|
||||
if device_online == true {
|
||||
straba_res = straba::fetch_data(None);
|
||||
@ -575,9 +616,9 @@ fn main_function(ipaddress: String, mqtt: Option<String>, inifile: Option<String
|
||||
mqtt_message = None;
|
||||
}
|
||||
|
||||
if device_online == true {
|
||||
if (GlobalConfiguration.mqttIPAddress.len() > 0) && (device_online == true) {
|
||||
// Render new image
|
||||
send_package(ipaddress.clone(), &last_data, &straba_res, mqtt_message);
|
||||
send_package(GlobalConfiguration.mqttIPAddress.clone(), &last_data, &straba_res, mqtt_message);
|
||||
}
|
||||
// Handle MQTT messages
|
||||
|
||||
@ -596,25 +637,14 @@ fn main() -> ExitCode {
|
||||
// one argument passed
|
||||
2 => {
|
||||
let ip = &args[1];
|
||||
return main_function(ip.to_string(), None, None);
|
||||
return main_function(ip.to_string(), None);
|
||||
}
|
||||
// two argument passed
|
||||
3 => {
|
||||
let ip = &args[1];
|
||||
let mqtt = &args[2];
|
||||
return main_function( ip.to_string(),
|
||||
Some(mqtt.to_string()),
|
||||
None
|
||||
);
|
||||
}
|
||||
// three argument passed
|
||||
4 => {
|
||||
let ip = &args[1];
|
||||
let mqtt = &args[2];
|
||||
let config = &args[3];
|
||||
return main_function( ip.to_string(),
|
||||
Some(mqtt.to_string()),
|
||||
Some(config.to_string())
|
||||
Some(mqtt.to_string())
|
||||
);
|
||||
}
|
||||
// all the other cases
|
||||
|
Loading…
x
Reference in New Issue
Block a user