master
noah metz 2024-01-18 17:28:08 -07:00
parent c6a19c4a63
commit fb92058ec7
1 changed files with 11 additions and 8 deletions

@ -1,4 +1,5 @@
use rumqttc::{MqttOptions, Client, QoS}; use rumqttc::{MqttOptions, Client, QoS, LastWill};
use bytes::Bytes;
use std::time::Duration; use std::time::Duration;
use std::thread; use std::thread;
use std::collections::hash_map::HashMap; use std::collections::hash_map::HashMap;
@ -258,24 +259,26 @@ fn main() {
let mut callbacks: HashMap<tm::NoticeId, NoticeCallback> = HashMap::new(); let mut callbacks: HashMap<tm::NoticeId, NoticeCallback> = HashMap::new();
callbacks.insert(tm::NoticeId::NoticeRealtimeScoreChanged, on_score_change); callbacks.insert(tm::NoticeId::NoticeRealtimeScoreChanged, on_score_change);
// Set client options
let mut mqttoptions = MqttOptions::new("vex-bridge", "localhost", 1883); let mut mqttoptions = MqttOptions::new("vex-bridge", "localhost", 1883);
mqttoptions.set_keep_alive(Duration::from_secs(5)); 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); 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 || let mqtt_thread = thread::spawn(move ||
for message in connection.iter() { for message in connection.iter() {
println!("Message = {:?}", message); println!("Message = {:?}", message);
} }
); );
let running = true;
while running { while running {
let notice = get_next_notice(); let notice = get_next_notice();
let callback = callbacks.get(&notice.id()); let callback = callbacks.get(&notice.id());