|
|
|
@ -0,0 +1,83 @@
|
|
|
|
|
package com.ruoyi.dataprocess.mqtt.client.listener;
|
|
|
|
|
|
|
|
|
|
import com.ruoyi.dataprocess.mqtt.client.config.MqttConfiguration;
|
|
|
|
|
import com.ruoyi.dataprocess.service.IDataProcessService;
|
|
|
|
|
import com.ruoyi.dataprocess.service.IDeviceStatusService;
|
|
|
|
|
import net.dreamlu.iot.mqtt.codec.ByteBufferUtil;
|
|
|
|
|
import net.dreamlu.iot.mqtt.codec.MqttQoS;
|
|
|
|
|
import net.dreamlu.iot.mqtt.spring.client.MqttClientSubscribe;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 客户端消息监听
|
|
|
|
|
*
|
|
|
|
|
* @author L.cm
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
|
|
|
|
public class MqttClientSubscribeListener {
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(MqttClientSubscribeListener.class);
|
|
|
|
|
|
|
|
|
|
public static final String TOPIC_TYPE_DATA_POSTFIX = "data";
|
|
|
|
|
public static final String TOPIC_TYPE_COMMAND_POSTFIX = "command";
|
|
|
|
|
public static final String TOPIC_TYPE_REPLY_POSTFIX = "reply";
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IDataProcessService dataProcessService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IDeviceStatusService deviceStatusService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private MqttConfiguration mqttConfiguration;
|
|
|
|
|
|
|
|
|
|
@MqttClientSubscribe(value = "/v1/#", qos = MqttQoS.AT_MOST_ONCE)
|
|
|
|
|
public void subQos0(String topic, ByteBuffer payload) {
|
|
|
|
|
String payloadString = new String(payload.array(), StandardCharsets.UTF_8);
|
|
|
|
|
Long start = System.currentTimeMillis();
|
|
|
|
|
String imagePath = mqttConfiguration.getImagePath();
|
|
|
|
|
String imagePatterns = mqttConfiguration.getImagePatterns();
|
|
|
|
|
String imageDomain = mqttConfiguration.getImageDomain();
|
|
|
|
|
String imagePrefix = mqttConfiguration.getImagePrefix();
|
|
|
|
|
logger.info("topic:{},start:{}ms", topic, start);
|
|
|
|
|
try {
|
|
|
|
|
if (topic.endsWith(TOPIC_TYPE_DATA_POSTFIX)) {
|
|
|
|
|
int processDataCount = dataProcessService.processBusinessData(payloadString,
|
|
|
|
|
imagePath, imagePatterns, imageDomain, imagePrefix);
|
|
|
|
|
Long end = System.currentTimeMillis();
|
|
|
|
|
logger.info("Process Data start:{}ms,end:{}ms,Spend Time:{}ms,Data Count:{}", start, end, end - start, processDataCount);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.error("Error processing business data,topic:{},start:{},error:{}", topic, start, e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@MqttClientSubscribe(value = "/device/status/v1", qos = MqttQoS.AT_MOST_ONCE)
|
|
|
|
|
public void subQos1(String topic, ByteBuffer payload) {
|
|
|
|
|
String payloadString = new String(payload.array(), StandardCharsets.UTF_8);
|
|
|
|
|
Long start = System.currentTimeMillis();
|
|
|
|
|
String clientId = mqttConfiguration.getClientId();
|
|
|
|
|
logger.info("topic:{},start:{}ms", topic, start);
|
|
|
|
|
try {
|
|
|
|
|
deviceStatusService.handleDeviceStatus(payloadString, clientId);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.error("Error processing business data,topic:{},start:{},error:{}", topic, start, e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
// @MqttClientSubscribe("/sys/${productKey}/${deviceName}/thing/sub/register")
|
|
|
|
|
// public void thingSubRegister(String topic, byte[] payload) {
|
|
|
|
|
// // 1.3.8 开始支持,@MqttClientSubscribe 注解支持 ${} 变量替换,会默认替换成 +
|
|
|
|
|
// // 注意:mica-mqtt 会先从 Spring boot 配置中替换参数 ${},如果存在配置会优先被替换。
|
|
|
|
|
// logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|