use std::{convert::Infallible, time::Duration};
use uuid::Uuid;
use crate::client::{DeviceBase, HlapiClient, HlapiClientError};
#[derive(Debug, Clone)]
pub struct Jukebox {
client: HlapiClient,
uuid: Uuid,
}
impl Jukebox {
pub fn get_elapsed(&self) -> Result<Option<Duration>, HlapiClientError> {
let raw: u32 = self.call("getElapsed", &[], &[])?.0;
if raw == 0 {
Ok(None)
} else {
Ok(Some(Duration::from_millis(raw as u64 * 50)))
}
}
pub fn get_song(&self) -> Result<Option<String>, HlapiClientError> {
Ok(self.call("getSong", &[], &[])?.0)
}
pub fn get_song_length(&self) -> Result<Option<Duration>, HlapiClientError> {
let raw: u32 = self.call("getSongLength", &[], &[])?.0;
if raw == 0 {
Ok(None)
} else {
Ok(Some(Duration::from_millis(raw as u64 * 50)))
}
}
pub fn is_playing(&self) -> Result<bool, HlapiClientError> {
Ok(self.call("isPlaying", &[], &[])?.0)
}
}
impl DeviceBase for Jukebox {
const TYPE: &'static str = "jukebox";
type Event = Infallible;
fn new(client: HlapiClient, uuid: Uuid) -> Result<Self, HlapiClientError> {
Ok(Self { client, uuid })
}
fn uuid_base(&self) -> Uuid {
self.uuid
}
fn client(&self) -> &HlapiClient {
&self.client
}
}