|
@@ -1,10 +1,8 @@
|
|
package org.jeecg.modules.mqtt.component;
|
|
package org.jeecg.modules.mqtt.component;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
-import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
|
|
|
-import org.eclipse.paho.client.mqttv3.MqttCallback;
|
|
|
|
-import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
|
|
-import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
|
|
|
+import org.eclipse.paho.client.mqttv3.*;
|
|
|
|
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
|
import org.jeecg.modules.iotedgeCollectData.entity.IotedgeCollectData;
|
|
import org.jeecg.modules.iotedgeCollectData.entity.IotedgeCollectData;
|
|
import org.jeecg.modules.iotedgeCollectData.service.IIotedgeCollectDataService;
|
|
import org.jeecg.modules.iotedgeCollectData.service.IIotedgeCollectDataService;
|
|
import org.jeecg.modules.iotedgeCollectData.service.impl.IotedgeCollectDataServiceImpl;
|
|
import org.jeecg.modules.iotedgeCollectData.service.impl.IotedgeCollectDataServiceImpl;
|
|
@@ -12,11 +10,13 @@ import org.jeecg.modules.mqtt.config.MqttConfig;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
/**
|
|
/**
|
|
* ClassName: PushCallback
|
|
* ClassName: PushCallback
|
|
@@ -27,22 +27,57 @@ import java.util.Map;
|
|
* @Create 2024/8/23 9:22
|
|
* @Create 2024/8/23 9:22
|
|
* @Version 1.0
|
|
* @Version 1.0
|
|
*/
|
|
*/
|
|
-@Component
|
|
|
|
|
|
+//@Component
|
|
public class PushCallback implements MqttCallback {
|
|
public class PushCallback implements MqttCallback {
|
|
private static final Logger logger = LoggerFactory.getLogger(MqttPushClient.class);
|
|
private static final Logger logger = LoggerFactory.getLogger(MqttPushClient.class);
|
|
|
|
|
|
- @Autowired
|
|
|
|
- private MqttConfig mqttConfig;
|
|
|
|
|
|
+// @Autowired
|
|
|
|
+// private MqttConfig mqttConfig;
|
|
|
|
|
|
- private static MqttClient client;
|
|
|
|
|
|
+ private MqttClient client;
|
|
|
|
+ private String host;
|
|
|
|
+ private String clientID;
|
|
|
|
+ private String username;
|
|
|
|
+ private String password;
|
|
|
|
+ private int timeout;
|
|
|
|
+ private int keepalive;
|
|
|
|
+ private String topic;
|
|
|
|
+
|
|
|
|
+// @Override
|
|
|
|
+// public void connectComplete(boolean b, String s) {
|
|
|
|
+// logger.info("丢失重连成功");
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ public PushCallback(){
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ public PushCallback(String host, String clientID, String username, String password, int timeout, int keepalive, String topic){
|
|
|
|
+ this.host = host;
|
|
|
|
+ this.clientID = clientID;
|
|
|
|
+ this.username = username;
|
|
|
|
+ this.password = password;
|
|
|
|
+ this.timeout = timeout;
|
|
|
|
+ this.keepalive = keepalive;
|
|
|
|
+ this.topic = topic;
|
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void connectionLost(Throwable throwable) {
|
|
public void connectionLost(Throwable throwable) {
|
|
// 连接丢失后,一般在这里面进行重连
|
|
// 连接丢失后,一般在这里面进行重连
|
|
logger.info("连接断开,可以做重连");
|
|
logger.info("连接断开,可以做重连");
|
|
- if (null != client) {
|
|
|
|
- mqttConfig.getMqttPushClient();
|
|
|
|
|
|
+ while (true){
|
|
|
|
+ try {
|
|
|
|
+ Thread.sleep(5000);
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ reconnectMqttClient();
|
|
|
|
+ if(this.client.isConnected()){
|
|
|
|
+ logger.info("重连成功");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -58,36 +93,67 @@ public class PushCallback implements MqttCallback {
|
|
logger.info("deliveryComplete---------" + iMqttDeliveryToken.isComplete());
|
|
logger.info("deliveryComplete---------" + iMqttDeliveryToken.isComplete());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public void reconnectMqttClient(){
|
|
|
|
+ String clientID1 = clientID + "_msg" + UUID.randomUUID();
|
|
|
|
+ logger.info("当前客户的mqttId是:" + clientID1);
|
|
|
|
+ try {
|
|
|
|
+ this.client = new MqttClient(host, clientID1, new MemoryPersistence());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+// e.printStackTrace();
|
|
|
|
+ logger.error(String.valueOf(e));
|
|
|
|
+ }
|
|
|
|
+ MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
+// options.setAutomaticReconnect(true);
|
|
|
|
+ options.setCleanSession(true);
|
|
|
|
+ options.setUserName(username);
|
|
|
|
+ options.setPassword(password.toCharArray());
|
|
|
|
+ options.setConnectionTimeout(timeout);
|
|
|
|
+ options.setKeepAliveInterval(keepalive);
|
|
|
|
+ this.client.setCallback(this);
|
|
|
|
+ try {
|
|
|
|
+ this.client.connect(options);
|
|
|
|
+ logger.info("重新订阅主题" + topic);
|
|
|
|
+ this.client.subscribe(topic);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+// e.printStackTrace();
|
|
|
|
+ logger.error(String.valueOf(e));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
private void saveDataInfo(String info) {
|
|
private void saveDataInfo(String info) {
|
|
- ArrayList<IotedgeCollectData> list = new ArrayList<>();
|
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(info);
|
|
|
|
- Map<String,Object> services = (Map<String, Object>) jsonObject.get("services");
|
|
|
|
- for (String key:services.keySet()) {
|
|
|
|
- Map<String,Object> tag = (Map<String,Object>) services.get(key);
|
|
|
|
- Map<String,Object> tagmap = (Map<String, Object>) tag.get("properties");
|
|
|
|
- for (String item:tagmap.keySet()) {
|
|
|
|
- Map<String,Object> valuemap = (Map<String, Object>) tagmap.get(item);
|
|
|
|
- String value1;
|
|
|
|
- if (valuemap.get("value") instanceof Integer){
|
|
|
|
- value1 = String.valueOf(valuemap.get("value"));
|
|
|
|
- }else {
|
|
|
|
- value1 = (String) valuemap.get("value");
|
|
|
|
- }
|
|
|
|
|
|
+ try {
|
|
|
|
+ ArrayList<IotedgeCollectData> list = new ArrayList<>();
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(info);
|
|
|
|
+ Map<String, Object> services = (Map<String, Object>) jsonObject.get("services");
|
|
|
|
+ for (String key : services.keySet()) {
|
|
|
|
+ Map<String, Object> tag = (Map<String, Object>) services.get(key);
|
|
|
|
+ Map<String, Object> tagmap = (Map<String, Object>) tag.get("properties");
|
|
|
|
+ for (String item : tagmap.keySet()) {
|
|
|
|
+ Map<String, Object> valuemap = (Map<String, Object>) tagmap.get(item);
|
|
|
|
+ String value1;
|
|
|
|
+ if (valuemap.get("value") instanceof Integer) {
|
|
|
|
+ value1 = String.valueOf(valuemap.get("value"));
|
|
|
|
+ } else {
|
|
|
|
+ value1 = (String) valuemap.get("value");
|
|
|
|
+ }
|
|
|
|
|
|
- IotedgeCollectData iotedgeCollectData = new IotedgeCollectData();
|
|
|
|
- iotedgeCollectData.setGroupid(jsonObject.get("g_id").toString());
|
|
|
|
- iotedgeCollectData.setPid(jsonObject.get("p_id").toString());
|
|
|
|
- iotedgeCollectData.setDevice(jsonObject.get("d_id").toString());
|
|
|
|
- iotedgeCollectData.setTime(jsonObject.get("ts").toString());
|
|
|
|
- iotedgeCollectData.setService(key);
|
|
|
|
- iotedgeCollectData.setProperty(item);
|
|
|
|
- iotedgeCollectData.setValue(value1);
|
|
|
|
|
|
+ IotedgeCollectData iotedgeCollectData = new IotedgeCollectData();
|
|
|
|
+ iotedgeCollectData.setGroupid(jsonObject.get("g_id").toString());
|
|
|
|
+ iotedgeCollectData.setPid(jsonObject.get("p_id").toString());
|
|
|
|
+ iotedgeCollectData.setDevice(jsonObject.get("d_id").toString());
|
|
|
|
+ iotedgeCollectData.setTime(jsonObject.get("ts").toString());
|
|
|
|
+ iotedgeCollectData.setService(key);
|
|
|
|
+ iotedgeCollectData.setProperty(item);
|
|
|
|
+ iotedgeCollectData.setValue(value1);
|
|
|
|
|
|
- list.add(iotedgeCollectData);
|
|
|
|
|
|
+ list.add(iotedgeCollectData);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ IIotedgeCollectDataService dataService = new IotedgeCollectDataServiceImpl();
|
|
|
|
+ dataService.saveBatch(list);
|
|
|
|
+ } catch (Exception e){
|
|
|
|
+ logger.error(String.valueOf(e));
|
|
}
|
|
}
|
|
- IIotedgeCollectDataService dataService = new IotedgeCollectDataServiceImpl();
|
|
|
|
- dataService.saveBatch(list);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|