Skip to content

Commit

Permalink
Merge pull request #5 from tsirysndr/feat/GetTransportInfo
Browse files Browse the repository at this point in the history
feat: add support for `GetTransportInfo` action
  • Loading branch information
tsirysndr authored Feb 12, 2023
2 parents 8f223bd + 4d65b65 commit 5c3e13d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "upnp-client"
version = "0.1.5"
version = "0.1.6"
edition = "2021"
repository = "https://github.com/tsirysndr/upnp-client-rs"
license = "MIT"
Expand Down
17 changes: 15 additions & 2 deletions src/media_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ use xml_builder::{XMLBuilder, XMLElement};

use crate::{
device_client::DeviceClient,
parser::{parse_duration, parse_position, parse_supported_protocols, parse_volume},
types::{Event, LoadOptions, Metadata, ObjectClass},
parser::{
parse_duration, parse_position, parse_supported_protocols, parse_transport_info,
parse_volume,
},
types::{Event, LoadOptions, Metadata, ObjectClass, TransportInfo},
BROADCAST_EVENT,
};

Expand Down Expand Up @@ -262,6 +265,16 @@ impl MediaRendererClient {
}
}
}

pub async fn get_transport_info(&self) -> Result<TransportInfo, Error> {
let mut params = HashMap::new();
params.insert("InstanceID".to_string(), "0".to_string());
let response = self
.device_client
.call_action("AVTransport", "GetTransportInfo", params)
.await?;
Ok(parse_transport_info(response.as_str())?)
}
}

fn build_metadata(m: Metadata, media_type: ObjectClass) -> String {
Expand Down
50 changes: 49 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use crate::types::{Action, Argument, Container, Device, Item, Metadata, Service};
use crate::types::{Action, Argument, Container, Device, Item, Metadata, Service, TransportInfo};
use anyhow::Error;
use elementtree::Element;
use surf::{http::Method, Client, Config, Url};
Expand Down Expand Up @@ -707,3 +707,51 @@ pub fn deserialize_content_directory(
}
Ok((containers, items))
}

pub fn parse_transport_info(xml: &str) -> Result<TransportInfo, Error> {
let parser = EventReader::from_str(xml);
let mut in_transport_state = false;
let mut in_transport_status = false;
let mut in_transport_play_speed = false;
let mut transport_info = TransportInfo::default();

for e in parser {
match e {
Ok(XmlEvent::StartElement { name, .. }) => {
if name.local_name == "CurrentTransportState" {
in_transport_state = true;
}
if name.local_name == "CurrentTransportStatus" {
in_transport_status = true;
}
if name.local_name == "CurrentSpeed" {
in_transport_play_speed = true;
}
}
Ok(XmlEvent::EndElement { name }) => {
if name.local_name == "CurrentTransportState" {
in_transport_state = false;
}
if name.local_name == "CurrentTransportStatus" {
in_transport_status = false;
}
if name.local_name == "CurrentSpeed" {
in_transport_play_speed = false;
}
}
Ok(XmlEvent::Characters(value)) => {
if in_transport_state {
transport_info.current_transport_state = value.clone();
}
if in_transport_status {
transport_info.current_transport_status = value.clone();
}
if in_transport_play_speed {
transport_info.current_speed = value.clone();
}
}
_ => {}
}
}
Ok(transport_info)
}
7 changes: 7 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,10 @@ pub struct Item {
pub duration: Option<String>,
pub object_class: Option<ObjectClass>,
}

#[derive(Debug, Clone, Default)]
pub struct TransportInfo {
pub current_transport_state: String,
pub current_transport_status: String,
pub current_speed: String,
}

0 comments on commit 5c3e13d

Please sign in to comment.