printnanny_cli/
cam.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::io;
use std::io::Write;

use anyhow::{Ok, Result};

use printnanny_gst_pipelines::factory::PrintNannyPipelineFactory;
use printnanny_settings::{cam::CameraVideoSource, SettingsFormat};

pub struct CameraCommand;

impl CameraCommand {
    async fn list(args: &clap::ArgMatches) -> Result<()> {
        let output = CameraVideoSource::from_libcamera_list().await?;
        let f: SettingsFormat = args.value_of_t("format").unwrap();

        let v = match f {
            SettingsFormat::Json => serde_json::to_vec_pretty(&output)?,
            SettingsFormat::Toml => toml::ser::to_vec(&output)?,
            _ => todo!(),
        };
        io::stdout().write_all(&v)?;

        Ok(())
    }

    async fn start_pipelines(args: &clap::ArgMatches) -> Result<()> {
        let address = args.value_of("http-address").unwrap();
        let port: i32 = args.value_of_t("http-port").unwrap();
        let factory = PrintNannyPipelineFactory::new(address.into(), port);
        factory.start_pipelines().await?;
        Ok(())
    }

    async fn stop_pipelines(args: &clap::ArgMatches) -> Result<()> {
        let address = args.value_of("http-address").unwrap();
        let port: i32 = args.value_of_t("http-port").unwrap();
        let factory = PrintNannyPipelineFactory::new(address.into(), port);
        factory.stop_pipelines().await?;
        Ok(())
    }

    // async fn start_multifilesink_listener(args: &clap::ArgMatches) -> Result<()> {
    //     let address = args.value_of("http-address").unwrap();
    //     let port: i32 = args.value_of_t("http-port").unwrap();
    //     let pipeline = args.value_of("pipeline").unwrap();
    //     let factory = PrintNannyPipelineFactory::new(address.into(), port);
    //     factory
    //         .run_multifilesink_fragment_publisher(pipeline)
    //         .await?;
    //     Ok(())
    // }

    pub async fn handle(args: &clap::ArgMatches) -> Result<()> {
        match args.subcommand() {
            Some(("list", args)) => Self::list(args).await,
            // Some(("start-multifilesink-listener", args)) => {
            //     Self::start_multifilesink_listener(args).await
            // }
            Some(("start-pipelines", args)) => Self::start_pipelines(args).await,
            Some(("stop-pipelines", args)) => Self::stop_pipelines(args).await,
            _ => unimplemented!(),
        }
    }
}