trackaudio 0.2.2

A high-level async client for the TrackAudio WebSocket API, enabling programmatic control, automation, and custom integrations for VATSIM voice communication.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# trackaudio

[![Crates.io](https://img.shields.io/crates/v/trackaudio.svg)](https://crates.io/crates/trackaudio)
[![Documentation](https://docs.rs/trackaudio/badge.svg)](https://docs.rs/trackaudio)
[![License: Apache-2.0 OR MIT](https://img.shields.io/badge/License-Apache--2.0%20OR%20MIT-blue.svg)](#license)

A Rust client library for [TrackAudio](https://github.com/pierr3/TrackAudio), a modern voice communication application for [VATSIM](https://vatsim.ent) air traffic controllers.

This crate provides a high-level, async API for controlling TrackAudio programmatically via its [WebSocket interface](https://github.com/pierr3/TrackAudio/wiki/SDK-documentation), enabling custom integrations, automation tools, and alternative user interfaces.

## Features

- ๐Ÿš€ **Async/await API** โ€“ Built on Tokio for efficient async I/O
- ๐Ÿ”’ **Type-safe** โ€“ Strongly-typed commands and events with full deserialization
- ๐Ÿ”„ **Request-response pattern** โ€“ High-level API for commands that expect responses
- ๐Ÿ“ก **Event streaming** โ€“ Subscribe to real-time events from TrackAudio
- ๐Ÿ”Œ **Automatic reconnection** โ€“ Resilient WebSocket connections with exponential backoff
- ๐Ÿงต **Thread-safe** โ€“ Client can be safely shared across threads
- ๐Ÿ” **Tracing support** โ€“ Optional integration with the `tracing` crate

## Quick start

Add the dependency to your `Cargo.toml`:

```toml
[dependencies]
trackaudio = "0.1"
```

Connect to the default instance locally and listen for events:

```rust
use trackaudio::{Command, Event, TrackAudioClient};

#[tokio::main]
async fn main() -> trackaudio::Result<()> {
    // Connect to the local TrackAudio instance (ws://127.0.0.1:49080/ws)
    let client = TrackAudioClient::connect_default().await?;

    // Subscribe to event stream
    let mut events = client.subscribe();

    // Send a command
    client.send(Command::PttPressed).await?;

    // Handle incoming events
    while let Ok(event) = events.recv().await {
        match event {
            Event::TxBegin(_) => println!("Started transmitting"),
            Event::RxBegin(rx) => println!("Receiving from {}", rx.callsign),
            _ => {}
        }
    }

    Ok(())
}
```

## High-level API

For common operations, use the `TrackAudioApi` wrapper:

```rust
use std::time::Duration;
use trackaudio::TrackAudioClient;

#[tokio::main]
async fn main() -> trackaudio::Result<()> {
    let client = TrackAudioClient::connect_default().await?;
    let api = client.api();

    // Add a station and wait for TrackAudio's response
    let station = api.add_station("LOVV_CTR", Some(Duration::from_secs(5))).await?;
    println!("Added station: {station:?}");

    // Change main volume
    let vol = api.change_main_volume(-20, None).await?;
    println!("Volume changed to {vol}");

    Ok(())
}
```

- Use `TrackAudioClient` if you want full control (raw events, raw commands).
- Use `TrackAudioApi` if you want convenience with timeout-guarded request/response helpers.

For more detailed examples and API docs, check out the [documentation](https://docs.rs/trackaudio).

## Examples

### Adding and configuring stations

```rust
use trackaudio::{Command, Frequency, TrackAudioClient};
use trackaudio::messages::commands::{BoolOrToggle, SetStationState};
use std::time::Duration;

#[tokio::main]
async fn main() -> trackaudio::Result<()> {
    let client = TrackAudioClient::connect_default().await?;
    let api = client.api();

    // Add a station
    let station = api.add_station("LOVV_CTR", Some(Duration::from_secs(5))).await?;

    if station.is_available {
        println!("โœ“ Station available on {}", station.frequency.unwrap());

        // Enable RX and TX on the station
        client.send(Command::SetStationState(SetStationState {
            frequency: station.frequency.unwrap(),
            rx: Some(BoolOrToggle::Bool(true)),
            tx: Some(BoolOrToggle::Bool(true)),
            xca: None,
            is_output_muted: None,
            headset: None,
        })).await?;
    } else {
        println!("โœ— Station not available");
    }

    Ok(())
}
```

### Working with frequencies

```rust
use trackaudio::Frequency;

// Create frequencies in different units
let freq_hz = Frequency::from_hz(132_600_000);
let freq_khz = Frequency::from_khz(132_600);
let freq_mhz = Frequency::from_mhz(132.600);

assert_eq!(freq_hz, freq_khz);
assert_eq!(freq_khz, freq_mhz);

// Convert between units
println!("Frequency: {} MHz", freq_hz.as_mhz());
println!("Frequency: {} kHz", freq_hz.as_khz());
println!("Frequency: {} Hz", freq_hz.as_hz());

// Convenient conversions
let freq: Frequency = 132.600_f64.into();  // from MHz
let freq: Frequency = 132_600_000_u64.into();  // from Hz
```

### Monitoring RX/TX activity

```rust
use trackaudio::{Event, TrackAudioClient};

#[tokio::main]
async fn main() -> trackaudio::Result<()> {
    let client = TrackAudioClient::connect_default().await?;
    let mut events = client.subscribe();

    println!("Monitoring radio activity...");

    while let Ok(event) = events.recv().await {
        match event {
            Event::RxBegin(rx) => {
                println!("๐Ÿ“ป RX Start: {} on {}", rx.callsign, rx.frequency);
            }
            Event::RxEnd(rx) => {
                println!("๐Ÿ“ป RX End: {} on {}", rx.callsign, rx.frequency);
                if let Some(active) = rx.active_transmitters {
                    if !active.is_empty() {
                        println!("   Still transmitting: {}", active.join(", "));
                    }
                }
            }
            Event::TxBegin(_) => {
                println!("๐ŸŽ™๏ธ  TX Start");
            }
            Event::TxEnd(_) => {
                println!("๐ŸŽ™๏ธ  TX End");
            }
            Event::StationStateUpdate(state) => {
                println!("๐Ÿ“ก Station update: {}", state.callsign);
            }
            _ => {}
        }
    }

    Ok(())
}
```

### Request-Response pattern

```rust
use trackaudio::TrackAudioClient;
use trackaudio::messages::commands::{GetStationState, GetStationStates};
use std::time::Duration;

#[tokio::main]
async fn main() -> trackaudio::Result<()> {
    let client = TrackAudioClient::connect_default().await?;

    // Request a specific station's state
    let station = client
        .request(
            GetStationState {
                callsign: "LOVV_CTR".to_string(),
            },
            Some(Duration::from_secs(5)),
        )
        .await?;

    println!("Station: {:?}", station);

    // Request all station states
    let states = client
        .request(GetStationStates, Some(Duration::from_secs(5)))
        .await?;

    println!("Total stations: {}", states.len());

    Ok(())
}
```

### Custom Configuration

```rust
use trackaudio::{TrackAudioClient, TrackAudioConfig};
use std::time::Duration;

#[tokio::main]
async fn main() -> trackaudio::Result<()> {
    let config = TrackAudioConfig::new("192.168.1.69:49080")?
        .with_capacity(512, 512)  // Increase channel capacities
        .with_ping_interval(Duration::from_secs(10));  // Adjust keepalive

    let client = TrackAudioClient::connect(config).await?;

    // Use client...

    Ok(())
}
```

### Reconnection Configuration

The client automatically reconnects on connection loss with exponential backoff:

```rust
use trackaudio::{TrackAudioClient, TrackAudioConfig, ConnectionState, ClientEvent, Event};
use std::time::Duration;

#[tokio::main]
async fn main() -> trackaudio::Result<()> {
    let config = TrackAudioConfig::default()
        .with_auto_reconnect(true)             // Enabled by default
        .with_max_reconnect_attempts(Some(10)) // None = infinite retries
        .with_backoff_config(
            Duration::from_secs(1),            // Initial backoff
            Duration::from_secs(60),           // Max backoff
            2.0,                               // Multiplier
        );

    let client = TrackAudioClient::connect(config).await?;

    // Monitor connection state changes
    let mut events = client.subscribe();
    tokio::spawn(async move {
        while let Ok(event) = events.recv().await {
            if let Event::Client(ClientEvent::ConnectionStateChanged(state)) = event {
                match state {
                    ConnectionState::Disconnected { reason } => {
                        println!("Disconnected: {reason}");
                    }
                    ConnectionState::Reconnecting { attempt, next_delay } => {
                        println!("Reconnecting (attempt {attempt}) in {next_delay:?}...");
                    }
                    ConnectionState::Connected => {
                        println!("Connected!");
                    }
                    _ => {}
                }
            }
        }
    });

    // Manually trigger reconnection if needed
    client.reconnect().await?;

    Ok(())
}
```

## Supported URL Formats

The client supports flexible URL formats for connecting to TrackAudio:

- Full WebSocket URL: `ws://127.0.0.1:49080/ws` or `ws://192.168.1.69/ws`
- Host only: `127.0.0.1` or `localhost` (uses default port 49080 and `/ws` path)
- Host and port: `127.0.0.1:12345` (uses `/ws` path)

**Note:** TrackAudio currently only supports IPv4 connections and the `ws://` scheme (no TLS/`wss://` support).

## API Overview

### Core Types

- **`TrackAudioClient`** โ€“ The core WebSocket client for connection management
- **`TrackAudioApi`** โ€“ High-level API wrapper with convenient methods
- **`TrackAudioConfig`** โ€“ Configuration for connection parameters
- **`Command`** โ€“ Commands to send to TrackAudio
- **`Event`** โ€“ Events emitted by TrackAudio
- **`Frequency`** โ€“ Radio frequency with convenient unit conversions

## Error Handling

All operations return a `Result<T, TrackAudioError>`:

```rust
use trackaudio::{TrackAudioClient, TrackAudioError};

match TrackAudioClient::connect_default().await {
    Ok(client) => {
        // Use client
    }
    Err(TrackAudioError::WebSocket(e)) => {
        eprintln!("Connection failed: {}", e);
    }
    Err(TrackAudioError::Timeout) => {
        eprintln!("Operation timed out");
    }
    Err(TrackAudioError::InvalidUrl(msg)) => {
        eprintln!("Invalid URL: {}", msg);
    }
    Err(e) => {
        eprintln!("Error: {}", e);
    }
}
```

## Features

- **`tracing`** (enabled by default) โ€“ Integrate with the [`tracing`]https://docs.rs/tracing crate for structured logging
- **`reconnect-jitter`** (enabled by default) โ€“ Add jitter to reconnection backoff to prevent thundering herd

To disable default features:

```toml
[dependencies]
trackaudio = { version = "0.1", default-features = false }
```

## Minimum Supported Rust Version (MSRV)

This crate requires Rust 1.85 or later.

## License

The `trackaudio` project and all its crates and packages are dual-licensed as

- **Apache License, Version 2.0** ([LICENSE-APACHE]LICENSE-APACHE or https://opensource.org/license/apache-2-0)
- **MIT license** ([LICENSE-MIT]LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

This means you can choose to use `trackaudio` under either the Apache-2.0 license or the MIT license.

### Contributions

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

## Contributing

Contributions are welcome! Whether you've found a bug, have a feature request, or want to improve the documentation, your input is valued.

### Reporting Issues

If you encounter a bug or have a feature request, please [open an issue](https://github.com/MorpheusXAUT/trackaudio-rs/issues) on GitHub. When reporting a bug, please include:

- A clear description of the problem
- Steps to reproduce the issue
- The version of this crate used
- Your TrackAudio version
- Your Rust version (`rustc --version`)
- Your operating system
- Any relevant error messages or logs

### Submitting Pull Requests

1. Fork the repository and create a feature branch
2. Write tests for new functionality (if applicable)
3. Ensure `cargo test`, `cargo fmt`, and `cargo clippy` pass
4. Update documentation for API changes
5. Submit your PR with a clear description of the changes

Note that this project uses [Conventional Commits](https://www.conventionalcommits.org) in combination with [release-plz](https://release-plz.dev/) for automatic [semantic versioning](https://semver.org/) and release automation.  
Please ensure your commits follow this format when submitting a PR.

For significant changes, please open an issue first to discuss your proposal.

## Resources

- [TrackAudio GitHub]https://github.com/pierr3/TrackAudio
- [TrackAudio SDK Documentation]https://github.com/pierr3/TrackAudio/wiki/SDK-documentation
- [VATSIM]https://www.vatsim.net/
- [Documentation]https://docs.rs/trackaudio

## Acknowledgments

This is an unofficial client library. TrackAudio is developed and maintained by [Pierre](https://github.com/pierr3).