Skip to main content

viva_camctl/
cmd_set.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 SetResponse<'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    value: String,
23    iface: Option<IfaceSelector>,
24    json: bool,
25) -> Result<()> {
26    let timeout = Duration::from_millis(DEFAULT_DISCOVERY_TIMEOUT_MS);
27    let device = common::select_device(ip, index, iface.as_ref(), timeout).await?;
28    info!(ip = %device.ip, "opening camera for set");
29    let mut camera = common::open_camera(&device)
30        .await
31        .context("open camera for set")?;
32    camera
33        .set(&name, &value)
34        .with_context(|| format!("write feature {name}"))?;
35    let read_back = camera
36        .get(&name)
37        .with_context(|| format!("read feature {name}"))?;
38
39    if json {
40        let payload = SetResponse {
41            name: &name,
42            value: read_back,
43        };
44        common::print_json(&payload)?;
45    } else {
46        println!("{}", read_back);
47    }
48
49    Ok(())
50}