Integrate straba.rs in MQTT
This commit is contained in:
31
src/main.rs
31
src/main.rs
@@ -3,6 +3,8 @@ use rumqttc::{AsyncClient, Event, MqttOptions, Packet, QoS};
|
||||
use std::time::Duration;
|
||||
use tokio::time::sleep;
|
||||
|
||||
mod straba;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
@@ -32,6 +34,35 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
println!("Published message to test/topic");
|
||||
|
||||
let departure_data = tokio::task::spawn_blocking(|| straba::fetch_data(Some(true)))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
if departure_data.failure {
|
||||
client
|
||||
.publish("departure/failure", QoS::AtLeastOnce, false, b"Failed to fetch departure data")
|
||||
.await?;
|
||||
} else {
|
||||
let outbound_msg = format!(
|
||||
"Outbound: {} in {} seconds",
|
||||
departure_data.outbound_station, departure_data.outbound_diff
|
||||
);
|
||||
let inbound_msg = format!(
|
||||
"Inbound: {} in {} seconds",
|
||||
departure_data.inbound_station, departure_data.inbound_diff
|
||||
);
|
||||
|
||||
client
|
||||
.publish("departure/outbound", QoS::AtLeastOnce, false, outbound_msg.as_bytes())
|
||||
.await?;
|
||||
client
|
||||
.publish("departure/inbound", QoS::AtLeastOnce, false, inbound_msg.as_bytes())
|
||||
.await?;
|
||||
|
||||
println!("{}", outbound_msg);
|
||||
println!("{}", inbound_msg);
|
||||
}
|
||||
|
||||
loop {
|
||||
match eventloop.poll().await {
|
||||
Ok(Event::Incoming(Packet::Publish(_))) => {}
|
||||
|
||||
Reference in New Issue
Block a user