Skip to content

Commit

Permalink
Add get operation for target values (eclipse-kuksa#52)
Browse files Browse the repository at this point in the history
* Add get operation for target values
  • Loading branch information
lukasmittag committed Jul 30, 2024
1 parent 7596725 commit ccc382c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
45 changes: 44 additions & 1 deletion databroker-cli/src/kuksa_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const TIMEOUT: Duration = Duration::from_millis(500);
const CLI_COMMANDS: &[(&str, &str, &str)] = &[
("connect", "[URI]", "Connect to server"),
("get", "<PATH> [[PATH] ...]", "Get signal value(s)"),
("gettarget", "<PATH> [[PATH] ...]", "Get target value(s)"),
("actuate", "<PATH> <VALUE>", "Set actuator signal"),
(
"subscribe",
Expand Down Expand Up @@ -237,6 +238,48 @@ pub async fn kuksa_main(_cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
}
}
}
"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());

Expand Down Expand Up @@ -908,7 +951,7 @@ impl<Term: Terminal> Completer<Term> 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)
Expand Down
2 changes: 1 addition & 1 deletion databroker/tests/read_write_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/kuksa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ impl KuksaClient {

pub async fn get_target_values(
&mut self,
paths: Vec<&str>,
paths: Vec<String>,
) -> Result<Vec<DataEntry>, 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(),
Expand Down

0 comments on commit ccc382c

Please sign in to comment.