matroskin 0.1.0

whatsminer api client lib
Documentation
//! Implement `get.fan.setting` command
//!
//! This command is used to get the miner settings summary.
//!
//! - Command: [GetFanSettings]
//! - ApiDoc: <https://apidoc.whatsminer.com/#api-Fan-get_fan_settings>
use core::str;

use serde::Deserialize;

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

/// This command represents the `get.fan.setting` operation.
///
/// It is used to get the miner settings summary.
///
/// - ApiDoc: <https://apidoc.whatsminer.com/#api-Fan-get_fan_settings>
///
/// # Example
/// ```rust,no_run
/// use matroskin::actor::Actor;
/// use matroskin::command::get_fan_setting::GetFanSettings;
/// 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 = GetFanSettings::default();
///     let response = actor.send(cmd).await?;
///     println!("Response: {:#?}", response);
///
///     Ok(())
/// }
/// ```
#[derive(Debug, Default)]
pub struct GetFanSettings;

/// [GetFanSettings] Response
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct GetFanSettingsResponse {
    pub fan_poweroff_cool: u8,
    pub fan_zero_speed: i64,
    pub fan_temp_offset: i64,
}

impl Command for GetFanSettings {
    type Response = GetFanSettingsResponse;
    const CMD_NAME: &'static str = "get.fan.setting";
    const LEVEL: CommandLevel = CommandLevel::Public;
    const MINIMAL_PERMISSIONS: PermissionLevel = PermissionLevel::ReadOnly;

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

#[cfg(test)]
mod tests {
    use super::*;

    const RESPONSE_JSON: &str = r#"{"code":0,"when":1692858102,"msg":{"fan-poweroff-cool":1,"fan-zero-speed":0,"fan-temp-offset":-10},"desc":"get.fan.setting"}"#;

    #[test]
    fn request_no_params_no_auth() {
        let cmd = GetFanSettings;
        let encoded = cmd.encode(None).unwrap();
        let json: serde_json::Value = serde_json::from_slice(&encoded.0).unwrap();
        assert_eq!(json["cmd"], "get.fan.setting");
        assert!(json.get("param").is_none());
        assert!(json.get("token").is_none());
    }

    #[test]
    fn deserialize_response_fixture() {
        let response = GetFanSettings::parse_response(RESPONSE_JSON.as_bytes()).unwrap();
        assert!(response.code.is_success());
        assert_eq!(response.desc, "get.fan.setting");
        assert_eq!(response.when, 1692858102);
        assert_eq!(response.msg.fan_poweroff_cool, 1);
        assert_eq!(response.msg.fan_zero_speed, 0);
        assert_eq!(response.msg.fan_temp_offset, -10);
    }
}