概述
物联网平台支持设备将本地日志上报到云端,在控制台进行查询和故障分析。在设备详情页,开启设备本地日志上报开关后,设备才能将本地日志上报到云端。
Step By Step
一、设备获取日志配置(通过该功能,设备端可以主动获取:设备本地日志上报开关的开启情况)
数据上行
- 请求Topic:/sys/${productKey}/${deviceName}/thing/config/log/get
- 响应Topic:/sys/${productKey}/${deviceName}/thing/config/log/get_reply
1、设备端订阅:响应Topic
2、设备端publish如下格式消息到服务端:
{
"id" : 123,
"version":"1.0",
"params" : {
"configScope":"device",
"getType":"content"
},
"method":"thing.config.log.get"
}
3、设备端即可获取设备本地日志上报开关的开启情况(mode为0表示未开启,mode为1表示开启)
4、Code Sample,参考链接:基于开源JAVA MQTT Client连接阿里云IoT
import com.alibaba.taro.AliyunIoTSignUtil;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.util.HashMap;
import java.util.Map;
public class IoTDemoPubSubDemoSLSGetPro {
// 设备三元组信息
public static String productKey = "a1qLU******";
public static String deviceName = "device1";
public static String deviceSecret = "cA9wzIM6bL2QI6Dg****************";
public static String regionId = "cn-shanghai";
// 物模型-属性上报topic
private static String pubTopic = "/sys/" + productKey + "/" + deviceName + "/thing/config/log/get";
// 自定义topic,在产品Topic列表位置定义
private static String subTopic = "/sys/" + productKey + "/" + deviceName + "/thing/config/log/get_reply";
private static MqttClient mqttClient;
public static void main(String [] args){
initAliyunIoTClient();
postDeviceProperties();
try {
mqttClient.subscribe(subTopic); // 订阅Topic
} catch (MqttException e) {
System.out.println("error:" + e.getMessage());
e.printStackTrace();
}
// 设置订阅监听
mqttClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable throwable) {
System.out.println("connection Lost");
}
@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
System.out.println("Sub message");
System.out.println("Topic : " + s);
System.out.println(new String(mqttMessage.getPayload())); //打印输出消息payLoad
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
}
/**
* 初始化 Client 对象
*/
private static void initAliyunIoTClient() {
try {
// 构造连接需要的参数
String clientId = "java" + System.currentTimeMillis();
Map<String, String> params = new HashMap<>(16);
params.put("productKey", productKey);
params.put("deviceName", deviceName);
params.put("clientId", clientId);
String timestamp = String.valueOf(System.currentTimeMillis());
params.put("timestamp", timestamp);
// cn-shanghai
String targetServer = "tcp://" + productKey + ".iot-as-mqtt."+regionId+".aliyuncs.com:1883";
String mqttclientId = clientId + "|securemode=3,signmethod=hmacsha1,timestamp=" + timestamp + "|";
String mqttUsername = deviceName + "&" + productKey;
String mqttPassword = AliyunIoTSignUtil.sign(params, deviceSecret, "hmacsha1");
connectMqtt(targetServer, mqttclientId, mqttUsername, mqttPassword);
} catch (Exception e) {
System.out.println("initAliyunIoTClient error " + e.getMessage());
}
}
public static void connectMqtt(String url, String clientId, String mqttUsername, String mqttPassword) throws Exception {
MemoryPersistence persistence = new MemoryPersistence();
mqttClient = new MqttClient(url, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
// MQTT 3.1.1
connOpts.setMqttVersion(4);
connOpts.setAutomaticReconnect(false);
connOpts.setConnectionTimeout(10);
// connOpts.setCleanSession(true);
connOpts.setCleanSession(false);
connOpts.setUserName(mqttUsername);
connOpts.setPassword(mqttPassword.toCharArray());
connOpts.setKeepAliveInterval(60);
mqttClient.connect(connOpts);
}
/**
* 汇报属性
*/
private static void postDeviceProperties() {
try {
//上报数据
//高级版 物模型-属性上报payload
System.out.println("上报设备日志:");
String payloadJson = "{\n" +
" \"id\" : 123,\n" +
" \"version\":\"1.0\",\n" +
" \"params\" : {\n" +
" \"configScope\":\"device\", \n" +
" \"getType\":\"content\"\n" +
" },\n" +
" \"method\":\"thing.config.log.get\"\n" +
"}";
MqttMessage message = new MqttMessage(payloadJson.getBytes("utf-8"));
message.setQos(1);
mqttClient.publish(pubTopic, message);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
5、Result:
Sub message
Topic : /sys/a1qLU******/device1/thing/config/log/get_reply
{"code":200,"data":{"content":{"mode":1},"getType":"content"},"id":"123","method":"thing.config.log.get","version":"1.0"}
二、设备接收订阅云端推送日志配置
数据下行
- Topic:/sys/${productKey}/${deviceName}/thing/config/log/push
1、设备端订阅下行Topic;
private static String subTopicSLS = "/sys/" + productKey + "/" + deviceName + "/thing/config/log/push";
mqttClient.subscribe(subTopicSLS);
2、平台修改:设备本地日志上报 开关状态
3、设备端监听情况
Sub message
Topic : /sys/a1qLU******/device1/thing/config/log/push
{"method":"thing.config.log.push","id":"1174554406","params":{"getType":"content","content":{"mode":0}},"version":"1.0"}
三、设备上报日志内容(与常规的物模型属性上报类似)
数据上行
- 请求Topic:/sys/${productKey}/${deviceName}/thing/log/post
- 响应Topic:/sys/${productKey}/${deviceName}/thing/log/post_reply
1、向请求Topic上行消息,格式如下:
{
"id" : 123,
"version":"1.0",
"params" :[{
"utcTime": "2020-04-24T15:15:27.464+0800",
"logLevel": "ERROR",
"module": "ModuleA",
"code" :"",
"traceContext": "123456",
"logContent" : "some log content"
}],
"method" : "thing.log.post"
}
2、Code Sample
/**
* 上报设备日志
*/
private static void postDeviceProperties() {
try {
//上报数据
//高级版 物模型-属性上报payload
System.out.println("上报设备日志:");
String payloadJson = "{\n" +
" \"id\" : 123,\n" +
" \"version\":\"1.0\",\n" +
" \"params\" :[{\n" +
" \"utcTime\": \"2020-04-24T15:15:27.464+0800\", \n" +
" \"logLevel\": \"ERROR\", \n" +
" \"module\": \"ModuleA\", \n" +
" \"code\" :\"\", \n" +
" \"traceContext\": \"123456\", \n" +
" \"logContent\" : \"some log content\" \n" +
" }], \n" +
" \"method\" : \"thing.log.post\"\n" +
"}";
MqttMessage message = new MqttMessage(payloadJson.getBytes("utf-8"));
message.setQos(1);
mqttClient.publish(pubTopic, message);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
3、控制台:设备本地日志查看