From 293050d070d3767c9ee98c76acf7275c5cdb74a6 Mon Sep 17 00:00:00 2001 From: Erwin Boskma Date: Thu, 7 Apr 2022 22:01:49 +0200 Subject: [PATCH] Remove obsolete function, make call_service async --- src/main.rs | 71 ++++++++--------------------------------------------- 1 file changed, 10 insertions(+), 61 deletions(-) diff --git a/src/main.rs b/src/main.rs index 573e60d..385651a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,12 +5,13 @@ use clap::Parser; use color_eyre::{eyre::WrapErr, Result}; use serde_json::json; use std::fmt::Display; -use tracing::{debug, trace}; +use tracing::debug; use tracing_error::ErrorLayer; use tracing_subscriber::{prelude::*, EnvFilter, Registry}; use tracing_tree::HierarchicalLayer; mod homeassistant; +mod output; /// Bert #[derive(Parser, Debug)] @@ -101,6 +102,7 @@ async fn main() -> Result<()> { if let Some(cmd) = cmd { debug!("Calling service '{cmd}' on entity '{entity}'"); call_service(cmd, host, entity, insecure, token) + .await .wrap_err_with(|| format!("Unable to execute command {cmd}"))?; } else { debug!("Retrieving state of '{entity}' on '{host}'"); @@ -132,63 +134,7 @@ fn setup(debug: bool) -> Result<()> { } #[tracing::instrument] -fn get_now_playing(host: String, entity: String, insecure: bool, token: String) -> Result<()> { - let url = format!( - "{}://{}/api/states/{}", - if insecure { "http" } else { "https" }, - host, - entity - ); - - let client = reqwest::blocking::Client::new(); - let response = client - .get(url) - .bearer_auth(token) - .header("Accept", "application/json") - .header("Content-Type", "application/json") - .send()? - .json::()?; - - trace!("Response: {:#?}", response); - - if response["state"] == "playing" { - let attributes = &response["attributes"]; - let maybe_channel = attributes["media_channel"].as_str(); - let artist = attributes["media_artist"] - .as_str() - .map_or("No artist", |artist| artist); - let title = attributes["media_title"] - .as_str() - .map_or("No title", |title| title); - let volume_raw = attributes["volume_level"] - .as_f64() - .map_or(-1., |volume| volume); - - let volume = if volume_raw >= 0. { - format!("[{:3.0}%] ", volume_raw * 100.) - } else { - String::new() - }; - - let now_playing = if let Some(channel) = maybe_channel { - format!("{volume}[{channel}] {artist} - {title}") - } else { - format!("{volume}{artist} - {title}") - }; - - println!("{}", html_escape::encode_text(&now_playing)); - } else { - let state = response["state"] - .as_str() - .map_or("State unknown", |state| state); - println!("Sonos {}", html_escape::encode_text(state)); - } - - Ok(()) -} - -#[tracing::instrument] -fn call_service( +async fn call_service( command: Command, host: String, entity: String, @@ -217,15 +163,18 @@ fn call_service( _ => json!({ "entity_id": entity }).to_string(), }; - let client = reqwest::blocking::Client::new(); + // let client = reqwest::blocking::Client::new(); + let client = reqwest::Client::new(); let response = client .post(url) .body(body) .bearer_auth(token) .header("Accept", "application/json") .header("Content-Type", "application/json") - .send()? - .json::()?; + .send() + .await? + .json::() + .await?; debug!("{:#?}", response);