Add CLI args for broker address and port
This commit is contained in:
@@ -8,3 +8,4 @@ tokio = { version = "1", features = ["full"] }
|
|||||||
rumqttc = "0.24"
|
rumqttc = "0.24"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
|
clap = { version = "4", features = ["derive"] }
|
||||||
|
|||||||
@@ -12,9 +12,14 @@ A simple Rust application that publishes messages to an MQTT broker.
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cargo run
|
cargo run -- --broker 192.168.1.100 --port 1883
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Arguments
|
||||||
|
|
||||||
|
- `broker`: MQTT broker address (default: `localhost`)
|
||||||
|
- `port`: MQTT broker port (default: `1883`)
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
Edit `src/main.rs` to customize:
|
Edit `src/main.rs` to customize:
|
||||||
|
|||||||
17
src/main.rs
17
src/main.rs
@@ -1,13 +1,26 @@
|
|||||||
|
use clap::Parser;
|
||||||
use rumqttc::{AsyncClient, Event, MqttOptions, Packet, QoS};
|
use rumqttc::{AsyncClient, Event, MqttOptions, Packet, QoS};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
#[command(version, about, long_about = None)]
|
||||||
|
struct Args {
|
||||||
|
#[arg(default_value = "localhost")]
|
||||||
|
broker: String,
|
||||||
|
|
||||||
|
#[arg(default_value_t = 1883)]
|
||||||
|
port: u16,
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let args = Args::parse();
|
||||||
|
|
||||||
let mut mqttoptions = MqttOptions::new(
|
let mut mqttoptions = MqttOptions::new(
|
||||||
"mqtt-publisher",
|
"mqtt-publisher",
|
||||||
"localhost",
|
args.broker,
|
||||||
1883,
|
args.port,
|
||||||
);
|
);
|
||||||
mqttoptions.set_keep_alive(Duration::from_secs(30));
|
mqttoptions.set_keep_alive(Duration::from_secs(30));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user