From ccc382c042be28e015cdfad170e56deb6bacdca6 Mon Sep 17 00:00:00 2001 From: Lukas Mittag Date: Tue, 30 Jul 2024 14:39:53 +0200 Subject: [PATCH] Add get operation for target values (#52) * Add get operation for target values --- databroker-cli/src/kuksa_cli.rs | 45 ++++++++++++++++++++++++++- databroker/tests/read_write_values.rs | 2 +- lib/kuksa/src/lib.rs | 4 +-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/databroker-cli/src/kuksa_cli.rs b/databroker-cli/src/kuksa_cli.rs index 2cd79288..5dd91099 100644 --- a/databroker-cli/src/kuksa_cli.rs +++ b/databroker-cli/src/kuksa_cli.rs @@ -36,6 +36,7 @@ const TIMEOUT: Duration = Duration::from_millis(500); const CLI_COMMANDS: &[(&str, &str, &str)] = &[ ("connect", "[URI]", "Connect to server"), ("get", " [[PATH] ...]", "Get signal value(s)"), + ("gettarget", " [[PATH] ...]", "Get target value(s)"), ("actuate", " ", "Set actuator signal"), ( "subscribe", @@ -237,6 +238,48 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box> { } } } + "gettarget" => { + interface.add_history_unique(line.clone()); + + if args.is_empty() { + print_usage(cmd); + continue; + } + let paths = args + .split_whitespace() + .map(|path| path.to_owned()) + .collect(); + match client.get_target_values(paths).await { + Ok(data_entries) => { + cli::print_resp_ok(cmd)?; + for entry in data_entries { + if let Some(val) = entry.actuator_target { + println!( + "{}: {} {}", + entry.path, + DisplayDatapoint(val), + entry + .metadata + .and_then(|meta| meta.unit) + .map(|unit| unit.to_string()) + .unwrap_or_else(|| "".to_string()) + ); + } else { + println!("{} is not an actuator.", entry.path); + } + } + } + Err(kuksa_common::ClientError::Status(err)) => { + cli::print_resp_err(cmd, &err)?; + } + Err(kuksa_common::ClientError::Connection(msg)) => { + cli::print_error(cmd, msg)?; + } + Err(kuksa_common::ClientError::Function(msg)) => { + cli::print_resp_err_fmt(cmd, format_args!("Error {msg:?}"))?; + } + } + } "token" => { interface.add_history_unique(line.clone()); @@ -908,7 +951,7 @@ impl Completer for CliCompleter { None } } - Some("get") | Some("metadata") => self.complete_entry_path(word), + Some("get") | Some("metadata") | Some("gettarget") => self.complete_entry_path(word), Some("subscribe") => { if words.count() == 0 { self.complete_entry_path(word) diff --git a/databroker/tests/read_write_values.rs b/databroker/tests/read_write_values.rs index 09f5e7b8..dad19144 100644 --- a/databroker/tests/read_write_values.rs +++ b/databroker/tests/read_write_values.rs @@ -172,7 +172,7 @@ async fn get_value(w: &mut DataBrokerWorld, value_type: ValueType, path: String) .as_mut() .expect("no Databroker client available, broker not started?"); match value_type { - ValueType::Target => match client.get_target_values(vec![&path]).await { + ValueType::Target => match client.get_target_values(vec![path]).await { Ok(res) => w.current_data_entries = Some(res), Err(e) => { debug!("failed to invoke Databroker's get operation: {:?}", e); diff --git a/lib/kuksa/src/lib.rs b/lib/kuksa/src/lib.rs index a51f9efa..8bd1a156 100644 --- a/lib/kuksa/src/lib.rs +++ b/lib/kuksa/src/lib.rs @@ -153,14 +153,14 @@ impl KuksaClient { pub async fn get_target_values( &mut self, - paths: Vec<&str>, + paths: Vec, ) -> Result, ClientError> { let mut get_result = Vec::new(); for path in paths { match self .get( - path, + &path, proto::v1::View::TargetValue, vec![ proto::v1::Field::ActuatorTarget.into(),