Skip to content

Commit

Permalink
[MQTT] Extract the port and the server for the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
MrVyM committed Nov 30, 2023
1 parent ba5a9b5 commit a456681
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MQTT_MAIN_TOPIC=robot
MQTT_CLIENT_NAME=Wall-e
MQTT_SERVER=localhost:4242
MQTT_SERVER=127.0.0.1:1883
SPEED_LIMIT_MIN=42
SPEED_LIMIT_MAX=42
WHEEL_DIAMETER=42
Expand Down
Empty file added src/mqtt/client/mod.rs
Empty file.
18 changes: 11 additions & 7 deletions src/mqtt/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rumqttc::{Client, Connection, MqttOptions};
use rumqttc::{Client, Connection, MqttOptions, QoS};
use std::time::Duration;

use crate::cli::ressources::Args;
Expand All @@ -23,12 +23,16 @@ pub fn connect_every_broker(mut commands: Commands, args: Res<Args>) {
/// Format of the broker <server:port>
///
/// Return: ClientConnection
fn connect_client(client_name: String, broker: &String) -> ClientConnection {
// TODO: Extract the port from the broker string <server:port>
let mut mqttoptions = MqttOptions::new(client_name, broker, 1883);
mqttoptions.set_keep_alive(Duration::from_secs(5));
fn connect_client(client_name: String, broker: &str) -> ClientConnection {
let args = broker.split(':').collect::<Vec<&str>>();
println!("Connecting to {} with {} port", args[0], args[1]);
let mut mqtt_options = MqttOptions::new(client_name, args[0], args[1].parse::<u16>().unwrap());
mqtt_options.set_keep_alive(Duration::from_secs(5));

let (client, connection) = Client::new(mqttoptions, 10);
println!("Connected to {}", broker);
let (mut client, connection) = Client::new(mqtt_options, 10);
client.subscribe("hello/world", QoS::AtLeastOnce).unwrap();
client
.publish("hello/world", QoS::AtLeastOnce, false, "test")
.unwrap();
ClientConnection(client, connection)
}

0 comments on commit a456681

Please sign in to comment.