张田野 1 tydzień temu
rodzic
commit
e6a88bfbfc

+ 12 - 2
src/com/mes/component/GunPanel.java

@@ -53,7 +53,8 @@ public class GunPanel extends JPanel {
     public GunPanel(YgslConfig config) {
         this.config = config;
         this.gunIndex = config.getGunIndex();
-        this.taskCount = config.getTaskCount();
+        // 颗数由扫码规则在开工前动态设定,配置中的 task_count 仅作占位
+        this.taskCount = 0;
         this.tighteningIds = new java.util.ArrayList<>();
 
         initUI();
@@ -320,11 +321,12 @@ public class GunPanel extends JPanel {
 
     public void reset() {
         currentCount = 0;
+        taskCount = 0; // 下一件由扫码规则重新设定
         tighteningIds.clear();
         SwingUtilities.invokeLater(() -> {
             torqueLabel.setText("");
             angleLabel.setText("");
-            scheduleLabel.setText("0/" + taskCount);
+            scheduleLabel.setText("0/0");
             torqueLabel.setForeground(Color.BLACK);
             angleLabel.setForeground(Color.BLACK);
         });
@@ -342,6 +344,14 @@ public class GunPanel extends JPanel {
         return taskCount;
     }
 
+    /** 按扫码规则动态设定本枪拧紧颗数 */
+    public void setTaskCount(int taskCount) {
+        this.taskCount = taskCount;
+        SwingUtilities.invokeLater(() -> {
+            scheduleLabel.setText(currentCount + "/" + taskCount);
+        });
+    }
+
     public boolean isOnline() {
         return isOnline;
     }

+ 120 - 152
src/com/mes/ui/MesClient.java

@@ -95,6 +95,21 @@ public class MesClient extends JFrame {
     public static JPanel gunPanelContainer;
     public static JLabel pzlabel;
 
+    /**
+     * TW 项目扫码前缀 → 螺栓安装颗数
+     * 四驱=10,后驱/二驱=6
+     */
+    private static final String[] BOLT_COUNT_10_PREFIXES = {
+            "5019016600341", // 国内四驱 B009-032
+            "5019016600571", // 海外四驱 B009-039
+            "5003136600651"  // 海外右舵四驱 B009-055
+    };
+    private static final String[] BOLT_COUNT_6_PREFIXES = {
+            "5019016600511", // 国内后驱 B009-034
+            "5003136600051", // 海外后驱 B009-037
+            "5003136600661"  // 海外右舵二驱 B009-056
+    };
+
     public static java.util.Timer submitCheckTimer;
 
     public static boolean autoSubmitEnabled = true;
@@ -190,7 +205,9 @@ public class MesClient extends JFrame {
 
         List<YgslConfig> configs = JdbcUtils.getEnabledYgslConfigs();
         System.out.println("initGunPanels - 获取到配置: " + configs.size() + " 把枪");
-        for (YgslConfig config : configs) {
+        // 本工位只使用一把枪
+        if (!configs.isEmpty()) {
+            YgslConfig config = configs.get(0);
             GunPanel gunPanel = new GunPanel(config);
             gunPanel.setTaskCompletedListener(new GunPanel.OnTaskCompletedListener() {
                 @Override
@@ -218,6 +235,40 @@ public class MesClient extends JFrame {
         System.out.println("已加载 " + gunPanels.size() + " 把扭力枪");
     }
 
+    /**
+     * 根据工件码前缀解析螺栓安装颗数;无法识别返回 -1
+     */
+    public static int resolveBoltCountBySn(String sn) {
+        if (sn == null || sn.trim().isEmpty()) {
+            return -1;
+        }
+        String code = sn.trim();
+        for (String prefix : BOLT_COUNT_10_PREFIXES) {
+            if (code.startsWith(prefix)) {
+                return 10;
+            }
+        }
+        for (String prefix : BOLT_COUNT_6_PREFIXES) {
+            if (code.startsWith(prefix)) {
+                return 6;
+            }
+        }
+        return -1;
+    }
+
+    /** 将扫码解析出的颗数应用到当前枪 */
+    public static boolean applyBoltCountBySn(String sn) {
+        int boltCount = resolveBoltCountBySn(sn);
+        if (boltCount < 0) {
+            return false;
+        }
+        for (GunPanel panel : gunPanels) {
+            panel.setTaskCount(boltCount);
+        }
+        System.out.println("扫码规则设定拧紧颗数: " + boltCount + ",SN=" + sn);
+        return true;
+    }
+
     public static void startSubmitCheckTimer() {
         if (submitCheckTimer != null) {
             submitCheckTimer.cancel();
@@ -630,12 +681,18 @@ public class MesClient extends JFrame {
         //弹窗扫工件码
         String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
         if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
+            // 按 TW 项目代码前缀判断拧紧颗数
+            if (!applyBoltCountBySn(scanBarcode)) {
+                JOptionPane.showMessageDialog(mesClientFrame,
+                        "无法识别该工件码对应的螺栓颗数规则,请确认扫码内容",
+                        "提示窗口", JOptionPane.WARNING_MESSAGE);
+                return;
+            }
+
             //获取用户
             getUser();
             //获取扫码内容36位
             String barcode36 = getBarcode(scanBarcode);//处理36为码
-            //工位号
-            String gw = "";
             switch(scan_type) {
                 case 1:
                     product_sn.setText(scanBarcode);
@@ -1081,27 +1138,36 @@ public class MesClient extends JFrame {
     }
 
     private static JTable configTable;
-    private static JButton addGunButton;
     private static JButton saveConfigButton;
 
     public static void initGunConfigPanel(JPanel panel) {
-        JLabel titleLabel = new JLabel("扭力枪配置管理");
+        JLabel titleLabel = new JLabel("扭力枪配置(仅一把)");
         titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
-        titleLabel.setBounds(350, 20, 300, 40);
+        titleLabel.setBounds(320, 20, 360, 40);
         panel.add(titleLabel);
 
-        String[] columnNames = {"枪号", "名称", "IP地址", "端口", "任务数", "启用", "自动提交"};
+        JLabel tipLabel = new JLabel("拧紧颗数由扫码规则自动判断,无需在此配置");
+        tipLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
+        tipLabel.setForeground(Color.GRAY);
+        tipLabel.setBounds(300, 55, 400, 25);
+        panel.add(tipLabel);
+
+        // 颗数不在配置中维护,只保留枪号/名称/IP/端口/启用/自动提交
+        String[] columnNames = {"枪号", "名称", "IP地址", "端口", "启用", "自动提交"};
         List<YgslConfig> configs = JdbcUtils.getYgslConfigs();
-        Object[][] data = new Object[configs.size()][7];
+        // 只展示一把枪(取枪号最小的一条;若库里有多条历史数据也只编辑第一把)
+        if (configs.size() > 1) {
+            configs = configs.subList(0, 1);
+        }
+        Object[][] data = new Object[configs.size()][6];
         for (int i = 0; i < configs.size(); i++) {
             YgslConfig config = configs.get(i);
             data[i][0] = config.getGunIndex();
             data[i][1] = config.getGunName();
             data[i][2] = config.getIpAddress();
             data[i][3] = config.getPort();
-            data[i][4] = config.getTaskCount();
-            data[i][5] = config.getEnabled() == 1 ? "是" : "否";
-            data[i][6] = config.getAutoSubmit() == 1 ? "是" : "否";
+            data[i][4] = config.getEnabled() == 1 ? "是" : "否";
+            data[i][5] = config.getAutoSubmit() == 1 ? "是" : "否";
         }
 
         configTable = new JTable(data, columnNames);
@@ -1110,20 +1176,9 @@ public class MesClient extends JFrame {
         configTable.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
 
         JScrollPane scrollPane = new JScrollPane(configTable);
-        scrollPane.setBounds(50, 70, 900, 400);
+        scrollPane.setBounds(50, 90, 900, 380);
         panel.add(scrollPane);
 
-        addGunButton = new JButton("添加枪");
-        addGunButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
-        addGunButton.setBounds(150, 490, 120, 40);
-        addGunButton.addActionListener(new ActionListener() {
-            @Override
-            public void actionPerformed(ActionEvent e) {
-                addNewGun(panel);
-            }
-        });
-        panel.add(addGunButton);
-
         saveConfigButton = new JButton("保存配置");
         saveConfigButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
         saveConfigButton.setBounds(300, 490, 120, 40);
@@ -1135,34 +1190,25 @@ public class MesClient extends JFrame {
         });
         panel.add(saveConfigButton);
 
-        JButton deleteButton = new JButton("删除选中");
-        deleteButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
-        deleteButton.setBounds(450, 490, 120, 40);
-        deleteButton.addActionListener(new ActionListener() {
+        JButton editButton = new JButton("编辑");
+        editButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
+        editButton.setBounds(450, 490, 120, 40);
+        editButton.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
-                int selectedRow = configTable.getSelectedRow();
-                if (selectedRow >= 0) {
-                    int confirm = JOptionPane.showConfirmDialog(mesClientFrame, "确定要删除选中的配置吗?", "确认删除", JOptionPane.YES_NO_OPTION);
-                    if (confirm == JOptionPane.YES_OPTION) {
-                        List<YgslConfig> allConfigs = JdbcUtils.getYgslConfigs();
-                        if (selectedRow < allConfigs.size()) {
-                            JdbcUtils.deleteYgslConfig(allConfigs.get(selectedRow).getId());
-                            reloadGunConfigs(panel);
-                        }
-                    }
-                } else {
-                    JOptionPane.showMessageDialog(mesClientFrame, "请先选中一行", "提示", JOptionPane.INFORMATION_MESSAGE);
-                }
+                editGunConfig(0, panel);
             }
         });
-        panel.add(deleteButton);
+        panel.add(editButton);
 
         configTable.addMouseListener(new MouseAdapter() {
-            public void mouseDoubleClick(MouseEvent e) {
-                int row = configTable.getSelectedRow();
-                if (row >= 0) {
-                    editGunConfig(row, panel);
+            @Override
+            public void mouseClicked(MouseEvent e) {
+                if (e.getClickCount() == 2) {
+                    int row = configTable.getSelectedRow();
+                    if (row >= 0) {
+                        editGunConfig(row, panel);
+                    }
                 }
             }
         });
@@ -1170,64 +1216,62 @@ public class MesClient extends JFrame {
 
     private static void editGunConfig(int row, JPanel panel) {
         List<YgslConfig> configs = JdbcUtils.getYgslConfigs();
-        if (row >= configs.size()) return;
+        if (configs.isEmpty()) return;
+        if (row >= configs.size()) row = 0;
 
         YgslConfig config = configs.get(row);
 
         JDialog dialog = new JDialog(mesClientFrame, "编辑扭力枪", true);
-        dialog.setSize(400, 400);
+        dialog.setSize(400, 360);
         dialog.setLocationRelativeTo(mesClientFrame);
         dialog.setLayout(null);
 
-        JLabel idLabel = new JLabel("ID: " + config.getId());
-        idLabel.setBounds(50, 20, 300, 30);
-        dialog.add(idLabel);
-
         JLabel nameLabel = new JLabel("枪号:");
-        nameLabel.setBounds(50, 60, 80, 30);
+        nameLabel.setBounds(50, 30, 80, 30);
         dialog.add(nameLabel);
         JTextField nameField = new JTextField(String.valueOf(config.getGunIndex()));
-        nameField.setBounds(140, 60, 180, 30);
+        nameField.setBounds(140, 30, 180, 30);
         dialog.add(nameField);
 
         JLabel gunNameLabel = new JLabel("名称:");
-        gunNameLabel.setBounds(50, 100, 80, 30);
+        gunNameLabel.setBounds(50, 70, 80, 30);
         dialog.add(gunNameLabel);
         JTextField gunNameField = new JTextField(config.getGunName());
-        gunNameField.setBounds(140, 100, 180, 30);
+        gunNameField.setBounds(140, 70, 180, 30);
         dialog.add(gunNameField);
 
         JLabel ipLabel = new JLabel("IP地址:");
-        ipLabel.setBounds(50, 140, 80, 30);
+        ipLabel.setBounds(50, 110, 80, 30);
         dialog.add(ipLabel);
         JTextField ipField = new JTextField(config.getIpAddress());
-        ipField.setBounds(140, 140, 180, 30);
+        ipField.setBounds(140, 110, 180, 30);
         dialog.add(ipField);
 
         JLabel portLabel = new JLabel("端口:");
-        portLabel.setBounds(50, 180, 80, 30);
+        portLabel.setBounds(50, 150, 80, 30);
         dialog.add(portLabel);
         JTextField portField = new JTextField(String.valueOf(config.getPort()));
-        portField.setBounds(140, 180, 180, 30);
+        portField.setBounds(140, 150, 180, 30);
         dialog.add(portField);
 
-        JLabel taskLabel = new JLabel("任务数:");
-        taskLabel.setBounds(50, 220, 80, 30);
-        dialog.add(taskLabel);
-        JTextField taskField = new JTextField(String.valueOf(config.getTaskCount()));
-        taskField.setBounds(140, 220, 180, 30);
-        dialog.add(taskField);
-
         JLabel enabledLabel = new JLabel("启用:");
-        enabledLabel.setBounds(50, 260, 80, 30);
+        enabledLabel.setBounds(50, 190, 80, 30);
         dialog.add(enabledLabel);
         JComboBox<String> enabledBox = new JComboBox<>(new String[]{"否", "是"});
-        enabledBox.setBounds(140, 260, 180, 30);
+        enabledBox.setBounds(140, 190, 180, 30);
         enabledBox.setSelectedIndex(config.getEnabled() == 1 ? 1 : 0);
         dialog.add(enabledBox);
 
+        JLabel autoLabel = new JLabel("自动提交:");
+        autoLabel.setBounds(50, 230, 80, 30);
+        dialog.add(autoLabel);
+        JComboBox<String> autoBox = new JComboBox<>(new String[]{"否", "是"});
+        autoBox.setBounds(140, 230, 180, 30);
+        autoBox.setSelectedIndex(config.getAutoSubmit() == 1 ? 1 : 0);
+        dialog.add(autoBox);
+
         JButton okButton = new JButton("保存");
-        okButton.setBounds(100, 310, 80, 30);
+        okButton.setBounds(100, 280, 80, 30);
         okButton.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
@@ -1236,8 +1280,9 @@ public class MesClient extends JFrame {
                     config.setGunName(gunNameField.getText());
                     config.setIpAddress(ipField.getText());
                     config.setPort(Integer.parseInt(portField.getText()));
-                    config.setTaskCount(Integer.parseInt(taskField.getText()));
+                    config.setTaskCount(0);
                     config.setEnabled(enabledBox.getSelectedIndex() == 1 ? 1 : 0);
+                    config.setAutoSubmit(autoBox.getSelectedIndex() == 1 ? 1 : 0);
 
                     JdbcUtils.updateYgslConfig(config);
                     dialog.dispose();
@@ -1250,84 +1295,6 @@ public class MesClient extends JFrame {
         dialog.add(okButton);
 
         JButton cancelButton = new JButton("取消");
-        cancelButton.setBounds(200, 310, 80, 30);
-        cancelButton.addActionListener(new ActionListener() {
-            @Override
-            public void actionPerformed(ActionEvent e) {
-                dialog.dispose();
-            }
-        });
-        dialog.add(cancelButton);
-
-        dialog.setVisible(true);
-    }
-
-    private static void addNewGun(JPanel panel) {
-        JDialog dialog = new JDialog(mesClientFrame, "添加扭力枪", true);
-        dialog.setSize(400, 350);
-        dialog.setLocationRelativeTo(mesClientFrame);
-        dialog.setLayout(null);
-
-        JLabel nameLabel = new JLabel("枪号:");
-        nameLabel.setBounds(50, 30, 80, 30);
-        dialog.add(nameLabel);
-        JTextField nameField = new JTextField();
-        nameField.setBounds(140, 30, 180, 30);
-        dialog.add(nameField);
-
-        JLabel gunNameLabel = new JLabel("名称:");
-        gunNameLabel.setBounds(50, 80, 80, 30);
-        dialog.add(gunNameLabel);
-        JTextField gunNameField = new JTextField("M5");
-        gunNameField.setBounds(140, 80, 180, 30);
-        dialog.add(gunNameField);
-
-        JLabel ipLabel = new JLabel("IP地址:");
-        ipLabel.setBounds(50, 130, 80, 30);
-        dialog.add(ipLabel);
-        JTextField ipField = new JTextField("192.168.5.100");
-        ipField.setBounds(140, 130, 180, 30);
-        dialog.add(ipField);
-
-        JLabel portLabel = new JLabel("端口:");
-        portLabel.setBounds(50, 180, 80, 30);
-        dialog.add(portLabel);
-        JTextField portField = new JTextField("4545");
-        portField.setBounds(140, 180, 180, 30);
-        dialog.add(portField);
-
-        JLabel taskLabel = new JLabel("任务数:");
-        taskLabel.setBounds(50, 230, 80, 30);
-        dialog.add(taskLabel);
-        JTextField taskField = new JTextField("12");
-        taskField.setBounds(140, 230, 180, 30);
-        dialog.add(taskField);
-
-        JButton okButton = new JButton("确定");
-        okButton.setBounds(100, 280, 80, 30);
-        okButton.addActionListener(new ActionListener() {
-            @Override
-            public void actionPerformed(ActionEvent e) {
-                try {
-                    int gunIndex = Integer.parseInt(nameField.getText());
-                    String gunName = gunNameField.getText();
-                    String ip = ipField.getText();
-                    int port = Integer.parseInt(portField.getText());
-                    int taskCount = Integer.parseInt(taskField.getText());
-
-                    YgslConfig config = new YgslConfig(gunIndex, gunName, ip, port, taskCount);
-                    JdbcUtils.insertYgslConfig(config);
-
-                    dialog.dispose();
-                    reloadGunConfigs(panel);
-                } catch (Exception ex) {
-                    JOptionPane.showMessageDialog(dialog, "输入格式错误,请检查", "错误", JOptionPane.ERROR_MESSAGE);
-                }
-            }
-        });
-        dialog.add(okButton);
-
-        JButton cancelButton = new JButton("取消");
         cancelButton.setBounds(200, 280, 80, 30);
         cancelButton.addActionListener(new ActionListener() {
             @Override
@@ -1350,10 +1317,11 @@ public class MesClient extends JFrame {
                 config.setGunIndex((Integer) configTable.getValueAt(i, 0));
                 config.setGunName((String) configTable.getValueAt(i, 1));
                 config.setIpAddress((String) configTable.getValueAt(i, 2));
-                config.setPort((Integer) configTable.getValueAt(i, 3));
-                config.setTaskCount((Integer) configTable.getValueAt(i, 4));
-                config.setEnabled(((String) configTable.getValueAt(i, 5)).equals("是") ? 1 : 0);
-                config.setAutoSubmit(((String) configTable.getValueAt(i, 6)).equals("是") ? 1 : 0);
+                Object portVal = configTable.getValueAt(i, 3);
+                config.setPort(portVal instanceof Integer ? (Integer) portVal : Integer.parseInt(String.valueOf(portVal)));
+                config.setTaskCount(0);
+                config.setEnabled(((String) configTable.getValueAt(i, 4)).equals("是") ? 1 : 0);
+                config.setAutoSubmit(((String) configTable.getValueAt(i, 5)).equals("是") ? 1 : 0);
 
                 JdbcUtils.updateYgslConfig(config);
             }

+ 7 - 0
src/com/mes/ui/MesRevice.java

@@ -54,6 +54,13 @@ public class MesRevice {
     public static void startRevice(String processMsgRet,String mes_msg){
         try{
             if(processMsgRet.equalsIgnoreCase("OK")) {
+                // 开工前再按扫码规则确认颗数(防止未扫码或规则未命中)
+                String sn = MesClient.product_sn.getText();
+                if (!MesClient.applyBoltCountBySn(sn)) {
+                    MesClient.setMenuStatus("无法识别工件码螺栓颗数规则", -1);
+                    return;
+                }
+
                 MesClient.work_status = 1;
                 MesClient.f_scan_data_bt_1.setEnabled(false);
 

+ 2 - 3
src/com/mes/util/JdbcUtils.java

@@ -274,10 +274,9 @@ public class JdbcUtils {
 	private static void initDefaultYgslConfig() throws SQLException {
 		Statement statement = conn.createStatement();
 		String now = DateLocalUtils.getCurrentTime();
+		// 本工位只配置一把枪;task_count 由扫码规则动态决定,入库仅占位
 		statement.executeUpdate("INSERT INTO ygsl_config (gun_index, gun_name, ip_address, port, task_count, enabled, auto_submit, create_time, update_time) VALUES "
-				+ "(1, 'M5', '192.168.5.100', 4545, 12, 1, 1, '" + now + "', '" + now + "')");
-		statement.executeUpdate("INSERT INTO ygsl_config (gun_index, gun_name, ip_address, port, task_count, enabled, auto_submit, create_time, update_time) VALUES "
-				+ "(2, 'M8', '192.168.5.101', 4545, 1, 1, 1, '" + now + "', '" + now + "')");
+				+ "(1, '枪1', '192.168.5.100', 4545, 0, 1, 1, '" + now + "', '" + now + "')");
 		System.out.println("默认扭力枪配置初始化完成!");
 		statement.close();
 	}

+ 8 - 2
src/com/mes/ygsl/YgslConfig.java

@@ -15,16 +15,22 @@ public class YgslConfig {
     public YgslConfig() {
     }
 
-    public YgslConfig(int gunIndex, String gunName, String ipAddress, int port, int taskCount) {
+    public YgslConfig(int gunIndex, String gunName, String ipAddress, int port) {
         this.gunIndex = gunIndex;
         this.gunName = gunName;
         this.ipAddress = ipAddress;
         this.port = port;
-        this.taskCount = taskCount;
+        this.taskCount = 0; // 颗数由扫码规则决定,不在配置中维护
         this.enabled = 1;
         this.autoSubmit = 1;
     }
 
+    /** @deprecated 颗数由扫码规则决定,保留兼容旧调用 */
+    public YgslConfig(int gunIndex, String gunName, String ipAddress, int port, int taskCount) {
+        this(gunIndex, gunName, ipAddress, port);
+        this.taskCount = taskCount;
+    }
+
     public int getId() {
         return id;
     }

+ 2 - 2
src/resources/config/config.properties

@@ -1,6 +1,6 @@
-mes.gw=OP390A
+mes.gw=OP400A
 #mes.server_ip=127.0.0.1
-mes.server_ip=192.168.18.99
+mes.server_ip=192.168.24.99
 mes.tcp_port=3000
 mes.heart_beat_cycle=60
 mes.line_sn=XT