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
use crate::messages::commands::{
AddStation, ChangeMainVolume, ChangeStationVolume, GetStationState, GetStationStates,
GetVoiceConnectedState, SetStationState,
};
use crate::messages::events::StationState;
use crate::{Command, Event, TrackAudioClient};
use std::fmt::Display;
use std::time::Duration;
/// A high-level client for interacting with the TrackAudio WebSocket API.
///
/// [`TrackAudioApi`] acts as a wrapper around the [`TrackAudioClient`], providing a higher-level
/// interface for interacting with TrackAudio, abstracting some of the asynchronous command- and
/// event-based architecture of the WebSocket API.
pub struct TrackAudioApi<'a> {
client: &'a TrackAudioClient,
}
impl TrackAudioClient {
/// Creates a higher-level [`TrackAudioApi`] from the current [`TrackAudioClient`] instance.
///
/// # Returns
/// - `TrackAudioApi`: API instance tied to the lifetime of `self`.
#[must_use]
pub fn api(&self) -> TrackAudioApi<'_> {
TrackAudioApi::new(self)
}
}
impl<'a> TrackAudioApi<'a> {
/// Creates a higher-level [`TrackAudioApi`] from the given [`TrackAudioClient`] instance.
/// - `TrackAudioApi`: API instance tied to the lifetime of `client`.
#[must_use]
pub fn new(client: &'a TrackAudioClient) -> Self {
Self { client }
}
/// Transmits a Push-To-Talk (PTT) command and awaits a corresponding event as a confirmation.
///
/// This function sends a [`Command::PttPressed`]/[`Command::PttReleased`] command depending on
/// the provided `active` flag to activate or deactivate transmission of audio and waits for a
/// corresponding [`Event::TxBegin`] or [`Event::TxEnd`] event as confirmation.
///
/// # Parameters
/// - `active`: A boolean indicating whether to activate or deactivate transmission
/// - `timeout`: An optional `Duration` to specify the maximum time to wait
/// for the confirmation event. If `None`, the function will wait indefinitely.
///
/// # Returns
/// - `Ok(())`: After receiving the expected confirmation event.
/// - `Err(TrackAudioError)`: An error if the operation failed or exceeded the provided timeout.
///
/// # Errors
/// - [`TrackAudioError::Timeout`](crate::TrackAudioError::Timeout): If the operation times out.
/// - [`TrackAudioError::Send`](crate::TrackAudioError::Send): If an error occurs while sending the command.
/// - [`TrackAudioError::Receive`](crate::TrackAudioError::Receive): If an error occurs while receiving events.
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self), err))]
pub async fn transmit(&self, active: bool, timeout: Option<Duration>) -> crate::Result<()> {
let cmd = if active {
Command::PttPressed
} else {
Command::PttReleased
};
self.client
.send_and_await(cmd, timeout, move |event| match event {
Event::TxBegin(_) if active => Some(()),
Event::TxEnd(_) if !active => Some(()),
_ => None,
})
.await
}
/// Adds a new station with the provided callsign and returns its state.
///
/// This function sends a [`Command::AddStation`] command to request the addition of
/// a new station identified by the given `callsign`. It waits for a corresponding
/// [`Event::StationStateUpdate`] event that matches the callsign and retrieves
/// the updated station state.
///
/// # Parameters
/// - `callsign`: Callsign of the station to be added, e.g. `"LOVV_CTR"`.
/// - `timeout`: An optional `Duration` to specify the maximum time to wait
/// for the response. If `None`, the function will wait indefinitely.
///
/// # Returns
/// - `Ok(StationState)`: The state of the newly added station if the operation
/// was successful.
/// - `Err(TrackAudioError)`: An error if the operation failed or exceeded the provided timeout.
///
/// # Errors
/// - [`TrackAudioError::Timeout`](crate::TrackAudioError::Timeout): If the operation times out.
/// - [`TrackAudioError::Send`](crate::TrackAudioError::Send): If an error occurs while sending the command.
/// - [`TrackAudioError::Receive`](crate::TrackAudioError::Receive): If an error occurs while receiving events.
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(callsign = %callsign), err))]
pub async fn add_station(
&self,
callsign: impl Into<String> + Display,
timeout: Option<Duration>,
) -> crate::Result<StationState> {
self.client
.request(
AddStation {
callsign: callsign.into(),
},
timeout,
)
.await
}
/// Changes the volume of a station and returns its updated state.
///
/// This function sends a [`Command::ChangeStationVolume`] command to modify the volume of
/// an existing station identified by the given `frequency_hz`. It waits for a corresponding
/// [`Event::StationStateUpdate`] event that matches the frequency and retrieves
/// the updated station state.
///
/// # Parameters
/// - `frequency_hz`: Callsign of the station to be added, e.g. `132_600_000`.
/// - `amount`: The amount to adjust the volume by, relative to the current value. This amount
/// is in the range -100..=100, the resulting volume will be clamped to 0..=100.
/// - `timeout`: An optional `Duration` to specify the maximum time to wait
/// for the response. If `None`, the function will wait indefinitely.
///
/// # Returns
/// - `Ok(StationState)`: The state of the updated station if the operation was successful.
/// - `Err(TrackAudioError)`: An error if the operation failed or exceeded the provided timeout.
///
/// # Errors
/// - [`TrackAudioError::Timeout`](crate::TrackAudioError::Timeout): If the operation times out.
/// - [`TrackAudioError::Send`](crate::TrackAudioError::Send): If an error occurs while sending the command.
/// - [`TrackAudioError::Receive`](crate::TrackAudioError::Receive): If an error occurs while receiving events.
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self), err))]
pub async fn change_station_volume(
&self,
frequency_hz: u64,
amount: i8,
timeout: Option<Duration>,
) -> crate::Result<StationState> {
self.client
.request(ChangeStationVolume::new(frequency_hz, amount), timeout)
.await
}
/// Changes the main volume and returns its updated state.
///
/// This function sends a [`Command::ChangeMainVolume`] command to modify the volume of
/// the main audio output. It waits for a corresponding [`Event::MainVolumeChange`] event
/// and retrieves the updated main output volume.
///
/// # Parameters
/// - `amount`: The amount to adjust the volume by, relative to the current value. This amount
/// is in the range -100..=100, the resulting volume will be clamped to 0..=100.
/// - `timeout`: An optional `Duration` to specify the maximum time to wait
/// for the response. If `None`, the function will wait indefinitely.
///
/// # Returns
/// - `Ok(f32)`: The updated main volume if the operation was successful.
/// - `Err(TrackAudioError)`: An error if the operation failed or exceeded the provided timeout.
///
/// # Errors
/// - [`TrackAudioError::Timeout`](crate::TrackAudioError::Timeout): If the operation times out.
/// - [`TrackAudioError::Send`](crate::TrackAudioError::Send): If an error occurs while sending the command.
/// - [`TrackAudioError::Receive`](crate::TrackAudioError::Receive): If an error occurs while receiving events.
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self), err))]
pub async fn change_main_volume(
&self,
amount: i8,
timeout: Option<Duration>,
) -> crate::Result<f32> {
self.client
.request(ChangeMainVolume::new(amount), timeout)
.await
}
/// Retrieves the current state of a specific station.
///
/// This function sends a [`Command::GetStationState`] command to request the current status of
/// a station identified by the given `callsign`. It waits for a corresponding
/// [`Event::StationStateUpdate`] event that matches the callsign and retrieves
/// the updated station state.
///
/// # Parameters
/// - `callsign`: Callsign of the station to retrieve the state for, e.g. `"LOVV_CTR"`.
/// - `timeout`: An optional `Duration` to specify the maximum time to wait
/// for the response. If `None`, the function will wait indefinitely.
///
/// # Returns
/// - `Ok(StationState)`: The state of the station if the operation was successful.
/// - `Err(TrackAudioError)`: An error if the operation failed or exceeded the provided timeout.
///
/// # Errors
/// - [`TrackAudioError::Timeout`](crate::TrackAudioError::Timeout): If the operation times out.
/// - [`TrackAudioError::Send`](crate::TrackAudioError::Send): If an error occurs while sending the command.
/// - [`TrackAudioError::Receive`](crate::TrackAudioError::Receive): If an error occurs while receiving events.
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(callsign = %callsign), err))]
pub async fn get_station_state(
&self,
callsign: impl Into<String> + Display,
timeout: Option<Duration>,
) -> crate::Result<StationState> {
self.client
.request(
GetStationState {
callsign: callsign.into(),
},
timeout,
)
.await
}
/// Updates a station's state and returns the resulting state.
///
/// This function sends a [`Command::SetStationState`] command to modify one or more properties
/// of an existing station identified by its frequency. It waits for a corresponding
/// [`Event::StationStateUpdate`] event that matches the frequency and retrieves
/// the updated station state.
///
/// Use [`SetStationState::new`] to construct the command with the builder pattern, or create
/// the struct directly.
///
/// # Parameters
/// - `cmd`: A [`SetStationState`] command describing which fields to change.
/// - `timeout`: An optional `Duration` to specify the maximum time to wait
/// for the response. If `None`, the function will wait indefinitely.
///
/// # Returns
/// - `Ok(StationState)`: The updated state of the station if the operation was successful.
/// - `Err(TrackAudioError)`: An error if the operation failed or exceeded the provided timeout.
///
/// # Errors
/// - [`TrackAudioError::Timeout`](crate::TrackAudioError::Timeout): If the operation times out.
/// - [`TrackAudioError::Send`](crate::TrackAudioError::Send): If an error occurs while sending the command.
/// - [`TrackAudioError::Receive`](crate::TrackAudioError::Receive): If an error occurs while receiving events.
///
/// # Examples
///
/// ```rust,no_run
/// use std::time::Duration;
/// use trackaudio::Frequency;
/// use trackaudio::messages::commands::SetStationState;
///
/// async fn example(api: trackaudio::TrackAudioApi<'_>) -> trackaudio::Result<()> {
/// // Using the builder pattern
/// let state = api
/// .set_station_state(
/// SetStationState::new(Frequency::from_mhz(132.600))
/// .rx(true)
/// .tx(true),
/// Some(Duration::from_secs(1)),
/// )
/// .await?;
/// Ok(())
/// }
/// ```
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self), err))]
pub async fn set_station_state(
&self,
cmd: SetStationState,
timeout: Option<Duration>,
) -> crate::Result<StationState> {
self.client.request(cmd, timeout).await
}
/// Retrieves the current state of all active stations.
///
/// This function sends a [`Command::GetStationStates`] command to request the current status of
/// all active stations. It waits for a corresponding [`Event::StationStates`] event
/// and returns the updated station state.
///
/// # Parameters
/// - `timeout`: An optional `Duration` to specify the maximum time to wait
/// for the response. If `None`, the function will wait indefinitely.
///
/// # Returns
/// - `Ok(Vec<StationState>)`: The state of all active stations if the operation was successful.
/// - `Err(TrackAudioError)`: An error if the operation failed or exceeded the provided timeout.
///
/// # Errors
/// - [`TrackAudioError::Timeout`](crate::TrackAudioError::Timeout): If the operation times out.
/// - [`TrackAudioError::Send`](crate::TrackAudioError::Send): If an error occurs while sending the command.
/// - [`TrackAudioError::Receive`](crate::TrackAudioError::Receive): If an error occurs while receiving events.
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self), err))]
pub async fn get_station_states(
&self,
timeout: Option<Duration>,
) -> crate::Result<Vec<StationState>> {
self.client.request(GetStationStates, timeout).await
}
/// Retrieves the current voice connection state.
///
/// This function sends a [`Command::GetVoiceConnectedState`] command to request the current
/// voice connection state. It waits for a corresponding [`Event::VoiceConnectedState`] event
/// and returns the current state.
///
/// # Parameters
/// - `timeout`: An optional `Duration` to specify the maximum time to wait
/// for the response. If `None`, the function will wait indefinitely.
///
/// # Returns
/// - `Ok(bool)`: The current voice connection state if the operation was successful.
/// - `Err(TrackAudioError)`: An error if the operation failed or exceeded the provided timeout.
///
/// # Errors
/// - [`TrackAudioError::Timeout`](crate::TrackAudioError::Timeout): If the operation times out.
/// - [`TrackAudioError::Send`](crate::TrackAudioError::Send): If an error occurs while sending the command.
/// - [`TrackAudioError::Receive`](crate::TrackAudioError::Receive): If an error occurs while receiving events.
pub async fn get_voice_connected_state(
&self,
timeout: Option<Duration>,
) -> crate::Result<bool> {
self.client.request(GetVoiceConnectedState, timeout).await
}
}