Skip to content

Commit

Permalink
[MQTT] Implement the system connect_every_broker in bevy
Browse files Browse the repository at this point in the history
  • Loading branch information
MrVyM committed Nov 29, 2023
1 parent 65af642 commit 0b80c2e
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/mqtt/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
use rumqttc::{Client, MqttOptions};
use rumqttc::{Client, Connection, MqttOptions};
use std::time::Duration;

use crate::cli::ressources::Args;
use bevy::prelude::*;

pub fn connect_every_broker(args: Res<Args>) {
#[derive(Component)]
struct ClientConnection(Client, Connection);

// NOTE: Implement sync for the derive Component
// Could be useful to find a better solution
unsafe impl Sync for ClientConnection {}

/// Connect every the program to every broker
/// Add a Component, ClientConnection for everty new connection
pub fn connect_every_broker(mut commands: Commands, args: Res<Args>) {
for broker in args.mqtt_server.iter() {
connect_client(broker);
commands.spawn(connect_client(broker));
}
}

fn connect_client(broker: &String) {
/// Connect a client with a mqtt broker
/// Format of the broker <server:port>
///
/// Return: ClientConnection
fn connect_client(broker: &String) -> ClientConnection {
// TODO: Extract the port from the broker string <server:port>
let mut mqttoptions = MqttOptions::new("Robot", broker, 1883);
mqttoptions.set_keep_alive(Duration::from_secs(5));

let (mut _client, mut _connection) = Client::new(mqttoptions, 10);
let (client, connection) = Client::new(mqttoptions, 10);
println!("Connected to {}", broker);
ClientConnection(client, connection)
}

0 comments on commit 0b80c2e

Please sign in to comment.