use crate::{error::Result, Bravia, RequestBodyBuilder, RequestBuilder};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::collections::HashMap;
const ENDPOINT: &str = "avContent";
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Content {
pub uri: String,
pub title: Option<String>,
#[serde(default)]
pub index: i32,
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct ExternalInputStatus {
pub icon: String,
pub connection: bool,
pub label: String,
pub title: String,
pub uri: String,
pub status: Option<String>,
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct PlayingContentInfo {
pub source: String,
pub title: String,
pub uri: String,
}
pub struct AvContentService<'a>(&'a Bravia);
impl<'a> AvContentService<'a> {
pub fn new(bravia: &'a Bravia) -> Self {
Self(bravia)
}
pub async fn get_content_count(
&self,
source: String,
content_type: Option<String>,
target: Option<String>,
version: Option<&str>,
) -> Result<usize> {
let mut params = Map::new();
params.insert(String::from("source"), Value::from(source));
if let Some(t) = content_type {
params.insert(String::from("type"), Value::from(t));
}
if let Some(version) = version {
if version == "1.1" {
if let Some(t) = target {
params.insert(String::from("target"), Value::from(t));
}
}
}
let body = RequestBodyBuilder::default()
.id(11)
.method("getContentCount")
.version(version)
.params(Value::from(params))
.build()?;
let req = RequestBuilder::default()
.endpoint(ENDPOINT)
.body(body)
.is_protected()
.has_result()
.get("count".into())
.make(self.0)
.await?;
Ok(serde_json::from_value(req)?)
}
pub async fn get_content_list(
&self,
uri: Option<String>,
st_idx: Option<u32>,
cnt: Option<u32>,
) -> Result<Vec<Content>> {
let mut params = Map::new();
if let Some(uri) = uri {
params.insert(String::from("uri"), Value::from(uri));
};
if let Some(st_idx) = st_idx {
params.insert(String::from("stIdx"), Value::from(st_idx));
}
if let Some(cnt) = cnt {
params.insert(String::from("cnt"), Value::from(cnt));
}
let body = RequestBodyBuilder::default()
.id(88)
.method("getContentList")
.version(Some("1.5"))
.params(Value::from(params))
.build()?;
let req = RequestBuilder::default()
.endpoint(ENDPOINT)
.body(body)
.is_protected()
.has_result()
.make(self.0)
.await?;
Ok(serde_json::from_value(req)?)
}
pub async fn get_current_external_input_status(
&self,
version: Option<&str>,
) -> Result<Vec<ExternalInputStatus>> {
let body = RequestBodyBuilder::default()
.id(105)
.method("getCurrentExternalInputsStatus")
.version(version)
.build()?;
let req = RequestBuilder::default()
.endpoint(ENDPOINT)
.body(body)
.has_result()
.make(self.0)
.await?;
Ok(serde_json::from_value(req)?)
}
pub async fn get_scheme_list(&self) -> Result<Vec<String>> {
let body = RequestBodyBuilder::default()
.id(1)
.method("getSchemeList")
.build()?;
let req = RequestBuilder::default()
.endpoint(ENDPOINT)
.body(body)
.has_result()
.make(self.0)
.await?;
let vec: Vec<HashMap<String, String>> = serde_json::from_value(req)?;
let mut result = Vec::new();
for map in vec {
result.push(map.into_values().collect());
}
Ok(result)
}
pub async fn get_source_list(&self, scheme: String) -> Result<Vec<String>> {
let mut params = Map::new();
params.insert(String::from("scheme"), Value::from(scheme));
let body = RequestBodyBuilder::default()
.id(1)
.method("getSourceList")
.params(Value::from(params))
.build()?;
let req = RequestBuilder::default()
.endpoint(ENDPOINT)
.body(body)
.has_result()
.make(self.0)
.await?;
let vec: Vec<HashMap<String, String>> = serde_json::from_value(req)?;
let mut result = Vec::new();
for map in vec {
result.push(map.into_values().collect());
}
Ok(result)
}
pub async fn get_playing_content_info(&self) -> Result<PlayingContentInfo> {
let body = RequestBodyBuilder::default()
.id(103)
.method("getPlayingContentInfo")
.build()?;
let req = RequestBuilder::default()
.endpoint(ENDPOINT)
.body(body)
.is_protected()
.has_result()
.make(self.0)
.await?;
Ok(serde_json::from_value(req)?)
}
pub async fn set_play_content(&self, uri: String) -> Result<()> {
let mut params = Map::new();
params.insert(String::from("uri"), Value::from(uri));
let body = RequestBodyBuilder::default()
.id(101)
.method("setPlayContent")
.params(Value::from(params))
.build()?;
RequestBuilder::default()
.endpoint(ENDPOINT)
.body(body)
.is_protected()
.make(self.0)
.await?;
Ok(())
}
}