Browse Source

iot改成可以配多个地址

menglu 3 years ago
parent
commit
9e88b805db

+ 11 - 9
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/service/DaemonThread.java

@@ -202,22 +202,24 @@ public class DaemonThread extends Thread {
 				}
 
 				try {
-					if (Constant.iot_websocket_url != null) {
-						log.warn("********************************" + "\t" + "iot-websocket");
-						IOTWebSocketClient.client = new IOTWebSocketClient(new URI(Constant.iot_websocket_url + "?projectId="
-								+ RepositoryContainer.RepositoryProject.projectId.substring(2) + "&type=iot,text,pointset"));
-						IOTWebSocketClient.client.connect();
+					for (String url : Constant.iot_websocket_url) {
+						String serviceUrl = url + "?projectId=" + RepositoryContainer.RepositoryProject.projectId.substring(2)
+								+ "&type=iot,text,pointset";
+						log.warn("********************************" + "\t" + "iot-websocket" + "\n" + serviceUrl);
+						IOTWebSocketClient client = new IOTWebSocketClient(new URI(serviceUrl));
+						client.connect();
 						// while (!myClient.getReadyState().equals(READYSTATE.OPEN)) {
 						// Thread.sleep(1000L);
 						// log.info("连接中···请稍后");
 						// }
 						// log.info("连接成功");
-						Thread.sleep(1000L);
+						IOTWebSocketClient.clientList.add(client);
 					}
+					Thread.sleep(1000L);
 					if (Constant.alarm_enable) {
-						log.warn("********************************" + "\t" + "alarm-websocket");
-						AlarmWebSocketClient.client = new AlarmWebSocketClient(
-								new URI(Constant.alarm_websocket_url + "/" + RepositoryContainer.RepositoryProject.projectId));
+						String serviceUrl = Constant.alarm_websocket_url + "/" + RepositoryContainer.RepositoryProject.projectId;
+						log.warn("********************************" + "\t" + "alarm-websocket" + "\n" + serviceUrl);
+						AlarmWebSocketClient.client = new AlarmWebSocketClient(new URI(serviceUrl));
 						AlarmWebSocketClient.client.connect();
 						// while (!myClient.getReadyState().equals(READYSTATE.OPEN)) {
 						// Thread.sleep(1000L);

+ 7 - 12
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/service/WebSocketCheckThread.java

@@ -31,13 +31,9 @@ public class WebSocketCheckThread extends Thread {
 	 */
 	@Scheduled(initialDelay = 60000, fixedDelay = 5000)
 	public void holderConneted() {
-		if (iot_websocket_reconnect) {
+		for (IOTWebSocketClient client : IOTWebSocketClient.clientList) {
 			try {
-				WebSocketClient outCallWebSocketClientHolder = IOTWebSocketClient.client;
-				if (null == outCallWebSocketClientHolder) {
-					log.debug("IOT当前连接还未建立");
-					return;
-				}
+				WebSocketClient outCallWebSocketClientHolder = client;
 				if (!outCallWebSocketClientHolder.isOpen()) {
 					log.error("IOT连接已经断开,正在重新连接,当前状态为[{}]", outCallWebSocketClientHolder.getReadyState());
 					if (outCallWebSocketClientHolder.getReadyState().equals(WebSocket.READYSTATE.CLOSING)
@@ -50,16 +46,15 @@ public class WebSocketCheckThread extends Thread {
 				log.error("IOT连接发生异常,尝试重新连接", e);
 				// 重连异常后删除旧的连接并重新连接
 				try {
-					if (!IOTWebSocketClient.client.isOpen()) {
-						IOTWebSocketClient.client.close();
+					if (!client.isOpen()) {
+						client.close();
 					}
 				} catch (Exception e2) {
 					log.error(e2.getMessage(), e2);
 				}
 				try {
-					IOTWebSocketClient.client = new IOTWebSocketClient(new URI(Constant.iot_websocket_url + "?projectId="
-							+ RepositoryContainer.RepositoryProject.projectId.substring(2) + "&type=iot,text,pointset"));
-					IOTWebSocketClient.client.connect();
+					// IOTWebSocketClient.client = new IOTWebSocketClient(serverUri);
+					client.connect();
 				} catch (Exception e2) {
 					log.error(e2.getMessage(), e2);
 				}
@@ -77,7 +72,7 @@ public class WebSocketCheckThread extends Thread {
 					log.error("Alarm连接已经断开,正在重新连接,当前状态为[{}]", outCallWebSocketClientHolder.getReadyState());
 					if (outCallWebSocketClientHolder.getReadyState().equals(WebSocket.READYSTATE.CLOSING)
 							|| outCallWebSocketClientHolder.getReadyState().equals(WebSocket.READYSTATE.CLOSED)
-							||outCallWebSocketClientHolder.getReadyState().equals(WebSocket.READYSTATE.NOT_YET_CONNECTED)) {
+							|| outCallWebSocketClientHolder.getReadyState().equals(WebSocket.READYSTATE.NOT_YET_CONNECTED)) {
 						outCallWebSocketClientHolder.reconnect();
 					}
 				}

+ 3 - 1
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/service/rest/BaseApi.java

@@ -113,7 +113,9 @@ public class BaseApi {
 
 	@GetMapping(path = "/iot_websocket_close")
 	public String iot_websocket_close() {
-		IOTWebSocketClient.client.close();
+		for (IOTWebSocketClient client : IOTWebSocketClient.clientList) {
+			client.close();
+		}
 		return "iot_websocket_close";
 	}
 

+ 6 - 4
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/util/Constant.java

@@ -4,6 +4,7 @@ import java.io.File;
 import java.io.InputStream;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
@@ -55,7 +56,7 @@ public class Constant {
 	public static String info_point;
 	public static boolean info_point_enable;
 	public static String info_point_version;
-	public static String iot_websocket_url;
+	public static List<String> iot_websocket_url = new ArrayList<String>();
 	public static String iot_collect_url;
 	public static String iot_project_url;
 	public static boolean alarm_enable;
@@ -247,10 +248,11 @@ public class Constant {
 				}
 
 				iter = document.selectNodes("/root/iot_websocket").iterator();
-				if (iter.hasNext()) {
+				while (iter.hasNext()) {
 					element = (Element) iter.next();
-					iot_websocket_url = element.attribute("url").getValue();
-					iot_websocket_url = StrUtil.removeSuffix(iot_websocket_url, "/");
+					String url = element.attribute("url").getValue();
+					url = StrUtil.removeSuffix(url, "/");
+					iot_websocket_url.add(url);
 				}
 				iter = document.selectNodes("/root/iot_collect").iterator();
 				if (iter.hasNext()) {

+ 1 - 1
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/websocket/AlarmWebSocketClient.java

@@ -18,7 +18,7 @@ import lombok.extern.slf4j.Slf4j;
 public class AlarmWebSocketClient extends WebSocketClient {
 	public static AlarmWebSocketClient client;
 
-	URI serverUri;
+	public URI serverUri;
 
 	public AlarmWebSocketClient(URI serverUri) {
 		super(serverUri);

+ 4 - 2
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/websocket/IOTWebSocketClient.java

@@ -2,7 +2,9 @@ package com.persagy.ibms.data.sdk.websocket;
 
 import java.net.URI;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.List;
 
 import org.java_websocket.client.WebSocketClient;
 import org.java_websocket.handshake.ServerHandshake;
@@ -22,9 +24,9 @@ import lombok.extern.slf4j.Slf4j;
 
 @Slf4j
 public class IOTWebSocketClient extends WebSocketClient {
-	public static IOTWebSocketClient client;
+	public static List<IOTWebSocketClient> clientList = new ArrayList<IOTWebSocketClient>();
 
-	URI serverUri;
+	public URI serverUri;
 
 	public IOTWebSocketClient(URI serverUri) {
 		super(serverUri);