matroskin 0.1.0

whatsminer api client lib
Documentation
//! Implement `get.device.custom_data` command
//!
//! This command is used to get miner device custom information.
//!
//! - Command: [GetDeviceCustomData]
//! - ApiDoc: <https://apidoc.whatsminer.com/#api-Device-device_get_custom_data>
use core::str;

use serde::Deserialize;

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

/// This command represents the `get.device.custom_data` operation.
///
/// It is used to get miner device custom information.
///
/// - ApiDoc: <https://apidoc.whatsminer.com/#api-Device-device_get_custom_data>
///
/// # Example
/// ```rust,no_run
/// use matroskin::actor::Actor;
/// use matroskin::command::get_device_custom_data::GetDeviceCustomData;
/// 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 = GetDeviceCustomData::default();
///     let response = actor.send(cmd).await?;
///     println!("Response: {:#?}", response);
///
///     Ok(())
/// }
/// ```
#[derive(Debug, Default)]
pub struct GetDeviceCustomData;

/// [GetDeviceCustomData] Response
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct GetDeviceCustomDataResponse {
    #[serde(rename = "CustomerSn")]
    pub custom_sn: String,
    pub msg0: String,
    pub msg1: String,
    pub msg2: String,
    pub msg3: String,
    pub msg4: String,
    pub msg5: String,
    pub msg6: String,
    pub msg7: String,
    pub msg8: String,
    pub msg9: String,
}

impl Command for GetDeviceCustomData {
    type Response = GetDeviceCustomDataResponse;
    const CMD_NAME: &'static str = "get.device.custom_data";
    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":1692685443,"msg":{"CustomerSn":"sn12345","msg0":"msg","msg1":"msg","msg2":"msg","msg3":"msg","msg4":"msg","msg5":"msg","msg6":"msg","msg7":"msg","msg8":"msg","msg9":"msg"},"desc":"get.device.custom_data"}"#;

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

    #[test]
    fn deserialize_response_fixture() {
        let response = GetDeviceCustomData::parse_response(RESPONSE_JSON.as_bytes()).unwrap();
        assert!(response.code.is_success());
        assert_eq!(response.desc, "get.device.custom_data");
        assert_eq!(response.when, 1692685443);
        assert_eq!(response.msg.custom_sn, "sn12345");
        assert_eq!(response.msg.msg0, "msg");
        assert_eq!(response.msg.msg9, "msg");
    }
}