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 openweathermap::forecast::Forecast;
|
||||||
use straba::NextDeparture;
|
use straba::NextDeparture;
|
||||||
use ini::Ini;
|
use ini::Ini;
|
||||||
|
use std::path::Path;
|
||||||
// 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;
|
||||||
@ -303,6 +304,7 @@ type UserTopics = RwLock<Vec<String>>;
|
|||||||
struct Config {
|
struct Config {
|
||||||
mqttPrefix: String,
|
mqttPrefix: String,
|
||||||
mqttIPAddress: String,
|
mqttIPAddress: String,
|
||||||
|
panelIPAddress: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -310,7 +312,8 @@ struct Config {
|
|||||||
fn read_ini_file(filename: String) -> Result<Config, std::io::Error> {
|
fn read_ini_file(filename: String) -> Result<Config, std::io::Error> {
|
||||||
|
|
||||||
let mut config = Config { mqttPrefix: "room/ledboard".to_string(),
|
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();
|
let i = Ini::load_from_file(filename).unwrap();
|
||||||
for (sec, prop) in i.iter() {
|
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();
|
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)
|
Ok(config)
|
||||||
@ -402,7 +412,7 @@ LEDboardClient <ip address>"
|
|||||||
println!("<ip of mqtt server>");
|
println!("<ip of mqtt server>");
|
||||||
println!("");
|
println!("");
|
||||||
println!("Config mode");
|
println!("Config mode");
|
||||||
println!("--mode config <file.ini>");
|
println!("--config <file.ini>");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_connection(ipaddress: String) -> bool {
|
fn check_connection(ipaddress: String) -> bool {
|
||||||
@ -439,37 +449,61 @@ struct Message {
|
|||||||
|
|
||||||
// Lazy static to load the config file content
|
// Lazy static to load the config file content
|
||||||
lazy_static! {
|
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
|
// 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");
|
let configOrMqttAddress: String = parameter2.unwrap();
|
||||||
|
if Path::new(&configOrMqttAddress).exists()
|
||||||
|
{
|
||||||
|
read_ini_file(configOrMqttAddress).expect("Failed to load config file");
|
||||||
println!("Config parsed");
|
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());
|
}
|
||||||
|
|
||||||
|
let mut device_online = false;
|
||||||
|
if GlobalConfiguration.panelIPAddress.len() > 0
|
||||||
|
{
|
||||||
|
device_online = check_connection(GlobalConfiguration.panelIPAddress.clone());
|
||||||
if !device_online {
|
if !device_online {
|
||||||
println!("{:} not online", &ipaddress);
|
println!("{:} not online", &GlobalConfiguration.panelIPAddress);
|
||||||
return ExitCode::FAILURE;
|
return ExitCode::FAILURE;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut mqtt_client: Option<paho_mqtt::AsyncClient> = None;
|
let mut mqtt_client: Option<paho_mqtt::AsyncClient> = None;
|
||||||
let mut mqtt_message: Arc<Mutex<Message>> = Arc::new(Mutex::new(Message{ string: None }));
|
let mut mqtt_message: Arc<Mutex<Message>> = Arc::new(Mutex::new(Message{ string: None }));
|
||||||
if mqtt.is_some() {
|
if GlobalConfiguration.mqttIPAddress.len() > 0
|
||||||
let mqtt_ip: String = mqtt.clone().unwrap();
|
{
|
||||||
// Define the set of options for the create.
|
// Define the set of options for the create.
|
||||||
// Use an ID for a persistent session.
|
// Use an ID for a persistent session.
|
||||||
let create_opts = paho_mqtt::CreateOptionsBuilder::new()
|
let create_opts = paho_mqtt::CreateOptionsBuilder::new()
|
||||||
.server_uri(mqtt_ip.clone())
|
.server_uri(GlobalConfiguration.mqttIPAddress.clone())
|
||||||
.client_id("ledboard")
|
.client_id("ledboard")
|
||||||
.finalize();
|
.finalize();
|
||||||
// Create a client.
|
// Create a client.
|
||||||
let local_mqtt = paho_mqtt::AsyncClient::new(create_opts).unwrap();
|
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.
|
// Define the set of options for the connection.
|
||||||
let conn_opts = paho_mqtt::ConnectOptionsBuilder::new()
|
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.outbound_station, straba_res.outbound_diff);
|
||||||
println!("{:?} {:?}s", straba_res.inbound_station , straba_res.inbound_diff);
|
println!("{:?} {:?}s", straba_res.inbound_station , straba_res.inbound_diff);
|
||||||
|
|
||||||
|
if GlobalConfiguration.panelIPAddress.len() > 0
|
||||||
|
{
|
||||||
// Render start
|
// Render start
|
||||||
send_package(ipaddress.clone(), &last_data, &straba_res, Some("MQTT: room/ledboard".to_string()));
|
send_package(GlobalConfiguration.panelIPAddress.clone(), &last_data, &straba_res, Some("MQTT: room/ledboard".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let st_now = SystemTime::now();
|
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 {
|
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
|
// 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);
|
||||||
@ -575,9 +616,9 @@ fn main_function(ipaddress: String, mqtt: Option<String>, inifile: Option<String
|
|||||||
mqtt_message = None;
|
mqtt_message = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
if device_online == true {
|
if (GlobalConfiguration.mqttIPAddress.len() > 0) && (device_online == true) {
|
||||||
// Render new image
|
// 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
|
// Handle MQTT messages
|
||||||
|
|
||||||
@ -596,25 +637,14 @@ fn main() -> ExitCode {
|
|||||||
// one argument passed
|
// one argument passed
|
||||||
2 => {
|
2 => {
|
||||||
let ip = &args[1];
|
let ip = &args[1];
|
||||||
return main_function(ip.to_string(), None, None);
|
return main_function(ip.to_string(), None);
|
||||||
}
|
}
|
||||||
// two argument passed
|
// two argument passed
|
||||||
3 => {
|
3 => {
|
||||||
let ip = &args[1];
|
let ip = &args[1];
|
||||||
let mqtt = &args[2];
|
let mqtt = &args[2];
|
||||||
return main_function( ip.to_string(),
|
return main_function( ip.to_string(),
|
||||||
Some(mqtt.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())
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// all the other cases
|
// all the other cases
|
||||||
|
Loading…
x
Reference in New Issue
Block a user