Skip to content

Latest commit

 

History

History
executable file
·
165 lines (135 loc) · 11.6 KB

网关使用示例.md

File metadata and controls

executable file
·
165 lines (135 loc) · 11.6 KB

网关使用示例

本文主要描述 SDK Demo 中网关设备的使用示例。

控制台创建设备

体验网关示例Demo需要在腾讯云物联网开发平台控制台(以下简称控制台)创建一个网关设备,一个智能灯设备,一个空调设备。请参考官网 用户指南-网关设备接入注:需要将智能灯设备和空调设备绑定为网关设备的子设备

填写认证连接设备的参数

编辑 app-config.json 文件中的参数配置信息

{
  "PRODUCT_ID":        "",
  "DEVICE_NAME":       "",
  "DEVICE_PSK":        "",
  "SUB_PRODUCT_ID":    "",
  "SUB_DEV_NAME":      "",
  "SUB_DEV_PSK":       "",
  "SUB_PRODUCT_ID2":   "",
  "SUB_DEV_NAME2":     "",
  "SUB_DEV_PSK2":      ""
}

如果控制台创建设备使用的是密钥认证方式,需要在 app-config.json 填写 PRODUCT_ID(网关设备的产品ID)、DEVICE_NAME(网关设备的设备名称)、DEVICE_PSK(网关设备的设备密钥)、SUB_PRODUCT_ID(智能灯设备的产品ID)、SUB_DEV_NAME(智能灯设备的设备名称)、SUB_PRODUCT_ID2(空调设备的产品ID)、SUB_DEV_NAME2(空调设备的设备名称),示例中使用的是密钥认证。

如果控制台创建设备使用的是证书认证方式,除了需要在 app-config.json 填写 PRODUCT_ID(产品ID)、DEVICE_NAME(设备名称),DEVICE_PSK(设备密钥)设置为null,还需通过 AssetManager 进行证书读取,在工程 explorer/device-android-demo/src/main 路径下创建 assets 目录并将设备证书、私钥放置在该目录中,在 IoTGatewayFragment.java 中配置 mDevCert(设备证书文件名称)mDevPriv(设备私钥文件名称)。

控制台上创建的每一个产品都可以设置该产品对应的数据模板,数据模板对应的功能包含三大类:属性,事件和行为。控制台数据模板的使用,请参考官网 数据模板 章节。

示例中的设备需要将控制台上下载的json文件导入工程中,用来规范上报属性、事件时数据的校验,请将json文件放到 assets 目录中,并设置对应网关及子设备的 mJsonFileName、mSubDev1JsonFileName(json文件名称)。

运行示例程序使网关设备进行 MQTT 认证连接上线

运行示例程序,在网关示例模块上点击网关上线按钮,进行认证连接,使网关设备上线。示例代码如下:

mGatewaySample.online();

观察Logcat日志。

I/TXGatewaySample: Using PSK
I/TXGATEWAYCLIENT: Start connecting to ssl://VOY2UGD9HH.iotcloud.tencentdevices.com:8883
D/iot.TXAlarmPingSender: Register alarmreceiver to Context iot.TXAlarmPingSender.pingSender.VOY2UGD9HHgateway1
D/iot.TXAlarmPingSender: Schedule next alarm at 1603183885475
D/iot.TXAlarmPingSender: Alarm scheule using setExactAndAllowWhileIdle, next: 240000
I/TXGATEWAYCLIENT: onSuccess!
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: Starting subscribe topic: $thing/down/property/VOY2UGD9HH/gateway1
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: Starting subscribe topic: $thing/down/event/VOY2UGD9HH/gateway1
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: Starting subscribe topic: $thing/down/action/VOY2UGD9HH/gateway1
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: Starting subscribe topic: $gateway/operation/result/VOY2UGD9HH/gateway1
D/TXGATEWAYCLIENT: Connected, then subscribe the gateway result topic
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: connectComplete. reconnect flag is false
D/TXGatewaySample: onSubscribeCompleted, status[OK], topics[[$thing/down/property/VOY2UGD9HH/gateway1]], userContext[MQTTRequest{requestType='subscribeTopic', requestId=0}], errMsg[subscribe success]
D/TXGatewaySample: onSubscribeCompleted, status[OK], topics[[$thing/down/action/VOY2UGD9HH/gateway1]], userContext[MQTTRequest{requestType='subscribeTopic', requestId=2}], errMsg[subscribe success]
D/TXGatewaySample: onSubscribeCompleted, status[OK], topics[[$gateway/operation/result/VOY2UGD9HH/gateway1]], userContext[], errMsg[subscribe success]
D/TXGatewaySample: onSubscribeCompleted, status[OK], topics[[$thing/down/event/VOY2UGD9HH/gateway1]], userContext[MQTTRequest{requestType='subscribeTopic', requestId=1}], errMsg[subscribe success]

以上是设备通过 MQTT 成功连接至云端并订阅网关设备关联的数据模板协议 Topic 消息的日志,在控制台可查看该网关设备的状态已更新为在线。

网关下线

运行示例程序,当网关设备已上线,在网关示例模块上点击网关下线按钮,使网关设备断开 MQTT 认证连接,网关设备下线。示例代码如下:

mGatewaySample.offline(); 

观察Logcat日志。

I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: Starting unSubscribe topic: $thing/down/property/VOY2UGD9HH/gateway1
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: Starting unSubscribe topic: $thing/down/event/VOY2UGD9HH/gateway1
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: Starting unSubscribe topic: $thing/down/action/VOY2UGD9HH/gateway1
D/TXGatewaySample: onUnSubscribeCompleted, status[OK], topics[[$thing/down/property/VOY2UGD9HH/gateway1]], userContext[MQTTRequest{requestType='subscribeTopic', requestId=3}], errMsg[unsubscribe success]
D/TXGatewaySample: onUnSubscribeCompleted, status[OK], topics[[$thing/down/event/VOY2UGD9HH/gateway1]], userContext[MQTTRequest{requestType='subscribeTopic', requestId=4}], errMsg[unsubscribe success]
D/TXGatewaySample: onUnSubscribeCompleted, status[OK], topics[[$thing/down/action/VOY2UGD9HH/gateway1]], userContext[MQTTRequest{requestType='subscribeTopic', requestId=5}], errMsg[unsubscribe success]
D/iot.TXAlarmPingSender: Unregister alarmreceiver to Context VOY2UGD9HHgateway1
D/TXGatewaySample: onDisconnectCompleted, status[OK], userContext[MQTTRequest{requestType='disconnect', requestId=1}], msg[disconnected to ssl://VOY2UGD9HH.iotcloud.tencentdevices.com:8883]

以上为网关设备成功断开 MQTT 认证连接并取消订阅网关设备关联的数据模板协议 Topic 消息的日志,在控制台可查看该网关设备的状态已更新为离线。

添加智能灯设备

运行示例程序,当网关设备已上线,在网关示例模块上点击添加智能灯按钮,使智能灯设备添加到网关设备的子设备中。示例代码如下:

Object obj =  mGatewaySample.addSubDev(mSubDev1ProductId, mSubDev1DeviceName);
if(null != obj) {
    mSubDev1 = (ProductLight)obj;
}

观察Logcat日志。

D/TXGATEWAYCLIENT: input product id is LWVUL5SZ2L, input device name is light1
    The hashed information is {LWVUL5SZ2Llight1=com.tencent.iot.explorer.device.android.gateway.TXGatewaySubdev@27282a6}
D/TXGATEWAYCLIENT: input product id is LWVUL5SZ2L, input device name is light1
D/TXGATEWAYCLIENT: The hashed information is {LWVUL5SZ2Llight1=com.tencent.iot.explorer.device.android.gateway.TXGatewaySubdev@27282a6}

以上是网关设备成功将智能灯设备添加到子设备的日志。

删除智能灯设备

运行示例程序,当网关设备已上线且已添加智能灯时,在网关示例模块上点击删除智能灯按钮,智能灯设备将被从网关设备的子设备中移除。示例代码如下:

mGatewaySample.delSubDev(mSubDev1ProductId, mSubDev1DeviceName);
mSubDev1 = null;

智能灯设备上线

运行示例程序,当网关设备已上线且已添加智能灯时,在网关示例模块上,点击智能灯对应的上线按钮,发布智能灯上线的 Topic 消息。示例代码如下:

mGatewaySample.onlineSubDev(mSubDev1ProductId, mSubDev1DeviceName);

观察Logcat日志。

D/TXGATEWAYCLIENT: input product id is LWVUL5SZ2L, input device name is light1
    The hashed information is {LWVUL5SZ2Llight1=com.tencent.iot.explorer.device.android.gateway.TXGatewaySubdev@2f548f5}
    set LWVUL5SZ2L & light1 to Online
D/TXGATEWAYCLIENT: publish message {"type":"online","payload":{"devices":[{"product_id":"LWVUL5SZ2L","device_name":"light1"}]}}
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: Starting publish topic: $gateway/operation/VOY2UGD9HH/gateway1 Message: {"type":"online","payload":{"devices":[{"product_id":"LWVUL5SZ2L","device_name":"light1"}]}}
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: deliveryComplete, token.getMessageId:0
D/TXGatewaySample: onPublishCompleted, status[OK], topics[[$gateway/operation/VOY2UGD9HH/gateway1]],  userContext[], errMsg[publish success]
D/TXGATEWAYCLIENT: message received $gateway/operation/result/VOY2UGD9HH/gateway1
D/TXGATEWAYCLIENT: got gate operation messga $gateway/operation/result/VOY2UGD9HH/gateway1{"type":"online","payload":{"devices":[{"product_id":"LWVUL5SZ2L","device_name":"light1","result":0}]}}
    input product id is LWVUL5SZ2L, input device name is light1
D/TXProductLight: dev[light1] online!

以上是网关设备成功发送智能灯上线 Topic 并且网关设备接收到了子设备上线的 Topic 消息的日志。网关设备代理子设备上下线的 Topic ,请参考官网 代理子设备上下线

智能灯设备下线

运行示例程序,当网关设备已上线且已添加智能灯时,在网关示例模块上,点击智能灯对应的下线按钮,发布智能灯下线的 Topic 消息。示例代码如下:

mGatewaySample.offlineSubDev(mSubDev1ProductId, mSubDev1DeviceName);

观察Logcat日志。

D/TXGATEWAYCLIENT: Try to find LWVUL5SZ2L & light1
    input product id is LWVUL5SZ2L, input device name is light1
    The hashed information is {LWVUL5SZ2Llight1=com.tencent.iot.explorer.device.android.gateway.TXGatewaySubdev@77dca3}
    set LWVUL5SZ2L & light1 to offline
D/TXGATEWAYCLIENT: publish message {"type":"offline","payload":{"devices":[{"product_id":"LWVUL5SZ2L","device_name":"light1"}]}}
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: Starting publish topic: $gateway/operation/VOY2UGD9HH/gateway1 Message: {"type":"offline","payload":{"devices":[{"product_id":"LWVUL5SZ2L","device_name":"light1"}]}}
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: deliveryComplete, token.getMessageId:0
D/TXGatewaySample: onPublishCompleted, status[OK], topics[[$gateway/operation/VOY2UGD9HH/gateway1]],  userContext[], errMsg[publish success]
D/TXGATEWAYCLIENT: message received $gateway/operation/result/VOY2UGD9HH/gateway1
    got gate operation messga $gateway/operation/result/VOY2UGD9HH/gateway1{"type":"offline","payload":{"devices":[{"product_id":"LWVUL5SZ2L","device_name":"light1","result":0}]}}

以上是网关设备成功发送智能灯下线 Topic 成功并且网关设备接收到了子设备下线的 Topic 消息的日志。网关设备代理子设备上下线的 Topic ,请参考官网 代理子设备上下线