@tencentcloud/sdk-mcp 1.7.7 → 1.7.9-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +7 -9
- package/index.js +8 -8
- package/knowledge/chunker.js +1 -0
- package/knowledge/doc-url-resolver.js +16 -9
- package/knowledge/embedding/build-vectors.js +92 -0
- package/knowledge/embedding/embed-model.js +41 -0
- package/knowledge/embedding/vector-augment.js +126 -0
- package/knowledge/embedding/vector-index.js +70 -0
- package/knowledge/frontmatter-meta.js +23 -0
- package/knowledge/path-meta.js +56 -0
- package/knowledge/search/exact-lookup.js +145 -16
- package/knowledge/search/filter-pool.js +77 -33
- package/knowledge/search/rank-profile.js +25 -0
- package/knowledge/search/requires-framework.js +4 -1
- package/knowledge/search/search-core.js +74 -13
- package/knowledge/search/search-knowledge.js +24 -34
- package/knowledge/search/search-pass.js +495 -169
- package/knowledge/search/url-row-scorer.js +24 -6
- package/package.json +3 -1
- package/resource/index/manifest.json +1 -1
- package/resource/index/vectors/index.json +4 -0
- package/resource/integration/callkit/preset/android/integration.md +0 -2
- package/resource/integration/callkit/preset/ios/integration.md +0 -2
- package/resource/integration/chat/preset/react/integration.md +9 -5
- package/resource/integration/chat/preset/uni-app/integration.md +4 -0
- package/resource/integration/chat/preset/vue/integration.md +10 -6
- package/resource/integration/livekit/preset/live/android/prepare-integration.md +0 -4
- package/resource/integration/livekit/preset/live/ios/prepare-integration.md +0 -4
- package/resource/integration/roomkit/preset/vue/integration.md +154 -208
- package/resource/knowledge/callkit/best-practice/android/video-chat-dating.md +0 -2
- package/resource/knowledge/callkit/core-sdk/android/answer-call.md +0 -2
- package/resource/knowledge/callkit/core-sdk/android/make-call.md +0 -2
- package/resource/knowledge/callkit/core-sdk/flutter/answer-call.md +0 -4
- package/resource/knowledge/callkit/core-sdk/flutter/make-call.md +0 -2
- package/resource/knowledge/callkit/core-sdk/flutter/offline-push.md +0 -2
- package/resource/knowledge/callkit/core-sdk/ios/answer-call.md +0 -2
- package/resource/knowledge/callkit/core-sdk/ios/make-call.md +0 -4
- package/resource/knowledge/callkit/core-sdk/ios/offline-push.md +0 -2
- package/resource/knowledge/callkit/core-sdk/uni-app/answer-call.md +0 -2
- package/resource/knowledge/callkit/core-sdk/uni-app/make-call.md +0 -2
- package/resource/knowledge/callkit/faq/ios.md +0 -2
- package/resource/knowledge/callkit/faq/uniapp-app.md +0 -2
- package/resource/knowledge/callkit/preset/miniprogram/integration.md +2 -3
- package/resource/knowledge/callkit/preset/miniprogram/uniapp-integration.md +0 -2
- package/resource/knowledge/chat/preset/ios-uikit/integration.md +0 -16
- package/resource/knowledge/chat/preset/react/h5/integration.md +16 -5
- package/resource/knowledge/chat/preset/vue/features/conversation-list.md +6 -3
- package/resource/knowledge/chat/preset/vue/h5/integration.md +7 -4
- package/resource/knowledge/iot/app/activate-service.md +95 -0
- package/resource/knowledge/iot/app/demo/android.md +93 -0
- package/resource/knowledge/iot/app/demo/ios.md +87 -0
- package/resource/knowledge/iot/app/demo/miniprogram.md +79 -0
- package/resource/knowledge/iot/app/device-binding.md +378 -0
- package/resource/knowledge/iot/app/device-sharing.md +414 -0
- package/resource/knowledge/iot/app/family-management.md +762 -0
- package/resource/knowledge/iot/app/integration/android.md +328 -0
- package/resource/knowledge/iot/app/integration/harmonyos.md +326 -0
- package/resource/knowledge/iot/app/integration/ios.md +351 -0
- package/resource/knowledge/iot/app/integration/miniprogram.md +268 -0
- package/resource/knowledge/iot/app/monitor-playback.md +557 -0
- package/resource/knowledge/iot/app/remote-control.md +139 -0
- package/resource/knowledge/iot/app/video-call.md +559 -0
- package/resource/knowledge/iot/app/wechat-call/device-call-miniprogram.md +204 -0
- package/resource/knowledge/iot/app/wechat-call/miniprogram-call-device.md +193 -0
- package/resource/knowledge/iot/client-api/app/android/txiotcallsession.md +189 -0
- package/resource/knowledge/iot/client-api/app/android/txiotcloudstorage.md +77 -0
- package/resource/knowledge/iot/client-api/app/android/txiotdevicemanager.md +175 -0
- package/resource/knowledge/iot/client-api/app/android/txiotengine.md +172 -0
- package/resource/knowledge/iot/client-api/app/android/txiotenginedef.md +163 -0
- package/resource/knowledge/iot/client-api/app/android/txiotfamilymanager.md +146 -0
- package/resource/knowledge/iot/client-api/app/android/txiotmonitorsession.md +246 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotcallsession.md +191 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotcloudstorage.md +77 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotdevicemanager.md +183 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotengine.md +186 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotenginedef.md +164 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotfamilymanager.md +150 -0
- package/resource/knowledge/iot/client-api/app/harmonyos/txiotmonitorsession.md +252 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotcallsession.md +199 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotcloudstorage.md +76 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotdevicemanager.md +173 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotengine.md +173 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotenginedef.md +171 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotfamilymanager.md +146 -0
- package/resource/knowledge/iot/client-api/app/ios/txiotmonitorsession.md +250 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotcallsession.md +180 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotcloudstorage.md +76 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotdevicemanager.md +281 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotengine.md +183 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotenginedef.md +152 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotfamilymanager.md +179 -0
- package/resource/knowledge/iot/client-api/app/miniprogram/txiotmonitorsession.md +226 -0
- package/resource/knowledge/iot/client-api/device/android-smart-device.md +813 -0
- package/resource/knowledge/iot/client-api/device/linux-rtos-device.md +2027 -0
- package/resource/knowledge/iot/device/activate-service.md +167 -0
- package/resource/knowledge/iot/device/ai-voice-chat/advanced-features/custom-agent.md +171 -0
- package/resource/knowledge/iot/device/ai-voice-chat/advanced-features/smart-device-control.md +721 -0
- package/resource/knowledge/iot/device/ai-voice-chat/integration-guide/android-smart-device.md +247 -0
- package/resource/knowledge/iot/device/ai-voice-chat/integration-guide/linux-rtos-device.md +368 -0
- package/resource/knowledge/iot/device/ai-voice-chat/overview.md +84 -0
- package/resource/knowledge/iot/device/cloud-storage.md +696 -0
- package/resource/knowledge/iot/device/data-model.md +972 -0
- package/resource/knowledge/iot/device/demo/android-smart-device.md +97 -0
- package/resource/knowledge/iot/device/demo/linux-rtos-device.md +106 -0
- package/resource/knowledge/iot/device/device-ota-upgrade.md +237 -0
- package/resource/knowledge/iot/device/dynamic-registration/android-smart-device.md +275 -0
- package/resource/knowledge/iot/device/dynamic-registration/linux-rtos-device.md +297 -0
- package/resource/knowledge/iot/device/login-and-register/android-smart-device.md +131 -0
- package/resource/knowledge/iot/device/login-and-register/linux-rtos-device.md +291 -0
- package/resource/knowledge/iot/device/monitor-pushing/android-smart-device.md +237 -0
- package/resource/knowledge/iot/device/monitor-pushing/linux-rtos-device.md +493 -0
- package/resource/knowledge/iot/device/video-call/android-smart-device.md +299 -0
- package/resource/knowledge/iot/device/video-call/linux-rtos-device.md +465 -0
- package/resource/knowledge/iot/device/wechat-call/android-smart-device.md +534 -0
- package/resource/knowledge/iot/device/wechat-call/linux-rtos-device.md +681 -0
- package/resource/knowledge/iot/kids-watch/overview.md +28 -0
- package/resource/knowledge/iot/security-camera/activate-service.md +74 -0
- package/resource/knowledge/iot/security-camera/monitor-playback/app-integration.md +302 -0
- package/resource/knowledge/iot/security-camera/monitor-playback/device-integration/android.md +237 -0
- package/resource/knowledge/iot/security-camera/monitor-playback/device-integration/linux-rtos.md +493 -0
- package/resource/knowledge/iot/security-camera/overview.md +31 -0
- package/resource/knowledge/iot/security-camera/ptz-control.md +153 -0
- package/resource/knowledge/iot/security-camera/recording-and-snapshot.md +182 -0
- package/resource/knowledge/iot/security-camera/voice-intercom.md +174 -0
- package/resource/knowledge/iot/smart-vision/serviceoverview.md +114 -0
- package/resource/knowledge/iot/voice-recorder/app-demo.md +99 -0
- package/resource/knowledge/iot/voice-recorder/device-demo.md +189 -0
- package/resource/knowledge/iot/voice-recorder/overview.md +64 -0
- package/resource/knowledge/livekit/core-sdk/features/android/base-beauty.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/android/gift.md +0 -4
- package/resource/knowledge/livekit/core-sdk/features/android/live-list.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/flutter/audio-effect.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/flutter/base-beauty.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/flutter/gift.md +0 -4
- package/resource/knowledge/livekit/core-sdk/features/ios/base-beauty.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/ios/gift.md +0 -4
- package/resource/knowledge/livekit/core-sdk/features/ios/live-list.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/uni-app/base-beauty.md +0 -2
- package/resource/knowledge/livekit/core-sdk/features/uni-app/gift.md +0 -4
- package/resource/knowledge/livekit/core-sdk/live/android/co-guest-interaction.md +0 -4
- package/resource/knowledge/livekit/core-sdk/live/android/host-interaction.md +0 -10
- package/resource/knowledge/livekit/core-sdk/live/android/integration.md +0 -2
- package/resource/knowledge/livekit/core-sdk/live/flutter/co-guest-interaction.md +0 -2
- package/resource/knowledge/livekit/core-sdk/live/flutter/host-interaction.md +0 -10
- package/resource/knowledge/livekit/core-sdk/live/flutter/integration.md +0 -2
- package/resource/knowledge/livekit/core-sdk/live/ios/co-guest-interaction.md +0 -4
- package/resource/knowledge/livekit/core-sdk/live/ios/host-interaction.md +0 -10
- package/resource/knowledge/livekit/core-sdk/live/ios/integration.md +0 -2
- package/resource/knowledge/livekit/core-sdk/live/uni-app/co-guest-interaction.md +0 -4
- package/resource/knowledge/livekit/core-sdk/live/uni-app/host-interaction.md +0 -4
- package/resource/knowledge/livekit/core-sdk/live/uni-app/integration.md +0 -2
- package/resource/knowledge/livekit/core-sdk/voice/android/co-guest-interaction.md +0 -2
- package/resource/knowledge/livekit/core-sdk/voice/android/host-interaction.md +0 -4
- package/resource/knowledge/livekit/core-sdk/voice/android/integration.md +0 -4
- package/resource/knowledge/livekit/core-sdk/voice/flutter/integration.md +0 -2
- package/resource/knowledge/livekit/core-sdk/voice/ios/co-guest-interaction.md +0 -2
- package/resource/knowledge/livekit/core-sdk/voice/ios/host-interaction.md +0 -4
- package/resource/knowledge/livekit/core-sdk/voice/ios/integration.md +0 -4
- package/resource/knowledge/livekit/core-sdk/voice/uni-app/co-guest-interaction.md +0 -2
- package/resource/knowledge/livekit/core-sdk/voice/uni-app/integration.md +0 -2
- package/resource/knowledge/livekit/faq/error-code.md +0 -2
- package/resource/knowledge/push/config/android/honor.md +16 -1
- package/resource/knowledge/push/config/android/huawei.md +20 -4
- package/resource/knowledge/push/config/android/meizu.md +19 -5
- package/resource/knowledge/push/config/android/vivo.md +20 -1
- package/resource/knowledge/push/config/donut.md +0 -2
- package/resource/knowledge/push/config/harmonyos.md +21 -5
- package/resource/knowledge/push/integration/android.md +0 -5
- package/resource/knowledge/push/integration/donut.md +0 -3
- package/resource/knowledge/push/integration/flutter.md +1 -4
- package/resource/knowledge/push/integration/harmonyos.md +0 -3
- package/resource/knowledge/push/integration/ios.md +0 -3
- package/resource/knowledge/push/integration/react-native.md +0 -5
- package/resource/knowledge/push/integration/uni-app.md +0 -3
- package/resource/knowledge/push/integration/unity.md +0 -3
- package/resource/knowledge/push/integration/unreal-engine.md +0 -3
- package/resource/knowledge/roomkit/core-sdk/android/call-user-to-room.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/android/room-participant-state.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/android/room-state.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/android/schedule-room.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/flutter/call-user-to-room.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/flutter/room-participant-state.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/flutter/room-state.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/flutter/schedule-room.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/ios/call-user-to-room.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/ios/room-participant-state.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/ios/room-state.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/ios/schedule-room.md +0 -2
- package/resource/knowledge/roomkit/core-sdk/vue/room-view.md +0 -3
- package/resource/knowledge/roomkit/preset/uniapp-app/integration.md +18 -14
- package/resource/knowledge/rtcengine/web/how_to_run_web_sdk_demo.md +0 -2
- package/resource/knowledge/rtcengine/web/plugin/23-advanced-support-detection.md +30 -0
- package/resource/knowledge/rtcengine/web/plugin/24-advanced-network-quality.md +0 -2
- package/resource/knowledge/rtcengine/web/plugin/31-advanced-plugins-overview.md +1 -1
- package/resource/knowledge/rtcengine/web/plugin/35-advanced-ai-denoiser.md +1 -1
- package/resource/knowledge/rtcengine/web/plugin/36-advanced-virtual-background.md +4 -0
- package/resource/knowledge/trtc/android/TRTCCloud.md +94 -85
- package/resource/knowledge/trtc/android/TRTCCloudDef.md +48 -46
- package/resource/knowledge/trtc/android/TRTCCloudListener.md +48 -48
- package/resource/knowledge/trtc/android/TXAudioEffectManager.md +18 -18
- package/resource/knowledge/trtc/android/TXDeviceManager.md +4 -4
- package/resource/knowledge/trtc/android/TXLiteAVCode.md +1 -0
- package/resource/knowledge/trtc/c++/ITRTCCloud.md +90 -90
- package/resource/knowledge/trtc/c++/ITXAudioEffectManager.md +18 -18
- package/resource/knowledge/trtc/c++/ITXDeviceManager.md +9 -9
- package/resource/knowledge/trtc/c++/TRTCCloudCallback.md +47 -47
- package/resource/knowledge/trtc/c++/TRTCTypeDef.md +47 -45
- package/resource/knowledge/trtc/c++/TXLiteAVCode.md +1 -0
- package/resource/knowledge/trtc/ios/TRTCCloud.md +93 -93
- package/resource/knowledge/trtc/ios/TRTCCloudDelegate.md +50 -50
- package/resource/knowledge/trtc/ios/TXAudioEffectManager.md +8 -8
- package/resource/knowledge/trtc/ios/TXDeviceManager.md +9 -9
- package/resource/knowledge/trtc/ios/TXLiteAVCode.md +1 -0
- package/resource/knowledge/trtc/ios/TXLiveSDKTypeDef.md +48 -46
- package/server-config.js +8 -9
- package/server.js +1 -1
- package/tools/common/finalize_answer.js +1 -1
- package/tools/common/present_framework_choice.js +1 -1
- package/tools/common/search_trtc_knowledge.js +22 -3
- package/tools/definitions/search_trtc_knowledge_def.js +3 -2
- package/utils/preflight-route.js +19 -2
- package/utils/report-cls-client.js +1 -1
- package/utils/report-es-client.js +1 -1
|
@@ -0,0 +1,972 @@
|
|
|
1
|
+
---
|
|
2
|
+
product: iot
|
|
3
|
+
framework:
|
|
4
|
+
- '*'
|
|
5
|
+
types:
|
|
6
|
+
- feature
|
|
7
|
+
retrievable_scope: always
|
|
8
|
+
feature_domain: device
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
本文将介绍如何基于腾讯云物联网设备端 SDK,接入物模型(Data Model)能力,演示属性的上报与控制、事件的上报、行为的下发与回复等核心流程。
|
|
12
|
+
|
|
13
|
+
## 关键概念
|
|
14
|
+
|
|
15
|
+
物模型(Data Model)是物理世界中的设备在云端的数字化抽象。它将设备能力拆解为三个维度:属性(Property)、事件(Event)、行为(Action),让云端、应用端可以用统一的语言理解设备。
|
|
16
|
+
|
|
17
|
+
#### 属性(Property)
|
|
18
|
+
|
|
19
|
+
设备运行时的状态或配置参数,既可被云端读取,也可被云端设置,是双向的。例如灯的开关、当前温度、亮度档位。
|
|
20
|
+
|
|
21
|
+
#### 事件(Event)
|
|
22
|
+
|
|
23
|
+
设备主动上报到云端的信息,常用于告警、故障、运行状态变化等不可预期的场景,由设备单向推送。
|
|
24
|
+
|
|
25
|
+
#### 行为(Action)
|
|
26
|
+
|
|
27
|
+
由云端下发给设备执行的命令,设备接收后需执行并回复结果。行为通常带有输入参数(云端下传)和输出参数(设备回传)。
|
|
28
|
+
|
|
29
|
+
#### data_id
|
|
30
|
+
|
|
31
|
+
属性、事件参数、行为参数的标识符,在同一物模型下唯一,调用 SDK 时必须与云端定义保持一致。
|
|
32
|
+
|
|
33
|
+
## 前提条件
|
|
34
|
+
|
|
35
|
+
在接入物模型通信功能前,若您是 Linux & RTOS 设备端,请先完成 Linux & RTOS 设备端的 登录与注册 文档跑通 `tc_iot_login` 登录功能;若您是 Android 智能设备端,请先完成 Android 智能设备端的 登录与注册 文档跑通登录功能,如果您已经完成,可以跳过该操作。
|
|
36
|
+
|
|
37
|
+
## 接入步骤
|
|
38
|
+
|
|
39
|
+
本节介绍如何调用 SDK 完成物模型的核心调用:上报属性、上报事件、接收云端对属性的设置、接收云端下发的行为并回复结果。
|
|
40
|
+
|
|
41
|
+
### 步骤 1:控制台定义物模型
|
|
42
|
+
|
|
43
|
+
控制台必须定义物模型,设备才可以上报物模型数据到服务器,上报未定义的物模型数据,服务器会拒绝。
|
|
44
|
+
1. 登录 [物联网开发平台控制台](https://console.cloud.tencent.com/iotexplorer),在产品列表中单击创建的产品名称进入详情。
|
|
45
|
+
|
|
46
|
+
2. 选择**功能定义**,单击**新增功能**或**导入物模型**,成功后会自动生成对应功能类型。
|
|
47
|
+
|
|
48
|
+
- 新增功能:在当前物模型基础上新增一条物模型属性,事件,或行为。
|
|
49
|
+
|
|
50
|
+
- 导入物模型:将同类产品的物模型配置文件导入当前产品。
|
|
51
|
+
|
|
52
|
+
新增功能可以选择新增属性、事件或行为。
|
|
53
|
+
|
|
54
|
+
【新增属性】
|
|
55
|
+
|
|
56
|
+
【新增事件】
|
|
57
|
+
|
|
58
|
+
【新增行为】
|
|
59
|
+
|
|
60
|
+
### 步骤 2:设备初始化物模型模块
|
|
61
|
+
|
|
62
|
+
登录成功后进行物模型模块初始化,Linux & RTOS 设备端调用 `tc_iot_data_model_init` 注册 `tc_iot_data_model_callback_t` 回调函数完成初始化;Android 智能设备端则通过 `TXIoTDataModelSession.getInstance()` 获取物模型会话单例,并调用 `addListener` 注册 `TXIoTDataModelSession.Listener` 回调。物模型的回调如下:
|
|
63
|
+
|
|
64
|
+
【Linux & RTOS】
|
|
65
|
+
|
|
66
|
+
|回调|说明|
|
|
67
|
+
|---------|---------|
|
|
68
|
+
|`on_report_property_result_cb`|属性上报后,收到服务器响应或设备发送超时后触发,属性上报结果通过 `data_model_report_result_t` 告知用户。|
|
|
69
|
+
|`on_receive_property_changed_cb`|云端修改属性值时,触发设备回调,云端修改后的属性值通过回调参数 `data_model_property` 告知用户。|
|
|
70
|
+
|`on_report_event_result_cb`|事件上报后,收到服务器响应或网络超时后触发,事件上报结果通过 `data_model_report_result_t` 告知用户。|
|
|
71
|
+
|`on_receive_new_action_cb`|云端调用行为时,触发设备回调,云端下发的行为输入参数通过回调参数 `data_model_action` 告知用户。|
|
|
72
|
+
|
|
73
|
+
【Android】
|
|
74
|
+
|
|
75
|
+
|回调|说明|
|
|
76
|
+
|---------|---------|
|
|
77
|
+
|`onReceivePropertyChanged`|云端修改属性值时,触发设备回调,云端修改后的属性值通过回调参数 TXIoTDataModelSession.Data 告知用户。|
|
|
78
|
+
|`onReceiveNewAction`|云端调用行为时,触发设备回调,云端下发的行为输入参数通过回调参数 TXIoTDataModelSession.Action 告知用户;返回 int 结果码(0 表示成功)。|
|
|
79
|
+
|
|
80
|
+
> **说明:**
|
|
81
|
+
> Android 智能设备端属性、事件的上报结果不通过监听器回调返回,而是通过对应上报接口(`reportProperty`、`reportEvent`)的 `TXIoTCallback` / `TXIoTValueCallback` 异步回调告知。
|
|
82
|
+
>
|
|
83
|
+
|
|
84
|
+
#### 初始化物模型模块
|
|
85
|
+
|
|
86
|
+
【Linux & RTOS】
|
|
87
|
+
``` c
|
|
88
|
+
|
|
89
|
+
static void on_report_property_result_cb(char *property_id, void *user_data,
|
|
90
|
+
data_model_report_result_t result_code);
|
|
91
|
+
static void on_receive_property_changed_cb(char *property_id,
|
|
92
|
+
tc_iot_data_model_property_t *data_model_property);
|
|
93
|
+
static void on_report_event_result_cb(char *event_id, void *user_data,
|
|
94
|
+
data_model_report_result_t result_code);
|
|
95
|
+
static int on_receive_new_action_cb(char *action_id,
|
|
96
|
+
tc_iot_data_model_action_t *data_model_action);
|
|
97
|
+
|
|
98
|
+
tc_iot_data_model_callback_t callback;
|
|
99
|
+
memset(&callback, 0, sizeof(callback));
|
|
100
|
+
callback.on_report_property_result_cb = on_report_property_result_cb;
|
|
101
|
+
callback.on_receive_property_changed_cb = on_receive_property_changed_cb;
|
|
102
|
+
callback.on_report_event_result_cb = on_report_event_result_cb;
|
|
103
|
+
callback.on_receive_new_action_cb = on_receive_new_action_cb;
|
|
104
|
+
|
|
105
|
+
tc_iot_error_e dm_rc = tc_iot_data_model_init(callback);
|
|
106
|
+
if (dm_rc != TC_IOT_ERR_SUCCESS) {
|
|
107
|
+
printf("tc_iot_data_model_init failed: %d\n", dm_rc);
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
【Android】
|
|
112
|
+
``` java
|
|
113
|
+
TXIoTDataModelSession dataModelSession = TXIoTDataModelSession.getInstance();
|
|
114
|
+
|
|
115
|
+
TXIoTDataModelSession.Listener listener = new TXIoTDataModelSession.Listener() {
|
|
116
|
+
@Override
|
|
117
|
+
public void onReceivePropertyChanged(TXIoTDataModelSession.Data property) {
|
|
118
|
+
// 云端修改属性,设备据此更新本地状态
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
@Override
|
|
122
|
+
public int onReceiveNewAction(TXIoTDataModelSession.Action action) {
|
|
123
|
+
// 云端下发行为,执行后返回结果码(0 表示成功)
|
|
124
|
+
return 0;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
dataModelSession.addListener(listener);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
属性和事件的上报结果:Linux & RTOS 设备端通过回调参数 `data_model_report_result_t` 告知,取值和说明如下;Android 智能设备端则通过上报接口(`reportProperty`、`reportEvent`)的 `TXIoTCallback` / `TXIoTValueCallback` 异步回调告知,`onSuccess` 表示上报成功,`onError(code, desc)` 表示上报失败。
|
|
132
|
+
|
|
133
|
+
【Linux & RTOS】
|
|
134
|
+
|
|
135
|
+
|取值|说明|
|
|
136
|
+
|---------|---------|
|
|
137
|
+
|DATA_MODEL_REPORT_SUCCESS|上报成功,云端已确认。|
|
|
138
|
+
|DATA_MODEL_REPORT_REJECTED|上报被云端拒绝。|
|
|
139
|
+
|DATA_MODEL_REPORT_NO_RESPONSE|上报无响应(通常为超时)。|
|
|
140
|
+
|DATA_MODEL_REPORT_LOCAL_TIMEOUT|本地超时。|
|
|
141
|
+
|
|
142
|
+
【Android】
|
|
143
|
+
|
|
144
|
+
|回调|说明|
|
|
145
|
+
|---------|---------|
|
|
146
|
+
|`onSuccess`|上报成功,云端已确认。|
|
|
147
|
+
|`onError(code, desc)`|上报失败,`code` 为错误码(如被云端拒绝、无响应、本地超时等),`desc` 为对应的错误描述。|
|
|
148
|
+
|
|
149
|
+
### 步骤 3:设备上报属性
|
|
150
|
+
|
|
151
|
+
上报属性到云端:Linux & RTOS 设备端调用 `tc_iot_data_model_report_property` 上报一条或多条属性,属性类型使用 `data_model_data_type_t`;Android 智能设备端调用 `TXIoTDataModelSession.reportProperty` 上报属性列表,属性类型使用 `TXIoTDataModelSession.DataType` 枚举。属性支持的数据类型如下:
|
|
152
|
+
|
|
153
|
+
【Linux & RTOS】
|
|
154
|
+
|
|
155
|
+
|取值|说明|
|
|
156
|
+
|---------|---------|
|
|
157
|
+
|DATA_MODEL_DATA_TYPE_BOOL|布尔值。|
|
|
158
|
+
|DATA_MODEL_DATA_TYPE_INT|32 位整数。|
|
|
159
|
+
|DATA_MODEL_DATA_TYPE_FLOAT|双精度浮点。|
|
|
160
|
+
|DATA_MODEL_DATA_TYPE_STRING|字符串。|
|
|
161
|
+
|DATA_MODEL_DATA_TYPE_TIME|32 位无符号时间戳(秒)。|
|
|
162
|
+
|DATA_MODEL_DATA_TYPE_ENUM|32 位无符号枚举。|
|
|
163
|
+
|
|
164
|
+
【Android】
|
|
165
|
+
|
|
166
|
+
|取值|说明|
|
|
167
|
+
|---------|---------|
|
|
168
|
+
|`DataType.BOOL`|布尔值。|
|
|
169
|
+
|`DataType.INT`|32 位整数。|
|
|
170
|
+
|`DataType.FLOAT`|双精度浮点。|
|
|
171
|
+
|`DataType.STRING`|字符串。|
|
|
172
|
+
|`DataType.TIME`|32 位无符号时间戳(秒)。|
|
|
173
|
+
|`DataType.ENUM`|32 位无符号枚举。|
|
|
174
|
+
|
|
175
|
+
上报一个布尔属性(灯的开关):
|
|
176
|
+
|
|
177
|
+
【Linux & RTOS】
|
|
178
|
+
``` c
|
|
179
|
+
#include "tc_iot_data_model.h"
|
|
180
|
+
|
|
181
|
+
tc_iot_data_model_property_t property[1];
|
|
182
|
+
memset(property, 0, sizeof(property));
|
|
183
|
+
|
|
184
|
+
char power_switch_id[] = "power_switch";
|
|
185
|
+
property[0].property.data_id = power_switch_id;
|
|
186
|
+
property[0].property.data_type = DATA_MODEL_DATA_TYPE_BOOL;
|
|
187
|
+
property[0].property.data_value.value_bool = true;
|
|
188
|
+
|
|
189
|
+
rc = tc_iot_data_model_report_property(property, 1, NULL);
|
|
190
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
191
|
+
printf("tc_iot_data_model_report_property failed: %d\n", rc);
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
上报多种不同类型的属性:
|
|
196
|
+
``` c
|
|
197
|
+
tc_iot_data_model_property_t property[4];
|
|
198
|
+
memset(property, 0, sizeof(property));
|
|
199
|
+
|
|
200
|
+
char id_power[] = "power_switch";
|
|
201
|
+
char id_bright[] = "brightness";
|
|
202
|
+
char id_color[] = "color";
|
|
203
|
+
char id_name[] = "device_name";
|
|
204
|
+
|
|
205
|
+
property[0].property.data_id = id_power;
|
|
206
|
+
property[0].property.data_type = DATA_MODEL_DATA_TYPE_BOOL;
|
|
207
|
+
property[0].property.data_value.value_bool = true;
|
|
208
|
+
|
|
209
|
+
property[1].property.data_id = id_bright;
|
|
210
|
+
property[1].property.data_type = DATA_MODEL_DATA_TYPE_INT;
|
|
211
|
+
property[1].property.data_value.value_int = 80;
|
|
212
|
+
|
|
213
|
+
property[2].property.data_id = id_color;
|
|
214
|
+
property[2].property.data_type = DATA_MODEL_DATA_TYPE_ENUM;
|
|
215
|
+
property[2].property.data_value.value_enum = 2;
|
|
216
|
+
|
|
217
|
+
property[3].property.data_id = id_name;
|
|
218
|
+
property[3].property.data_type = DATA_MODEL_DATA_TYPE_STRING;
|
|
219
|
+
property[3].property.data_value.value_string = "test_device_001";
|
|
220
|
+
|
|
221
|
+
rc = tc_iot_data_model_report_property(property, 4, NULL);
|
|
222
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
223
|
+
printf("tc_iot_data_model_report_property failed: %d\n", rc);
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
【Android】
|
|
228
|
+
``` java
|
|
229
|
+
TXIoTDataModelSession.Data power = new TXIoTDataModelSession.Data();
|
|
230
|
+
power.id = "power_switch";
|
|
231
|
+
power.dataType = TXIoTDataModelSession.DataType.BOOL;
|
|
232
|
+
power.dataValue = new TXIoTDataModelSession.DataValue();
|
|
233
|
+
power.dataValue.valueBool = true;
|
|
234
|
+
|
|
235
|
+
List<TXIoTDataModelSession.Data> propertyList = new ArrayList<>();
|
|
236
|
+
propertyList.add(power);
|
|
237
|
+
|
|
238
|
+
dataModelSession.reportProperty(propertyList, new TXIoTValueCallback<List<TXIoTDataModelSession.ResultItem>>() {
|
|
239
|
+
@Override
|
|
240
|
+
public void onSuccess(List<TXIoTDataModelSession.ResultItem> results) {
|
|
241
|
+
// 上报成功,results 中为每个属性的处理结果
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
@Override
|
|
245
|
+
public void onError(int code, String desc) {
|
|
246
|
+
// 上报失败
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
上报多种不同类型的属性:
|
|
252
|
+
``` java
|
|
253
|
+
List<TXIoTDataModelSession.Data> propertyList = new ArrayList<>();
|
|
254
|
+
|
|
255
|
+
TXIoTDataModelSession.Data power = new TXIoTDataModelSession.Data();
|
|
256
|
+
power.id = "power_switch";
|
|
257
|
+
power.dataType = TXIoTDataModelSession.DataType.BOOL;
|
|
258
|
+
power.dataValue = new TXIoTDataModelSession.DataValue();
|
|
259
|
+
power.dataValue.valueBool = true;
|
|
260
|
+
propertyList.add(power);
|
|
261
|
+
|
|
262
|
+
TXIoTDataModelSession.Data brightness = new TXIoTDataModelSession.Data();
|
|
263
|
+
brightness.id = "brightness";
|
|
264
|
+
brightness.dataType = TXIoTDataModelSession.DataType.INT;
|
|
265
|
+
brightness.dataValue = new TXIoTDataModelSession.DataValue();
|
|
266
|
+
brightness.dataValue.valueInt = 80;
|
|
267
|
+
propertyList.add(brightness);
|
|
268
|
+
|
|
269
|
+
TXIoTDataModelSession.Data color = new TXIoTDataModelSession.Data();
|
|
270
|
+
color.id = "color";
|
|
271
|
+
color.dataType = TXIoTDataModelSession.DataType.ENUM;
|
|
272
|
+
color.dataValue = new TXIoTDataModelSession.DataValue();
|
|
273
|
+
color.dataValue.valueEnum = 2;
|
|
274
|
+
propertyList.add(color);
|
|
275
|
+
|
|
276
|
+
TXIoTDataModelSession.Data name = new TXIoTDataModelSession.Data();
|
|
277
|
+
name.id = "device_name";
|
|
278
|
+
name.dataType = TXIoTDataModelSession.DataType.STRING;
|
|
279
|
+
name.dataValue = new TXIoTDataModelSession.DataValue();
|
|
280
|
+
name.dataValue.valueString = "test_device_001";
|
|
281
|
+
propertyList.add(name);
|
|
282
|
+
|
|
283
|
+
dataModelSession.reportProperty(propertyList, new TXIoTValueCallback<List<TXIoTDataModelSession.ResultItem>>() {
|
|
284
|
+
@Override
|
|
285
|
+
public void onSuccess(List<TXIoTDataModelSession.ResultItem> results) { }
|
|
286
|
+
|
|
287
|
+
@Override
|
|
288
|
+
public void onError(int code, String desc) { }
|
|
289
|
+
});
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
> **说明:**
|
|
293
|
+
> - tc_iot_data_model_report_property 是异步接口,返回值仅表示参数是否合法以及消息是否成功入队;真正的上报结果通过 on_report_property_result_cb 异步回调返回。
|
|
294
|
+
> - data_id 必须与控制台物模型中定义的属性标识符完全一致,否则云端会返回错误码。
|
|
295
|
+
> - data_type 与 data_value 的 union 字段必须一一对应(例如 data_type=INT 时只能写 value_int)。
|
|
296
|
+
|
|
297
|
+
### 步骤 4:设备上报事件
|
|
298
|
+
|
|
299
|
+
上报事件到云端:Linux & RTOS 设备端调用 `tc_iot_data_model_report_event` 上报一个或多个事件,事件类型使用 `data_model_event_type_t`;Android 智能设备端调用 `TXIoTDataModelSession.reportEvent` 上报事件,事件类型使用 `TXIoTDataModelSession.EventType` 枚举。事件类型如下:
|
|
300
|
+
|
|
301
|
+
【Linux & RTOS】
|
|
302
|
+
|
|
303
|
+
|取值|说明|
|
|
304
|
+
|---------|---------|
|
|
305
|
+
|DATA_MODEL_EVENT_TYPE_INFO|普通信息事件,例如设备上线、心跳等。|
|
|
306
|
+
|DATA_MODEL_EVENT_TYPE_ALERT|告警事件,例如温度过高、门被打开。|
|
|
307
|
+
|DATA_MODEL_EVENT_TYPE_FAULT|故障事件,例如传感器异常。|
|
|
308
|
+
|
|
309
|
+
【Android】
|
|
310
|
+
|
|
311
|
+
|取值|说明|
|
|
312
|
+
|---------|---------|
|
|
313
|
+
|`EventType.INFO`|普通信息事件,例如设备上线、心跳等。|
|
|
314
|
+
|`EventType.ALERT`|告警事件,例如温度过高、门被打开。|
|
|
315
|
+
|`EventType.FAULT`|故障事件,例如传感器异常。|
|
|
316
|
+
|
|
317
|
+
上报一个告警事件(带参数):
|
|
318
|
+
|
|
319
|
+
【Linux & RTOS】
|
|
320
|
+
``` c
|
|
321
|
+
#include "tc_iot_data_model.h"
|
|
322
|
+
|
|
323
|
+
tc_iot_data_model_event_t event[1];
|
|
324
|
+
memset(event, 0, sizeof(event));
|
|
325
|
+
|
|
326
|
+
char event_id[] = "temperature_alert";
|
|
327
|
+
event[0].event_id = event_id;
|
|
328
|
+
event[0].event_type = DATA_MODEL_EVENT_TYPE_ALERT;
|
|
329
|
+
|
|
330
|
+
data_model_data_item_t event_data[1];
|
|
331
|
+
memset(event_data, 0, sizeof(event_data));
|
|
332
|
+
|
|
333
|
+
char temp_id[] = "temperature";
|
|
334
|
+
event_data[0].data_id = temp_id;
|
|
335
|
+
event_data[0].data_type = DATA_MODEL_DATA_TYPE_INT;
|
|
336
|
+
event_data[0].data_value.value_int = 85;
|
|
337
|
+
|
|
338
|
+
event[0].event_data_list = event_data;
|
|
339
|
+
event[0].event_data_num = 1;
|
|
340
|
+
|
|
341
|
+
rc = tc_iot_data_model_report_event(event, 1, NULL);
|
|
342
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
343
|
+
printf("tc_iot_data_model_report_event failed: %d\n", rc);
|
|
344
|
+
}
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
上报一个故障事件(不带参数):
|
|
348
|
+
``` c
|
|
349
|
+
tc_iot_data_model_event_t fault[1];
|
|
350
|
+
memset(fault, 0, sizeof(fault));
|
|
351
|
+
|
|
352
|
+
fault[0].event_id = "device_error";
|
|
353
|
+
fault[0].event_type = DATA_MODEL_EVENT_TYPE_FAULT;
|
|
354
|
+
fault[0].event_data_num = 0;
|
|
355
|
+
fault[0].event_data_list = NULL;
|
|
356
|
+
|
|
357
|
+
rc = tc_iot_data_model_report_event(fault, 1, NULL);
|
|
358
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
359
|
+
printf("tc_iot_data_model_report_event failed: %d\n", rc);
|
|
360
|
+
}
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
【Android】
|
|
364
|
+
``` java
|
|
365
|
+
import com.tencent.liteav.iot.TXIoTCallback;
|
|
366
|
+
|
|
367
|
+
TXIoTDataModelSession.Data temp = new TXIoTDataModelSession.Data();
|
|
368
|
+
temp.id = "temperature";
|
|
369
|
+
temp.dataType = TXIoTDataModelSession.DataType.INT;
|
|
370
|
+
temp.dataValue = new TXIoTDataModelSession.DataValue();
|
|
371
|
+
temp.dataValue.valueInt = 85;
|
|
372
|
+
|
|
373
|
+
List<TXIoTDataModelSession.Data> eventData = new ArrayList<>();
|
|
374
|
+
eventData.add(temp);
|
|
375
|
+
|
|
376
|
+
TXIoTDataModelSession.Event event = new TXIoTDataModelSession.Event();
|
|
377
|
+
event.id = "temperature_alert";
|
|
378
|
+
event.eventType = TXIoTDataModelSession.EventType.ALERT;
|
|
379
|
+
event.eventDataList = eventData;
|
|
380
|
+
|
|
381
|
+
dataModelSession.reportEvent(event, new TXIoTCallback() {
|
|
382
|
+
@Override
|
|
383
|
+
public void onSuccess() {
|
|
384
|
+
// 事件上报成功
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
@Override
|
|
388
|
+
public void onError(int code, String desc) {
|
|
389
|
+
// 事件上报失败
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
上报一个故障事件(不带参数):
|
|
395
|
+
``` java
|
|
396
|
+
TXIoTDataModelSession.Event fault = new TXIoTDataModelSession.Event();
|
|
397
|
+
fault.id = "device_error";
|
|
398
|
+
fault.eventType = TXIoTDataModelSession.EventType.FAULT;
|
|
399
|
+
fault.eventDataList = null;
|
|
400
|
+
|
|
401
|
+
dataModelSession.reportEvent(fault, new TXIoTCallback() {
|
|
402
|
+
@Override
|
|
403
|
+
public void onSuccess() { }
|
|
404
|
+
|
|
405
|
+
@Override
|
|
406
|
+
public void onError(int code, String desc) { }
|
|
407
|
+
});
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
> **说明:**
|
|
411
|
+
> - 事件的 event_data_list 中的 data_id 同样必须与云端物模型中该事件定义的参数标识符一致。
|
|
412
|
+
> - 事件如果没有参数,可以将 event_data_num 置为 0、event_data_list 置为 NULL。
|
|
413
|
+
|
|
414
|
+
### 步骤 5:控制台查看设备上报的属性和事件
|
|
415
|
+
1. 登录 [物联网开发平台控制台](https://console.cloud.tencent.com/iotexplorer),在**设备**列表单击设备名称进入详情页面,选择**物模型数据**即可查看设备上报的属性和事件,
|
|
416
|
+
|
|
417
|
+
2. 选择需要查看的属性,单击**查看**。
|
|
418
|
+
|
|
419
|
+
可以查看属性的变化曲线,以及每次上报的属性值。
|
|
420
|
+
|
|
421
|
+
3. 在设备详情页选择**物模型数据**后,单击**事件**,即可查看设备上报的所有事件数据。
|
|
422
|
+
|
|
423
|
+
### 步骤 6:控制台修改属性和调用行为
|
|
424
|
+
1. 登录 [物联网开发平台控制台](https://console.cloud.tencent.com/iotexplorer),在**设备**列表单击设备名称进入详情页面。
|
|
425
|
+
|
|
426
|
+
2. 选择**在线调试** > **属性调试**,勾选要修改的属性,填写修改后的数据,单击**发送**即可
|
|
427
|
+
|
|
428
|
+
3. 选择**在线调试** > **行为调用**,在左侧设置行为参数并发送,右侧会显示行为调用时间,以及设备对行为调用的响应报文和响应时间。
|
|
429
|
+
|
|
430
|
+
### 步骤 7:设备端接收属性变更
|
|
431
|
+
|
|
432
|
+
当云端修改了设备的某个属性时,SDK 会通知上层:Linux & RTOS 设备端通过 `on_receive_property_changed_cb` 回调触发,Android 智能设备端通过 `TXIoTDataModelSession.Listener` 的 `onReceivePropertyChanged` 回调触发。
|
|
433
|
+
|
|
434
|
+
Linux & RTOS 设备端根据回调参数 `tc_iot_data_model_property_t` 确定属性名称、数据类型与数据值;Android 智能设备端根据回调参数 `TXIoTDataModelSession.Data` 的 `id`、`dataType` 与 `dataValue` 确定对应信息。
|
|
435
|
+
|
|
436
|
+
【Linux & RTOS】
|
|
437
|
+
``` c
|
|
438
|
+
static void on_receive_property_changed_cb(char *property_id,
|
|
439
|
+
tc_iot_data_model_property_t *data_model_property) {
|
|
440
|
+
if (property_id == NULL || data_model_property == NULL) {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
data_model_data_item_t *item = &data_model_property->property;
|
|
445
|
+
printf("[demo] property changed: id=%s, type=%d\n", item->data_id, (int)item->data_type);
|
|
446
|
+
|
|
447
|
+
switch (item->data_type) {
|
|
448
|
+
case DATA_MODEL_DATA_TYPE_BOOL:
|
|
449
|
+
// 根据 item->data_value.value_bool 调整设备状态
|
|
450
|
+
break;
|
|
451
|
+
case DATA_MODEL_DATA_TYPE_INT:
|
|
452
|
+
// 根据 item->data_value.value_int 调整设备状态
|
|
453
|
+
break;
|
|
454
|
+
case DATA_MODEL_DATA_TYPE_FLOAT:
|
|
455
|
+
// 根据 item->data_value.value_float 调整设备状态
|
|
456
|
+
break;
|
|
457
|
+
case DATA_MODEL_DATA_TYPE_STRING:
|
|
458
|
+
// 根据 item->data_value.value_string 调整设备状态
|
|
459
|
+
break;
|
|
460
|
+
case DATA_MODEL_DATA_TYPE_ENUM:
|
|
461
|
+
// 根据 item->data_value.value_enum 调整设备状态
|
|
462
|
+
break;
|
|
463
|
+
case DATA_MODEL_DATA_TYPE_TIME:
|
|
464
|
+
// 根据 item->data_value.value_time 调整设备状态
|
|
465
|
+
break;
|
|
466
|
+
default:
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
【Android】
|
|
474
|
+
``` java
|
|
475
|
+
@Override
|
|
476
|
+
public void onReceivePropertyChanged(TXIoTDataModelSession.Data property) {
|
|
477
|
+
if (property == null) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
switch (property.dataType) {
|
|
481
|
+
case BOOL:
|
|
482
|
+
// 根据 property.dataValue.valueBool 调整设备状态
|
|
483
|
+
break;
|
|
484
|
+
case INT:
|
|
485
|
+
// 根据 property.dataValue.valueInt 调整设备状态
|
|
486
|
+
break;
|
|
487
|
+
case FLOAT:
|
|
488
|
+
// 根据 property.dataValue.valueFloat 调整设备状态
|
|
489
|
+
break;
|
|
490
|
+
case STRING:
|
|
491
|
+
// 根据 property.dataValue.valueString 调整设备状态
|
|
492
|
+
break;
|
|
493
|
+
case ENUM:
|
|
494
|
+
// 根据 property.dataValue.valueEnum 调整设备状态
|
|
495
|
+
break;
|
|
496
|
+
case TIME:
|
|
497
|
+
// 根据 property.dataValue.valueTime 调整设备状态
|
|
498
|
+
break;
|
|
499
|
+
default:
|
|
500
|
+
break;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
> **说明:**
|
|
506
|
+
> 收到属性变更回调后,设备如果修改了属性值,可以将最新的属性值上报云端:Linux & RTOS 设备端调用 `tc_iot_data_model_report_property`,Android 智能设备端调用 `TXIoTDataModelSession.reportProperty`。
|
|
507
|
+
>
|
|
508
|
+
|
|
509
|
+
### 步骤 8:设备端接收行为调用
|
|
510
|
+
|
|
511
|
+
当云端调用设备的行为时,SDK 会通知上层:Linux & RTOS 设备端通过 `on_receive_new_action_cb` 回调触发,Android 智能设备端通过 `TXIoTDataModelSession.Listener` 的 `onReceiveNewAction` 回调触发;如果行为定义了输出参数:Linux & RTOS 设备端需在回调里给 `action_output_data_list` / `action_output_data_num` 赋值,Android 智能设备端需给 `action.actionOutputDataList` 赋值,SDK 会自动把结果回传给云端。
|
|
512
|
+
|
|
513
|
+
【Linux & RTOS】
|
|
514
|
+
``` c
|
|
515
|
+
static int on_receive_new_action_cb(char *action_id, tc_iot_data_model_action_t *data_model_action) {
|
|
516
|
+
if (action_id == NULL || data_model_action == NULL) {
|
|
517
|
+
return -1;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
printf("[demo] receive new action: id=%s, token=%s, timestamp=%u, input_num=%d\n",
|
|
521
|
+
action_id,
|
|
522
|
+
data_model_action->token ? data_model_action->token : "",
|
|
523
|
+
(unsigned)data_model_action->timestamp,
|
|
524
|
+
data_model_action->action_input_data_num);
|
|
525
|
+
|
|
526
|
+
// 1. 解析输入参数
|
|
527
|
+
for (int i = 0; i < data_model_action->action_input_data_num; i++) {
|
|
528
|
+
data_model_data_item_t *input = &data_model_action->action_input_data_list[i];
|
|
529
|
+
printf(" input[%d]: id=%s, type=%d\n", i, input->data_id, (int)input->data_type);
|
|
530
|
+
// 根据业务需要读取 input->data_value ...
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// 2. 执行行为逻辑(同步执行,避免在回调里做长时间阻塞)
|
|
534
|
+
|
|
535
|
+
// 3. 填充输出参数(按云端定义的输出参数顺序和类型)
|
|
536
|
+
int output_num = data_model_action->action_input_data_num; // 示例:与输入参数个数保持一致
|
|
537
|
+
data_model_action->action_output_data_num = output_num;
|
|
538
|
+
data_model_action->action_output_data_list = NULL;
|
|
539
|
+
if (output_num > 0) {
|
|
540
|
+
data_model_action->action_output_data_list =
|
|
541
|
+
(data_model_data_item_t *)calloc(output_num, sizeof(data_model_data_item_t));
|
|
542
|
+
if (data_model_action->action_output_data_list == NULL) {
|
|
543
|
+
return -2; // 内存不足,回复云端失败
|
|
544
|
+
}
|
|
545
|
+
for (int i = 0; i < output_num; i++) {
|
|
546
|
+
data_model_data_item_t *input = &data_model_action->action_input_data_list[i];
|
|
547
|
+
data_model_data_item_t *output = &data_model_action->action_output_data_list[i];
|
|
548
|
+
|
|
549
|
+
output->data_id = input->data_id; // 与云端约定好的输出参数标识
|
|
550
|
+
output->data_type = input->data_type; // 与云端约定好的输出参数类型
|
|
551
|
+
if (input->data_type == DATA_MODEL_DATA_TYPE_INT) {
|
|
552
|
+
output->data_value.value_int = input->data_value.value_int + 1;
|
|
553
|
+
} else {
|
|
554
|
+
output->data_value = input->data_value;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// 4. 返回 0:执行成功;返回非 0:执行失败,云端会收到对应错误码
|
|
560
|
+
return 0;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
【Android】
|
|
566
|
+
``` java
|
|
567
|
+
@Override
|
|
568
|
+
public int onReceiveNewAction(TXIoTDataModelSession.Action action) {
|
|
569
|
+
if (action == null) {
|
|
570
|
+
return -1;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// 1. 解析输入参数
|
|
574
|
+
if (action.actionInputDataList != null) {
|
|
575
|
+
for (TXIoTDataModelSession.Data input : action.actionInputDataList) {
|
|
576
|
+
// 根据 input.dataType 读取 input.dataValue ...
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// 2. 执行行为逻辑(同步执行,避免在回调里做长时间阻塞)
|
|
581
|
+
|
|
582
|
+
// 3. 填充输出参数(按云端定义的输出参数顺序和类型)
|
|
583
|
+
List<TXIoTDataModelSession.Data> outputList = new ArrayList<>();
|
|
584
|
+
if (action.actionInputDataList != null) {
|
|
585
|
+
for (TXIoTDataModelSession.Data input : action.actionInputDataList) {
|
|
586
|
+
TXIoTDataModelSession.Data output = new TXIoTDataModelSession.Data();
|
|
587
|
+
output.id = input.id;
|
|
588
|
+
output.dataType = input.dataType;
|
|
589
|
+
output.dataValue = new TXIoTDataModelSession.DataValue();
|
|
590
|
+
if (input.dataType == TXIoTDataModelSession.DataType.INT) {
|
|
591
|
+
output.dataValue.valueInt = input.dataValue.valueInt + 1;
|
|
592
|
+
} else {
|
|
593
|
+
output.dataValue = input.dataValue;
|
|
594
|
+
}
|
|
595
|
+
outputList.add(output);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
action.actionOutputDataList = outputList;
|
|
599
|
+
|
|
600
|
+
// 4. 返回 0:执行成功;返回非 0:执行失败,云端会收到对应错误码
|
|
601
|
+
return 0;
|
|
602
|
+
}
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
> **说明:**
|
|
606
|
+
> - 如果行为没有输出参数:Linux & RTOS 设备端把 `action_output_data_num` 置为 0、`action_output_data_list` 置为 NULL 即可;Android 智能设备端把 `action.actionOutputDataList` 置为空列表或 null 即可。
|
|
607
|
+
> - 返回 0 表示执行成功,云端收到的 action_reply 消息中 code=0、status=success;返回非 0 时 code=返回值、status=action execution failed。Linux & RTOS 与 Android 两端行为一致。
|
|
608
|
+
|
|
609
|
+
## 完整示例代码
|
|
610
|
+
|
|
611
|
+
【Linux & RTOS】
|
|
612
|
+
``` c
|
|
613
|
+
#include <stdio.h>
|
|
614
|
+
#include <stdlib.h>
|
|
615
|
+
#include <string.h>
|
|
616
|
+
#include <unistd.h>
|
|
617
|
+
|
|
618
|
+
#include "tc_iot.h"
|
|
619
|
+
#include "tc_iot_data_model.h"
|
|
620
|
+
|
|
621
|
+
static volatile int s_login_done = 0;
|
|
622
|
+
static tc_iot_error_e s_login_rc = TC_IOT_ERR_SUCCESS;
|
|
623
|
+
|
|
624
|
+
// 登录回调
|
|
625
|
+
static void on_login(tc_iot_error_e error_code, const char *error_message) {
|
|
626
|
+
s_login_rc = error_code;
|
|
627
|
+
s_login_done = 1;
|
|
628
|
+
if (error_code == TC_IOT_ERR_SUCCESS) {
|
|
629
|
+
printf("[demo] login success\n");
|
|
630
|
+
} else {
|
|
631
|
+
printf("[demo] login failed: %d, msg=%s\n", error_code,
|
|
632
|
+
error_message ? error_message : "");
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
// 属性上报结果回调
|
|
637
|
+
static void on_report_property_result_cb(char *property_id, void *user_data,
|
|
638
|
+
data_model_report_result_t result_code) {
|
|
639
|
+
printf("[demo] report property result: id=%s, code=%d\n",
|
|
640
|
+
property_id ? property_id : "(null)", (int)result_code);
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// 事件上报结果回调
|
|
644
|
+
static void on_report_event_result_cb(char *event_id, void *user_data,
|
|
645
|
+
data_model_report_result_t result_code) {
|
|
646
|
+
printf("[demo] report event result: id=%s, code=%d\n",
|
|
647
|
+
event_id ? event_id : "(null)", (int)result_code);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// 云端修改属性
|
|
651
|
+
static void on_receive_property_changed_cb(char *property_id,
|
|
652
|
+
tc_iot_data_model_property_t *data_model_property) {
|
|
653
|
+
if (!property_id || !data_model_property) {
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
data_model_data_item_t *item = &data_model_property->property;
|
|
657
|
+
printf("[demo] property changed: id=%s, type=%d\n", item->data_id, (int)item->data_type);
|
|
658
|
+
// 按 item->data_type 解析 item->data_value 并更新本地状态 ...
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// 云端下发行为
|
|
662
|
+
static int on_receive_new_action_cb(char *action_id, tc_iot_data_model_action_t *data_model_action) {
|
|
663
|
+
if (!action_id || !data_model_action) {
|
|
664
|
+
return -1;
|
|
665
|
+
}
|
|
666
|
+
printf("[demo] receive new action: id=%s, input_num=%d\n",
|
|
667
|
+
action_id, data_model_action->action_input_data_num);
|
|
668
|
+
|
|
669
|
+
// 解析输入参数并执行业务逻辑(示例:直接回传)
|
|
670
|
+
int n = data_model_action->action_input_data_num;
|
|
671
|
+
data_model_action->action_output_data_num = n;
|
|
672
|
+
data_model_action->action_output_data_list = NULL;
|
|
673
|
+
if (n > 0) {
|
|
674
|
+
data_model_action->action_output_data_list =
|
|
675
|
+
(data_model_data_item_t *)calloc(n, sizeof(data_model_data_item_t));
|
|
676
|
+
if (!data_model_action->action_output_data_list) {
|
|
677
|
+
return -2;
|
|
678
|
+
}
|
|
679
|
+
for (int i = 0; i < n; i++) {
|
|
680
|
+
data_model_data_item_t *in = &data_model_action->action_input_data_list[i];
|
|
681
|
+
data_model_data_item_t *out = &data_model_action->action_output_data_list[i];
|
|
682
|
+
out->data_id = in->data_id;
|
|
683
|
+
out->data_type = in->data_type;
|
|
684
|
+
out->data_value = in->data_value; // 示例:原样回传
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
return 0;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
int main(int argc, char **argv) {
|
|
691
|
+
if (argc < 7) {
|
|
692
|
+
printf("Usage: %s -p <product_id> -d <device_id> -s <device_secret>\n", argv[0]);
|
|
693
|
+
return 0;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
const char *product_id = NULL;
|
|
697
|
+
const char *device_id = NULL;
|
|
698
|
+
const char *device_secret = NULL;
|
|
699
|
+
for (int i = 1; i < argc; i++) {
|
|
700
|
+
if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) {
|
|
701
|
+
product_id = argv[++i];
|
|
702
|
+
} else if (strcmp(argv[i], "-d") == 0 && i + 1 < argc) {
|
|
703
|
+
device_id = argv[++i];
|
|
704
|
+
} else if (strcmp(argv[i], "-s") == 0 && i + 1 < argc) {
|
|
705
|
+
device_secret = argv[++i];
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
if (!product_id || !device_id || !device_secret) {
|
|
709
|
+
printf("device info error\n");
|
|
710
|
+
return 1;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// 1. 初始化 SDK
|
|
714
|
+
tc_iot_config_s config;
|
|
715
|
+
memset(&config, 0, sizeof(config));
|
|
716
|
+
config.storage_path = "./";
|
|
717
|
+
config.log_level = TC_IOT_LOG_LEVEL_INFO;
|
|
718
|
+
|
|
719
|
+
tc_iot_error_e rc = tc_iot_init(&config);
|
|
720
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
721
|
+
printf("tc_iot_init failed: %d\n", rc);
|
|
722
|
+
return 1;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
// 2. 登录
|
|
726
|
+
tc_iot_device_info_s device_info;
|
|
727
|
+
device_info.product_id = product_id;
|
|
728
|
+
device_info.device_id = device_id;
|
|
729
|
+
device_info.device_secret = device_secret;
|
|
730
|
+
|
|
731
|
+
rc = tc_iot_login(&device_info, on_login);
|
|
732
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
733
|
+
printf("tc_iot_login call failed: %d\n", rc);
|
|
734
|
+
tc_iot_deinit();
|
|
735
|
+
return 1;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
int waited_ms = 0;
|
|
739
|
+
while (!s_login_done && waited_ms < 5000) {
|
|
740
|
+
usleep(100 * 1000);
|
|
741
|
+
waited_ms += 100;
|
|
742
|
+
}
|
|
743
|
+
if (!s_login_done || s_login_rc != TC_IOT_ERR_SUCCESS) {
|
|
744
|
+
printf("login timeout or failed\n");
|
|
745
|
+
tc_iot_deinit();
|
|
746
|
+
return 1;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// 3. 初始化物模型模块
|
|
750
|
+
tc_iot_data_model_callback_t callback = {
|
|
751
|
+
.on_report_property_result_cb = on_report_property_result_cb,
|
|
752
|
+
.on_receive_property_changed_cb = on_receive_property_changed_cb,
|
|
753
|
+
.on_report_event_result_cb = on_report_event_result_cb,
|
|
754
|
+
.on_receive_new_action_cb = on_receive_new_action_cb,
|
|
755
|
+
};
|
|
756
|
+
rc = tc_iot_data_model_init(callback);
|
|
757
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
758
|
+
printf("tc_iot_data_model_init failed: %d\n", rc);
|
|
759
|
+
tc_iot_logout();
|
|
760
|
+
tc_iot_deinit();
|
|
761
|
+
return 1;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// 4. 上报一个布尔属性:power_switch = true
|
|
765
|
+
tc_iot_data_model_property_t property[1];
|
|
766
|
+
memset(property, 0, sizeof(property));
|
|
767
|
+
char id_power[] = "power_switch";
|
|
768
|
+
property[0].property.data_id = id_power;
|
|
769
|
+
property[0].property.data_type = DATA_MODEL_DATA_TYPE_BOOL;
|
|
770
|
+
property[0].property.data_value.value_bool = true;
|
|
771
|
+
|
|
772
|
+
rc = tc_iot_data_model_report_property(property, 1, NULL);
|
|
773
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
774
|
+
printf("tc_iot_data_model_report_property failed: %d\n", rc);
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// 5. 上报一个告警事件:temperature_alert,参数 temperature = 85
|
|
778
|
+
tc_iot_data_model_event_t event[1];
|
|
779
|
+
memset(event, 0, sizeof(event));
|
|
780
|
+
char id_event[] = "temperature_alert";
|
|
781
|
+
event[0].event_id = id_event;
|
|
782
|
+
event[0].event_type = DATA_MODEL_EVENT_TYPE_ALERT;
|
|
783
|
+
|
|
784
|
+
data_model_data_item_t event_data[1];
|
|
785
|
+
memset(event_data, 0, sizeof(event_data));
|
|
786
|
+
char id_temp[] = "temperature";
|
|
787
|
+
event_data[0].data_id = id_temp;
|
|
788
|
+
event_data[0].data_type = DATA_MODEL_DATA_TYPE_INT;
|
|
789
|
+
event_data[0].data_value.value_int = 85;
|
|
790
|
+
|
|
791
|
+
event[0].event_data_list = event_data;
|
|
792
|
+
event[0].event_data_num = 1;
|
|
793
|
+
|
|
794
|
+
rc = tc_iot_data_model_report_event(event, 1, NULL);
|
|
795
|
+
if (rc != TC_IOT_ERR_SUCCESS) {
|
|
796
|
+
printf("tc_iot_data_model_report_event failed: %d\n", rc);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
// 6. 等待云端交互(属性修改、行为调用等),观察回调打印
|
|
800
|
+
sleep(10);
|
|
801
|
+
|
|
802
|
+
// 7. 释放资源
|
|
803
|
+
tc_iot_data_model_deinit();
|
|
804
|
+
tc_iot_logout();
|
|
805
|
+
tc_iot_deinit();
|
|
806
|
+
printf("[demo] main exit\n");
|
|
807
|
+
return 0;
|
|
808
|
+
|
|
809
|
+
```
|
|
810
|
+
|
|
811
|
+
【Android】
|
|
812
|
+
``` java
|
|
813
|
+
import android.content.Context;
|
|
814
|
+
import java.util.ArrayList;
|
|
815
|
+
import java.util.List;
|
|
816
|
+
|
|
817
|
+
import com.tencent.liteav.iot.TXIoTCallback;
|
|
818
|
+
import com.tencent.liteav.iot.TXIoTDeviceEngine;
|
|
819
|
+
import com.tencent.liteav.iot.TXIoTDataModelSession;
|
|
820
|
+
import com.tencent.liteav.iot.TXIoTValueCallback;
|
|
821
|
+
|
|
822
|
+
public class DataModelDemo {
|
|
823
|
+
|
|
824
|
+
private TXIoTDataModelSession mDataModelSession;
|
|
825
|
+
private TXIoTDataModelSession.Listener mListener;
|
|
826
|
+
|
|
827
|
+
public void start(Context context, String productId, String deviceId, String deviceSecret) {
|
|
828
|
+
// 1. 初始化 SDK
|
|
829
|
+
TXIoTDeviceEngine engine = TXIoTDeviceEngine.getInstance(context);
|
|
830
|
+
TXIoTDeviceEngine.Config config = new TXIoTDeviceEngine.Config();
|
|
831
|
+
config.storagePath = context.getFilesDir().getAbsolutePath();
|
|
832
|
+
config.logLevel = TXIoTDeviceEngine.LogLevel.INFO;
|
|
833
|
+
engine.initSDK(config);
|
|
834
|
+
|
|
835
|
+
// 2. 登录
|
|
836
|
+
TXIoTDeviceEngine.DeviceInfo deviceInfo = new TXIoTDeviceEngine.DeviceInfo();
|
|
837
|
+
deviceInfo.productId = productId;
|
|
838
|
+
deviceInfo.deviceId = deviceId;
|
|
839
|
+
deviceInfo.deviceSecret = deviceSecret;
|
|
840
|
+
|
|
841
|
+
engine.login(deviceInfo, new TXIoTCallback() {
|
|
842
|
+
@Override
|
|
843
|
+
public void onSuccess() {
|
|
844
|
+
onLoginSuccess();
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
@Override
|
|
848
|
+
public void onError(int code, String desc) {
|
|
849
|
+
// 登录失败处理
|
|
850
|
+
}
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
private void onLoginSuccess() {
|
|
855
|
+
// 3. 获取物模型会话并注册监听器
|
|
856
|
+
mDataModelSession = TXIoTDataModelSession.getInstance();
|
|
857
|
+
mListener = new TXIoTDataModelSession.Listener() {
|
|
858
|
+
@Override
|
|
859
|
+
public void onReceivePropertyChanged(TXIoTDataModelSession.Data property) {
|
|
860
|
+
// 云端修改属性
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
@Override
|
|
864
|
+
public int onReceiveNewAction(TXIoTDataModelSession.Action action) {
|
|
865
|
+
// 云端下发行为,执行并回填输出参数
|
|
866
|
+
return 0;
|
|
867
|
+
}
|
|
868
|
+
};
|
|
869
|
+
mDataModelSession.addListener(mListener);
|
|
870
|
+
|
|
871
|
+
// 4. 上报一个布尔属性:power_switch = true
|
|
872
|
+
TXIoTDataModelSession.Data power = new TXIoTDataModelSession.Data();
|
|
873
|
+
power.id = "power_switch";
|
|
874
|
+
power.dataType = TXIoTDataModelSession.DataType.BOOL;
|
|
875
|
+
power.dataValue = new TXIoTDataModelSession.DataValue();
|
|
876
|
+
power.dataValue.valueBool = true;
|
|
877
|
+
|
|
878
|
+
List<TXIoTDataModelSession.Data> propertyList = new ArrayList<>();
|
|
879
|
+
propertyList.add(power);
|
|
880
|
+
mDataModelSession.reportProperty(propertyList,
|
|
881
|
+
new TXIoTValueCallback<List<TXIoTDataModelSession.ResultItem>>() {
|
|
882
|
+
@Override
|
|
883
|
+
public void onSuccess(List<TXIoTDataModelSession.ResultItem> results) { }
|
|
884
|
+
|
|
885
|
+
@Override
|
|
886
|
+
public void onError(int code, String desc) { }
|
|
887
|
+
});
|
|
888
|
+
|
|
889
|
+
// 5. 上报一个告警事件:temperature_alert,参数 temperature = 85
|
|
890
|
+
TXIoTDataModelSession.Data temp = new TXIoTDataModelSession.Data();
|
|
891
|
+
temp.id = "temperature";
|
|
892
|
+
temp.dataType = TXIoTDataModelSession.DataType.INT;
|
|
893
|
+
temp.dataValue = new TXIoTDataModelSession.DataValue();
|
|
894
|
+
temp.dataValue.valueInt = 85;
|
|
895
|
+
|
|
896
|
+
List<TXIoTDataModelSession.Data> eventData = new ArrayList<>();
|
|
897
|
+
eventData.add(temp);
|
|
898
|
+
|
|
899
|
+
TXIoTDataModelSession.Event event = new TXIoTDataModelSession.Event();
|
|
900
|
+
event.id = "temperature_alert";
|
|
901
|
+
event.eventType = TXIoTDataModelSession.EventType.ALERT;
|
|
902
|
+
event.eventDataList = eventData;
|
|
903
|
+
|
|
904
|
+
mDataModelSession.reportEvent(event, new TXIoTCallback() {
|
|
905
|
+
@Override
|
|
906
|
+
public void onSuccess() { }
|
|
907
|
+
|
|
908
|
+
@Override
|
|
909
|
+
public void onError(int code, String desc) { }
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
public void release() {
|
|
914
|
+
// 6. 释放资源
|
|
915
|
+
if (mDataModelSession != null && mListener != null) {
|
|
916
|
+
mDataModelSession.removeListener(mListener);
|
|
917
|
+
}
|
|
918
|
+
TXIoTDeviceEngine.getInstance(null).logout(new TXIoTCallback() {
|
|
919
|
+
@Override
|
|
920
|
+
public void onSuccess() { }
|
|
921
|
+
|
|
922
|
+
@Override
|
|
923
|
+
public void onError(int code, String desc) { }
|
|
924
|
+
});
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
```
|
|
928
|
+
|
|
929
|
+
## 编译并运行
|
|
930
|
+
1. 在项目根目录执行 CMake 编译。
|
|
931
|
+
|
|
932
|
+
``` bash
|
|
933
|
+
cmake -B build
|
|
934
|
+
cmake --build build -j
|
|
935
|
+
```
|
|
936
|
+
2. 运行可执行文件,传入设备三元组。
|
|
937
|
+
|
|
938
|
+
``` bash
|
|
939
|
+
./build/iot_quickstart_demo -p YOUR_PRODUCT_ID -d YOUR_DEVICE_NAME -s 'YOUR_DEVICE_SECRET'
|
|
940
|
+
```
|
|
941
|
+
3. 观察日志输出,出现以下内容代表登录与信令发送均成功。
|
|
942
|
+
|
|
943
|
+
```
|
|
944
|
+
[demo] login success
|
|
945
|
+
[demo] report property result: id=power_switch, code=0
|
|
946
|
+
[demo] report event result: id=temperature_alert, code=0
|
|
947
|
+
```
|
|
948
|
+
|
|
949
|
+
云端在控制台对设备发起一次行为调用(如 set_brightness),设备端会打印:
|
|
950
|
+
|
|
951
|
+
```
|
|
952
|
+
[demo] receive new action: id=set_brightness, input_num=1 input[0]: id=brightness, type=2
|
|
953
|
+
```
|
|
954
|
+
|
|
955
|
+
云端在控制台修改一次属性(如把 brightness 改为 90),设备端会打印:
|
|
956
|
+
|
|
957
|
+
```
|
|
958
|
+
[demo] property changed: id=brightness, type=2
|
|
959
|
+
```
|
|
960
|
+
|
|
961
|
+
## 常见问题
|
|
962
|
+
|
|
963
|
+
|现象|排查建议|
|
|
964
|
+
|---------|---------|
|
|
965
|
+
|`tc_iot_data_model_init` 返回 TC_IOT_ERR_NOT_INITIALIZED|<br>确认在调用前已成功调用 tc_iot_init 和 tc_iot_login,且 on_login 回调返回 TC_IOT_ERR_SUCCESS。<br>|
|
|
966
|
+
|<br>`tc_iot_data_model_init` 返回 TC_IOT_ERR_ALREADY_INITIALIZED|<br>物模型模块只允许初始化一次,确认没有重复调用,或先调用 tc_iot_data_model_deinit 再重新 init。<br>|
|
|
967
|
+
|tc_iot_data_model_report_property 返回 TC_IOT_ERR_INVALID_ARGUMENT|检查 property 指针是否为空、property_num 是否 ≤ 0。|
|
|
968
|
+
|云端收不到上报的属性 / 事件|检查 data_id、event_id 是否与控制台物模型定义完全一致;data_type 与 data_value 的 union 字段是否对应正确。|
|
|
969
|
+
|on_report_property_result_cb 回调里 result_code ≠ DATA_MODEL_REPORT_SUCCESS|查看 SDK 日志中的错误码;常见原因有网络抖动、MQTT 断连、设备未登录。|
|
|
970
|
+
|云端行为调用,设备执行后并响应后,云端没有收到结果 / 一直 pending|确认 on_receive_new_action_cb 在合理时间内返回(0 表示成功);回调中不要做阻塞或 sleep;输出参数按云端约定填写。|
|
|
971
|
+
|编译报错找不到头文件|确认 CMakeLists.txt 中 target_include_directories 正确指向 tc_iot_sdk/include。|
|
|
972
|
+
|