Refactor fun_publishinfoviamqtt
This commit is contained in:
parent
c07d6db238
commit
c8d4ab3ba3
@ -297,6 +297,8 @@ fn render_strab(display: &mut UdpDisplay, straba_res: &NextDeparture) {
|
|||||||
// we need to wrap the data in a lock, like a Mutex or RwLock.
|
// we need to wrap the data in a lock, like a Mutex or RwLock.
|
||||||
type UserTopics = RwLock<Vec<String>>;
|
type UserTopics = RwLock<Vec<String>>;
|
||||||
|
|
||||||
|
const DEFAULT_REFRESH_INTERVAL: u32 = 50;
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// Define a struct to hold the INI configuration
|
// Define a struct to hold the INI configuration
|
||||||
@ -305,6 +307,7 @@ struct Config {
|
|||||||
mqttPrefix: String,
|
mqttPrefix: String,
|
||||||
mqttIPAddress: String,
|
mqttIPAddress: String,
|
||||||
panelIPAddress: String,
|
panelIPAddress: String,
|
||||||
|
refreshInterval: u32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -313,7 +316,9 @@ 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() };
|
panelIPAddress: String::new(),
|
||||||
|
refreshInterval: DEFAULT_REFRESH_INTERVAL
|
||||||
|
};
|
||||||
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() {
|
||||||
|
|
||||||
@ -452,7 +457,9 @@ lazy_static! {
|
|||||||
static ref GlobalConfiguration: Config = Config {
|
static ref GlobalConfiguration: Config = Config {
|
||||||
mqttIPAddress : "".to_string(),
|
mqttIPAddress : "".to_string(),
|
||||||
panelIPAddress : "".to_string(),
|
panelIPAddress : "".to_string(),
|
||||||
mqttPrefix : "test/ledboard".to_string() };
|
mqttPrefix : "test/ledboard".to_string(),
|
||||||
|
refreshInterval: DEFAULT_REFRESH_INTERVAL
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -500,7 +507,9 @@ fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
|
|||||||
/* Panel and MQTT Configured*/
|
/* Panel and MQTT Configured*/
|
||||||
let mut config = Config { mqttPrefix: "room/ledboard".to_string(),
|
let mut config = Config { mqttPrefix: "room/ledboard".to_string(),
|
||||||
mqttIPAddress: configOrMqttAddress.clone(),
|
mqttIPAddress: configOrMqttAddress.clone(),
|
||||||
panelIPAddress: parameter1.clone() };
|
panelIPAddress: parameter1.clone(),
|
||||||
|
refreshInterval: DEFAULT_REFRESH_INTERVAL
|
||||||
|
};
|
||||||
//FIXME how is th global config updated ?
|
//FIXME how is th global config updated ?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -606,14 +615,7 @@ fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
|
|||||||
}
|
}
|
||||||
else if GlobalConfiguration.mqttPrefix.len() > 0
|
else if GlobalConfiguration.mqttPrefix.len() > 0
|
||||||
{
|
{
|
||||||
let topicInStation: String = format!("{}{}", GlobalConfiguration.mqttPrefix, "/inbound/station");
|
fun_publishinfoviamqtt(mqtt_client, &straba_res);
|
||||||
let stationName: String = format!("{}", straba_res.inbound_station);
|
|
||||||
if (mqtt_client.is_some()) {
|
|
||||||
publish_message(mqtt_client.unwrap(), topicInStation.as_str(), stationName.as_str());
|
|
||||||
println!("MQTT published {:?} = {:?}s", topicInStation, straba_res.outbound_station);
|
|
||||||
} else {
|
|
||||||
println!("MQTT not ready... {:?} = {:?}s", topicInStation, straba_res.outbound_station);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -635,7 +637,7 @@ fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (straba_res.request_time + 50) < seconds as i64 {
|
if (straba_res.request_time + (GlobalConfiguration.refreshInterval as i64)) < seconds as i64 {
|
||||||
if GlobalConfiguration.mqttIPAddress.len() > 0
|
if GlobalConfiguration.mqttIPAddress.len() > 0
|
||||||
{
|
{
|
||||||
device_online = check_connection(GlobalConfiguration.mqttIPAddress.clone());
|
device_online = check_connection(GlobalConfiguration.mqttIPAddress.clone());
|
||||||
@ -666,6 +668,17 @@ fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn fun_publishinfoviamqtt(mqtt_client: Option<AsyncClient>, straba_res: &NextDeparture) {
|
||||||
|
let topicInStation: String = format!("{}{}", GlobalConfiguration.mqttPrefix, "/inbound/station");
|
||||||
|
let stationName: String = format!("{}", straba_res.inbound_station);
|
||||||
|
if (mqtt_client.is_some()) {
|
||||||
|
publish_message(mqtt_client.unwrap(), topicInStation.as_str(), stationName.as_str());
|
||||||
|
println!("MQTT published {:?} = {:?}s", topicInStation, straba_res.outbound_station);
|
||||||
|
} else {
|
||||||
|
println!("MQTT not ready... {:?} = {:?}s", topicInStation, straba_res.outbound_station);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> ExitCode {
|
fn main() -> ExitCode {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
match args.len() {
|
match args.len() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user