Skip to content

Commit

Permalink
tech(cli) : Using clap.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
urvoy committed Jul 29, 2021
1 parent 83b59b2 commit 829217e
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 25 deletions.
1 change: 0 additions & 1 deletion .env

This file was deleted.

180 changes: 180 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@ name = "logs-publisher"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# Mqtt
paho-mqtt = "0.9"

# Watching file
#hotwatch = "0.4.5"
#crossbeam-channel = "0.4.0"
#notify = "4.0.17"
#linemux = "0.2"
logwatcher = "0.1.1"

#log
log = "0.4.14"
simple_logger = "1.11.0"

clap = "2.33.3"

#tokio = { version = "1", features = ["full"] }
62 changes: 45 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
use std::{process, thread};
use paho_mqtt::{QOS_0};
use std::{process};
use paho_mqtt::QOS_0;
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::fs::File;
use std::io::BufReader;
use std::error::Error;

use std::time::Duration;

use log::{error, LevelFilter};
use logwatcher::{LogWatcher, LogWatcherAction};
use simple_logger::SimpleLogger;

use clap::{App, Arg};

#[macro_use]
extern crate log;

static URL: &str = "tcp://127.0.0.1:1883";
fn main() -> std::io::Result<()> {
// create cli app and get Arg
let matches = App::new("logs-publisher")
.version(env!("CARGO_PKG_VERSION"))
.author("Urvoy L")
.about("Read the log file and publish log to MQTT topic")
.arg(
Arg::with_name("file").short("f").long("file").help("Set the file to read").required(true).takes_value(true),
)
.arg(
Arg::with_name("broker_url").short("u").long("broker_url").help("Set the URL of MQTT broker").required(true).takes_value(true)
)
.arg(
Arg::with_name("topic").short("t").long("topic").help("MQTT Topic target for publish").required(true).takes_value(true)
)
.get_matches();

let broker_url = matches.value_of("broker_url").unwrap();
let file_path = matches.value_of("file").unwrap();
let topic = matches.value_of("topic").unwrap();

SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap();

fn main() -> std::io::Result<()> {
info!("Starting program ...");

let cli = paho_mqtt::Client::new(URL).unwrap_or_else(|err| {
let cli = paho_mqtt::Client::new(broker_url).unwrap_or_else(|err| {
error!("Error creating the client: {:?}", err);
process::exit(1);
});

let conn_opts = paho_mqtt::ConnectOptionsBuilder::new().clean_session(true)
// .client_id(std::env::var("MQTT_CLIENT_ID").unwrap_or_else(|_| "logs-publisher".to_string()))
.finalize();

// Connect and wait for it to complete or fail
Expand All @@ -33,18 +51,28 @@ fn main() -> std::io::Result<()> {
process::exit(1);
}

// let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/data.json");
let path = Path::new("/Users/urvoy/Developpement/MOB_MDGate/logs/2021-07-27 13-49-47/").join("MDGate.log");
info!("MQTT Connected to {}", broker_url);

let path = Path::new(file_path);

let mut log_watcher = LogWatcher::register(path).unwrap();

log_watcher.watch(&mut move |line: String| {
let msg = paho_mqtt::Message::new("toto/tata", line, QOS_0);
cli.publish(msg).unwrap();
let msg = paho_mqtt::Message::new(topic, line.clone(), QOS_0);
if cli.is_connected() {
cli.publish(msg).unwrap();
info!("{}", line);
} else {
info!("MQTT cli not connected when publish, try to reconnect to {}", broker_url);
if cli.reconnect().is_ok() {
cli.publish(msg).unwrap();
info!("{}", line);
} else {
warn!("Fail to MQTT reconnect");
}
}
LogWatcherAction::None
});

Ok(())
}


0 comments on commit 829217e

Please sign in to comment.