matroskin 0.1.0

whatsminer api client lib
Documentation
//! Implement `set.miner.upfreq_speed` command
//!
//! Sets the speed at which the miner adjusts its frequency to reach a stable hash rate.
//!
//! - Command: [SetMinerUpfreqSpeed]
//! - ApiDoc: <https://apidoc.whatsminer.com/#api-Miner-btminer_set_upfreq_speed>
use core::str;

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

/// This command represents the `set.miner.upfreq_speed` operation.
///
/// Sets the speed at which the miner adjusts its frequency.
/// A higher value results in faster startup but may impact stability.
///
/// - ApiDoc: <https://apidoc.whatsminer.com/#api-Miner-btminer_set_upfreq_speed>
///
/// # Example
/// ```rust,no_run
/// use matroskin::actor::Actor;
/// use matroskin::account::Account;
/// use matroskin::password::Password;
/// use matroskin::command::set_miner_upfreq_speed::SetMinerUpfreqSpeed;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let actor = Actor::new("10.10.10.10:4433", Account::Super, Password::Super);
///     actor.connect()?;
///
///     let cmd = SetMinerUpfreqSpeed::new(7);
///     let response = actor.send(cmd).await?;
///     println!("Response: {:#?}", response);
///
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetMinerUpfreqSpeed {
    /// Upfreq speed (0 to 10)
    pub speed: u8,
}

impl SetMinerUpfreqSpeed {
    /// Create a new `set.miner.upfreq_speed` command.
    pub const fn new(speed: u8) -> Self {
        Self { speed }
    }
}

impl Command for SetMinerUpfreqSpeed {
    type Response = String;
    const CMD_NAME: &'static str = "set.miner.upfreq_speed";
    const LEVEL: CommandLevel = CommandLevel::Secured;
    const MINIMAL_PERMISSIONS: PermissionLevel = PermissionLevel::ReadWrite;

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

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

    use super::*;

    const RESPONSE_JSON: &str =
        r#"{"code":0,"when":1692685496,"msg":"ok","desc":"set.miner.upfreq_speed"}"#;

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

        let auth = AuthData::new::<SetMinerUpfreqSpeed>(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.miner.upfreq_speed");
        assert_eq!(json["param"], 7);
        assert_eq!(json["account"], "super");
        assert!(json["ts"].as_u64().unwrap() > 0);
        assert_eq!(json["token"].as_str().unwrap().len(), 8);
    }

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

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