jingbo пре 3 недеља
родитељ
комит
9da4da7256
2 измењених фајлова са 495 додато и 0 уклоњено
  1. 374 0
      src/com/mes/component/GunPanel.java
  2. 121 0
      src/com/mes/ygsl/YgslConfig.java

+ 374 - 0
src/com/mes/component/GunPanel.java

@@ -0,0 +1,374 @@
+package com.mes.component;
+
+import com.mes.ui.MesClient;
+import com.mes.ui.YgslUtil;
+import com.mes.util.JdbcUtils;
+import com.mes.ygsl.YgslClient;
+import com.mes.ygsl.YgslConfig;
+
+import javax.swing.*;
+import java.awt.*;
+import java.util.List;
+import java.util.Timer;
+import java.util.TimerTask;
+
+public class GunPanel extends JPanel {
+
+    private YgslConfig config;
+    private int gunIndex;
+
+    private int currentCount = 0;
+    private int taskCount = 0;
+    private boolean isOnline = false;
+    private boolean isConnected = false;
+
+    private YgslClient gunClient;
+    private Timer heartBeatTimer;
+    private List<String> tighteningIds;
+
+    private JLabel nameLabel;
+    private JLabel statusLabel;
+    private JLabel scheduleLabel;
+    private JLabel torqueLabel;
+    private JLabel angleLabel;
+
+    private OnTighteningDataListener dataListener;
+    private OnTaskCompletedListener taskCompletedListener;
+    private OnStatusChangeListener statusChangeListener;
+
+    public interface OnTighteningDataListener {
+        void onTighteningData(GunPanel panel, String tighteningStatus, String torque, String angle,
+                             String tighteningID, String jobID, String pos);
+    }
+
+    public interface OnTaskCompletedListener {
+        void onTaskCompleted(GunPanel panel);
+    }
+
+    public interface OnStatusChangeListener {
+        void onStatusChanged(GunPanel panel, boolean isOnline);
+    }
+
+    public GunPanel(YgslConfig config) {
+        this.config = config;
+        this.gunIndex = config.getGunIndex();
+        this.taskCount = config.getTaskCount();
+        this.tighteningIds = new java.util.ArrayList<>();
+
+        initUI();
+    }
+
+    private void initUI() {
+        setLayout(null);
+        setPreferredSize(new Dimension(280, 160));
+        setOpaque(false);
+
+        JLabel titleLabel = new JLabel(config.getGunName());
+        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
+        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 28));
+        titleLabel.setForeground(Color.BLACK);
+        titleLabel.setBounds(0, 10, 200, 35);
+        add(titleLabel);
+
+        statusLabel = new JLabel("离线");
+        statusLabel.setHorizontalAlignment(SwingConstants.CENTER);
+        statusLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
+        statusLabel.setBounds(200, 12, 70, 28);
+        statusLabel.setForeground(Color.RED);
+        add(statusLabel);
+
+        JLabel scheduleTitleLabel = new JLabel("进度:");
+        scheduleTitleLabel.setHorizontalAlignment(SwingConstants.RIGHT);
+        scheduleTitleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 22));
+        scheduleTitleLabel.setForeground(Color.BLACK);
+        scheduleTitleLabel.setBounds(10, 55, 70, 30);
+        add(scheduleTitleLabel);
+
+        scheduleLabel = new JLabel("0/" + taskCount);
+        scheduleLabel.setHorizontalAlignment(SwingConstants.LEFT);
+        scheduleLabel.setFont(new Font("微软雅黑", Font.BOLD, 22));
+        scheduleLabel.setForeground(Color.BLACK);
+        scheduleLabel.setBounds(85, 55, 100, 30);
+        add(scheduleLabel);
+
+        JLabel torqueTitleLabel = new JLabel("扭矩:");
+        torqueTitleLabel.setHorizontalAlignment(SwingConstants.RIGHT);
+        torqueTitleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 22));
+        torqueTitleLabel.setForeground(Color.BLACK);
+        torqueTitleLabel.setBounds(10, 95, 70, 30);
+        add(torqueTitleLabel);
+
+        torqueLabel = new JLabel("");
+        torqueLabel.setHorizontalAlignment(SwingConstants.LEFT);
+        torqueLabel.setFont(new Font("微软雅黑", Font.BOLD, 22));
+        torqueLabel.setForeground(Color.BLACK);
+        torqueLabel.setBounds(85, 95, 120, 30);
+        add(torqueLabel);
+
+        JLabel angleTitleLabel = new JLabel("角度:");
+        angleTitleLabel.setHorizontalAlignment(SwingConstants.RIGHT);
+        angleTitleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 22));
+        angleTitleLabel.setForeground(Color.BLACK);
+        angleTitleLabel.setBounds(10, 125, 70, 30);
+        add(angleTitleLabel);
+
+        angleLabel = new JLabel("");
+        angleLabel.setHorizontalAlignment(SwingConstants.LEFT);
+        angleLabel.setFont(new Font("微软雅黑", Font.BOLD, 22));
+        angleLabel.setForeground(Color.BLACK);
+        angleLabel.setBounds(85, 125, 120, 30);
+        add(angleLabel);
+    }
+
+    public void connect() {
+        if (gunClient != null) {
+            return;
+        }
+
+        gunClient = new YgslClient(config.getIpAddress(), config.getPort(), gunIndex);
+        gunClient.setGunPanel(this);
+
+        try {
+            gunClient.run();
+            isConnected = true;
+            startHeartBeatTimer();
+        } catch (Exception e) {
+            e.printStackTrace();
+            isConnected = false;
+            scheduleReconnect();
+        }
+    }
+
+    public void disconnect() {
+        stopHeartBeatTimer();
+        if (gunClient != null) {
+            gunClient.close();
+            gunClient = null;
+        }
+        isConnected = false;
+        isOnline = false;
+        updateStatusUI();
+    }
+
+    private void scheduleReconnect() {
+        Timer reconnectTimer = new Timer();
+        reconnectTimer.schedule(new TimerTask() {
+            @Override
+            public void run() {
+                if (!isConnected) {
+                    connect();
+                }
+            }
+        }, 3000);
+    }
+
+    private void startHeartBeatTimer() {
+        stopHeartBeatTimer();
+        heartBeatTimer = new Timer();
+        heartBeatTimer.schedule(new TimerTask() {
+            @Override
+            public void run() {
+                if (gunClient != null && isConnected) {
+                    if (isOnline) {
+                        YgslUtil.comHeart(gunClient);
+                    } else {
+                        scheduleReconnect();
+                    }
+                }
+                updateStatusUI();
+            }
+        }, 1000, 3000);
+    }
+
+    private void stopHeartBeatTimer() {
+        if (heartBeatTimer != null) {
+            heartBeatTimer.cancel();
+            heartBeatTimer = null;
+        }
+    }
+
+    public void setOnline(boolean online) {
+        this.isOnline = online;
+        updateStatusUI();
+        if (statusChangeListener != null) {
+            statusChangeListener.onStatusChanged(this, online);
+        }
+    }
+
+    private void updateStatusUI() {
+        SwingUtilities.invokeLater(() -> {
+            if (isOnline) {
+                statusLabel.setText("在线");
+                statusLabel.setForeground(Color.GREEN);
+            } else {
+                statusLabel.setText("离线");
+                statusLabel.setForeground(Color.RED);
+            }
+        });
+    }
+
+    public void processTighteningData(String content) {
+        if (MesClient.work_status != 1) {
+            YgslUtil.lastTighteningResultDataAcknowledge(gunClient);
+            return;
+        }
+
+        String tighteningStatus = com.mes.ygsl.YgslParam.getTighteningStatus(content);
+        String torqueStatus = com.mes.ygsl.YgslParam.getTorqueStatus(content);
+        String angleStatus = com.mes.ygsl.YgslParam.getAngleStatus(content);
+        String torque = com.mes.ygsl.YgslParam.getTorque(content);
+        String angle = com.mes.ygsl.YgslParam.getAngle(content);
+        String tighteningID = com.mes.ygsl.YgslParam.getTighteningID(content);
+        String jobID = com.mes.ygsl.YgslParam.getJobID(content);
+
+        if (tighteningID.isEmpty() || tighteningIds.contains(tighteningID)) {
+            YgslUtil.lastTighteningResultDataAcknowledge(gunClient);
+            return;
+        }
+
+        tighteningIds.add(tighteningID);
+
+        String pos = String.valueOf(gunIndex);
+
+        Boolean checkRet = JdbcUtils.checkTighteningById(tighteningID, pos, jobID);
+        if (!checkRet) {
+            if (tighteningStatus.equals("0")) {
+                if (torqueStatus.equals("1")) {
+                    torqueLabel.setForeground(Color.BLACK);
+                } else {
+                    torqueLabel.setForeground(Color.RED);
+                }
+                if (angleStatus.equals("1")) {
+                    angleLabel.setForeground(Color.BLACK);
+                } else {
+                    angleLabel.setForeground(Color.RED);
+                }
+            } else {
+                torqueLabel.setForeground(Color.BLACK);
+                angleLabel.setForeground(Color.BLACK);
+                if (tighteningStatus.equals("1")) {
+                    currentCount++;
+                }
+            }
+
+            final int displayCount = tighteningStatus.equals("1") ? currentCount : currentCount + 1;
+            SwingUtilities.invokeLater(() -> {
+                torqueLabel.setText(torque);
+                angleLabel.setText(angle);
+                scheduleLabel.setText(displayCount + "/" + taskCount);
+            });
+
+            JdbcUtils.insertTighteningData(
+                    MesClient.mes_gw, MesClient.mes_line_sn, MesClient.product_sn.getText(),
+                    tighteningStatus, torqueStatus, angleStatus, "", "", "", torque,
+                    "", "", "", angle, tighteningID, jobID, String.valueOf(displayCount),
+                    pos, MesClient.user_menu.getText()
+            );
+
+            if (currentCount >= taskCount) {
+                disableTool();
+                if (taskCompletedListener != null) {
+                    taskCompletedListener.onTaskCompleted(this);
+                }
+            }
+        }
+
+        YgslUtil.lastTighteningResultDataAcknowledge(gunClient);
+
+        if (dataListener != null) {
+            dataListener.onTighteningData(this, tighteningStatus, torque, angle, tighteningID, jobID, pos);
+        }
+    }
+
+    public void enableTool() {
+        if (gunClient != null && isOnline) {
+            YgslUtil.enableTool(gunClient);
+        }
+    }
+
+    public void disableTool() {
+        if (gunClient != null && isOnline) {
+            YgslUtil.disableTool(gunClient);
+        }
+    }
+
+    public void pSet() {
+        if (gunClient != null && isOnline) {
+            YgslUtil.pSet(gunClient);
+        }
+    }
+
+    public void startJob() {
+        if (gunClient != null && isOnline) {
+            YgslUtil.selectJob(gunClient);
+        }
+    }
+
+    public void reset() {
+        currentCount = 0;
+        tighteningIds.clear();
+        SwingUtilities.invokeLater(() -> {
+            torqueLabel.setText("");
+            angleLabel.setText("");
+            scheduleLabel.setText("0/" + taskCount);
+            torqueLabel.setForeground(Color.BLACK);
+            angleLabel.setForeground(Color.BLACK);
+        });
+    }
+
+    public boolean isTaskCompleted() {
+        return currentCount >= taskCount;
+    }
+
+    public int getCurrentCount() {
+        return currentCount;
+    }
+
+    public int getTaskCount() {
+        return taskCount;
+    }
+
+    public boolean isOnline() {
+        return isOnline;
+    }
+
+    public YgslConfig getConfig() {
+        return config;
+    }
+
+    public int getGunIndex() {
+        return gunIndex;
+    }
+
+    public YgslClient getGunClient() {
+        return gunClient;
+    }
+
+    public int getAutoSubmit() {
+        return config.getAutoSubmit();
+    }
+
+    public void setDataListener(OnTighteningDataListener listener) {
+        this.dataListener = listener;
+    }
+
+    public void setTaskCompletedListener(OnTaskCompletedListener listener) {
+        this.taskCompletedListener = listener;
+    }
+
+    public void setStatusChangeListener(OnStatusChangeListener listener) {
+        this.statusChangeListener = listener;
+    }
+
+    public void destroy() {
+        disconnect();
+    }
+
+    public void updateConfig(YgslConfig newConfig) {
+        this.config = newConfig;
+        this.taskCount = newConfig.getTaskCount();
+        SwingUtilities.invokeLater(() -> {
+            scheduleLabel.setText(currentCount + "/" + taskCount);
+        });
+    }
+}

+ 121 - 0
src/com/mes/ygsl/YgslConfig.java

@@ -0,0 +1,121 @@
+package com.mes.ygsl;
+
+public class YgslConfig {
+    private int id;
+    private int gunIndex;
+    private String gunName;
+    private String ipAddress;
+    private int port;
+    private int taskCount;
+    private int enabled;
+    private int autoSubmit;
+    private String createTime;
+    private String updateTime;
+
+    public YgslConfig() {
+    }
+
+    public YgslConfig(int gunIndex, String gunName, String ipAddress, int port, int taskCount) {
+        this.gunIndex = gunIndex;
+        this.gunName = gunName;
+        this.ipAddress = ipAddress;
+        this.port = port;
+        this.taskCount = taskCount;
+        this.enabled = 1;
+        this.autoSubmit = 1;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public int getGunIndex() {
+        return gunIndex;
+    }
+
+    public void setGunIndex(int gunIndex) {
+        this.gunIndex = gunIndex;
+    }
+
+    public String getGunName() {
+        return gunName;
+    }
+
+    public void setGunName(String gunName) {
+        this.gunName = gunName;
+    }
+
+    public String getIpAddress() {
+        return ipAddress;
+    }
+
+    public void setIpAddress(String ipAddress) {
+        this.ipAddress = ipAddress;
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public void setPort(int port) {
+        this.port = port;
+    }
+
+    public int getTaskCount() {
+        return taskCount;
+    }
+
+    public void setTaskCount(int taskCount) {
+        this.taskCount = taskCount;
+    }
+
+    public int getEnabled() {
+        return enabled;
+    }
+
+    public void setEnabled(int enabled) {
+        this.enabled = enabled;
+    }
+
+    public int getAutoSubmit() {
+        return autoSubmit;
+    }
+
+    public void setAutoSubmit(int autoSubmit) {
+        this.autoSubmit = autoSubmit;
+    }
+
+    public String getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(String createTime) {
+        this.createTime = createTime;
+    }
+
+    public String getUpdateTime() {
+        return updateTime;
+    }
+
+    public void setUpdateTime(String updateTime) {
+        this.updateTime = updateTime;
+    }
+
+    @Override
+    public String toString() {
+        return "YgslConfig{" +
+                "id=" + id +
+                ", gunIndex=" + gunIndex +
+                ", gunName='" + gunName + '\'' +
+                ", ipAddress='" + ipAddress + '\'' +
+                ", port=" + port +
+                ", taskCount=" + taskCount +
+                ", enabled=" + enabled +
+                ", autoSubmit=" + autoSubmit +
+                '}';
+    }
+}