framesmith-cli 0.1.0

CLI tool for controlling Samsung Frame TVs over the local network
use std::net::IpAddr;
use std::path::Path;
use std::time::Duration;

use anyhow::{Context, Result};
use framesmith::{FileAuthTokenStore, FrameTv};

pub async fn connect(host: &str, token_file: &Path, timeout_secs: u64) -> Result<FrameTv> {
    let ip: IpAddr = host
        .parse()
        .with_context(|| format!("invalid IP address: {host}"))?;

    // Ensure token file parent directory exists
    if let Some(parent) = token_file.parent() {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("failed to create directory: {}", parent.display()))?;
    }

    let tv = FrameTv::connection_builder(ip)
        .auth_token_store(FileAuthTokenStore::new(token_file))
        .connection_timeout(Duration::from_secs(timeout_secs))
        .connect()
        .await
        .context("failed to connect to TV")?;

    Ok(tv)
}