use anyhow::{Context, Result}; use lesavka_common::lesavka::{ CapturePowerCommand, Empty, SetCapturePowerRequest, relay_client::RelayClient, }; use tonic::{Request, transport::Channel}; use super::state::CapturePowerStatus; pub fn fetch_capture_power(server_addr: &str) -> Result { with_runtime(async move { let mut client = connect(server_addr).await?; let reply = client .get_capture_power(Request::new(Empty {})) .await .context("querying capture power state")? .into_inner(); Ok(map_state(reply)) }) } pub fn set_capture_power_mode( server_addr: &str, command: CapturePowerCommand, ) -> Result { with_runtime(async move { let mut client = connect(server_addr).await?; let reply = client .set_capture_power(Request::new(SetCapturePowerRequest { enabled: matches!(command, CapturePowerCommand::ForceOn), command: command as i32, })) .await .context("setting capture power state")? .into_inner(); Ok(map_state(reply)) }) } fn with_runtime(future: F) -> Result where F: std::future::Future>, { tokio::runtime::Builder::new_current_thread() .enable_all() .build() .context("building launcher power runtime")? .block_on(future) } async fn connect(server_addr: &str) -> Result> { let channel = Channel::from_shared(server_addr.to_string()) .context("invalid launcher server address")? .tcp_nodelay(true) .connect() .await .context("connecting launcher to relay host")?; Ok(RelayClient::new(channel)) } fn map_state(reply: lesavka_common::lesavka::CapturePowerState) -> CapturePowerStatus { CapturePowerStatus { available: reply.available, enabled: reply.enabled, unit: reply.unit, detail: reply.detail, active_leases: reply.active_leases, mode: reply.mode, } }