@@ -7,11 +7,14 @@ use std::{
7
7
use strum:: VariantNames ;
8
8
9
9
use crate :: constants:: { AVAILABLE_BAUDRATES , AVAILABLE_REFRESH_RATES } ;
10
+ use crate :: errors:: { CONVERT_TO_STR_FAILURE , SHARED_STATE_LOCK_MUTEX_FAILURE } ;
10
11
use crate :: log_panel:: LogLevel ;
11
- use crate :: types:: FlowControl ;
12
+ use crate :: output:: CsvLogging ;
13
+ use crate :: shared_state:: SharedState ;
14
+ use crate :: types:: { FlowControl , RealtimeDelay } ;
12
15
use crate :: {
13
16
common_constants:: { SbpLogging , Tabs } ,
14
- connection:: Connection ,
17
+ connection:: { Connection , ConnectionState } ,
15
18
} ;
16
19
17
20
#[ derive( Debug ) ]
@@ -233,3 +236,48 @@ fn is_baudrate(br: &str) -> Result<(), String> {
233
236
AVAILABLE_BAUDRATES
234
237
) )
235
238
}
239
+
240
+ /// Start connections based on CLI options.
241
+ ///
242
+ /// # Parameters
243
+ /// - `opt`: CLI Options to start specific connection type.
244
+ /// - `connection_state`: The Server state to start a specific connection.
245
+ /// - `client_send`: Client Sender channel for communication from backend to frontend.
246
+ /// - `shared_state`: The shared state for validating another connection is not already running.
247
+ pub fn handle_cli ( opt : CliOptions , connection_state : & ConnectionState , shared_state : SharedState ) {
248
+ if let Some ( opt_input) = opt. input {
249
+ match opt_input {
250
+ Input :: Tcp { host, port } => {
251
+ connection_state. connect_to_host ( host, port) ;
252
+ }
253
+ Input :: File { file_in } => {
254
+ let filename = file_in. display ( ) . to_string ( ) ;
255
+ connection_state. connect_to_file ( filename, RealtimeDelay :: On , opt. exit_after ) ;
256
+ }
257
+ Input :: Serial {
258
+ serialport,
259
+ baudrate,
260
+ flow_control,
261
+ } => {
262
+ let serialport = serialport. display ( ) . to_string ( ) ;
263
+ connection_state. connect_to_serial ( serialport, baudrate, flow_control) ;
264
+ }
265
+ }
266
+ }
267
+ if let Some ( folder) = opt. dirname {
268
+ shared_state. set_logging_directory ( PathBuf :: from ( folder) ) ;
269
+ }
270
+ let log_level = if let Some ( log_level_) = opt. log_level {
271
+ ( * log_level_) . clone ( )
272
+ } else {
273
+ LogLevel :: INFO
274
+ } ;
275
+ shared_state. set_log_level ( log_level) ;
276
+ let mut shared_data = shared_state. lock ( ) . expect ( SHARED_STATE_LOCK_MUTEX_FAILURE ) ;
277
+ ( * shared_data) . logging_bar . csv_logging = CsvLogging :: from ( opt. csv_log ) ;
278
+ if let Some ( sbp_log) = opt. sbp_log {
279
+ ( * shared_data) . logging_bar . sbp_logging =
280
+ SbpLogging :: from_str ( & sbp_log. to_string ( ) ) . expect ( CONVERT_TO_STR_FAILURE ) ;
281
+ }
282
+ log:: logger ( ) . flush ( ) ;
283
+ }
0 commit comments