设备管理

更新时间:2025-12-16 06:38:59下载pdf

设备管理主要提供设备完成配网后的相关操作,包含设备状态变化监听、设备重命名、设备固件升级、设备移除、设备恢复出厂设置、设备信息导出等操作。

进行设备操作之前,确保已经成功 获取家庭下设备、群组

功能说明

设备管理相关类:

类名 说明
TSmartDevice 设备管理
TSmartDeviceModel 设备数据模型

ThingSmartDeviceModel 数据模型:

属性 类型 说明
devId string 设备唯一 ID
name string 设备名称
iconUrl string 设备图标 URL
isOnline boolean 设备在线状态。此状态包含 Wi-Fi、局域网、或蓝牙在线状态,只要其中任意一个网络在线,即为在线
isCloudOnline boolean 设备 Wi-Fi 在线状态
getIsLocalOnline boolean 设备局域网在线状态
isShare boolean 是否为分享设备
dps DpsType 设备功能点数据
dpCodes Record<string, Object> 设备功能点数据,表现为键值(code-value)形式
schemaArray Array 设备 DP 规则信息
productId string 设备所对应的产品 ID
capability number 设备产品能力值
supportGroup boolean 是否支持创建群组
gwType string v 代表虚拟设备,空代表真实设备
pv number 设备协议版本,Wi-Fi 协议版本或蓝牙协议版本
lpv number 设备局域网协议版本。默认为空,该值在设备局域网连接成功后,才会有值
latitude string 纬度
longitude string 经度
localKey string 设备通信使用的 key
uuid string 设备 UUID
ownerId string 设备所在家庭 ID
roomId number 设备所在房间 ID
timezoneId string 设备时区
nodeId string 设备短地址,非子设备类型值为空。用于区分网关下子设备的唯一地址
parentId string 父设备(上一级)ID,非子设备类型值为空。子设备用于寻找对应的网关设备 ID。蓝牙 Mesh 子设备或为 Mesh ID 或对应的网关设备 ID
devKey string 标准蓝牙 Mesh 设备蓝牙通信 key
standard boolean 是否为标准化产品设备。如果为标准设备,可以使用标准设备控制功能
standSchemaModel TSmartStandSchemaModel 设备标准 DP 规则信息。
activeTime number 激活时间
homeDisplayOrder number 设备序号,家庭查询设备列表时,可通过该属性进行排序
sharedTime number 分享时间
accessType number 区分设备的接入方式:0:涂鸦 DP 接入1:Matter 协议接入2:TuyaLink 接入
category string 设备品类缩写,如:dj 表示灯具,详见 类目code列表

注册设备服务

此方法调用后才可使用局域网、MQTT、蓝牙在离线等功能,在登录成功之后调用。

接口说明

export class TSmartDevice {
	public static register(uid: string)
}

参数说明:

参数名 释义 是否可选
uid 用户 ID 必选

示例代码

TSmartDevice.register(userModel.uid)

初始化设备

根据设备 ID 去初始化设备控制类。

  • 您需要通过 ThingSmartHome 初始化一个 home 实例,然后调用 getHomeDetailWithSuccess:failure: 查询家庭详情。只有同步过家庭的详情后,初始化设备才能成功。
  • 错误的设备 ID 可能会导致初始化失败,此时设备的实例返回 nil
    接口说明
export class TSmartDevice {
  public static create(devId: string): TSmartDevice | undefined
}

参数说明

参数名 释义 是否可选
devId 设备 ID 必选

返回值

TSmartDevice | undefined

示例代码

const device = TSmartDevice.create(devId)

设备状态监听

实现 TSmartDeviceListener 后,您可以在设备状态更变的回调中进行处理,刷新 App 设备控制面板的 UI。

接口说明

export class TSmartDevice {
	public registerListener(listener: TSmartDeviceListener): void
}

参数说明

参数名 释义 是否可选
listener 设备状态监听回调 必选
export interface TSmartDeviceListener {
  onDpsChanged: (devId: string, dps: DpsType, dpsTime: Record<string, Object>) => void;
  onOnlineChanged: (devId: string) => void;
  onDeviceInfoUpdated: (devId: string) => void;
  onDeviceRemoved: (devId: string) => void
  onWarningInfoUpdate: (devId: string, info: Record<string, Object>) => void
  onSignalQuery: (devId: string, signal: number) => void
}

示例代码

public device: TSmartDevice = TSmartDevice.create(devId)

new Action('注册设备监听', async () => {
  	this.device.registerListener({
      	// 设备dp点发生变化
        onDpsChanged: (devId: string, dps: DpsType, dpsTime: Record<string, Object>) => {
          console.log(`onDpsChanged devId=${devId}, dps=${dps}`) 
        },
      	// 设备在线状态发生变化
        onOnlineChanged: async (devId: string) => {
          const o = await this.deviceModel!.isOnline()
          console.log("在线状态:" + (o ? "在线" : "离线"))
        },
      	// 设备信息更新
        onDeviceInfoUpdated: (devId: string) => {
          console.log(devId + "设备信息更新")
        },
      	// 设备被移除
        onDeviceRemoved: (devId: string) => {
          console.log(devId + "设备被移除")
        },
      	// 设备收到警告信息
        onWarningInfoUpdate: (devId: string, info: Record<string, Object>) => {
          console.log(devId + "设备收到警告信息")
        },
        onSignalQuery: (devId: string, signal: number) => {
          console.log(devId + "设备信号查询结果:" + signal)
        }
      })
}),

查询设备信息

查询单个 DP 数据,查询后会通过 TSmartDeviceListener 监听器的onDpsChanged: (devId: string, dps: DpsType, dpsTime: Record<string, Object>) => void 方法回调数据。

注意- 该接口主要是针对不主动发送数据的设备 DP,例如倒计时信息查询。常规查询 DP 数据值时,可通过 TSmartDeviceModel.dps 获取。

  • 下发格式详见示例

示例代码

async queryDP() {
    let dpResult: Record<string, null> = {}
    dpResult[this.swichId] = null
    await this.device?.publishDps(JSON.stringify(dpResult))
  }

修改设备名称

接口说明

export class TSmartDevice {
	public async updateName(name: string)
}

参数说明

参数名 释义 是否可选
name 设备的新名称 必选

返回值: void

示例代码

public device: TSmartDevice = TSmartDevice.create(devId)

new Action('修改设备名称', () => {
	this.device.updateName("new name")
}),

移除设备和恢复出厂设置

设备被移除后,会重新进入待配网状态,默认进入快连模式。

设备恢复出厂设置后,会重新进入待配网状态,Wi-Fi 设备默认进入快连模式。设备的相关数据会被清除掉。

接口说明

export class TSmartDevice {
	public remove(reset: boolean = false)
}

参数说明

参数名 释义 是否可选
reset 是否需要恢复出厂设置 可选,默认不恢复出厂设置

返回值:
void

示例代码

public device: TSmartDevice = TSmartDevice.create(devId)

new Action('移除设备', () => {
	this.device.remove()
}),

查询 Wi-Fi 信号强度

查询设备 Wi-Fi 信号,通过callback回调数据。接口说明

export class TSmartDevice {
	public queryWifiSignal(callback:(signal:number) => void)
}

参数说明

参数 说明
callback 发送查询 Wi-Fi 强度成功回调

示例代码

public device: TSmartDevice = TSmartDevice.create(devId)

new Action('查询设备信号', () => {
	this.device!.queryWifiSignal(signal => {
      console.log("signal is =" + signal)
    })
}),

查询网关子设备列表

如果是网关设备,可以查询网关下子设备的列表。

接口说明

export class TSmartDevice {
	public async getSubDeviceList(): Promise<TSmartDeviceModel[]>
}

示例代码

public device: TSmartDevice = TSmartDevice.create(devId)

async getSubDeviceList() {
    const subDeviceList = await this.device?.getSubDeviceList()
  }

低功耗设备唤醒

对于部分低功耗设备,通常在控制设备之前需要先将唤醒设备,可以通过以下接口尝试唤醒设备。

注意

唤醒低功耗设备需要一定的时间(具体看设备,通常需要 3~5 秒),建议合适的时机前置调用。

接口说明

export class TSmartDevice {
	public async awakeDevice(): Promise<boolean | undefined>
}

示例代码

public device: TSmartDevice = TSmartDevice.create(devId)

async awakeDevice() {
    await this.device?.awakeDevice()
  }