Skip to content

Commit

Permalink
Merge pull request #11 from chrivers/chrivers/z2mdump
Browse files Browse the repository at this point in the history
Added z2mdump example, to make it easier to gather debug info
  • Loading branch information
chrivers authored Aug 24, 2024
2 parents 490b9c3 + c0cedf7 commit 0f339be
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions examples/z2mdump.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use clap::Parser;
use futures::StreamExt;
use hyper::Uri;
use serde::Deserialize;
use tokio_tungstenite::{connect_async, tungstenite::Message};

use bifrost::error::ApiResult;

#[derive(Parser, Debug)]
struct Args {
/// Url to websocket (example: ws://example.org:8080/)
url: Uri,
}

#[derive(Debug, Deserialize)]
struct Z2mMessage {
topic: String,
}

#[tokio::main]
async fn main() -> ApiResult<()> {
pretty_env_logger::formatted_builder()
.filter_level(log::LevelFilter::Debug)
.parse_default_env()
.init();

let args = match Args::try_parse() {
Ok(args) => args,
Err(err) => {
log::error!("Argument error: {err}");
std::process::exit(1);
}
};

let (mut socket, _) = connect_async(args.url).await?;

loop {
let Some(pkt) = socket.next().await else {
break;
};

let Message::Text(txt) = pkt? else { break };

let json: Z2mMessage = serde_json::from_str(&txt)?;

if json.topic.starts_with("bridge/") {
log::info!("Got message [{}]", json.topic);
println!("{}", txt);
} else {
log::info!("No more z2m bridge messages");
break;
}
}

Ok(())
}

0 comments on commit 0f339be

Please sign in to comment.