matroskin 0.1.0

whatsminer api client lib
Documentation
//! Implement `set.fan.temp_offset` command
//!
//! Sets the target temperature offset for the fan control system, in degrees Celsius.
//! This feature is only available on air-cooled devices.
//!
//! - Command: [SetFanTempOffset]
//! - ApiDoc: <https://apidoc.whatsminer.com/#api-Fan-btminer_set_fan_temp_offset>
use core::str;

use crate::{
    command::{Command, CommandLevel},
    error::Result,
};

/// This command represents the `set.fan.temp_offset` operation.
///
/// Sets the target temperature offset for the fan control system, in degrees Celsius.
/// This feature is only available on air-cooled devices.
///
/// - ApiDoc: <https://apidoc.whatsminer.com/#api-Fan-btminer_set_fan_temp_offset>
///
/// # Example
/// ```rust,no_run
/// use matroskin::actor::Actor;
/// use matroskin::command::set_fan_temp_offset::SetFanTempOffset;
/// use matroskin::account::Account;
/// use matroskin::password::Password;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let addr = "10.10.10.10:4433";
///     let username = Account::Super;
///     let password = Password::Super;
///     let actor = Actor::new(addr, username, password);
///     actor.connect()?;
///
///     let cmd = SetFanTempOffset::new(-10); // Set offset to -10°C
///     let response = actor.send(cmd).await?;
///     println!("Response: {:#?}", response);
///
///     Ok(())
/// }
/// ```
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct SetFanTempOffset {
    /// Temperature offset in °C (-30 to 0)
    pub offset: i8,
}

impl SetFanTempOffset {
    /// Create a new `set.fan.temp_offset` command.
    pub const fn new(offset: i8) -> Self {
        Self { offset }
    }
}

impl Command for SetFanTempOffset {
    type Response = String;
    const CMD_NAME: &'static str = "set.fan.temp_offset";
    const LEVEL: CommandLevel = CommandLevel::Secured;

    fn params(&self) -> Result<Option<impl serde::Serialize>> {
        Ok(Some(self.offset))
    }
}

#[cfg(test)]
mod tests {
    use crate::account::Account;
    use crate::auth_data::AuthData;

    use super::*;

    const RESPONSE_JSON: &str =
        r#"{"code":0,"when":1692685512,"msg":"ok","desc":"set.fan.temp_offset"}"#;

    #[test]
    fn request_has_param() {
        let cmd = SetFanTempOffset::new(-10);
        assert!(cmd.is_secured());
        assert!(!cmd.is_encrypted());

        let auth = AuthData::new::<SetFanTempOffset>(Account::Super, "pass", "salt").unwrap();
        let encoded = cmd.encode(Some(auth)).unwrap();
        let json: serde_json::Value = serde_json::from_slice(&encoded.0).unwrap();

        assert_eq!(json["cmd"], "set.fan.temp_offset");
        assert_eq!(json["param"], -10);
        assert_eq!(json["account"], "super");
        assert!(json["ts"].as_u64().unwrap() > 0);
        assert_eq!(json["token"].as_str().unwrap().len(), 8);
    }

    #[test]
    fn request_zero_offset() {
        let cmd = SetFanTempOffset::new(0);
        let auth = AuthData::new::<SetFanTempOffset>(Account::Super, "pass", "salt").unwrap();
        let encoded = cmd.encode(Some(auth)).unwrap();
        let json: serde_json::Value = serde_json::from_slice(&encoded.0).unwrap();

        assert_eq!(json["param"], 0);
    }

    #[test]
    fn deserialize_response_fixture() {
        let response = SetFanTempOffset::parse_response(RESPONSE_JSON.as_bytes()).unwrap();
        assert!(response.code.is_success());
        assert_eq!(response.desc, "set.fan.temp_offset");
        assert_eq!(response.when, 1692685512);
        assert_eq!(response.msg, "ok");
    }
}