|
|
|
@ -1,17 +1,32 @@ |
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
extern crate log;
|
|
|
|
use log::{error, info, debug};
|
|
|
|
use std::{thread, error::Error, time::Duration};
|
|
|
|
|
|
|
|
use log::{error, warn, info, debug};
|
|
|
|
use simplelog::{ TermLogger, LevelFilter, ConfigBuilder, TerminalMode, ColorChoice};
|
|
|
|
|
|
|
|
use paho_mqtt::Message;
|
|
|
|
use paho_mqtt as paho;
|
|
|
|
|
|
|
|
mod cli_args;
|
|
|
|
mod config;
|
|
|
|
mod mqtt;
|
|
|
|
|
|
|
|
fn handle_msg( msg: &Message )
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
fn try_reconnect( mqtt: &paho::Client, timeout: u32 ) -> bool {
|
|
|
|
warn!("MQTT server lost, trying to reconnect...");
|
|
|
|
for _ in 0..timeout {
|
|
|
|
thread::sleep(Duration::from_secs(1));
|
|
|
|
if mqtt.reconnect().is_ok() {
|
|
|
|
info!("MQTT server reconnected");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error!("MQTT server timed out");
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
fn handle_msg( msg: &paho::Message )
|
|
|
|
{
|
|
|
|
info!("New message: {}", msg)
|
|
|
|
}
|
|
|
|
@ -27,12 +42,15 @@ fn main_impl( args: &cli_args::Args ) -> Result<(), Box<dyn Error>> { |
|
|
|
let rx = mqtt.start_consuming();
|
|
|
|
mqtt::subscribe( &mqtt, &config.topics, args.verbose );
|
|
|
|
|
|
|
|
// handle ^C signal to quit gracefully
|
|
|
|
let ctrlc_mqtt = mqtt.clone();
|
|
|
|
ctrlc::set_handler(move || {
|
|
|
|
ctrlc_mqtt.stop_consuming()
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// main event loop
|
|
|
|
for msg in rx.iter() {
|
|
|
|
if !mqtt.is_connected() {
|
|
|
|
if args.verbose {
|
|
|
|
println!( )
|
|
|
|
}
|
|
|
|
if !mqtt.is_connected() && !try_reconnect(&mqtt, config.mqtt.timeout) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
@ -42,8 +60,8 @@ fn main_impl( args: &cli_args::Args ) -> Result<(), Box<dyn Error>> { |
|
|
|
}
|
|
|
|
|
|
|
|
if mqtt.is_connected() {
|
|
|
|
println!("Disconnecting");
|
|
|
|
//mqtt.unsubscribe_many(topics)
|
|
|
|
info!("Disconnecting");
|
|
|
|
mqtt.disconnect(None)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|