day01-init 5 ماه پیش
والد
کامیت
f5cde7b238
2فایلهای تغییر یافته به همراه92 افزوده شده و 82 حذف شده
  1. 55 35
      src/com/mes/component/MesClientComponent.java
  2. 37 47
      src/com/mes/ui/MesRevice.java

+ 55 - 35
src/com/mes/component/MesClientComponent.java

@@ -38,7 +38,7 @@ public class MesClientComponent extends JPanel {
     public JButton resetBtn;
     // 工作状态
     // 0:未扫码,等待允许扫码信号 1:扫码成功,质量检查 2:质量合格,开始做件 3:加工完成,提交结果
-    public static Integer work_status = 0;
+    public Integer work_status = 0;
 
 
     public JLabel sllabel1;
@@ -46,42 +46,53 @@ public class MesClientComponent extends JPanel {
     public JLabel sllabel2;
 
 
-    // UI
+    // UI(布局随 workWidth、workHeight 缩放,避免写死 350×400 下的像素坐标)
     public MesClientComponent(Oprno opno){
         oprnoClass = opno;
+        int w = workWidth;
+        int h = workHeight;
         this.setLayout(null);
         this.setBackground(Color.WHITE);
-        // 设置灰色边框
         this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
+
+        int margin = Math.max(8, w / 40);
+        int hTitle = Math.max(36, (int) Math.round(h * 0.15));
+        int hStatus = Math.max(32, (int) Math.round(h * 0.15));
+        int hSn = Math.max(32, (int) Math.round(h * 0.15));
+        int hBtn = Math.max(40, (int) Math.round(h * 0.15));
+        int hLab = Math.max(28, (int) Math.round(h * 0.125));
+        int gap = Math.max(4, margin / 2);
+
+        int y = 0;
         JLabel oprnoLabel = new JLabel(oprnoClass.getOprno());
         oprnoLabel.setHorizontalAlignment(SwingConstants.CENTER);
         oprnoLabel.setForeground(Color.BLACK);
-        oprnoLabel.setFont(new Font("微软雅黑", Font.PLAIN, 30));
-        oprnoLabel.setBounds(0, 0, workWidth, 60);
+        int titleFont = Math.max(16, Math.min(36, w / 11));
+        oprnoLabel.setFont(new Font("微软雅黑", Font.PLAIN, titleFont));
+        oprnoLabel.setBounds(0, y, w, hTitle);
         this.add(oprnoLabel);
-        // 状态栏
+        y += hTitle;
+
         status_menu = new JButton("等待上料信号");
-        // 状态栏位置
-        status_menu.setBounds(10, 60, workWidth - 20, 60);
-        // 状态栏文字颜色
+        status_menu.setBounds(margin, y, w - 2 * margin, hStatus);
         status_menu.setForeground(Color.GREEN);
-        // 为状态按钮添加点击事件监听器
         status_menu.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
             }
         });
-        // 设置状态栏按钮字体为微软雅黑,普通样式,大小20
-        status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
-        // 设置状态栏按钮背景色为黑色
+        int statusFont = Math.max(14, Math.min(22, w / 16));
+        status_menu.setFont(new Font("微软雅黑", Font.PLAIN, statusFont));
         status_menu.setBackground(Color.BLACK);
-        // 将状态栏按钮添加到当前面板
         this.add(status_menu);
+        y += hStatus + gap;
+
         productSn = new JTextField("");
         productSn.setHorizontalAlignment(SwingConstants.CENTER);
         productSn.setForeground(Color.BLACK);
-        productSn.setFont(new Font("微软雅黑", Font.PLAIN, 20));
+        int snFont = Math.max(14, Math.min(22, w / 16));
+        productSn.setFont(new Font("微软雅黑", Font.PLAIN, snFont));
         productSn.setEditable(false);
-        productSn.setBounds(10, 130, workWidth-20, 60);
+        productSn.setBounds(margin, y, w - 2 * margin, hSn);
         productSn.addFocusListener(new FocusAdapter() {
             @Override
             public void focusGained(FocusEvent e) {
@@ -89,19 +100,22 @@ public class MesClientComponent extends JPanel {
             }
         });
         this.add(productSn);
+        y += hSn + gap;
 
         scanBtn = new JButton("扫码");
         final MesClientComponent thisComponent = this;
         scanBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 System.out.println("扫码");
-                // 获取工件码
                 thisComponent.scanBtnClick();
             }
         });
         scanBtn.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
-        scanBtn.setFont(new Font("微软雅黑", Font.PLAIN, 32));
-        scanBtn.setBounds(10, 200, workWidth/2 - 5, 60);
+        int btnFont = Math.max(16, Math.min(32, w / 10));
+        scanBtn.setFont(new Font("微软雅黑", Font.PLAIN, btnFont));
+        int btnGap = gap;
+        int btnW = (w - 2 * margin - btnGap) / 2;
+        scanBtn.setBounds(margin, y, btnW, hBtn);
         this.add(scanBtn);
 
         resetBtn = new JButton("刷新");
@@ -111,41 +125,47 @@ public class MesClientComponent extends JPanel {
             }
         });
         resetBtn.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
-        resetBtn.setFont(new Font("微软雅黑", Font.PLAIN, 32));
-        resetBtn.setBounds(170, 200, workWidth/2 - 5, 60);
+        resetBtn.setFont(new Font("微软雅黑", Font.PLAIN, btnFont));
+        resetBtn.setBounds(margin + btnW + btnGap, y, btnW, hBtn);
         this.add(resetBtn);
+        y += hBtn + gap;
+
         sllabel1 = new JLabel("上料允许");
         sllabel1.setHorizontalAlignment(SwingConstants.CENTER);
         sllabel1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
         sllabel1.setForeground(Color.BLACK);
-        sllabel1.setBounds(0, 280, workWidth/2, 50);
+        sllabel1.setBounds(0, y, w / 2, hLab);
         this.add(sllabel1);
         sllabel2 = new JLabel("下料允许");
         sllabel2.setHorizontalAlignment(SwingConstants.CENTER);
         sllabel2.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
         sllabel2.setForeground(Color.BLACK);
-        sllabel2.setBounds(190, 280, workWidth/2, 50);
+        sllabel2.setBounds(w / 2, y, w - w / 2, hLab);
         this.add(sllabel2);
-        // 加入定时任务
+
         this.startOprnoTimer();
     }
 
     // 扫码按钮
     public void scanBtnClick() {
-        if (!(work_status == 0)){
+        if (work_status != 0) {
             status_menu.setText("工件加工中,请勿重复扫码");
-        }else {
+            return;
+        }
+        try {
             String sn = S7PLCUtils.getSnInOprno(oprnoClass, MesClient.s7PLC);
-            if (sn.equals("")){
-                if (productSn.getText().isEmpty()){
-                    // 状态栏改变
-                    status_menu.setText("扫码结果为空");
-                    productSn.setText(oprnoClass.getMes_in());
-                }
-            }else {
-                productSn.setText(sn);
-                work_status = 1;
+
+            if (sn == null || sn.trim().isEmpty()) {
+                setMenuStatus("扫码结果为空", -1);
+                return;
             }
+            sn = sn.trim();
+            productSn.setText(sn);
+            work_status = 1;
+            setMenuStatus("已读取工件码,等待校验", 0);
+        } catch (Exception e) {
+            log.error("读取 PLC 工件码失败", e);
+            setMenuStatus("读取 PLC 失败", -1);
         }
     }
 

+ 37 - 47
src/com/mes/ui/MesRevice.java

@@ -1,8 +1,8 @@
 package com.mes.ui;
 
-import com.mes.component.MesClientComponent;
 import com.mes.netty.ProtocolParam;
 import com.mes.component.MyDialog;
+import com.mes.component.MesClientComponent;
 import com.mes.util.ErrorMsg;
 
 import javax.swing.*;
@@ -10,46 +10,44 @@ import java.awt.*;
 
 public class MesRevice {
 
+    private static void setStatusByMsgOprno(String mes_msg, String msg, int error) {
+        try {
+            String oprno = ProtocolParam.getOprno(mes_msg);
+            if (oprno != null) {
+                oprno = oprno.trim();
+            }
+            if (oprno == null || oprno.isEmpty()) {
+                MesClient.setMenuStatus(msg, error);
+                return;
+            }
+            MesClientComponent component = MesClient.componentMap.get(oprno);
+            if (component == null) {
+                MesClient.setMenuStatus("未找到工位面板:" + oprno + " " + msg, error);
+                return;
+            }
+            component.setMenuStatus(msg, error);
+        } catch (Exception e) {
+            MesClient.setMenuStatus(msg, error);
+        }
+    }
+
     // 质量查询
     public static void checkQualityRevice(String processMsgRet,String mes_msg){
         try{
             if(processMsgRet.equalsIgnoreCase("UD")) {
-//                String sn = ProtocolParam.getSn(mes_msg).trim();
-//                MesClient.status_menu.setForeground(Color.GREEN);
-//                MesClient.check_quality_result = true;//质量合格,可以绑定加工
-//                MesClient.status_menu.setText("该工件可以加工");
-//                MesClient.work_status = 1;
-//                MesClient.f_scan_data_bt_1.setEnabled(false);
-//                MesClient.finish_ok_bt.setEnabled(true);
-//                MesClient.finish_ng_bt.setEnabled(true);
-//
-//                String oprno = ProtocolParam.getOprno(mes_msg).trim();
-//                if(oprno.equals("OP400")){
-//                    String lx = ProtocolParam.getLx(mes_msg);
-//                    if(lx.equals("FX")){
-//                        MesClient.fxlabel.setVisible(true);
-//                    }
-//                }
-
-//                String qret = "OK";
-//                MesClient.getUser();
-//                DataUtil.sendQuality(MesClient.nettyClient,sn,qret,MesClient.user20);
-                // 获取工位号
                 String oprno = ProtocolParam.getOprno(mes_msg).trim();
-                // 找到对应的组件并修改状态
                 MesClientComponent component = MesClient.componentMap.get(oprno);
-                // 发送允许启动信号
+                if (component == null) {
+                    MesClient.setMenuStatus("未找到工位面板:" + oprno, -1);
+                    return;
+                }
                 MesClient.s7PLC.writeBoolean(component.oprnoClass.getMes_in(), true);
-                // 开始加工报文
                 DataUtil.startWorkByGw(MesClient.nettyClient,ProtocolParam.getSn(mes_msg).trim(),MesClient.user20,component.oprnoClass.getOprno());
-                if (component != null) {
-                    component.setMenuStatus("质量检查合格,开始加工", 0);
-                    component.work_status = 2; // 更新工作状态为开始加工
-                }
+                component.setMenuStatus("质量检查合格,开始加工", 0);
+                component.work_status = 2;
             }else {
-//                MesClient.check_quality_result = false;
                 String lmsg = ErrorMsg.getErrorMsg(processMsgRet, ProtocolParam.getLx(mes_msg));
-                MesClient.setMenuStatus(lmsg,-1);
+                setStatusByMsgOprno(mes_msg, lmsg, -1);
             }
         }catch (Exception e){
             e.printStackTrace();
@@ -60,11 +58,6 @@ public class MesRevice {
     public static void startRevice(String processMsgRet,String mes_msg){
         try{
             if(processMsgRet.equalsIgnoreCase("UD")) {
-//                String oprno = ProtocolParam.getOprno(mes_msg).trim();
-//                String sn = ProtocolParam.getSn(mes_msg).trim();
-//                String barcode36 = MesClient.getBarcode(sn);//处理36为码
-//                MesClient.getUser();
-//                DataUtil.startWork(MesClient.nettyClient,barcode36,MesClient.user20);
             }
         }catch (Exception e){
             e.printStackTrace();
@@ -100,23 +93,20 @@ public class MesRevice {
     // 上传质量
     public static void updateResultRevice(String processMsgRet,String mes_msg){
         try{
-
             if(processMsgRet.equalsIgnoreCase("OK")) {
-                // 获取工位号
                 String oprno = ProtocolParam.getOprno(mes_msg).trim();
-                // 找到对应的组件并修改状态
                 MesClientComponent component = MesClient.componentMap.get(oprno);
-                // 发送允许下料信号
+                if (component == null) {
+                    MesClient.setMenuStatus("未找到工位面板:" + oprno, -1);
+                    return;
+                }
                 MesClient.s7PLC.writeBoolean(component.oprnoClass.getMes_out(),true);
-                // 加工完成信号
                 MesClient.s7PLC.writeBoolean(component.oprnoClass.getProcess_ok(),false);
-
-                if (component != null) {
-                    component.setMenuStatus("提交成功", 0);
-                    component.work_status = 3;
-                }
+                component.setMenuStatus("提交成功", 0);
+                component.work_status = 3;
             }else{
-                MesClient.setMenuStatus("结果提交失败,请重试",-1);
+                String lmsg = ErrorMsg.getErrorMsg(processMsgRet, ProtocolParam.getLx(mes_msg));
+                setStatusByMsgOprno(mes_msg, lmsg, -1);
             }
         }catch (Exception e){
             e.printStackTrace();