framesmith-cli 0.1.0

CLI tool for controlling Samsung Frame TVs over the local network
use anyhow::{Result, bail};

use crate::cli::{MotionCommand, MotionSensitivityValue, MotionTimerValue};
use crate::executor::Executor;
use crate::ipc::{Request, Response};

pub async fn run(exec: &Executor, sub: &MotionCommand, json: bool) -> Result<()> {
    let request = match sub {
        MotionCommand::Timer { value } => {
            let minutes = match value {
                MotionTimerValue::Off => "off",
                MotionTimerValue::Min5 => "5",
                MotionTimerValue::Min15 => "15",
                MotionTimerValue::Min30 => "30",
                MotionTimerValue::Min60 => "60",
                MotionTimerValue::Min120 => "120",
                MotionTimerValue::Min240 => "240",
            };
            Request::MotionTimer {
                minutes: minutes.to_string(),
            }
        }
        MotionCommand::Sensitivity { value } => {
            let level = match value {
                MotionSensitivityValue::Low => "low",
                MotionSensitivityValue::Medium => "medium",
                MotionSensitivityValue::High => "high",
            };
            Request::MotionSensitivity {
                level: level.to_string(),
            }
        }
    };

    let response = exec.send_request(request).await?;
    let data = match &response {
        Response::Ok { data } => data,
        Response::Error { message } => bail!("{message}"),
        Response::TvDisconnected { message } => bail!("{message}"),
    };

    match sub {
        MotionCommand::Timer { .. } => {
            if json {
                println!("{}", serde_json::to_string_pretty(data)?);
            } else {
                println!("Motion timer set.");
            }
        }
        MotionCommand::Sensitivity { .. } => {
            if json {
                println!("{}", serde_json::to_string_pretty(data)?);
            } else {
                println!("Motion sensitivity set.");
            }
        }
    }
    Ok(())
}