|
| 1 | +use clap::Parser; |
| 2 | +use futures::StreamExt; |
| 3 | +use geoutils::Location; |
| 4 | +use ntrip_client::{ |
| 5 | + config::{NtripConfig, NtripCredentials}, |
| 6 | + NtripClient, |
| 7 | +}; |
| 8 | +use tokio::select; |
| 9 | +use tracing::{debug, error, info, level_filters::LevelFilter}; |
| 10 | +use tracing_subscriber::{fmt::Subscriber as FmtSubscriber, EnvFilter}; |
| 11 | + |
| 12 | +/// NTRIP command line tool |
| 13 | +#[derive(Clone, PartialEq, Debug, Parser)] |
| 14 | +struct Args { |
| 15 | + #[clap()] |
| 16 | + /// NTRIP server identifier or URI ("rtk2go", "linz" etc., or "[ntrip|http|https]://host:port") |
| 17 | + pub ntrip_host: NtripConfig, |
| 18 | + |
| 19 | + #[clap(flatten)] |
| 20 | + pub ntrip_creds: NtripCredentials, |
| 21 | + |
| 22 | + #[clap(subcommand)] |
| 23 | + pub command: Commands, |
| 24 | + |
| 25 | + #[clap(long, default_value = "info")] |
| 26 | + /// Set log level |
| 27 | + pub log_level: LevelFilter, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Clone, PartialEq, Debug, Parser)] |
| 31 | +pub enum Commands { |
| 32 | + /// List mount points on an NTRIP server |
| 33 | + List, |
| 34 | + /// Find the nearest mount point to a specified location |
| 35 | + FindNearest { |
| 36 | + #[clap()] |
| 37 | + lat: f64, |
| 38 | + #[clap()] |
| 39 | + lon: f64, |
| 40 | + }, |
| 41 | + /// Subscribe to a specified mount point and print received RTCM messages |
| 42 | + Subscribe { |
| 43 | + #[clap()] |
| 44 | + mount: String, |
| 45 | + }, |
| 46 | +} |
| 47 | + |
| 48 | +#[tokio::main] |
| 49 | +async fn main() -> Result<(), anyhow::Error> { |
| 50 | + // Parse command line arguments |
| 51 | + let args = Args::parse(); |
| 52 | + |
| 53 | + // Setup logging |
| 54 | + let filter = EnvFilter::from_default_env().add_directive(args.log_level.into()); |
| 55 | + let _ = FmtSubscriber::builder() |
| 56 | + .compact() |
| 57 | + .without_time() |
| 58 | + .with_max_level(args.log_level) |
| 59 | + .with_env_filter(filter) |
| 60 | + .try_init(); |
| 61 | + |
| 62 | + info!("Start NTRIP/RTMP tool"); |
| 63 | + |
| 64 | + debug!("Args {args:?}"); |
| 65 | + |
| 66 | + // Setup interrupt / exit handler |
| 67 | + let (exit_tx, mut exit_rx) = tokio::sync::broadcast::channel(1); |
| 68 | + let e = exit_tx.clone(); |
| 69 | + tokio::task::spawn(async move { |
| 70 | + tokio::signal::ctrl_c().await.unwrap(); |
| 71 | + debug!("Received Ctrl-C, shutting down..."); |
| 72 | + e.send(()).unwrap(); |
| 73 | + }); |
| 74 | + |
| 75 | + let mut client = NtripClient::new(args.ntrip_host.clone(), args.ntrip_creds.clone()).await?; |
| 76 | + |
| 77 | + match args.command { |
| 78 | + Commands::List => { |
| 79 | + // List available NTRIP mounts using SNIP |
| 80 | + info!("Listing NTRIP mounts"); |
| 81 | + |
| 82 | + let info = client.list_mounts().await.unwrap(); |
| 83 | + |
| 84 | + for s in info.services { |
| 85 | + info!( |
| 86 | + "{} - {} ({:.3}, {:.3})", |
| 87 | + s.name, |
| 88 | + s.details, |
| 89 | + s.location.latitude(), |
| 90 | + s.location.longitude() |
| 91 | + ); |
| 92 | + } |
| 93 | + }, |
| 94 | + Commands::FindNearest { lat, lon } => { |
| 95 | + // Find the nearest NTRIP mount to the specified location |
| 96 | + info!("Finding nearest NTRIP mount to ({}, {})", lat, lon); |
| 97 | + |
| 98 | + let info = client.list_mounts().await.unwrap(); |
| 99 | + |
| 100 | + let target_location = Location::new(lat, lon); |
| 101 | + |
| 102 | + match info.find_nearest(&target_location) { |
| 103 | + Some((s, d)) => { |
| 104 | + info!( |
| 105 | + "Nearest mount: {} - {} ({:.3}, {:.3}), {:.3} km away", |
| 106 | + s.name, |
| 107 | + s.details, |
| 108 | + s.location.latitude(), |
| 109 | + s.location.longitude(), |
| 110 | + d / 1000.0 |
| 111 | + ); |
| 112 | + }, |
| 113 | + None => { |
| 114 | + info!("No mounts found"); |
| 115 | + }, |
| 116 | + } |
| 117 | + }, |
| 118 | + Commands::Subscribe { mount } => { |
| 119 | + // Subscribe to the specified NTRIP mount |
| 120 | + debug!("Connecting to NTRIP server"); |
| 121 | + |
| 122 | + // Setup the NTRIP client |
| 123 | + let mut client = client.mount(mount, exit_tx.clone()).await?; |
| 124 | + |
| 125 | + // Process incoming RTCM messages |
| 126 | + loop { |
| 127 | + select! { |
| 128 | + m = client.next() => match m { |
| 129 | + Some(m) => { |
| 130 | + info!("Received RTCM message: {:?}", m); |
| 131 | + }, |
| 132 | + None => { |
| 133 | + error!("NTRIP client stream ended"); |
| 134 | + break; |
| 135 | + } |
| 136 | + }, |
| 137 | + _ = exit_rx.recv() => { |
| 138 | + info!("Exiting on signal"); |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + debug!("Exiting"); |
| 147 | + |
| 148 | + Ok(()) |
| 149 | +} |
0 commit comments