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 brewing stand device
///
/// [OC2 Docs](https://fnuecke.github.io/oc2/manual/en_us/device/brewing_stand.html)
pub struct BrewingStand {
    client: HlapiClient,
    uuid: Uuid,
}

impl BrewingStand {
    /// Gets the remaining brew time. [`None`] if not currently brewing.
    pub fn get_brew_time(&self) -> Result<Option<Duration>, HlapiClientError> {
        let raw: u32 = self.call("getBrewTime", &[], &[])?.0;
        if raw == 0 {
            Ok(None)
        } else {
            Ok(Some(Duration::from_millis(raw as u64 * 50)))
        }
    }

    /// Gets how many brews the stand has remaining with the current fuel level
    pub fn get_fuel(&self) -> Result<u32, HlapiClientError> {
        Ok(self.call("getFuel", &[], &[])?.0)
    }
}

impl DeviceBase for BrewingStand {
    const TYPE: &'static str = "brewing_stand";

    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
    }
}