物模型功能
更新时间:2025-12-31 14:54:05
物模型SDK提供了App端的物模型(属性、事件、服务),用来开发设备界面,实现手机对设备的查看和控制。
// 导入物模型头文件
import { RTCXThingModel, RTCXThingModelType } from 'rtcxsdk'
初始化
- 初始化物对象
export default class ThingModelPanel implements IThingModelPanel {
deviceInfo: ISimpleDeviceInfo;
constructor(playDeviceInfo: PlayDeviceInfo)
}
调用示例如下:
this.thingModel = new RTCXThingModel(PlayDeviceInfo)
- 通过订阅监听状态变化,监听获取设备上下线消息、属性变化消息、解绑消息、事件触发消息、设备透传消息
export interface IThingModelPanel {
/**
* 订阅
* @param callback - 回调
*/
subscribe(callback: IPanelEventCallback): void;
/**
* 取消订阅
* @param callback - 回调
*/
unsubscribe(callback: IPanelEventCallback): void;
}
调用示例如下:
- 订阅消息通知
this.msgNotify: IPanelEventCallback = (iotId: string, eventType: RTCXThingModelType.SubscribeEventTypeEnum, data?: Object) =>{
if (eventType == RTCXThingModelType.SubscribeEventTypeEnum.DEVICE_UNBIND) {
showToast(this, `设备解绑`)
} else if (eventType == RTCXThingModelType.SubscribeEventTypeEnum.DEVICE_STATUS) {
//说明:status表示设备生命周期,目前有以下几个状态,0:未激活;1:上线;2:休眠;3:离线;8:禁用;
showToast(this, `设备状态变化`)
} else if (eventType == RTCXThingModelType.SubscribeEventTypeEnum.DEVICE_THING_PROPERTIES_CHANGE) {
showToast(this, `设备物模型属性变化`)
} else if (eventType == RTCXThingModelType.SubscribeEventTypeEnum.DEVICE_EVENT_BATCH_NOTIFY) {
showToast(this, ` ${item.getShowName()} 设备产生了告警事件消息`)
} else if (eventType == RTCXThingModelType.SubscribeEventTypeEnum.DEV_TRANS_MSG) {
showToast(this, `设备透传消息`)
}
}
this.thingModel.subscribe(this.msgNotify)
- 取消订阅消息通知
this.thingModel.unsubscribe(this.msgNotify)
物的模型
export interface IThingModelPanel {
/**
* 获取物模型模板
*/
getThingTsl(): Promise<ThingTSL|undefined>;
}
调用示例如下:
const tsl = this.thingModel.getThingTsl()
设备属性
export interface IThingModelPanel {
/**
* 获取物模型属性
*/
getThingProperties(): Promise<PropertiesMap|void>;
/**
* 设置物模型属性
* @param items - 属性
* @param lensId - 镜头ID 可行
*/
setProperties(items: Record<string, Object>, lensId?: number): Promise<boolean>;
/**
* 设置属性的超时时间,最小5秒
* @param timeout - 超时时间 单位毫秒
*/
setPropertiesTimeout(timeoutMS: number): void;
}
调用示例如下:
- 获取物的属性
this.propertiesMap = await this.thingModel!.getThingProperties() as RTCXThingModelType.PropertiesMap
RTCXLogger.i(this.TAG,'获取属性成功')
- 设置物的属性
const result = this.thingModel?.setProperties({'StreamVideoQuality': 0});
if (result) {
RTCXLogger.i(this.TAG,'设置属性成功')
}
设备服务
export interface IThingModelPanel {
/**
*
* @param identifier - 服务标识
* @param args - 参数
* @param hasResponse - 是否有返回数据 默认false
* @param lensId - 镜头ID
* @param timeout - 超时时间
*/
invokeService<T>(identifier: string, args?: Record<string, Object>, hasResponse?: boolean, lensId?: number, timeout?: number): Promise<InvokeServiceRsp<T> | void>;
}
调用示例如下:
- 获取缩略图
const result = await this.thingModel?.invokeService<IDevPicture>('GetDevPicture',{'definition': 1, "picType": "jpg"})
if (result) {
const rsp = result as RTCXThingModelType.InvokeServiceRsp<IDevPicture>
if (rsp.code == 200) {
const devPicture = rsp.data as IDevPicture
RTCXLogger.i(TAG,`设备缩略图:${thingM.deviceInfo.iotId}, ${devPicture.downloadUrl}`)
this.deviceThumnails[thingM.deviceInfo.iotId] = devPicture
} else {
showToast(this, `获取缩略图失败`)
}
}