| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- package com.mes.component;
- import com.mes.ui.DataUtil;
- import com.mes.ui.MesClient;
- import com.mes.ui.Oprno;
- import com.mes.util.S7PLCUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.FocusAdapter;
- import java.awt.event.FocusEvent;
- import java.util.Timer;
- import java.util.TimerTask;
- public class MesClientComponent extends JPanel {
- public static final Logger log = LoggerFactory.getLogger(MesClientComponent.class);
- // 工位类
- public Oprno oprnoClass = new Oprno();
- // 工位号
- // 线体
- public static String lineSn = "XT";
- // 输入框
- public JTextField productSn;
- // 状态栏
- public JButton status_menu;
- // 宽度
- public static Integer workWidth = 350;
- // 高度
- public static Integer workHeight = 400;
- // 扫码按钮
- public JButton scanBtn;
- // 刷新按钮
- public JButton resetBtn;
- // 工作状态
- // 0:未扫码,等待允许扫码信号 1:扫码成功,质量检查 2:质量合格,开始做件 3:加工完成,提交结果
- public Integer work_status = 0;
- public JLabel sllabel1;
- public JLabel sllabel2;
- // 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);
- 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(margin, y, w - 2 * margin, hStatus);
- status_menu.setForeground(Color.GREEN);
- status_menu.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- }
- });
- 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);
- int snFont = Math.max(14, Math.min(22, w / 16));
- productSn.setFont(new Font("微软雅黑", Font.PLAIN, snFont));
- productSn.setEditable(false);
- productSn.setBounds(margin, y, w - 2 * margin, hSn);
- productSn.addFocusListener(new FocusAdapter() {
- @Override
- public void focusGained(FocusEvent e) {
- }
- });
- 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")));
- 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("刷新");
- resetBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- resetBtnClick();
- }
- });
- resetBtn.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
- 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, 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(w / 2, y, w - w / 2, hLab);
- this.add(sllabel2);
- this.startOprnoTimer();
- }
- // 扫码按钮
- public void scanBtnClick() {
- if (work_status != 0) {
- status_menu.setText("工件加工中,请勿重复扫码");
- return;
- }
- try {
- String sn = "";
- boolean hgA = MesClient.s7PLC.readBoolean(oprnoClass.getHgA_out());
- boolean hgB = MesClient.s7PLC.readBoolean(oprnoClass.getHgB_out());
- if (!hgA && !hgB) {
- sn = "";
- }
- if (hgA) {
- sn = MesClient.s7PLC.readString(oprnoClass.getHgA_sn());
- }else if (hgB) {
- sn = MesClient.s7PLC.readString(oprnoClass.getHgB_sn());
- }else {
- sn = MesClient.s7PLC.readString(oprnoClass.getProduct_sn());
- }
- if (sn == null || sn.trim().isEmpty() || "".equals(sn)) {
- setMenuStatus("扫码结果为空", -1);
- return;
- }
- sn = sn.trim();
- productSn.setText(sn);
- work_status = 1;
- setMenuStatus("已读取工件码,等待校验", 0);
- } catch (Exception e) {
- log.error("读取 PLC 工件码失败", e);
- setMenuStatus("读取 PLC 失败", -1);
- }
- }
- // 定时任务
- public Timer oprnoTimer;
- public void startOprnoTimer() {
- if(oprnoTimer!=null) {
- oprnoTimer.cancel();
- }
- oprnoTimer = new Timer();
- oprnoTimer.schedule(new TimerTask() {
- public void run() {
- if (work_status == 0){
- // 未扫码,等待允许扫码信号
- // 当遍历出有上料点位为true的工位时
- if (MesClient.s7PLC.readBoolean(oprnoClass.getHgA_out()) || MesClient.s7PLC.readBoolean(oprnoClass.getHgB_out())) {
- MesClient.s7PLC.writeBoolean(oprnoClass.getMes_out(),false);
- String sn = "";
- boolean hgA = MesClient.s7PLC.readBoolean(oprnoClass.getHgA_out());
- boolean hgB = MesClient.s7PLC.readBoolean(oprnoClass.getHgB_out());
- if (hgA) {
- // 获取A号料
- sn = MesClient.s7PLC.readString(oprnoClass.getHgA_sn());
- }
- if (hgB) {
- sn = MesClient.s7PLC.readString(oprnoClass.getHgB_sn());
- }
- if (sn == null || sn.trim().isEmpty() || "".equals(sn)){
- setMenuStatus("扫码结果为空", -1);
- return;
- }
- productSn.setText(sn);
- work_status = 1;
- }
- }else if (work_status == 1){
- // 已扫码,等待允许加工信号
- String sn = productSn.getText();
- if (sn == null || sn.trim().isEmpty() || "".equals(sn)) {
- setMenuStatus("读码结果为空", -1);
- productSn.setText("");
- work_status = 0;
- return;
- }
- Boolean checkQualityResult = DataUtil.checkQualityByGw(MesClient.nettyClient, sn,MesClient.user20,oprnoClass.getOprno());
- if (!checkQualityResult){
- setMenuStatus("消息发送失败,请重试",-1);
- }
- }else if (work_status == 2){
- if (MesClient.s7PLC.readBoolean(oprnoClass.getOut_ok())){
- MesClient.s7PLC.writeBoolean(oprnoClass.getMes_in(),false);
- }
- if (MesClient.s7PLC.readBoolean(oprnoClass.getProcess_ok())){
- MesClient.s7PLC.writeBoolean(oprnoClass.getMes_out(),true);
- String sn = productSn.getText();
- String ret = "OK";
- if (MesClient.s7PLC.readBoolean(oprnoClass.getProcess_result_ng())){
- ret = "NG";
- }
- Boolean result = DataUtil.sendQualityByGW(MesClient.nettyClient, sn, ret, MesClient.user20, oprnoClass.getOprno());
- if (!result){
- setMenuStatus("消息发送失败,请重试",-1);
- }
- }
- }else if (work_status == 3){
- productSn.setText("");
- work_status = 0;
- }
- }
- },100,1000);
- }
- // 刷新
- public void resetBtnClick() {
- productSn.setText("");
- work_status = 0;
- setMenuStatus("请扫码",0);
- }
- public void setMenuStatus(String msg,int error){
- if(error == 0){
- this.status_menu.setForeground(Color.GREEN);
- }else{
- this.status_menu.setForeground(Color.RED);
- }
- this.status_menu.setText(msg);
- }
- }
|