zxl пре 4 недеља
родитељ
комит
44218f824b

+ 81 - 0
src/main/java/com/mes/config/HgLineProfiles.java

@@ -0,0 +1,81 @@
+package com.mes.config;
+
+import java.util.List;
+
+/**
+ * 弧焊产线 PLC IP 预设:配置 {@code mes.line-profile} 后自动套到五站。
+ * <ul>
+ *   <li>T13J:.51 / .56 / .61 / .66 / .71</li>
+ *   <li>T1E:.26 / .31 / .36 / .41 / .46</li>
+ *   <li>T1GC:.2 / .6 / .11 / .16 / .21</li>
+ * </ul>
+ */
+public final class HgLineProfiles {
+
+	/** 默认产线 */
+	public static final String DEFAULT = "T13J";
+
+	private static final String[] IPS_T13J = {
+			"192.168.31.51", "192.168.31.56", "192.168.31.61", "192.168.31.66", "192.168.31.71"
+	};
+	private static final String[] IPS_T1E = {
+			"192.168.31.26", "192.168.31.31", "192.168.31.36", "192.168.31.41", "192.168.31.46"
+	};
+	private static final String[] IPS_T1GC = {
+			"192.168.31.2", "192.168.31.6", "192.168.31.11", "192.168.31.16", "192.168.31.21"
+	};
+
+	private HgLineProfiles() {
+	}
+
+	/**
+	 * 规范化产线名;未知则回落 {@link #DEFAULT}。
+	 */
+	public static String normalize(String profile) {
+		if (profile == null) {
+			return DEFAULT;
+		}
+		String p = profile.trim().toUpperCase();
+		if ("T1E".equals(p) || "T1GC".equals(p) || "T13J".equals(p)) {
+			return p;
+		}
+		return DEFAULT;
+	}
+
+	/** 该产线 1#~5# 的 PLC IP(长度 5) */
+	public static String[] ipsOf(String profile) {
+		String p = normalize(profile);
+		if ("T1E".equals(p)) {
+			return IPS_T1E.clone();
+		}
+		if ("T1GC".equals(p)) {
+			return IPS_T1GC.clone();
+		}
+		return IPS_T13J.clone();
+	}
+
+	/**
+	 * 按工位 id(1~5)或列表顺序,把产线 IP 写进 stations.plcIp。
+	 *
+	 * @return 实际生效的产线名
+	 */
+	public static String applyToStations(String profile, List<MesClientProperties.StationConfig> stations) {
+		String p = normalize(profile);
+		if (stations == null || stations.isEmpty()) {
+			return p;
+		}
+		String[] ips = ipsOf(p);
+		for (MesClientProperties.StationConfig s : stations) {
+			if (s == null) {
+				continue;
+			}
+			int idx = s.getId() - 1;
+			if (idx < 0 || idx >= ips.length) {
+				// id 异常时按列表顺序兜底无意义,跳过
+				continue;
+			}
+			s.setPlcIp(ips[idx]);
+		}
+		return p;
+	}
+}

+ 15 - 2
src/main/java/com/mes/config/MesClientProperties.java

@@ -36,7 +36,12 @@ public class MesClientProperties {
 	 * 收到 SSE 入队推送后,空闲工位是否自动 next。
 	 * 收到 SSE 入队推送后,空闲工位是否自动 next。
 	 */
 	 */
 	private boolean autoNextOnPush = true;
 	private boolean autoNextOnPush = true;
-	/** 五工位配置(编码 + PLC) */
+	/**
+	 * 产线开关:T13J / T1E / T1GC。
+	 * 启动时按 {@link HgLineProfiles} 自动填充五站 plc-ip。
+	 */
+	private String lineProfile = HgLineProfiles.DEFAULT;
+	/** 五工位配置(编码 + Tab;plc-ip 由 line-profile 自动适配) */
 	private List<StationConfig> stations = new ArrayList<StationConfig>();
 	private List<StationConfig> stations = new ArrayList<StationConfig>();
 
 
 	/** 兼容旧字段,主流程不读 */
 	/** 兼容旧字段,主流程不读 */
@@ -54,7 +59,7 @@ public class MesClientProperties {
 		private int id;
 		private int id;
 		/** 柔性工位编码(入队/取码/过站,不分 A/B) */
 		/** 柔性工位编码(入队/取码/过站,不分 A/B) */
 		private String stationSn = "";
 		private String stationSn = "";
-		/** 该站 PLC IP(三菱) */
+		/** 该站 PLC IP(三菱);启动时由 line-profile 自动填充 */
 		private String plcIp = "";
 		private String plcIp = "";
 		/** Tab 显示名 */
 		/** Tab 显示名 */
 		private String title = "";
 		private String title = "";
@@ -156,6 +161,14 @@ public class MesClientProperties {
 		this.autoNextOnPush = autoNextOnPush;
 		this.autoNextOnPush = autoNextOnPush;
 	}
 	}
 
 
+	public String getLineProfile() {
+		return lineProfile;
+	}
+
+	public void setLineProfile(String lineProfile) {
+		this.lineProfile = lineProfile;
+	}
+
 	public List<StationConfig> getStations() {
 	public List<StationConfig> getStations() {
 		return stations;
 		return stations;
 	}
 	}

+ 4 - 0
src/main/java/com/mes/service/HgPlcClient.java

@@ -102,8 +102,12 @@ public class HgPlcClient {
 	/**
 	/**
 	 * 建立 MC UDP 通道;UDP 无长连接握手,用轻量读验证可达。
 	 * 建立 MC UDP 通道;UDP 无长连接握手,用轻量读验证可达。
 	 * 失败只记日志并指数退避,避免关机站把整条 tick 线程堵死。
 	 * 失败只记日志并指数退避,避免关机站把整条 tick 线程堵死。
+	 * 已连接直接返回(避免 initPlc 与 tick 各连一次)。
 	 */
 	 */
 	public synchronized void connect() {
 	public synchronized void connect() {
+		if (isConnected()) {
+			return;
+		}
 		if (plcIp.isEmpty()) {
 		if (plcIp.isEmpty()) {
 			connected = false;
 			connected = false;
 			log.warn("工位[{}] PLC IP 为空,跳过连接", stationSn);
 			log.warn("工位[{}] PLC IP 为空,跳过连接", stationSn);

+ 12 - 3
src/main/java/com/mes/ui/MainFrame.java

@@ -1,6 +1,7 @@
 package com.mes.ui;
 package com.mes.ui;
 
 
 import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
 import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
+import com.mes.config.HgLineProfiles;
 import com.mes.config.MesClientProperties;
 import com.mes.config.MesClientProperties;
 import com.mes.service.MesFlexQueuePushClient;
 import com.mes.service.MesFlexQueuePushClient;
 import com.mes.util.SingleInstanceLock;
 import com.mes.util.SingleInstanceLock;
@@ -111,9 +112,8 @@ public class MainFrame extends JFrame {
 					UiTheme.applyLookAndFeel();
 					UiTheme.applyLookAndFeel();
 					mesClientFrame = new MainFrame();
 					mesClientFrame = new MainFrame();
 					mesClientFrame.setVisible(true);
 					mesClientFrame.setVisible(true);
-					startTickTimer();
 					startPush();
 					startPush();
-					// PLC 在后台连:假 IP/离线时不卡 EDT
+					// 先串行连 PLC,再开 tick,避免与 maintainConnection 抢连、重复打「已连接」日志
 					new Thread(new Runnable() {
 					new Thread(new Runnable() {
 						@Override
 						@Override
 						public void run() {
 						public void run() {
@@ -124,6 +124,7 @@ public class MainFrame extends JFrame {
 									log.warn("PLC 初始化异常: {}", ex.getMessage());
 									log.warn("PLC 初始化异常: {}", ex.getMessage());
 								}
 								}
 							}
 							}
+							startTickTimer();
 						}
 						}
 					}, "hg-plc-init").start();
 					}, "hg-plc-init").start();
 				} catch (Exception e) {
 				} catch (Exception e) {
@@ -165,14 +166,22 @@ public class MainFrame extends JFrame {
 		user20 = mes_user_code;
 		user20 = mes_user_code;
 		mes_auto_next_on_push = p.isAutoNextOnPush();
 		mes_auto_next_on_push = p.isAutoNextOnPush();
 		List<MesClientProperties.StationConfig> list = p.getStations();
 		List<MesClientProperties.StationConfig> list = p.getStations();
+		// 产线开关:按 T13J / T1E / T1GC 自动套五站 PLC IP
+		String line = HgLineProfiles.applyToStations(p.getLineProfile(), list);
+		p.setLineProfile(line);
 		if (list == null || list.isEmpty()) {
 		if (list == null || list.isEmpty()) {
 			log.warn("mes.stations 为空,请在 application.yml 配置五工位");
 			log.warn("mes.stations 为空,请在 application.yml 配置五工位");
 		} else {
 		} else {
-			log.info("加载工位数={} upload={} profile={}", list.size(), mes_upload_path, mes_param_profile);
+			log.info("加载工位数={} 产线={} upload={} profile={}",
+					list.size(), line, mes_upload_path, mes_param_profile);
 			for (MesClientProperties.StationConfig s : list) {
 			for (MesClientProperties.StationConfig s : list) {
 				log.info("工位 id={} sn={} plc={}", s.getId(), s.getStationSn(), s.getPlcIp());
 				log.info("工位 id={} sn={} plc={}", s.getId(), s.getStationSn(), s.getPlcIp());
 			}
 			}
 		}
 		}
+		// 标题带上产线,现场一眼能看出来
+		if (mes_app_title.indexOf(line) < 0) {
+			mes_app_title = mes_app_title + " [" + line + "]";
+		}
 	}
 	}
 
 
 	public MainFrame() {
 	public MainFrame() {

+ 5 - 5
src/main/resources/config/application.yml

@@ -22,24 +22,24 @@ mes:
   device-type: 弧焊
   device-type: 弧焊
   device-heartbeat-minutes: 5
   device-heartbeat-minutes: 5
   auto-next-on-push: true
   auto-next-on-push: true
+  # 产线(改这一处即可,五站 PLC IP 自动适配):
+  #   T13J → 192.168.31.51 / .56 / .61 / .66 / .71
+  #   T1E  → 192.168.31.26 / .31 / .36 / .41 / .46
+  #   T1GC → 192.168.31.2  / .6  / .11 / .16 / .21
+  line-profile: T13J
   stations:
   stations:
     - id: 1
     - id: 1
       title: 弧焊1#
       title: 弧焊1#
       station-sn: HG01
       station-sn: HG01
-      plc-ip: 192.168.31.51
     - id: 2
     - id: 2
       title: 弧焊2#
       title: 弧焊2#
       station-sn: HG02
       station-sn: HG02
-      plc-ip: 192.168.31.56
     - id: 3
     - id: 3
       title: 弧焊3#
       title: 弧焊3#
       station-sn: HG03
       station-sn: HG03
-      plc-ip: 192.168.31.61
     - id: 4
     - id: 4
       title: 弧焊4#
       title: 弧焊4#
       station-sn: HG04
       station-sn: HG04
-      plc-ip: 192.168.31.66
     - id: 5
     - id: 5
       title: 弧焊5#
       title: 弧焊5#
       station-sn: HG05
       station-sn: HG05
-      plc-ip: 192.168.31.71