Skip to main content

viva_camctl/
cmd_get.rs

1use std::net::Ipv4Addr;
2use std::time::Duration;
3
4use anyhow::{Context, Result};
5use serde::Serialize;
6use tracing::info;
7
8use viva_gige::nic::IfaceSelector;
9
10use crate::common::{self, DEFAULT_DISCOVERY_TIMEOUT_MS};
11
12#[derive(Serialize)]
13struct FeatureValue<'a> {
14    name: &'a str,
15    value: String,
16}
17
18pub async fn run(
19    ip: Option<Ipv4Addr>,
20    index: Option<usize>,
21    name: String,
22    iface: Option<IfaceSelector>,
23    json: bool,
24) -> Result<()> {
25    let timeout = Duration::from_millis(DEFAULT_DISCOVERY_TIMEOUT_MS);
26    let device = common::select_device(ip, index, iface.as_ref(), timeout).await?;
27    info!(ip = %device.ip, "opening camera for get");
28    let camera = common::open_camera(&device)
29        .await
30        .context("open camera for get")?;
31    let value = camera
32        .get(&name)
33        .with_context(|| format!("read feature {name}"))?;
34
35    if json {
36        let payload = FeatureValue { name: &name, value };
37        common::print_json(&payload)?;
38    } else {
39        println!("{}", value);
40    }
41
42    Ok(())
43}