oc2_hlapi_v2 0.2.0

A client for the new version of OpenComputers II's HLAPI
Documentation
use std::{convert::Infallible, time::Duration};

use uuid::Uuid;

use crate::client::{DeviceBase, HlapiClient, HlapiClientError};

#[derive(Debug, Clone)]
/// A jukebox device
///
/// [OC2 Docs](https://fnuecke.github.io/oc2/manual/en_us/device/jukebox.html)
pub struct Jukebox {
    client: HlapiClient,
    uuid: Uuid,
}

impl Jukebox {
    /// Gets how long the jukebox has been playing the current song. [`None`] if no song is playing,
    /// or if the song just started playing in the current game tick.
    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)))
        }
    }

    /// Gets the song that the jukebox is playing.  [`None`] if no song is
    /// playing.
    pub fn get_song(&self) -> Result<Option<String>, HlapiClientError> {
        Ok(self.call("getSong", &[], &[])?.0)
    }

    /// Gets how long the song the jukebox is playing lasts in total. [`None`] if no song is
    /// playing.
    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)))
        }
    }

    /// Gets whether the jukebox is currently playing a song
    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
    }
}