Remove obsolete function, make call_service async
This commit is contained in:
parent
1878c99482
commit
293050d070
1 changed files with 10 additions and 61 deletions
71
src/main.rs
71
src/main.rs
|
@ -5,12 +5,13 @@ use clap::Parser;
|
||||||
use color_eyre::{eyre::WrapErr, Result};
|
use color_eyre::{eyre::WrapErr, Result};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use tracing::{debug, trace};
|
use tracing::debug;
|
||||||
use tracing_error::ErrorLayer;
|
use tracing_error::ErrorLayer;
|
||||||
use tracing_subscriber::{prelude::*, EnvFilter, Registry};
|
use tracing_subscriber::{prelude::*, EnvFilter, Registry};
|
||||||
use tracing_tree::HierarchicalLayer;
|
use tracing_tree::HierarchicalLayer;
|
||||||
|
|
||||||
mod homeassistant;
|
mod homeassistant;
|
||||||
|
mod output;
|
||||||
|
|
||||||
/// Bert
|
/// Bert
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
|
@ -101,6 +102,7 @@ async fn main() -> Result<()> {
|
||||||
if let Some(cmd) = cmd {
|
if let Some(cmd) = cmd {
|
||||||
debug!("Calling service '{cmd}' on entity '{entity}'");
|
debug!("Calling service '{cmd}' on entity '{entity}'");
|
||||||
call_service(cmd, host, entity, insecure, token)
|
call_service(cmd, host, entity, insecure, token)
|
||||||
|
.await
|
||||||
.wrap_err_with(|| format!("Unable to execute command {cmd}"))?;
|
.wrap_err_with(|| format!("Unable to execute command {cmd}"))?;
|
||||||
} else {
|
} else {
|
||||||
debug!("Retrieving state of '{entity}' on '{host}'");
|
debug!("Retrieving state of '{entity}' on '{host}'");
|
||||||
|
@ -132,63 +134,7 @@ fn setup(debug: bool) -> Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument]
|
#[tracing::instrument]
|
||||||
fn get_now_playing(host: String, entity: String, insecure: bool, token: String) -> Result<()> {
|
async fn call_service(
|
||||||
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::<serde_json::Value>()?;
|
|
||||||
|
|
||||||
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(
|
|
||||||
command: Command,
|
command: Command,
|
||||||
host: String,
|
host: String,
|
||||||
entity: String,
|
entity: String,
|
||||||
|
@ -217,15 +163,18 @@ fn call_service(
|
||||||
_ => json!({ "entity_id": entity }).to_string(),
|
_ => 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
|
let response = client
|
||||||
.post(url)
|
.post(url)
|
||||||
.body(body)
|
.body(body)
|
||||||
.bearer_auth(token)
|
.bearer_auth(token)
|
||||||
.header("Accept", "application/json")
|
.header("Accept", "application/json")
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.send()?
|
.send()
|
||||||
.json::<serde_json::Value>()?;
|
.await?
|
||||||
|
.json::<serde_json::Value>()
|
||||||
|
.await?;
|
||||||
|
|
||||||
debug!("{:#?}", response);
|
debug!("{:#?}", response);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue