本文主要描述设备端如何发布事件上报以及多事件上报的 Topic 。
运行示例程序,在数据模板模块上点击设备上线
按钮且设备成功上线后,点击事件上报
按钮,拼接设备信息参数,发布事件类型的 Topic:
$thing/up/event/{ProductID}/{DeviceName}
示例代码如下:
String eventId = "status_report";
String type = "info";
JSONObject params = new JSONObject();
params.put("status",0);
params.put("message","");
if(Status.OK != mDataTemplateSample.eventSinglePost(eventId, type, params)){
mParent.printLogInfo(TAG, "single event post failed!", mLogInfoText, TXLog.LEVEL_ERROR);
}
观察Logcat日志。
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: Starting publish topic: $thing/up/event/LWVUL5SZ2L/light1 Message: {"method":"event_post","clientToken":"LWVUL5SZ2Llight15","eventId":"status_report","type":"info","timestamp":1603160347446,"params":{"status":0,"message":""}}
D/TXDataTemplateFragment: onPublishCompleted, status[OK], topics[[$thing/up/event/LWVUL5SZ2L/light1]], userContext[], errMsg[publish success]
D/TXDataTemplateFragment: receive command, topic[$thing/down/event/LWVUL5SZ2L/light1], message[{"method":"event_reply","clientToken":"LWVUL5SZ2Llight15","code":0,"status":"","data":{}}]
D/TXDATATEMPLATE: event down stream message received : {"method":"event_reply","clientToken":"LWVUL5SZ2Llight15","code":0,"status":"","data":{}}
D/TXDataTemplateFragment: reply received : {"method":"event_reply","clientToken":"LWVUL5SZ2Llight15","code":0,"status":"","data":{}}
以上是设备成功发布单个事件上报Topic的日志。如果已订阅 Topic,设备会接收到如上日志中的event_reply消息。在控制台创建的对应设备中,可查看到对应的设备事件,若传入的type为info时,代表信息类型的事件。控制台中查看设备事件,请参考 设备调试 章节。
运行示例程序,在数据模板模块上点击设备上线
按钮且设备成功上线后,点击多事件上报
按钮,拼接设备信息参数,发布事件类型的 Topic:
$thing/up/event/{ProductID}/{DeviceName}
示例代码如下:
JSONArray events = new JSONArray();
//event:status_report
try {
JSONObject event = new JSONObject();
event.put("eventId","status_report");
event.put("type", "info");
event.put("timestamp", System.currentTimeMillis());
JSONObject params = new JSONObject();
params.put("status",0);
params.put("message","");
event.put("params", params);
events.put(event);
} catch (JSONException e) {
mParent.printLogInfo(TAG, "Construct params failed!", mLogInfoText, TXLog.LEVEL_ERROR);
return;
}
//event:low_voltage
try {
JSONObject event = new JSONObject();
event.put("eventId","low_voltage");
event.put("type", "alert");
event.put("timestamp", System.currentTimeMillis());
JSONObject params = new JSONObject();
params.put("voltage",1.000000f);
event.put("params", params);
events.put(event);
} catch (JSONException e) {
mParent.printLogInfo(TAG, "Construct params failed!", mLogInfoText, TXLog.LEVEL_ERROR);
return;
}
//event:hardware_fault
try {
JSONObject event = new JSONObject();
event.put("eventId","hardware_fault");
event.put("type", "fault");
event.put("timestamp", System.currentTimeMillis());
JSONObject params = new JSONObject();
params.put("name","");
params.put("error_code",1);
event.put("params", params);
events.put(event);
} catch (JSONException e) {
mParent.printLogInfo(TAG, "Construct params failed!", mLogInfoText, TXLog.LEVEL_ERROR);
return;
}
if(Status.OK != mDataTemplateSample.eventsPost(events)){
mParent.printLogInfo(TAG, "events post failed!", mLogInfoText, TXLog.LEVEL_ERROR);
}
观察Logcat日志。
I/com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection: Starting publish topic: $thing/up/event/LWVUL5SZ2L/light1 Message: {"method":"events_post","clientToken":"LWVUL5SZ2Llight16","events":[{"eventId":"status_report","type":"info","timestamp":1603160653628,"params":{"status":0,"message":""}},{"eventId":"low_voltage","type":"alert","timestamp":1603160653628,"params":{"voltage":1}},{"eventId":"hardware_fault","type":"fault","timestamp":1603160653628,"params":{"name":"","error_code":1}}]}
D/TXDataTemplateFragment: onPublishCompleted, status[OK], topics[[$thing/up/event/LWVUL5SZ2L/light1]], userContext[], errMsg[publish success]
D/TXDataTemplateFragment: receive command, topic[$thing/down/event/LWVUL5SZ2L/light1], message[{"method":"events_reply","clientToken":"LWVUL5SZ2Llight16","code":0,"status":"","data":{}}]
D/TXDATATEMPLATE: event down stream message received : {"method":"events_reply","clientToken":"LWVUL5SZ2Llight16","code":0,"status":"","data":{}}
D/TXDataTemplateFragment: reply received : {"method":"events_reply","clientToken":"LWVUL5SZ2Llight16","code":0,"status":"","data":{}}
以上是设备成功发布多个事件上报Topic的日志。如果已订阅 Topic,设备会接收到如上日志中的events_reply消息。在控制台创建的对应设备中,可查看到对应的设备事件,若传入的type为info时,代表信息类型的事件;若传入的type为alert时,代表告警类型的事件;若传入的type为fault时,代表故障类型的事件。控制台中查看设备事件,请参考 设备调试 章节。