网关相关接口

更新时间:2023-10-12 08:00:25下载pdf

本文介绍网关相关的接口。

前提条件

使用 gatewayApi 需先安装依赖,单击此处 安装依赖。

解绑、绑定 BLE 设备

名称

bleSubDevRelationUpdate

描述

绑定 BLE 设备到网关、从网关解绑 BLE 设备。

请求参数

参数
数据类型
说明
是否必选
sourceMeshId String 子设备当前所属的 meshId 或网关设备 ID。绑定到网关下时,传网关的 sigmeshId。从网关解绑时,传网关的设备 ID。
nodes BleNodeInfo[] BLE 设备信息列表
targetMeshId String | Null 绑定目标的 meshId。绑定到网关下时,传网关的设备 ID。从网关解绑时,传 null。

BleNodeInfo

参数
数据类型
说明
devId String BLE 设备的设备 ID
uuid String BLE 设备的 uuid

请求示例

import { gatewayApi } from '@tuya/tuya-panel-api';
import { GatewayUtils } from '@tuya/tuya-panel-gateway-sdk';
import { TYSdk } from 'tuya-panel-kit';

const { bleSubDevRelationUpdate } = gatewayApi.relationApi;
const { isBlueSub, getAllDevice, isAddableDevice } = GatewayUtils;

// 获取家庭下所有设备
const devList = await getAllDevice();

// 筛选出可以绑定到网关下的子设备列表
const addableDeviceList = devList.filter(subDev =>
  isAddableDevice({
    devInfo: subDev,
    gatewayDevInfo: TYSdk.devInfo,
  })
);

// 过滤出其中类型为 BLE 的设备列表
const bleDevList = addableDeviceList.filter(device => isBlueSub(device.capability));

// 拼装接口需要的 node 信息
const nodes = bleDevList.map(device => ({ devId: device.devId, uuid: device.uuid }));

/**
 * desc: 绑定 BLE 设备
 */
bleSubDevRelationUpdate(TYSdk.devInfo.sigmeshId, nodes, TYSdk.devInfo.devId)
  .then(response => {
    console.log(response);
  })
  .catch();

/**
 * desc: 解绑 BLE 设备
 */
bleSubDevRelationUpdate(TYSdk.devInfo.devId, nodes, null)
  .then(response => {
    console.log(response);
  })
  .catch();

解绑、绑定 SIG MESH 设备

名称

sigmeshSubDevRelationUpdate

描述

绑定 SIG MESH 设备到网关、从网关解绑 SIG MESH 设备。

请求参数

参数
数据类型
说明
是否必选
sourceMeshId String 子设备当前所属的 meshId 或网关设备 ID。绑定到网关下时,传子设备的 meshId。从网关解绑时,传网关的设备 ID。
nodeIds String[] SIG MESH 设备 nodeId 列表。
targetMeshId String | Null 绑定目标的 meshId。绑定到网关下时,传网关的设备 ID。从网关解绑时,传 null。

请求示例

import { gatewayApi } from '@tuya/tuya-panel-api';
import { GatewayUtils } from '@tuya/tuya-panel-gateway-sdk';
import { TYSdk } from 'tuya-panel-kit';

const { sigmeshSubDevRelationUpdate } = gatewayApi.relationApi;
const { isSigmeshSub, getAllDevice, isAddableDevice } = GatewayUtils;

// 获取家庭下所有设备
const devList = await getAllDevice();
// 筛选出可以绑定到网关下的子设备列表
const addableDeviceList = devList.filter(subDev =>
  isAddableDevice({
    devInfo: subDev,
    gatewayDevInfo: TYSdk.devInfo,
  })
);
// 过滤出其中为 SIG MESH 的设备列表
const sigmeshDevList = addableDeviceList.filter(device => isSigmeshSub(device.capability));

// 拼装接口需要的 node 信息
const nodeIds = sigmeshDevList.map(device => device.nodeId);

// 找到 带有 meshId 的 SIG MESH 设备
const deviceWithMeshId = sigmeshDevList.find(device => device.meshId !== undefined);

// 如果找不到则不能进入下一步
if (!deviceWithMeshId) return;

/**
 * desc: 绑定 SIG MESH 设备
 */
sigmeshSubDevRelationUpdate(deviceWithMeshId.meshId, nodeIds, TYSdk.devInfo.devId)
  .then(response => {
    console.log(response);
  })
  .catch();

/**
 * desc: 解绑 SIG MESH 设备
 */
sigmeshSubDevRelationUpdate(TYSdk.devInfo.devId, nodeIds, null)
  .then(response => {
    console.log(response);
  })
  .catch();

解绑、绑定 Beacon 设备

名称

beaconSubDevRelationUpdate

描述

绑定 Beacon 设备到网关、从网关解绑 Beacon 设备。

请求参数

参数
数据类型
说明
是否必选
sourceMeshId String 子设备当前所属的网关设备 ID。绑定到网关下时,传null。从网关解绑时,传网关的设备 ID。
nodeIds BeaconNodeInfo[] Beacon 设备信息列表。
targetMeshId String | Null 绑定目标的 meshId。绑定到网关下时,传网关的设备 ID。从网关解绑时,传 null。

BeaconNodeInfo

参数
数据类型
说明
devId String Beacon 设备的设备 ID
mac String Beacon 设备的 MAC地址,全部小写并且无冒号分隔 。例如:“0800200a8c6d”

请求示例

import { gatewayApi } from '@tuya/tuya-panel-api';
import { GatewayUtils } from '@tuya/tuya-panel-gateway-sdk';
import { TYSdk } from 'tuya-panel-kit';

const { beaconSubDevRelationUpdate } = gatewayApi.relationApi;
const { isBeaconSub, getAllDevice, isAddableDevice, transformMac } = GatewayUtils;

// 获取家庭下所有设备
const devList = await getAllDevice();

// 筛选出可以绑定到网关下的子设备列表
const addableDeviceList = devList.filter(subDev =>
  isAddableDevice({
    devInfo: subDev,
    gatewayDevInfo: TYSdk.devInfo,
    // 这里要传true,否则筛选结果里不会有 Beacon 设备
    supportBeacon: true,
  })
);

// 过滤出其中为 Beacon 的设备列表
const beaconDevList = addableDeviceList.filter(device => isBeaconSub(device.capability));

// 拼装接口需要的 node 信息
const nodeIds = beaconDevList.map(device => ({
  devId: device.devId,
  mac: transformMac(device.mac, true, false),
}));

/**
 * desc: 绑定 Beacon 设备
 */
beaconSubDevRelationUpdate(null, nodeIds, TYSdk.devInfo.devId)
  .then(response => {
    console.log(response);
  })
  .catch();

/**
 * desc: 解绑 Beacon 设备
 */
beaconSubDevRelationUpdate(TYSdk.devInfo.devId, nodeIds, null)
  .then(response => {
    console.log(response);
  })
  .catch();