Send Cached DP Data to Low Power Devices

Last Updated on : 2024-06-25 03:37:13download

The typical functionality of a low power device is to report device status and important events. However, users may need to set device parameters or control the device remotely. In this case, the cloud will send a data point (DP) command to the device. If the device is in sleep mode, it will fail to receive the command.

To solve this problem, the DP data can be cached in the cloud. When the low power device wakes up, it can proactively retrieve the cached data from the cloud. This can reduce parameter setting failures for low power devices and simplify user operations.

This solution enables the device to retrieve the cached data after waking up, so it is not suitable for time-sensitive use cases.

Description

The process of retrieving the cached DP data from the cloud:

  1. The mobile app sends the DP command to the cloud.

  2. The cloud caches the received DP command.

  3. After the device wakes up, it queries and retrieves the cached DP data from the cloud.

  4. The device acts on the DP commands and reports the status of the respective DP.

  5. The cached DP data is deleted automatically after it is sent to the device. If no DP data is cached in the cloud, the device will receive a null response to its query.

How it works

AppCloudDeviceLow Power DP CacheDelivery is enabled.Users set the deviceon the panel.Call the DP command cacheAPI and send the DP commandto the cloud.Cached successfullyScheduled wake-upor external wake-upQuery the cached DPcommand.Return data.Delete cached data.Act on the command.Report the current devicestatus.Sync status.Update status.opt[Cached DP command exists.]AppCloudDevice

Development guide

Reference the header

  • smart_frame.h

How to

Create a product on the Tuya Developer Platform and then enter the product development process. In Function Definition > Advanced Functions, enable Low Power DP Cache Delivery.

  • After this function is enabled, the command sent from the app panel will be delivered only through the cache channel even when the device is online.
  • If you use a custom panel, your panel needs to support sending DP to the cloud for caching.

If you cannot find Low Power DP Cache Delivery in advanced functions, this is because the product solution does not have this function bound. To request binding them with your product, contact your account manager or submit a service ticket.

Send Cached DP Data to Low Power Devices

After the device wakes up and gets connected, you can call query cached DP for low power devices.

API description

Query cached DP for low power devices

  • The return value OPRT_OK indicates the communication with the cloud works fine but does not mean the cached DP data is obtained.
  • Only when the output parameter obj_dps or raw_dps has a value, it indicates the cached DP data is obtained.

This API is used to retrieve the cached DP data from the cloud.

/**
* @brief Definition of received structured dp
*/
typedef struct {
    /** see DP_CMD_TYPE_E */
    DP_CMD_TYPE_E cmd_tp;
    /** see DP_TRANS_TYPE_T */
    DP_TRANS_TYPE_T dtt_tp;
    /** if(NULL == cid) then the cid represents gwid */
    CHAR_T *cid;
    /** mb id */
    CHAR_T *mb_id;
    /** count of dp */
    UINT_T dps_cnt;
    /** the dp data */
    TY_OBJ_DP_S dps[0];
} TY_RECV_OBJ_DP_S;

/**
* @brief Definition of structured dp
*/
typedef struct {
    /** dp id */
    BYTE_T dpid;
    /** data len */
    UINT_T len;
    /** dp value, see TY_OBJ_DP_VALUE_U */
    BYTE_T* data;
} TY_RAW_DP_S;

/**
* @brief Definition of received raw dp
*/
typedef struct {
    /** see DP_CMD_TYPE_E */
    DP_CMD_TYPE_E cmd_tp;
    /** see DP_TRANS_TYPE_T */
    DP_TRANS_TYPE_T dtt_tp;
    /** if(NULL == cid) then the cid represents gwid */
    CHAR_T *cid;
    /** mb id */
    CHAR_T *mb_id;

    /** count of dp */
    UINT_T dps_cnt;
    /** the dp data */
    TY_RAW_DP_S dps[0];
} TY_RECV_MULTI_RAW_DP_S;

/**
* @brief Query dp for low power
*
* @param[in] dps: The DP ID to query.
* @param[in] cnt: The number of DP IDs to query.
* @param[out] obj_dps: The cached object-type DP data.
* @param[out] raw_dps: The cached raw-type DP data.
*
* @note This API is used for querying DP for low power
*
* @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
*/
OPERATE_RET sf_dp_low_power_query(IN CONST UCHAR_T *dps, IN CONST UINT_T cnt,
                                  OUT TY_RECV_OBJ_DP_S **obj_dps, OUT TY_RECV_MULTI_RAW_DP_S **raw_dps);

Example

service_query_lowpower_dp/example_query_lowpower_dp.c in TuyaOS example collection contains the example code.

FAQs

Can I cache multiple DPs for a single device?

Yes, you can.

Will all the commands be cached if a DP is set repeatedly?

No, it is not possible. Only the last command set for a specific DP of the device will be cached.

Can the device pull the cached data for multiple raw-type DPs in one go?

It is possible for TuyaOS v3.6.0 and later. For frameworks prior to v3.6.0, you can retrieve the cached data for one raw-type DP in a single request. You can retrieve the cached data for multiple object-type DPs in a single request.