子设备添加

更新时间:2024-06-25 06:15:03下载pdf

子设备添加 是子设备接入网关的第一步,本文将详细介绍 子设备添加 流程。

功能描述

子设备添加 是子设备通过网关绑定到云端的过程,也称设备激活。从网关使用者的角度来看,标准的操作流程如下:

  1. 打开涂鸦 智能生活 App。如无该 App,请在各移动应用中心下载。

  2. 选择网关进入网关面板主页,单击 添加子设备,让网关处于允许添加子设备状态。

  3. 长按子设备复位按键,让子设备进入配网状态,进入配网状态的子设备定时广播。

  4. 子设备加入到网关所在的网络后,网关把子设备绑定到云端,App 上会显示已入绑定的子设备列表,再单击 App 上的 完成 结束入网。再单击 完成 结束入网。

设备入网的交互流程示意图:

AppCloudSDKApplicationSub devicePermit Add Device Command (TRUE)Permit Add Device Command (TRUE)'dev_add' callbackDo somethingJoin networkCall 'tuya_iot_gw_bind_dev' APIBind RequestBind Response'dev_bind' callbackDevice list requestDevice list responsePermit Add Device Command (FALSE)Permit Add Device Command (FALSE)'dev_add' callbackAppCloudSDKApplicationSub device

根据以上示意图,SDK 与应用涉及三部分交互:

  1. SDK 通知应用允许或禁止子设备入网。
  2. 应用调用 SDK 的设备绑定接口,把子设备绑定到云端。
  3. SDK 通知应用子设备的绑定结果。

使用方法

  • 调用 tuya_subdev_user_sigle_type_reg 接口注册设备管理回调,并实现dev_adddev_bind 的回调接口。
  • 实现网关与子设备的通信逻辑开发。
  • 子设备入网后,调用 tuya_iot_gw_bind_dev 接口把子设备绑定到云端。

数据结构

/**
 * @brief The sub-device management callback
 */
typedef struct __ty_gw_subdev_mgr_cbs_s {
    GW_PERMIT_ADD_DEV_CB                dev_add;            /**< add device callback function, see GW_PERMIT_ADD_DEV_CB */
    GW_DEV_DEL_CB                       dev_del;            /**< delete device callback function, see GW_DEV_DEL_CB */
    DEV_RESET_IFM_CB                    dev_reset;          /**< device reset callback function, see DEV_RESET_IFM_CB */
    GW_BIND_DEV_INFORM_CB               dev_bind;           /**< device bind result callback function, see GW_BIND_DEV_INFORM_CB */

    DEV_OBJ_DP_CMD_CB                   dp_cmd_obj;         /**< obj dp command, see DEV_OBJ_DP_CMD_CB */
    DEV_RAW_DP_CMD_CB                   dp_cmd_raw;         /**< raw dp command, see DEV_RAW_DP_CMD_CB */
    DEV_DP_QUERY_CB                     dp_query;           /**<query dp, see DEV_DP_QUERY_CB */

    DEV_HEARTBEAT_SEND_CB               dev_hb;             /**< device heartbeat query callback function, see DEV_HEARTBEAT_SEND_CB */
    DEV_UG_INFORM_CB                    dev_upgrade;        /**< device upgrade callback function, see DEV_UG_INFORM_CB */
    GW_DEV_WAKEUP_CB                    dev_wakeup;         /**< waking up low-power device callback function, see GW_DEV_WAKEUP_CB */
    GW_DEV_GRP_INFM_CB                  dev_grp_info;       /**< group action callback function, see GW_DEV_GRP_INFM_CB */
    GW_DEV_SCENE_INFM_CB                dev_sce_info;       /**< scene action callback function, see GW_DEV_SCENE_INFM_CB */

    GW_DEV_SIGMESH_TOPO_UPDAET_CB       bt_topo_update;     /**< adding Bluetooth mesh device to gateway callback function, see GW_DEV_SIGMESH_TOPO_UPDAET_CB */
    GW_DEV_SIGMESH_DEV_CACHE_DEL_CB     bt_cache_del;       /**< removing Bluetooth mesh device from gateway callback function, see GW_DEV_SIGMESH_DEV_CACHE_DEL_CB */
    GW_DEV_SIGMESH_CONN_CB              bt_conn;            /**< Bluetooth mesh device connection function, see GW_DEV_SIGMESH_CONN_CB */

    DEV_ONLINE_STAT_SEND_CB             dev_online;         /**< device online status changed callback function, see DEV_ONLINE_STAT_SEND_CB */
}TY_GW_SUBDEV_MGR_CBS_S;

API 说明

子设备绑定

/**
	* @brief  [Compatible interface] bind a sub-device to gateway
	*
	* @param tp sub-device type
	* @param uddd user-defined type
	* @param id sub-device id
	* @param pk sub-device product key
	* @param ver sub-device version
	*
	* @return OPERATE_RET OPRT_OK is success
*/
#define tuya_iot_gw_bind_dev(tp, uddd, id, pk, ver) \
tuya_iot_gw_bind_dev_attr(tp, uddd, 0, id, pk, ver, NULL, 0, FALSE, NULL)

调用 tuya_iot_gw_bind_dev 可以向云端对该子设备进行绑定,绑定成功后将返回 OPRT_OK

使用示例

 /**
  * @brief The callback to invoke when to add devices.
  * @param[in]     tp          : The type of protocol
  * @param[in]     permit      : Whether to join or not
  * @param[in]     timeout     : Timeout
  */
  STATIC BOOL_T __dev_add_cb(CONST GW_PERMIT_DEV_TP_T tp, CONST BOOL_T permit, CONST UINT_T timeout)
  {
    /**
     * TODO:
     * permit == TRUE
     *    allow sub-device to join network and timeout is activated
     *
     * permit == FALSE
     *    stop net-in.
     */

    /**
     * Here we have an example showing how a sub-device join
     * a) Using fix-information sub-device
     * b) if it is not bound, bind it.
     */
      DEV_DESC_IF_S *dev_if = NULL;
      CHAR_T *dev_id = "abcdefabcdef";
      CHAR_T *pid    = "d1xabwgg"; // The PID of a three-way switch created in Tuya Developer Platform.
      if (permit) {
          dev_if = tuya_iot_get_dev_if(dev_id);
          if (dev_if && dev_if->bind) {
              return TRUE;
          }
          tuya_iot_gw_bind_dev(DEV_ATTACH_MOD_1, 0, dev_id, pid, "1.0.0");
      }

      return TRUE;
  }

 /**
  * @brief  The callback to invoke when to bind devices.
  * @param[in]     dev_id      : The device ID.
  * @param[in]     result      : The bind result.
  */
  STATIC VOID __dev_bind_cb(CONST CHAR_T *dev_id, CONST OPERATE_RET result)
  {
     /**
      * TODO:
      * result != OPRT_OK
      *     a) bind failed , do something?
      *     b) remove sub-device
      *
      * result == OPRT_OK
      *     a) maybe you should set heartbeat
      *     b) read sub-device's state & report
      */

     /**
      * Here we have an example showing what we should do after binding is successful
      *     a) set sub-device heartbeat : 2 mins timeout & 3 times
      *     b) report DP information
      */
      if (result == OPRT_OK) {
          tuya_iot_set_dev_hb_cfg(dev_id, 120, 3, FALSE);

          TY_OBJ_DP_S *dps = (TY_OBJ_DP_S *)Malloc(3 * SIZEOF(TY_OBJ_DP_S));
          if (dps == NULL) {
              return;
          }

          // all switch on
          for (int i = 0; i < 3; i++) {
              dps[i].dpid = (i + 1);
              dps[i].type = PROP_BOOL;
              dps[i].value.dp_bool = TRUE;
          }
          dev_report_dp_json_async(dev_id, dps, 3);
      }
  }


  TY_GW_SUBDEV_MGR_CBS_S dev_mgr_cbs = {
      .dev_add       = __dev_add_cb,
      .dev_bind      = __dev_bind_cb,
  };

  tuya_subdev_user_sigle_type_reg( &dev_mgr_cbs , DEV_ATTACH_MOD_1 );