matroskin 0.1.0

whatsminer api client lib
Documentation
//! Implement `set.miner.power_limit` command
//!
//! Sets the maximum power consumption for the miner in watts.
//!
//! - Command: [SetMinerPowerLimit]
//! - ApiDoc: <https://apidoc.whatsminer.com/#api-Miner-btminer_set_power_limit>
use core::str;

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

/// This command represents the `set.miner.power_limit` operation.
///
/// Sets the maximum power consumption for the miner in watts.
/// The power limit cannot exceed the device's standard operating power.
/// The device will restart to apply the new setting. Range: 0-99999.
///
/// - ApiDoc: <https://apidoc.whatsminer.com/#api-Miner-btminer_set_power_limit>
///
/// # Example
/// ```rust,no_run
/// use matroskin::actor::Actor;
/// use matroskin::account::Account;
/// use matroskin::password::Password;
/// use matroskin::command::set_miner_power_limit::SetMinerPowerLimit;
///
/// #[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 = SetMinerPowerLimit::new(3000);
///     let response = actor.send(cmd).await?;
///     println!("Response: {:#?}", response);
///
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetMinerPowerLimit {
    /// Power limit in watts
    pub limit: u32,
}

impl SetMinerPowerLimit {
    /// Create a new `set.miner.power_limit` command.
    pub const fn new(limit: u32) -> Self {
        Self { limit }
    }
}

impl Command for SetMinerPowerLimit {
    type Response = String;
    const CMD_NAME: &'static str = "set.miner.power_limit";
    const LEVEL: CommandLevel = CommandLevel::Secured;
    fn params(&self) -> Result<Option<impl serde::Serialize>> {
        Ok(Some(self.limit))
    }
}

#[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.power_limit"}"#;

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

        let auth = AuthData::new::<SetMinerPowerLimit>(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.power_limit");
        assert_eq!(json["param"], 3000);
        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 = SetMinerPowerLimit::new(3000);
        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"], 3000);
    }

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