-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MQTT] Implement the system connect_every_broker in bevy
- Loading branch information
Showing
1 changed file
with
20 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |