Bulk Report DPs

Last Updated on : 2024-07-26 09:17:37download

TuyaOS allows you to report the data points (DP) of multiple sub-devices at once, synchronizing the status of a fleet of devices simultaneously.

This topic describes the API for bulk reporting DPs and includes example code to demonstrate its proper use.

API description

Report data

/**
 * @brief Asynchronously send data to the cloud.
 *
 * @param[in] pro      Protocol identifier. For bulk DP reporting, it should be set to 30.
 * @param[in] data    Data string in JSON format. A sample format used for bulk DP reporting: [{"cid": "aaaa", "dps": {"1": true}}, ...].
 * @param[in] qos      MQTT Quality of Service level. It is recommended to use level 1 to ensure reliable data transmission.
 * @param[in] to_lmt  Timeout duration (in seconds). If acknowledgement is not received within this time, a timeout occurs.
 * @param[in] cb       Callback invoked after an asynchronous operation.
 * @param[in] prv_data  The context data of the callback, which can be used to store additional information.
 *
 * @return OPRT_OK  Indicates success; other values indicate an error. For the definition of error codes, please refer to tuya_error_code.h. 
 */
OPERATE_RET mqc_prot_data_rept_seq(IN CONST UINT_T pro, IN CONST CHAR_T *data,
                                   IN CONST BYTE_T qos, IN CONST UINT_T to_lmt,
                                   IN CONST MQ_PUB_ASYNC_IFM_CB cb, IN VOID *prv_data);

Example

STATIC VOID __mqc_pub_cb(IN CONST OPERATE_RET op_ret,IN CONST VOID *prv_data)
{
    PR_DEBUG("mqtt publish result: %d", op_ret);

    if (op_ret == OPRT_OK) { // success
        // TODO
    } else {                 // timeout
        // TODO
    }
}

STATIC VOID __dp_report_batch_example(VOID)
{
    OPERATE_RET op_ret = OPRT_OK;
    CONST CHAR_T *data = "[ \
        {\"cid\": \"aaaa\", \"dps\": {\"1\": true, \"2\": true}}, \
        {\"cid\": \"bbbb\", \"dps\": {\"1\": true, \"2\": true}} \
    ]";

    op_ret = mqc_prot_data_rept_seq(30, data, 1, 10, __mqc_pub_cb, NULL);
    if (op_ret != OPRT_OK) {
        PR_ERR("mqc_prot_data_rept_seq err: %d", op_ret);
        return;
    }
}