From 2b5e6d9fe9a192ac868d830d777644f744ceff80 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 09 Dec 2017 21:10:29 +0000 Subject: [PATCH] Use Durations rather than hh:mm:ss strings --- Cargo.toml | 2 +- src/device.rs | 38 +++++++++++++++++++++++++++++++------- tests/integration_test.rs | 2 +- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1697f95..f0ee944 100644 --- a/Cargo.toml +++ a/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "sonos" version = "0.1.0" -authors = ["Jordan Doyle "] +authors = ["Jordan Doyle "] license = "MIT" [dependencies] diff --git a/src/device.rs b/src/device.rs index b70e956..ef7b8ce 100644 --- a/src/device.rs +++ a/src/device.rs @@ -1,13 +1,14 @@ extern crate regex; extern crate reqwest; extern crate xmltree; use std::net::IpAddr; +use std::io::Read; +use std::time::Duration; use error::*; use self::xmltree::Element; use self::reqwest::header::{ContentType, Headers}; use self::regex::Regex; -use std::io::Read; #[derive(Debug)] pub struct Speaker { @@ -28,8 +29,8 @@ pub album: String, pub queue_position: u64, pub uri: String, - pub duration: String, - pub relative_time: String, + pub duration: Duration, + pub running_time: Duration, } #[derive(Debug, PartialEq)] @@ -269,7 +270,15 @@ } /// Seek to a time on the current track - pub fn seek(&self, hours: &u8, minutes: &u8, seconds: &u8) -> Result<()> { + pub fn seek(&self, time: &Duration) -> Result<()> { + const SECS_PER_MINUTE: u64 = 60; + const MINS_PER_HOUR: u64 = 60; + const SECS_PER_HOUR: u64 = 3600; + + let seconds = time.as_secs() % SECS_PER_MINUTE; + let minutes = (time.as_secs() / SECS_PER_MINUTE) % MINS_PER_HOUR; + let hours = time.as_secs() / SECS_PER_HOUR; + self.soap( "MediaRenderer/AVTransport/Control", "urn:schemas-upnp-org:service:AVTransport:1", @@ -503,6 +512,21 @@ let metadata = metadata .get_child("item") .chain_err(|| "Failed to parse XML from Sonos controller")?; + + // convert the given hh:mm:ss to a Duration + let duration: Vec = element_to_string(resp.get_child("TrackDuration") + .chain_err(|| "Failed to get track duration")?) + .splitn(3, ":") + .map(|s| s.parse::().unwrap()) + .collect(); + let duration = Duration::from_secs((duration[0] * 3600) + (duration[1] * 60) + duration[2]); + + let running_time: Vec = element_to_string(resp.get_child("RelTime") + .chain_err(|| "Failed to get relative time")?) + .splitn(3, ":") + .map(|s| s.parse::().unwrap()) + .collect(); + let running_time = Duration::from_secs((running_time[0] * 3600) + (running_time[1] * 60) + running_time[2]); Ok(Track { title: element_to_string(metadata @@ -520,10 +544,8 @@ .unwrap(), uri: element_to_string(resp.get_child("TrackURI") .chain_err(|| "Failed to get track uri")?), - duration: element_to_string(resp.get_child("TrackDuration") - .chain_err(|| "Failed to get track duration")?), - relative_time: element_to_string(resp.get_child("RelTime") - .chain_err(|| "Failed to get relative time")?), + duration, + running_time, }) } } diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 61d7188..db9aff4 100644 --- a/tests/integration_test.rs +++ a/tests/integration_test.rs @@ -83,7 +83,7 @@ #[test] #[should_panic] fn fail_on_set_invalid_volume() { - sonos::discover().unwrap()[0] + get_speaker() .set_volume(101) .expect_err("Didn't fail on invalid volume"); } -- rgit 0.1.3