全部產品
Search
文件中心

IoT Platform:裝置影子

更新時間:Jun 30, 2024

背景資訊

裝置影子是指通過特別的Topic在雲端構建一個裝置的影子,裝置同步狀態至雲端。當裝置離線時,雲端仍可以快速通過影子擷取到裝置的狀態。

說明 對於使用物模型在物聯網平台進行定義的產品,其屬性會儲存在雲端,無需使用裝置影子來告知物聯網平台儲存其資料。

操作步驟

  1. 裝置C-SDK提供介面IOT_Shadow_Construct ,建立裝置影子。函式宣告如下:
    /**
    * @brief Construct the Device Shadow.
    * This function initialize the data structures, establish MQTT connection.
    * and subscribe the topic: "/shadow/get/${YourProduct_key}/${YourDevice_name}".
    *
    * @param [in] pparam: The specific initial parameter.
    * @retval NULL : Construct shadow failed.
    * @retval NOT_NULL : Construct success.
    * @see None.
    */
    void *IOT_Shadow_Construct(iotx_shadow_para_pt pparam);
                        
  2. 使用IOT_Shadow_RegisterAttribute介面,註冊裝置影子的屬性。函式宣告如下:
    /**
    * @brief Create a data type registered to the server.
    *
    * @param [in] handle: The handle of device shadow.
    * @param [in] pattr: The parameter which registered to the server.
    * @retval SUCCESS_RETURN : Success.
    * @retval other : See iotx_err_t.
    * @see None.
    */
    iotx_err_t IOT_Shadow_RegisterAttribute(void *handle, iotx_shadow_attr_pt pattr);
                        
  3. 裝置影子在每次開機時,裝置C-SDK提供IOT_Shadow_Pull介面,從雲端同步裝置狀態。函式宣告如下:
    /**
    * @brief Synchronize device shadow data from cloud.
    * It is a synchronous interface.
    * @param [in] handle: The handle of device shadow.
    * @retval SUCCESS_RETURN : Success.
    * @retval other : See iotx_err_t.
    * @see None.
    */
    iotx_err_t IOT_Shadow_Pull(void *handle);
                        
  4. 當裝置端狀態發生變化時,裝置C-SDK提供介面IOT_Shadow_PushFormat_Init、IOT_Shadow_PushFormat_Add和IOT_Shadow_PushFormat_Finalize介面更新狀態,通過介面IOT_Shadow_Push將狀態同步到雲端。函式宣告如下:
    /**
    * @brief Start a process the structure of the data type format.
    *
    * @param [in] handle: The handle of device shadow.
    * @param [out] pformat: The format struct of device shadow.
    * @param [in] buf: The buf which store device shadow.
    * @param [in] size: Maximum length of device shadow attribute.
    * @retval SUCCESS_RETURN : Success.
    * @retval other : See iotx_err_t.
    * @see None.
    */
    iotx_err_t IOT_Shadow_PushFormat_Init(
                        void *handle,
                        format_data_pt pformat,
                        char *buf,
                        uint16_t size);
    
    
    /**
    * @brief Format the attribute name and value for update.
    *
    * @param [in] handle: The handle of device shadow.
    * @param [in] pformat: The format struct of device shadow.
    * @param [in] pattr: To have created the data type of the format in the add member attributes.
    * @retval SUCCESS_RETURN : Success.
    * @retval other : See iotx_err_t.
    * @see None.
    */
    iotx_err_t IOT_Shadow_PushFormat_Add(
                        void *handle,
                        format_data_pt pformat,
                        iotx_shadow_attr_pt pattr);
    
    
    /**
    * @brief Complete a process the structure of the data type format.
    *
    * @param [in] handle: The handle of device shadow.
    * @param [in] pformat: The format struct of device shadow.
    * @retval SUCCESS_RETURN : Success.
    * @retval other : See iotx_err_t.
    * @see None.
    */
    iotx_err_t IOT_Shadow_PushFormat_Finalize(void *handle, format_data_pt pformat);
                        
  5. 當裝置需要與雲端中斷連線時,裝置C-SDK提供介面IOT_Shadow_DeleteAttribute和IOT_Shadow_Destroy,刪除雲端建立的屬性並釋放裝置影子。函式宣告如下:
    /**
    * @brief Deconstruct the specific device shadow.
    *
    * @param [in] handle: The handle of device shadow.
    * @retval SUCCESS_RETURN : Success.
    * @retval other : See iotx_err_t.
    * @see None.
    */
    iotx_err_t IOT_Shadow_Destroy(void *handle);