|
|
@@ -1,6 +1,7 @@
|
|
|
package com.ruoyi.data.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.json.JSONArray;
|
|
|
import cn.hutool.core.lang.UUID;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
@@ -25,6 +26,7 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttCallback;
|
|
|
+import org.eclipse.paho.client.mqttv3.MqttException;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -70,40 +72,43 @@ public class MqttServiceImpl implements MqttService {
|
|
|
|
|
|
@Value("${mqtt.topic}")
|
|
|
private String mqttTopic;
|
|
|
-// @Value("${mqtt.url}")
|
|
|
-// private String mqttUrl;
|
|
|
+
|
|
|
+ @Value("${mqtt.saveAndForward}")
|
|
|
+ private Boolean saveAndForward;
|
|
|
|
|
|
@Override
|
|
|
public void pubMqttData(String mqttStr) {
|
|
|
- JSONObject jsonObject = new JSONObject(mqttStr);
|
|
|
- Long sensorId = Long.valueOf((String) jsonObject.get("sensorId"));
|
|
|
- TblSensor tblSensor = tblSensorMapper.selectById(sensorId);
|
|
|
- Long deviceId = tblSensor.getDeviceId();
|
|
|
- SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
- Date date = new Date(System.currentTimeMillis());
|
|
|
- JSONObject mqttMsg = new JSONObject();
|
|
|
- mqttMsg.put("deviceSn",deviceId);
|
|
|
- mqttMsg.put("sensorSn",sensorId);
|
|
|
- mqttMsg.put("sensorType",tblSensor.getSensorType());
|
|
|
- mqttMsg.put("created_time",formatter.format(date));
|
|
|
- JSONArray sensorData = new JSONArray();
|
|
|
- JSONArray dataArry = new JSONArray(jsonObject.get("data"));
|
|
|
- for(Object obj: dataArry){
|
|
|
- JSONObject dataObj = new JSONObject(obj);
|
|
|
- dataObj.remove("gcdinfo");
|
|
|
- sensorData.add(dataObj);
|
|
|
- }
|
|
|
- mqttMsg.put("sensorData",sensorData);
|
|
|
- MqttObj mqttObj = new MqttObj();
|
|
|
- mqttObj.setEquipmentId(deviceId);
|
|
|
- List<MqttObj> mqttObjList = tblEquipmentMqttMapper.selectMqttListByDeviceId(mqttObj);
|
|
|
- TblRecord tblRecord = new TblRecord();
|
|
|
- tblRecord.setEquipmentId(deviceId);
|
|
|
- tblRecord.setJson(mqttStr);
|
|
|
- tblRecord.setCreateBy("admin");
|
|
|
- tblRecord.setUpdateBy("admin");
|
|
|
- tblRecord.setSensorId(sensorId);
|
|
|
- tblRecordMapper.insert(tblRecord);
|
|
|
+ if(saveAndForward) {
|
|
|
+
|
|
|
+ JSONObject jsonObject = new JSONObject(mqttStr);
|
|
|
+ Long sensorId = Long.valueOf((String) jsonObject.get("sensorId"));
|
|
|
+ TblSensor tblSensor = tblSensorMapper.selectById(sensorId);
|
|
|
+ Long deviceId = tblSensor.getDeviceId();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date date = new Date(System.currentTimeMillis());
|
|
|
+ JSONObject mqttMsg = new JSONObject();
|
|
|
+ mqttMsg.put("deviceSn", deviceId);
|
|
|
+ mqttMsg.put("sensorSn", sensorId);
|
|
|
+ mqttMsg.put("sensorType", tblSensor.getSensorType());
|
|
|
+ mqttMsg.put("created_time", formatter.format(date));
|
|
|
+ JSONArray sensorData = new JSONArray();
|
|
|
+ JSONArray dataArry = new JSONArray(jsonObject.get("data"));
|
|
|
+ for (Object obj : dataArry) {
|
|
|
+ JSONObject dataObj = new JSONObject(obj);
|
|
|
+ dataObj.remove("gcdinfo");
|
|
|
+ sensorData.add(dataObj);
|
|
|
+ }
|
|
|
+ mqttMsg.put("sensorData", sensorData);
|
|
|
+ MqttObj mqttObj = new MqttObj();
|
|
|
+ mqttObj.setEquipmentId(deviceId);
|
|
|
+ List<MqttObj> mqttObjList = tblEquipmentMqttMapper.selectMqttListByDeviceId(mqttObj);
|
|
|
+ TblRecord tblRecord = new TblRecord();
|
|
|
+ tblRecord.setEquipmentId(deviceId);
|
|
|
+ tblRecord.setJson(mqttStr);
|
|
|
+ tblRecord.setCreateBy("admin");
|
|
|
+ tblRecord.setUpdateBy("admin");
|
|
|
+ tblRecord.setSensorId(sensorId);
|
|
|
+ tblRecordMapper.insert(tblRecord);
|
|
|
// TblSensorRecordBo tblSensorRecord = new TblSensorRecordBo();
|
|
|
// tblSensorRecord.setEquipmentId(deviceId);
|
|
|
// tblSensorRecord.setJson(mqttStr);
|
|
|
@@ -123,13 +128,23 @@ public class MqttServiceImpl implements MqttService {
|
|
|
}
|
|
|
for(MqttObj obj:mqttObjList){
|
|
|
if(obj.getStatus() == 1) {
|
|
|
- JSONObject topicObj = obj.getTopicQos(protocolType);
|
|
|
+ JSONArray topicObj1 = JSONUtil.parseArray(obj.getServerTopic());
|
|
|
MQTTConnect mqttConnect = new MQTTConnect();
|
|
|
try {
|
|
|
mqttConnect.createMqttClient(obj.getServerAddress(), obj.getUuid(), obj.getAccount(), obj.getPassword(), new Callback());
|
|
|
- if (topicObj != null) {
|
|
|
- String topic = topicObj.get("name").toString().replace("#", "");
|
|
|
- mqttConnect.pub(topic, mqttMsg.toString(), Integer.valueOf((String) topicObj.get("qos")));
|
|
|
+ if (topicObj1.size() >0) {
|
|
|
+ String finalProtocolType = protocolType;
|
|
|
+ topicObj1.forEach(i->{
|
|
|
+ JSONObject p = (JSONObject)i;
|
|
|
+ String topic = p.getStr("name");
|
|
|
+ topic = StrUtil.replace(topic,"$protocolType$", finalProtocolType);
|
|
|
+ topic = StrUtil.replace(topic,"$sensorId$", sensorId.toString());
|
|
|
+ try {
|
|
|
+ mqttConnect.pub(topic, mqttMsg.toString(), (Integer) p.get("qos"));
|
|
|
+ } catch (MqttException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
} else {
|
|
|
String topic = "sensor/"+protocolType+"/" + sensorId;
|
|
|
mqttConnect.pub(topic, mqttMsg.toString(), 0);
|
|
|
@@ -139,6 +154,7 @@ public class MqttServiceImpl implements MqttService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|