diff --git a/src/main.rs b/src/main.rs index 7eac028..62c668e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ -use rumqttc::{MqttOptions, Client, QoS}; +use rumqttc::{MqttOptions, Client, QoS, LastWill}; +use bytes::Bytes; use std::time::Duration; use std::thread; use std::collections::hash_map::HashMap; @@ -258,24 +259,26 @@ fn main() { let mut callbacks: HashMap = HashMap::new(); callbacks.insert(tm::NoticeId::NoticeRealtimeScoreChanged, on_score_change); - // Set client options let mut mqttoptions = MqttOptions::new("vex-bridge", "localhost", 1883); mqttoptions.set_keep_alive(Duration::from_secs(5)); + mqttoptions.set_last_will(LastWill{ + topic: String::from("bridge/status"), + message: Bytes::from("{\"online\": false}"), + qos: QoS::AtLeastOnce, + retain: true, + }); - // Create a new client(10 is bounded channel capacity?) let (mut client, mut connection) = Client::new(mqttoptions, 10); - client.subscribe("bridge", QoS::AtMostOnce).unwrap(); + client.subscribe("bridge", QoS::AtLeastOnce).unwrap(); + client.publish("bridge/status", QoS::AtLeastOnce, true, "{\"online\": true}").unwrap(); - client.publish("bridge/status", QoS::AtLeastOnce, false, "{\"status\": true}").unwrap(); - - // Create a thread to own connection and handle it's incoming messages - let running = true; let mqtt_thread = thread::spawn(move || for message in connection.iter() { println!("Message = {:?}", message); } ); + let running = true; while running { let notice = get_next_notice(); let callback = callbacks.get(¬ice.id());