rkusa / DATIS

DCS World Automatic Terminal Information Service (ATIS) broadcasted through Simple Radio Standalone (SRS)
MIT License
91 stars 22 forks source link

[code sub] port option on datis\crates\radio-station #52

Closed robgrahamau closed 4 years ago

robgrahamau commented 4 years ago

Just code to add port option to radio-station so it's not limited to 5002

main.rs

#![warn(rust_2018_idioms)]

#[macro_use]
extern crate log;

mod radio_station;

use std::str::FromStr;

use radio_station::RadioStation;

#[tokio::main]
pub async fn main() -> Result<(), anyhow::Error> {
    env_logger::Builder::new()
        .filter_level(log::LevelFilter::Info)
        .try_init()
        .unwrap();

    let matches = clap::App::new("dcs-radio-station")
        .version(env!("CARGO_PKG_VERSION"))
        .arg(
            clap::Arg::with_name("frequency")
                .short("f")
                .long("freq")
                .default_value("251000000")
                .help("Sets the SRS frequency (in Hz, e.g. 255000000 for 255MHz)")
                .takes_value(true),
        )
        .arg(
            clap::Arg::with_name("port")
                .short("p")
                .long("port")
                .default_value("5002")
                .help("Sets the SRS Port")
                .takes_value(true),
        )
        .arg(
            clap::Arg::with_name("loop")
                .short("l")
                .long("loop")
                .help("Enables endlessly looping the audio file(s)"),
        )
        .arg(
            clap::Arg::with_name("PATH")
                .help("Sets the path audio file(s) should be read from")
                .required(true)
                .index(1),
        )
        .get_matches();

    // Calling .unwrap() is safe here because "INPUT" is required
    let path = matches.value_of("PATH").unwrap();
    let should_loop = matches.is_present("loop");
    let port = matches.value_of("port").unwrap();
    let port = if let Ok(n) = u16::from_str(port) {
        n
    } else {
        error! ("The provided Port is not a valid number");
        return Ok(());
    };
    let freq = matches.value_of("frequency").unwrap();
    let freq = if let Ok(n) = u64::from_str(freq) {
        n
    } else {
        error!("The provided frequency is not a valid number");
        return Ok(());
    };

    let mut station = RadioStation::new("DCS Radio Station");
    station.set_frequency(freq);
    station.set_position(0.0, 0.0, 8000.);
    station.set_port(port);

    info!("Start playing ...");
    station.play(path, should_loop).await?;

    Ok(())
}
rkusa commented 4 years ago

Reasonable addition, I'll add that 👍 If you like the use-case of the radio station, you might also want to checkout out https://github.com/ace747/SR-Music

robgrahamau commented 4 years ago

already used Ace's app, while it's good this ones light weight doesn't use a lot of resources etc and while it's putting the virtual units at map 0,0 like eam which i've no issue with because i know i could over ride again in the code with another new set of commands, it allows for secure radios etc.

rkusa commented 4 years ago

Added, thanks for the code-snippet 👍 If I ever find the time for it, I am btw going to make it possible to start a radio station similar to a ATIS station, by naming a unit something like: RADIO D:\Music.