nle-cloud-sdk 0.1.1

Unofficial asynchronous Rust SDK for the NLECloud (新大陆物联网云平台) IoT platform
Documentation
use crate::client::NleCloudClient;
use crate::error::{Result, SdkError};
use reqwest::Method;
use serde::Serialize;

impl NleCloudClient {
    /// Dispatches a control command to an actuator on a device.
    ///
    /// The `data` parameter can be a switch command (`1`/`0`), integer, float, string, or complex JSON value.
    ///
    /// Endpoint: `POST /Cmds?deviceId={deviceId}&apiTag={apiTag}`
    pub async fn send_cmd<T: Serialize>(
        &self,
        device_id: i32,
        api_tag: &str,
        data: &T,
        token_override: Option<&str>,
    ) -> Result<()> {
        if device_id <= 0 {
            return Err(SdkError::InvalidParameter(
                "deviceId must be greater than zero".to_string(),
            ));
        }
        if api_tag.trim().is_empty() {
            return Err(SdkError::InvalidParameter(
                "apiTag cannot be empty".to_string(),
            ));
        }

        let device_id_str = device_id.to_string();
        let query = [("deviceId", device_id_str.as_str()), ("apiTag", api_tag)];

        let body = serde_json::to_value(data)?;
        self.request_empty(
            Method::POST,
            "Cmds",
            &query,
            Some(body),
            token_override,
            true,
        )
        .await
    }
}