在腾讯云物联网开发平台控制台(以下简称控制台)创建产品时,会默认生成一套产品的数据模板和一些标准功能,用户也可以自定义功能。数据模板对应的功能包含三大类:属性,事件和行为。控制台数据模板的使用,可参考官网 数据模板 章节。
产品定义数据模板后,设备可以按照数据模板中的定义上报属性、事件,并可对设备下发远程控制指令,即对可写的设备属性进行修改。数据模板的管理详见 产品定义。数据模板协议包括设备属性上报、设备远程控制、获取设备最新上报信息、设备事件上报、设备行为。对应的定义和云端下发控制指令使用的 Topic 请参考官网 数据模板协议 章节。
本文主要描述 SDK Demo 对数据模板中相关联的属性上报部分。
运行示例程序,在数据模板模块上点击设备上线
按钮且设备成功上线后,点击属性上报
按钮,发布属性类型的 Topic:
$thing/up/property/{ProductID}/{DeviceName}
示例代码如下:
JSONObject property = new JSONObject();
try {
//此处按照您的数据模板进行设置,以下为参考示例。
JSONObject structJson = new JSONObject();
structJson.put("bool_param", 1); // 布尔类型
structJson.put("int_param", 10); // 整数类型
structJson.put("str_param", "testStrAndroid"); // 字符串类型
structJson.put("float_param", 2.1001); // 浮点类型
structJson.put("enum_param", 1); // 枚举类型
structJson.put("time_param", 1577871650); // 时间戳类型
property.put("struct", structJson); // 自定义结构体属性
property.put("power_switch",0); // 创建产品时,选择产品品类为智能城市-公共事业-路灯照明,数据模板中系统推荐的标准功能属性
property.put("color",0); // 创建产品时,选择产品品类为智能城市-公共事业-路灯照明,数据模板中系统推荐的标准功能属性
property.put("brightness",0); // 创建产品时,选择产品品类为智能城市-公共事业-路灯照明,数据模板中系统推荐的标准功能属性
property.put("name","test"); // 创建产品时,选择产品品类为智能城市-公共事业-路灯照明,数据模板中系统推荐的标准功能属性
JSONArray arrInt = new JSONArray(); // 整数数组
arrInt.put(1);
arrInt.put(3);
arrInt.put(5);
arrInt.put(7);
property.put("arrInt", arrInt);
JSONArray arrStr = new JSONArray(); // 字符串数组
arrStr.put("aaa");
arrStr.put("bbb");
arrStr.put("ccc");
arrStr.put("");
property.put("arrString", arrStr);
JSONArray arrFloat = new JSONArray(); // 浮点数组
arrFloat.put(5.001);
arrFloat.put(0.003);
arrFloat.put(0.004);
arrFloat.put(0.007);
property.put("arrFloat", arrFloat);
JSONArray arrStruct = new JSONArray(); // 结构体数组
for (int i = 0; i < 7; i++) {
JSONObject structEleJson = new JSONObject();
structEleJson.put("boolM", 0); // 布尔型参数
structEleJson.put("intM", 0); // 整数型参数
structEleJson.put("stringM", "string"); // 字符串参数
structEleJson.put("floatM", 0.1); // 浮点型参数
structEleJson.put("enumM", 0); // 枚举型参数
structEleJson.put("timeM", 1577871650); // 时间型参数
arrStruct.put(structEleJson);
}
property.put("arrStruct", arrStruct);
} catch (JSONException e) {
mParent.printLogInfo(TAG, "Construct property json failed!", mLogInfoText, TXLog.LEVEL_ERROR);
return;
}
if(Status.OK != mDataTemplateSample.propertyReport(property, null)) {
mParent.printLogInfo(TAG, "property report failed!", mLogInfoText, TXLog.LEVEL_ERROR);
}
观察Logcat日志。
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: Starting publish topic: $thing/up/property/LWVUL5SZ2L/light1 Message: {"method":"report","clientToken":"LWVUL5SZ2Llight110","timestamp":1603159172854,"params":{"power_switch":0,"color":0,"brightness":0,"name":"test"}}
D/TXDataTemplateFragment: onPublishCompleted, status[OK], topics[[$thing/up/property/LWVUL5SZ2L/light1]], userContext[], errMsg[publish success]
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: Received topic: $thing/down/property/LWVUL5SZ2L/light1, id: 0, message: {"method":"report_reply","clientToken":"LWVUL5SZ2Llight110","code":0,"status":"success"}
D/TXDataTemplateFragment: receive command, topic[$thing/down/property/LWVUL5SZ2L/light1], message[{"method":"report_reply","clientToken":"LWVUL5SZ2Llight110","code":0,"status":"success"}]
D/TXDATATEMPLATE: property down stream message received {"method":"report_reply","clientToken":"LWVUL5SZ2Llight110","code":0,"status":"success"}
D/TXDATATEMPLATE: handle_reply: reply OK! ClientToken:LWVUL5SZ2Llight110
D/TXDataTemplateFragment: reply received : {"method":"report_reply","clientToken":"LWVUL5SZ2Llight110","code":0,"status":"success"}
以上日志为 发布属性上报的 Topic 成功,如果已订阅 Topic,会接收到如上日志中的report_reply消息。同时在控制台创建的对应设备中,可以查看到对应的设备日志,在线调试中也可以看到设备的实时属性已更改为属性上报中对应设置的属性值。控制台中查看设备日志以及设备的在线调试,请参考 设备调试 章节。