wangxichen 1 месяц назад
Родитель
Сommit
11f48a817e

+ 44 - 14
src/com/mes/component/WorkJPanelOP10.java

@@ -279,6 +279,7 @@ public class WorkJPanelOP10 extends JPanel {
         this.add(pnCombo);
 
         startOprnoTimer();
+        startLightTimer();
         heartData();
 
         // 信号监控按钮
@@ -336,9 +337,13 @@ public class WorkJPanelOP10 extends JPanel {
         cjTimerText2 = new Timer();
         cjTimerText2.schedule(new TimerTask() {
             public void run() {
-                if(MesClient.isLogin == 1 && isLocal == 0){
-                    plcHeart = !plcHeart;
-                    writeBoolean(plcDb+".0.4",plcHeart);
+                try{
+                    if(MesClient.isLogin == 1 && isLocal == 0){
+                        plcHeart = !plcHeart;
+                        writeBoolean(plcDb+".0.4",plcHeart);
+                    }
+                }catch (Throwable t){
+                    log.error("<"+oprno+pos+">心跳异常", t);
                 }
             }
         }, 1000,1000);
@@ -356,20 +361,12 @@ public class WorkJPanelOP10 extends JPanel {
             public void run() {
                 try{
                     if(MesClient.isLogin == 1){
+                        // 信号灯由 lightTimer 单独刷新,这里只做业务判断
                         Boolean retFeeding = readFeeding();
-                        feedinglabel.setIcon(retFeeding?imageGreen:imageGray);
-
-                        Boolean retFeedingAllow = readAllowFeeding();
-                        feedingmeslabel.setIcon(retFeedingAllow?imageGreen:imageGray);
 
                         Boolean retRunning = readRunning();
-                        runlabel.setIcon(retRunning?imageGreen:imageGray);
 
                         Boolean retBlanking = readBlanking();
-                        blankinglabel.setIcon(retBlanking?imageGreen:imageGray);
-
-                        Boolean retAllowBlanking = readAllowBlanking();
-                        blankingmeslabelOP10.setIcon(retAllowBlanking?imageGreen:imageGray);
 
                         if(!retBlanking){
                             writeAllowBlanking(false);
@@ -404,13 +401,46 @@ public class WorkJPanelOP10 extends JPanel {
                         }
                     }
 
-                }catch (Exception e) {
-                    log.error(e.getMessage());
+                }catch (Throwable t) {
+                    // 捕获 Throwable,防止 Error 导致 Timer 线程退出
+                    log.error("<"+oprno+pos+">轮询异常", t);
                 }
             }
         }, 100,1000);
     }
 
+    // 信号灯单独刷新,与业务轮询解耦,业务卡住时灯仍实时
+    public Timer lightTimer;
+    public void startLightTimer() {
+        if(lightTimer!=null) {
+            lightTimer.cancel();
+        }
+        lightTimer = new Timer();
+        lightTimer.schedule(new TimerTask() {
+            public void run() {
+                try{
+                    if(MesClient.isLogin != 1 || isLocal == 1){
+                        return;
+                    }
+                    boolean feeding = s7PLC.readBoolean(plcDb+".0.0");
+                    boolean feedingAllow = s7PLC.readBoolean(plcDb+".0.3");
+                    boolean running = s7PLC.readBoolean(plcDb+".0.1");
+                    boolean blanking = s7PLC.readBoolean(plcDb+".38.3");
+                    boolean allowBlanking = s7PLC.readBoolean(plcDb+".0.5");
+                    SwingUtilities.invokeLater(() -> {
+                        feedinglabel.setIcon(feeding?imageGreen:imageGray);
+                        feedingmeslabel.setIcon(feedingAllow?imageGreen:imageGray);
+                        runlabel.setIcon(running?imageGreen:imageGray);
+                        blankinglabel.setIcon(blanking?imageGreen:imageGray);
+                        blankingmeslabelOP10.setIcon(allowBlanking?imageGreen:imageGray);
+                    });
+                }catch (Throwable t) {
+                    log.error("<"+oprno+pos+">信号灯刷新异常: "+t.getMessage());
+                }
+            }
+        }, 500,1000);
+    }
+
     // 下料处理:存码给 OP040、写下料反馈、上报质量数据(tjFlag=1 或 2 收到 38.3 时调用)
     private void doBlanking(){
         MesClient.newSn = productSn.getText();

+ 46 - 14
src/com/mes/component/WorkJPanelOP20.java

@@ -329,6 +329,7 @@ public class WorkJPanelOP20 extends JPanel {
 
         startOprnoTimer();
 
+        startLightTimer();
 
         monitorDeviceStatus();
 
@@ -387,9 +388,13 @@ public class WorkJPanelOP20 extends JPanel {
         cjTimerText2 = new Timer();
         cjTimerText2.schedule(new TimerTask() {
             public void run() {
-                if(MesClient.isLogin == 1 && isLocal == 0){
-                    plcHeart = !plcHeart;
-                    writeBoolean(plcDb+".0.6",plcHeart);
+                try{
+                    if(MesClient.isLogin == 1 && isLocal == 0){
+                        plcHeart = !plcHeart;
+                        writeBoolean(plcDb+".0.6",plcHeart);
+                    }
+                }catch (Throwable t){
+                    log.error("<"+oprno+pos+">心跳异常", t);
                 }
             }
         }, 1000,1000);
@@ -426,8 +431,8 @@ public class WorkJPanelOP20 extends JPanel {
                             errflag2 = 1;
                             DataUtil.deviceAlarm(oprno+pos,productSn.getText(),content,MesClient.user_menu.getText());
                         }
-                    }catch (Exception e){
-
+                    }catch (Throwable t){
+                        log.error("<"+oprno+pos+">报警监控异常", t);
                     }
                 }
             }
@@ -447,24 +452,18 @@ public class WorkJPanelOP20 extends JPanel {
                 try{
 
                     if(MesClient.isLogin == 1){
+                        // 信号灯由 lightTimer 单独刷新,这里只做业务判断
                         Boolean retFeeding = readBoolean(plcDb+".0.0");
-                        feedinglabel.setIcon(retFeeding?imageGreen:imageGray);
 
                         Boolean retFeedingAllow = readBoolean(plcDb+".0.4");
-                        feedingmeslabel.setIcon(retFeedingAllow?imageGreen:imageGray);
 
                         if(!retFeeding && retFeedingAllow){
                             writeAllowFeeding(false);
                         }
 
                         Boolean retBlanking = readBoolean(plcDb+".0.1");
-                        blankinglabel.setIcon(retBlanking?imageGreen:imageGray);
-
-                        Boolean retAllowBlanking = readBoolean(plcDb+".0.5");
-                        blankingmeslabel.setIcon(retAllowBlanking?imageGreen:imageGray);
 
                         Boolean retRunning = readBoolean(plcDb+".0.2");
-                        runlabel.setIcon(retRunning?imageGreen:imageGray);
 
                         if(retBlanking && productSn.getText().isEmpty()){
                             writeAllowBlanking(true);
@@ -527,13 +526,46 @@ public class WorkJPanelOP20 extends JPanel {
 
 
 
-                }catch (Exception e) {
-                    log.error(e.getMessage());
+                }catch (Throwable t) {
+                    // 捕获 Throwable,防止 Error 导致 Timer 线程退出
+                    log.error("<"+oprno+pos+">轮询异常", t);
                 }
             }
         }, 100,1500);
     }
 
+    // 信号灯单独刷新,与业务轮询解耦,业务卡住时灯仍实时
+    public Timer lightTimer;
+    public void startLightTimer() {
+        if(lightTimer!=null) {
+            lightTimer.cancel();
+        }
+        lightTimer = new Timer();
+        lightTimer.schedule(new TimerTask() {
+            public void run() {
+                try{
+                    if(MesClient.isLogin != 1 || isLocal == 1){
+                        return;
+                    }
+                    boolean feeding = s7PLC.readBoolean(plcDb+".0.0");
+                    boolean feedingAllow = s7PLC.readBoolean(plcDb+".0.4");
+                    boolean running = s7PLC.readBoolean(plcDb+".0.2");
+                    boolean blanking = s7PLC.readBoolean(plcDb+".0.1");
+                    boolean allowBlanking = s7PLC.readBoolean(plcDb+".0.5");
+                    SwingUtilities.invokeLater(() -> {
+                        feedinglabel.setIcon(feeding?imageGreen:imageGray);
+                        feedingmeslabel.setIcon(feedingAllow?imageGreen:imageGray);
+                        runlabel.setIcon(running?imageGreen:imageGray);
+                        blankinglabel.setIcon(blanking?imageGreen:imageGray);
+                        blankingmeslabel.setIcon(allowBlanking?imageGreen:imageGray);
+                    });
+                }catch (Throwable t) {
+                    log.error("<"+oprno+pos+">信号灯刷新异常: "+t.getMessage());
+                }
+            }
+        }, 500,1000);
+    }
+
     public void barCode2(){
         String sn = "";
         if(isScan == 1){

+ 3 - 2
src/com/mes/component/WorkJPanelOP25.java

@@ -390,8 +390,9 @@ public class WorkJPanelOP25 extends JPanel {
                             lastFaceState = currentFace;
                         }
                     }
-                } catch (Exception e) {
-                    log.error("监控异常: " + e.getMessage());
+                } catch (Throwable t) {
+                    // 捕获 Throwable,防止 Error 导致 Timer 线程退出
+                    log.error("监控异常", t);
                 }
             }
         }, 1000, 1500);

+ 46 - 14
src/com/mes/component/WorkJPanelOP30.java

@@ -316,6 +316,7 @@ public class WorkJPanelOP30 extends JPanel {
         });
 
         startOprnoTimer();
+        startLightTimer();
         heartData();
 
         monitorDeviceStatus();
@@ -380,9 +381,13 @@ public class WorkJPanelOP30 extends JPanel {
         cjTimerText2 = new Timer();
         cjTimerText2.schedule(new TimerTask() {
             public void run() {
-                if(MesClient.isLogin == 1 && isLocal == 0){
-                    plcHeart = !plcHeart;
-                    writeBoolean(plcDb+".0.6",plcHeart);
+                try{
+                    if(MesClient.isLogin == 1 && isLocal == 0){
+                        plcHeart = !plcHeart;
+                        writeBoolean(plcDb+".0.6",plcHeart);
+                    }
+                }catch (Throwable t){
+                    log.error("<"+oprno+pos+">心跳异常", t);
                 }
             }
         }, 1000,1000);
@@ -418,8 +423,8 @@ public class WorkJPanelOP30 extends JPanel {
                             errflag2 = 1;
                             DataUtil.deviceAlarm(oprno+pos,productSn.getText(),content,MesClient.user_menu.getText());
                         }
-                    }catch (Exception e){
-
+                    }catch (Throwable t){
+                        log.error("<"+oprno+pos+">报警监控异常", t);
                     }
                 }
             }
@@ -439,24 +444,18 @@ public class WorkJPanelOP30 extends JPanel {
                 try{
 
                     if(MesClient.isLogin == 1){
+                        // 信号灯由 lightTimer 单独刷新,这里只做业务判断
                         Boolean retFeeding = readFeeding();
-                        feedinglabel.setIcon(retFeeding?imageGreen:imageGray);
 
                         Boolean retFeedingAllow = readAllowFeeding();
-                        feedingmeslabel.setIcon(retFeedingAllow?imageGreen:imageGray);
 
                         if(!retFeeding && retFeedingAllow){
                             writeAllowFeeding(false);
                         }
 
                         Boolean retBlanking = readBlanking();
-                        blankinglabel.setIcon(retBlanking?imageGreen:imageGray);
-
-                        Boolean retAllowBlanking = readAllowBlanking();
-                        blankingmeslabel.setIcon(retAllowBlanking?imageGreen:imageGray);
 
                         Boolean retRunning = readRunning();
-                        runlabel.setIcon(retRunning?imageGreen:imageGray);
 
                         if(retBlanking && productSn.getText().isEmpty()){
                             writeAllowBlanking(true);
@@ -521,13 +520,46 @@ public class WorkJPanelOP30 extends JPanel {
 
 
 
-                }catch (Exception e) {
-                    log.error(e.getMessage());
+                }catch (Throwable t) {
+                    // 捕获 Throwable,防止 Error 导致 Timer 线程退出
+                    log.error("<"+oprno+pos+">轮询异常", t);
                 }
             }
         }, 100,1500);
     }
 
+    // 信号灯单独刷新,与业务轮询解耦,业务卡住时灯仍实时
+    public Timer lightTimer;
+    public void startLightTimer() {
+        if(lightTimer!=null) {
+            lightTimer.cancel();
+        }
+        lightTimer = new Timer();
+        lightTimer.schedule(new TimerTask() {
+            public void run() {
+                try{
+                    if(MesClient.isLogin != 1 || isLocal == 1){
+                        return;
+                    }
+                    boolean feeding = s7PLC.readBoolean(plcDb+".0.0");
+                    boolean feedingAllow = s7PLC.readBoolean(plcDb+".0.4");
+                    boolean running = s7PLC.readBoolean(plcDb+".0.2");
+                    boolean blanking = s7PLC.readBoolean(plcDb+".0.1");
+                    boolean allowBlanking = s7PLC.readBoolean(plcDb+".0.5");
+                    SwingUtilities.invokeLater(() -> {
+                        feedinglabel.setIcon(feeding?imageGreen:imageGray);
+                        feedingmeslabel.setIcon(feedingAllow?imageGreen:imageGray);
+                        runlabel.setIcon(running?imageGreen:imageGray);
+                        blankinglabel.setIcon(blanking?imageGreen:imageGray);
+                        blankingmeslabel.setIcon(allowBlanking?imageGreen:imageGray);
+                    });
+                }catch (Throwable t) {
+                    log.error("<"+oprno+pos+">信号灯刷新异常: "+t.getMessage());
+                }
+            }
+        }, 500,1000);
+    }
+
     public void barCode(){
 
         //如果为空则不进行上料

+ 31 - 8
src/com/mes/component/WorkJPanelScan.java

@@ -215,6 +215,7 @@ public class WorkJPanelScan extends JPanel {
         this.add(feedingmeslabel);
 
         startOprnoTimer();
+        startLightTimer();
 //        resetScanA();
 
         // 信号监控按钮
@@ -293,12 +294,7 @@ public class WorkJPanelScan extends JPanel {
             public void run() {
                 try{
                     if(MesClient.isLogin == 1){
-                        Boolean retFeeding = readScanFeeding();
-                        feedinglabel.setIcon(retFeeding?imageGreen:imageGray);
-
-                        Boolean retFeedingAllow = readScanFinish();
-                        feedingmeslabel.setIcon(retFeedingAllow?imageGreen:imageGray);
-
+                        // 信号灯由 lightTimer 单独刷新,这里只做业务判断
                         //真扫码
                         Boolean retScanFeeding = readScanFeeding();
                         if(retScanFeeding){
@@ -307,13 +303,40 @@ public class WorkJPanelScan extends JPanel {
                             writeScanFinish(false);
                         }
                     }
-                }catch (Exception e) {
-                    log.error(e.getMessage());
+                }catch (Throwable t) {
+                    // 捕获 Throwable,防止 Error 导致 Timer 线程退出
+                    log.error("<"+oprno+pos+">轮询异常", t);
                 }
             }
         }, 100,1500);
     }
 
+    // 信号灯单独刷新,与业务轮询解耦,业务卡住时灯仍实时
+    public Timer lightTimer;
+    public void startLightTimer() {
+        if(lightTimer!=null) {
+            lightTimer.cancel();
+        }
+        lightTimer = new Timer();
+        lightTimer.schedule(new TimerTask() {
+            public void run() {
+                try{
+                    if(MesClient.isLogin != 1 || isLocal == 1){
+                        return;
+                    }
+                    boolean feeding = s7PLC.readBoolean(plcDb+".0.0");
+                    boolean finish = s7PLC.readBoolean(plcDb+".0.1");
+                    SwingUtilities.invokeLater(() -> {
+                        feedinglabel.setIcon(feeding?imageGreen:imageGray);
+                        feedingmeslabel.setIcon(finish?imageGreen:imageGray);
+                    });
+                }catch (Throwable t) {
+                    log.error("<"+oprno+pos+">信号灯刷新异常: "+t.getMessage());
+                }
+            }
+        }, 500,1000);
+    }
+
     public void barCode2(){
         String sn = readQrcode();
         if(pos.equals("A")){

+ 28 - 42
src/com/mes/util/JdbcUtils.java

@@ -18,8 +18,12 @@ public class JdbcUtils {
     public static String Drivde="org.sqlite.JDBC";
 	public static String DATABASE_URL="jdbc:sqlite:mes_db.db";
     
-    public static Connection getConn(){
+    // 全局复用一个连接,已打开则直接返回,避免每次新建导致连接泄漏
+    public static synchronized Connection getConn(){
         try {
+            if(conn != null && !conn.isClosed()){
+                return conn;
+            }
             Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
             conn = DriverManager.getConnection(DATABASE_URL);//连接数据库zhou.db,不存在则创建
             create_bw_record();//初始化结构表
@@ -98,22 +102,16 @@ public class JdbcUtils {
         statement.close();
     }
 
-	public static CacheData getCache(String key) {
-		//SQL语句
-		String sql = "select id,sn,content from cache where sn = '"+key+"' order by id asc limit 1";
-		Connection conn = JdbcUtils.getConn();
-		Statement stmt=null;
-		ResultSet ret = null;
+	public static synchronized CacheData getCache(String key) {
 		String content = "";
-		try {
-			stmt = conn.createStatement();
-			//执行语句,得到结果集
-			ret = stmt.executeQuery(sql);
-			while (ret.next()) {
-				content = ret.getString(3);
+		// 复用全局连接,不再关闭,避免关掉其他线程正在用的连接
+		try (PreparedStatement ps = getConn().prepareStatement("select content from cache where sn = ? order by id asc limit 1")) {
+			ps.setString(1, key);
+			try (ResultSet ret = ps.executeQuery()) {
+				if (ret.next()) {
+					content = ret.getString(1);
+				}
 			}
-			ret.close();
-			conn.close();//关闭连接
 		} catch (SQLException e1) {
 			e1.printStackTrace();
 		}
@@ -124,40 +122,28 @@ public class JdbcUtils {
 		return JSON.parseObject(content,CacheData.class);
 	}
 
-	public static Boolean setCache(String key,String value) {
-		String record_time = DateLocalUtils.getCurrentTime();
-		Connection conn = JdbcUtils.getConn();
+	public static synchronized Boolean setCache(String key,String value) {
 		if(value == null || value.trim().isEmpty()) {
 			return false;
 		}
-		value = CommonUtils.base64Encode(value);
-		CacheData content = getCache(key);
-		if(content == null) { // 新增
-			try {
-				Statement statement=conn.createStatement();
-				statement.executeUpdate("INSERT INTO cache (sn,content,record_time) VALUES"
-						+ " ('"+key+"', '"+value+"','"+record_time+"')");//向数据库中插入数据
-				statement.close();
-			}catch (Exception e) {
-				e.printStackTrace();
-			}
-		}else{
-			try{
-				Statement statement=conn.createStatement();   //创建连接对象,是Java的一个操作数据库的重要接口
-				statement.executeUpdate("update cache set content = '"+value.trim() + "' where sn = '"+key+"'");
-				statement.close();
-			}catch (Exception e) {
-				e.printStackTrace();
-			}
+		value = CommonUtils.base64Encode(value).trim();
+		// sn 有唯一索引,存在则覆盖,不存在则新增,避免并发 insert 冲突
+		try (PreparedStatement ps = getConn().prepareStatement("INSERT OR REPLACE INTO cache (sn,content,record_time) VALUES (?,?,?)")) {
+			ps.setString(1, key);
+			ps.setString(2, value);
+			ps.setString(3, DateLocalUtils.getCurrentTime());
+			ps.executeUpdate();
+		}catch (Exception e) {
+			e.printStackTrace();
 		}
 		return true;
 	}
 
     //插入数据
-    public static boolean insertData(String gw, String gy, String bw, String message_type, String sn) {
+    public static synchronized boolean insertData(String gw, String gy, String bw, String message_type, String sn) {
     	boolean ret = false;
     	String record_time = DateLocalUtils.getCurrentTime();
-    	if(conn==null) {
+    	if(getConn()==null) {
     		ret = false;
     	}else {
     		try {
@@ -178,10 +164,10 @@ public class JdbcUtils {
     }
 
 	// 向 submit_record表 插入提交记录数据
-	public static boolean insertSubmitRecord(String oprno, String sn, String bw){
+	public static synchronized boolean insertSubmitRecord(String oprno, String sn, String bw){
 		boolean ret = false;
 		String record_time = DateLocalUtils.getCurrentTime();
-		if(conn==null) {
+		if(getConn()==null) {
 			ret = false;
 		}else {
 			try {
@@ -228,7 +214,7 @@ public class JdbcUtils {
         statement.close();
     }
     
-    public static void close(){
+    public static synchronized void close(){
     	log.info("SQLite数据库连接关闭!");
         try {
         	if(conn!=null) {