package com.mes.ui; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.fazecast.jSerialComm.SerialPort; import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp; import com.mes.component.MesRadio; import com.mes.component.MesWebView; import com.mes.netty.NettyClient; import com.mes.util.DateLocalUtils; import com.mes.util.JdbcUtils; import javafx.embed.swing.JFXPanel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.time.LocalDateTime; import java.util.*; import java.util.List; import java.util.Timer; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class MesClient extends JFrame { private static final Logger log = LoggerFactory.getLogger(MesClient.class); private static String Drivde = "org.sqlite.JDBC"; public static int mes_auth = 0; // 权限级别 0=无权限 1=操作工人 2=管理员 public static String mes_gw = ""; // 工位号 public static String mes_gw_des = ""; // 工位名称 public static String mes_server_ip = ""; // 服务器IP地址 public static int mes_tcp_port = 3000; // TCP服务端口 public static int mes_heart_beat_cycle = 10; // 心跳周期 public static int mes_heart_icon_cycle = 1; public static String mes_line_sn = ""; // 产线编号 //TCP连接 public static NettyClient nettyClient; //TCP连接状态 public static boolean tcp_connect_flag = false; //TCP连接请求 public static boolean connect_request_flag = false; //session public static String sessionid = ""; public static JPanel contentPane; public static MesClient mesClientFrame; public static JTabbedPane tabbedPane; public static JScrollPane indexScrollPaneA; public static JScrollPane searchScrollPane; public static JScrollPane searchScrollPaneDj; public static Boolean check_quality_result = false; public static Integer work_status = 0; public static JButton heart_beat_menu; public static JButton status_menu; public static JButton user_menu; public static int scan_type = 0; public static JButton finish_ok_bt; public static JButton finish_ng_bt; public static JTextField product_sn; public static JButton f_scan_data_bt_1; public static JTextField product_sn_lb; public static JButton f_scan_data_bt_2; public static String user20 = ""; public static JFrame welcomeWin; public static JPanel indexPanelB; public static MesWebView jfxPanel = null; public static JPanel indexPanelC; public static MesWebView jfxPanel2 = null; public static MesRadio mesRadioHj; public static JTextField param1; public static JTextField param2; public static JTextField param3; public static JTextField param4; public static ModbusTcp plcA = new ModbusTcp(1, "192.168.1.7"); // public static ModbusTcp plcB = new ModbusTcp(1, "192.168.1.8"); public static Timer cjTimer; public static Timer cjTimer2; public static Timer cjTimer3; public static Short aSetNum = 48; public static Short sortA = 0; public static Short aMax = 0; public static Short aFinish = 0; //合格数量 public static List alist = new ArrayList<>(); public static Short bSetNum = 10; public static Short sortB = 0; public static Short bMax = 0; public static List blist = new ArrayList<>(); public static Short deviceControl = 0; // 0=本地 1=远程 public static Integer tjStatus = 0; // 1=提交失败 public static String tjFlagTextErr = "结果上传MES失败,请重试"; public static String curSn = ""; public static Integer zdStatusA = 0; public static String foutA = ""; public static String fmaxA = ""; public static String fminA = ""; public static String soutA = ""; public static String smaxA = ""; public static String sminA = ""; public static String curA = ""; public static JButton resetResultA; public static JButton okResultA; public static JButton ngResultA; public static JTable table; public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" }; public static Object[][] rowData = null; public static Boolean checkState = false; // 点检状态 false=未点检 true=已点检 public static SerialPort serialPort; // 串口对象 public static JLabel lblNewLabel; public static java.util.Timer deviceHeartTimer = null; public static Short curInit = 0; // 初始拉铆数量 public static Boolean fxModeVal = false; public static JButton fxMode; public static JButton fxMaxBtn; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { try{ //读文件配置 readProperty(); // 显示界面 mesClientFrame = new MesClient(); mesClientFrame.setVisible(false); JdbcUtils.getConn(); welcomeWin = new LoginFarme(); welcomeWin.setVisible(true); getPlcParam(); upParams(); setDeviceStart(); getMaterailData(); deviceOnline(); }catch (Exception e){ log.error("系统初始化异常: ", e); e.printStackTrace(); } } }); } public static java.util.Timer cjTimer4; public static void setDeviceStart() { if(cjTimer4!=null) { cjTimer4.cancel(); } cjTimer4 = new Timer(); cjTimer4.schedule(new TimerTask() { public void run() { try{ if(work_status == 1 && zdStatusA == 0){ List yxstatus = plcA.readCoil(456,1); if(yxstatus.size() >= 1 && !yxstatus.get(0)){ ModbusUtil.setPowerOn(MesClient.plcA); // 远程关机 } } }catch (Exception e){ log.error("设备启动监控异常: ", e); e.printStackTrace(); } } }, 1000,1000); } //读配置文件 private static void readProperty() throws IOException{ String enconding = "UTF-8"; InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties"); Properties pro = new Properties(); BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding)); pro.load(br); mes_gw = pro.getProperty("mes.gw"); // mes_gw_des = pro.getProperty("mes.gw_des"); mes_server_ip = pro.getProperty("mes.server_ip"); mes_tcp_port = Integer.parseInt(pro.getProperty("mes.tcp_port")); mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle")); mes_line_sn = pro.getProperty("mes.line_sn"); mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw); log.info("加载配置: gw={}, gw_des={}, server_ip={}, tcp_port={}, heart_beat_cycle={}", mes_gw, mes_gw_des, mes_server_ip, mes_tcp_port, mes_heart_beat_cycle); } public static void getPlcParam() { if(cjTimer!=null) { cjTimer.cancel(); } cjTimer = new Timer(); cjTimer.schedule(new TimerTask() { public void run() { try{ ModbusUtil.getDataA(plcA); }catch (Exception e){ e.printStackTrace(); } // try{ // ModbusUtil.getDataB(plcB); // }catch (Exception e){ // e.printStackTrace(); // } } }, 1000,500); } public static java.util.Timer cjTimer5; public static void deviceOnline() { if(cjTimer5!=null) { cjTimer5.cancel(); } cjTimer5 = new Timer(); cjTimer5.schedule(new TimerTask() { public void run() { try{ boolean lamaoret = ModbusUtil.getOnOff(plcA); if(lamaoret){ lblNewLabel.setText("A枪:在线"); lblNewLabel.setForeground(Color.GREEN); }else{ lblNewLabel.setText("A枪:离线"); lblNewLabel.setForeground(Color.RED); } if(work_status == 1){ List yxstatus = plcA.readCoil(3128,1); if(yxstatus.size() >= 1 && !yxstatus.get(0)){ if(!MesClient.curSn.isEmpty() && MesClient.aMax > 0 && MesClient.aMax == MesClient.sortA){ ModbusUtil.setPowerOff(plcA); }else{ ModbusUtil.setPowerOn(plcA); } } }else{ List yxstatus = plcA.readCoil(3128,1); if(yxstatus.size() >= 1 && yxstatus.get(0)){ ModbusUtil.setPowerOff(plcA); } } }catch (Exception e){ e.printStackTrace(); } } }, 1000,1000); } public static void upParams() { if(cjTimer2!=null) { cjTimer2.cancel(); } cjTimer2 = new Timer(); cjTimer2.schedule(new TimerTask() { public void run() { try{ List prods = JdbcUtils.getProds(); log.info("【参数定时上传】获取到待同步的生产数据,记录数: {}", prods.size()); if(prods.size() > 0){ log.debug("【参数定时上传】请求数据详情: {}", JSON.toJSONString(prods)); // 定时上传参数 JSONObject retObj = DataUtil.upParams(JSON.toJSONString(prods)); log.info("【参数定时上传】服务端返回结果: {}", retObj != null ? retObj.toJSONString() : "null"); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { // 更新sqlite已同步 for(ProdReq prodReq:prods){ JdbcUtils.updateProdSync(prodReq.getId(),1); } log.info("【参数定时上传】本地 SQLite 状态更新完成"); } else { log.warn("【参数定时上传】接口返回非 true,未更新本地状态"); } } }catch (Exception e){ log.error("【参数定时上传】发生异常: ", e); e.printStackTrace(); } } }, 1000,30*1000); } // 初始化TCP public static void initTcpConnection() { try { if(nettyClient==null) { //初始化TCP连接 nettyClient = new NettyClient(); //TCP连接状态 tcp_connect_flag = false; //设置TCP请求状态 connect_request_flag = true; DataUtil.synrTcp(nettyClient,mes_gw); } } catch (Exception e) { log.error("初始化TCP连接异常: ", e); e.printStackTrace(); } } //启动心跳包程序 public static java.util.Timer heartBeatTimer; public static java.util.Timer heartBeatIconTimer; public static boolean iconREDFlag = true; public static void startHeartBeatTimer() { if(heartBeatTimer!=null) { heartBeatTimer.cancel(); } heartBeatTimer = new java.util.Timer(); heartBeatTimer.schedule(new TimerTask() { public void run() { if(nettyClient!=null&&tcp_connect_flag) { DataUtil.heartBeat(nettyClient,mes_gw); heart_beat_menu.setText(DateLocalUtils.getCurrentTime()); } } }, 100,mes_heart_beat_cycle*1000); //心跳显示图标 if(heartBeatIconTimer!=null) { heartBeatIconTimer.cancel(); } heartBeatIconTimer = new Timer(); heartBeatIconTimer.schedule(new TimerTask() { public void run() { if(tcp_connect_flag) { if(iconREDFlag) { heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png"))); iconREDFlag = false; }else { heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png"))); iconREDFlag = true; } heart_beat_menu.setText(DateLocalUtils.getCurrentTime()); heart_beat_menu.repaint(); }else { heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png"))); heart_beat_menu.setText(DateLocalUtils.getCurrentTime()); heart_beat_menu.repaint(); //若未连接则尝试连接 if(nettyClient!=null&&!tcp_connect_flag){ log.warn("TCP已断开,尝试重新连接"); //TCP重新同步连接 if(!connect_request_flag) { //设置TCP请求状态,只重新同步连接一次 connect_request_flag = true; DataUtil.synrTcp(nettyClient,mes_gw); } } } } }, 100,mes_heart_icon_cycle*1000); } //设置tcp连接状态显示 public static void setTcpStatus() { if(tcp_connect_flag) { status_menu.setText("已连接MES服务器"); heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png"))); heart_beat_menu.repaint(); }else { status_menu.setText("未连接MES服务器"); heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png"))); heart_beat_menu.repaint(); } } public static void initWarehouseData(){ resetScanA(); } public static void resetScanA() { log.info("【系统操作】执行产品及面板状态重置 (resetScanA)"); work_status = 0; check_quality_result = false; MesClient.finish_ok_bt.setEnabled(false); MesClient.finish_ng_bt.setEnabled(false); product_sn.setText(""); curSn = ""; product_sn_lb.setText(""); f_scan_data_bt_2.setEnabled(false); MesClient.f_scan_data_bt_1.setEnabled(true); MesClient.setMenuStatus("请扫工件码",0); MesClient.aMax = 0; MesClient.aFinish = 0; MesClient.bMax = 0; MesClient.alist = new ArrayList<>(); MesClient.blist = new ArrayList<>(); MesClient.sortB = 0; MesClient.sortA = 0; MesClient.param1.setText(""); MesClient.param2.setText(""); MesClient.param3.setText(""); MesClient.param4.setText(""); MesClient.param3.setVisible(false); MesClient.param4.setVisible(false); ModbusUtil.setPowerOff(MesClient.plcA); // 远程关机 // ModbusUtil.setMesUnAllow(MesClient.plcA, MesClient.deviceHeartTimer); ModbusUtil.setTask(MesClient.plcA,aSetNum); updateMaterailData(); zdStatusA = 0; resetResultA.setEnabled(false); okResultA.setEnabled(false); ngResultA.setEnabled(false); // shiftUserCheck(); } public static int userLoginHours;//用户登录所处小时 public static JTextField fxMaxNum; //换班用户信息检查 private static void shiftUserCheck() { LocalDateTime now = LocalDateTime.now(); // 判断时间范围 if (userLoginHours >= 8 && userLoginHours < 20) { int hour = now.getHour(); if (hour >= 20 || hour < 8) { logoff(); } } else { int hour = now.getHour(); if (hour >= 8 && hour < 20) { logoff(); } } } public static void resetScanB() { } //获取用户20位 public static void getUser() { user20 = user_menu.getText().toString(); String space_tmp1 = ""; if(user20.length()<20) { for(int i=0;i<20-user20.length();i++) { space_tmp1 = space_tmp1 + " "; } } user20 = user20 + space_tmp1; } //获取barcode内容36位 public static String getBarcode(String barcodeTmp) { String barcodeRet = barcodeTmp; if(barcodeTmp.equalsIgnoreCase("")) { return ""; }else { if(barcodeTmp.length()<36) { String space = ""; for(int i=0;i<36-barcodeTmp.length();i++) { space = space + " "; } barcodeRet = barcodeTmp + space; } } return barcodeRet; } public static void scanBarcode() { // if(work_status == 1){ // JOptionPane.showMessageDialog(mesClientFrame,"工作中,勿扫码","提示窗口", JOptionPane.INFORMATION_MESSAGE); // return; // } String scanBarcodeTitle = ""; switch(scan_type) { case 1: product_sn.setText(""); scanBarcodeTitle = "请扫工件码"; break; } //弹窗扫工件码 String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle); if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) { //获取用户 getUser(); //获取扫码内容36位 String barcode36 = getBarcode(scanBarcode);//处理36为码 //工位号 String gw = ""; switch(scan_type) { case 1: product_sn.setText(scanBarcode); MesClient.curSn = scanBarcode; break; } //刷新界面 mesClientFrame.repaint(); if(!tcp_connect_flag) { JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } if(!fxModeVal){ // 查询工件质量 log.info("【质量检查-扫码触发】准备发送扫码质量询问报文, SN: {}, User: {}", barcode36, user20); Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20); log.info("【质量检查-扫码触发】请求发送结果: {}", sendret); if(!sendret){ JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } }else{ MesClient.check_quality_result = true;//质量合格,可以绑定加工 MesClient.setMenuStatus("该工件可以加工",0); MesClient.getUser(); DataUtil.startWork(MesClient.nettyClient,barcode36,MesClient.user20); MesClient.f_scan_data_bt_2.setEnabled(true); } }else { JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } } public static void logoff() { log.info("【系统操作】用户执行退出登录 (logoff)"); // welcomeWin.setVisible(true); mesClientFrame.setVisible(false); nettyClient = null; // if(heartBeatTimer!=null) { // heartBeatTimer.cancel(); // } // if(heartBeatIconTimer!=null) { // heartBeatIconTimer.cancel(); // } tcp_connect_flag = false; connect_request_flag = false; } public MesClient() { setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png"))); setTitle("MES系统客户端:"+mes_gw + "- " + mes_gw_des); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(0, 0, 1024, 768); JMenuBar menuBar = new JMenuBar(); menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26)); setJMenuBar(menuBar); JMenu fileMenu = new JMenu("用户"); fileMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png"))); fileMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20)); menuBar.add(fileMenu); JMenuItem exitMenuItem = new JMenuItem("退出"); exitMenuItem.setIcon(new ImageIcon(MesClient.class.getResource("/bg/logoff.png"))); exitMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 22)); fileMenu.add(exitMenuItem); exitMenuItem.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) {//只能检测到mousePressed事件 super.mouseClicked(e); //dispose(); logoff(); } }); JMenu settingMenu = new JMenu("设置"); //settingMenu.setVisible(false); settingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png"))); settingMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20)); menuBar.add(settingMenu); JMenuItem resetTcpMenu = new JMenuItem("\u91CD\u8FDEMES"); resetTcpMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png"))); resetTcpMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20)); resetTcpMenu.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { super.mouseClicked(e); log.info("【系统设置】用户点击了 [重连MES] 菜单"); //重连mes if(nettyClient != null && nettyClient.future != null && nettyClient.future.channel() != null) { nettyClient.future.channel().close(); } } }); settingMenu.add(resetTcpMenu); JMenuItem resetTcpMenu_1 = new JMenuItem("刷新工件"); resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png"))); resetTcpMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 20)); resetTcpMenu_1.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { super.mouseClicked(e); log.info("【系统设置】用户点击了 [刷新工件] 菜单"); resetScanA(); } }); settingMenu.add(resetTcpMenu_1); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BorderLayout(0, 0)); JToolBar toolBar = new JToolBar(); contentPane.add(toolBar, BorderLayout.NORTH); JLabel equipment_statu_label = new JLabel("状态:"); equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER); equipment_statu_label.setForeground(Color.BLACK); equipment_statu_label.setFont(new Font("微软雅黑", Font.PLAIN, 20)); equipment_statu_label.setBackground(Color.LIGHT_GRAY); toolBar.add(equipment_statu_label); status_menu = new JButton("设备未连接MES服务器"); if(tcp_connect_flag) { status_menu.setText("已连接MES服务器"); status_menu.setForeground(Color.GREEN); }else { status_menu.setText("未连接MES服务器"); status_menu.setForeground(Color.DARK_GRAY); } status_menu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); status_menu.setForeground(Color.GREEN); status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20)); status_menu.setBackground(Color.BLACK); toolBar.add(status_menu); JLabel space_1 = new JLabel(" "); toolBar.add(space_1); JLabel heart_beat_status_label = new JLabel("心跳:"); heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER); heart_beat_status_label.setForeground(Color.BLACK); heart_beat_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20)); heart_beat_status_label.setBackground(Color.LIGHT_GRAY); toolBar.add(heart_beat_status_label); heart_beat_menu = new JButton("2024-02-20 23:20:10"); heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png"))); heart_beat_menu.setForeground(Color.GREEN); heart_beat_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20)); heart_beat_menu.setBackground(Color.BLACK); toolBar.add(heart_beat_menu); JLabel space_2 = new JLabel(" "); toolBar.add(space_2); JLabel user_status_label = new JLabel("登录用户:"); user_status_label.setHorizontalAlignment(SwingConstants.CENTER); user_status_label.setForeground(Color.BLACK); user_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20)); user_status_label.setBackground(Color.LIGHT_GRAY); toolBar.add(user_status_label); user_menu = new JButton("JinJuShi"); user_menu.setForeground(Color.GREEN); user_menu.setFont(new Font("微软雅黑", Font.PLAIN, 22)); user_menu.setBackground(Color.BLACK); toolBar.add(user_menu); JLabel space_3 = new JLabel(" "); toolBar.add(space_3); JLabel space_4 = new JLabel(" "); toolBar.add(space_4); tabbedPane = new JTabbedPane(JTabbedPane.TOP); tabbedPane.setMinimumSize(new Dimension(400, 50)); tabbedPane.setFont(new Font("宋体", Font.BOLD, 22)); contentPane.add(tabbedPane); //首页 JPanel indexPanelA = new JPanel(); indexScrollPaneA = new JScrollPane(indexPanelA); indexPanelA.setLayout(null); product_sn = new JTextField(); product_sn.setHorizontalAlignment(SwingConstants.CENTER); product_sn.setEditable(false); product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28)); product_sn.setBounds(81, 70, 602, 70); indexPanelA.add(product_sn); product_sn.setColumns(10); f_scan_data_bt_1 = new JButton("扫码"); f_scan_data_bt_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { log.info("【总控操作】点击了 [扫码(工件)] 按钮"); scan_type = 1; scanBarcode(); } }); f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png"))); f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 32)); f_scan_data_bt_1.setBounds(693, 70, 198, 70); indexPanelA.add(f_scan_data_bt_1); // String[] hjtitles = new String[]{"焊机1","焊机2","焊机3"}; // String[] hjvals = new String[]{"HJ001","HJ002","HJ003"}; // mesRadioHj = new MesRadio(hjtitles,hjvals); // mesRadioHj.setSize(500,50); // mesRadioHj.setBounds(190,170,500,50); // indexPanelA.add(mesRadioHj); JLabel lbLabel = new JLabel("冷板码:"); lbLabel.setFont(new Font("微软雅黑", Font.PLAIN, 24)); lbLabel.setBounds(20, 165, 100, 50); indexPanelA.add(lbLabel); product_sn_lb = new JTextField(); product_sn_lb.setHorizontalAlignment(SwingConstants.CENTER); product_sn_lb.setEditable(false); product_sn_lb.setFont(new Font("微软雅黑", Font.PLAIN, 28)); product_sn_lb.setBounds(100, 160, 580, 70); indexPanelA.add(product_sn_lb); product_sn_lb.setColumns(10); f_scan_data_bt_2 = new JButton("扫码"); f_scan_data_bt_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // scanBarcodeLb(); // scan_type = 1; log.info("【总控操作】点击了 [扫码(冷板)] 按钮"); if (product_sn.getText().isEmpty()){ setMenuStatus("请在工件上料完成后扫码!",1); return; } if (work_status == 1&&check_quality_result){ scanBarcodeLb(); }else{ setMenuStatus("请在工件质量检测合格后扫码!",1); } } }); f_scan_data_bt_2.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png"))); f_scan_data_bt_2.setFont(new Font("微软雅黑", Font.PLAIN, 30)); f_scan_data_bt_2.setBounds(693, 160, 140, 70); f_scan_data_bt_2.setEnabled(false); indexPanelA.add(f_scan_data_bt_2); finish_ok_bt = new JButton("OK"); finish_ok_bt.setEnabled(false); finish_ok_bt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { log.info("【总控操作】点击了 [OK] 按钮,当前产品SN: {}, 操作用户: {}", product_sn.getText(), user_menu.getText()); if(work_status == 1 && check_quality_result){ if (product_sn_lb.getText().equals("")){ JOptionPane.showMessageDialog(MesClient.mesClientFrame,"请绑定冷板码","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } String sn = getBarcode(product_sn.getText()); getUser(); String qret = "OK"; log.info("【判定上传】准备发送质量判定结果, SN: {}, 结果: {}", sn, qret); Boolean sendret = DataUtil.sendQuality(nettyClient,sn,qret,user20); log.info("【判定上传】发送结果: {}", sendret); if(!sendret){ MesClient.setMenuStatus("消息发送失败,请重试",-1); // JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } } } }); finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png"))); finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32)); // finish_ok_bt.setBounds(185, 291, 240, 80); finish_ok_bt.setBounds(185, 371, 240, 80); finish_ok_bt.setEnabled(false); indexPanelA.add(finish_ok_bt); finish_ng_bt = new JButton("NG"); finish_ng_bt.setEnabled(false); finish_ng_bt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { log.info("【总控操作】点击了 [NG] 按钮,当前产品SN: {}, 操作用户: {}", product_sn.getText(), user_menu.getText()); if(work_status == 1 && check_quality_result){ String sn = getBarcode(product_sn.getText()); getUser(); String qret = "NG"; log.info("【判定上传】准备发送质量判定结果, SN: {}, 结果: {}", sn, qret); Boolean sendret = DataUtil.sendQuality(nettyClient,sn,qret,user20); log.info("【判定上传】发送结果: {}", sendret); if(!sendret){ MesClient.setMenuStatus("消息发送失败,请重试",-1); // JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } } } }); finish_ng_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ng_bg.png"))); finish_ng_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32)); finish_ng_bt.setBounds(508, 371, 240, 80); finish_ng_bt.setEnabled(false); indexPanelA.add(finish_ng_bt); lblNewLabel = new JLabel("A枪"); lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20)); // lblNewLabel.setBounds(204, 186, 166, 28); lblNewLabel.setBounds(304, 186, 166, 28); indexPanelA.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u9884\u8BBE\u6570\u91CF"); lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); // lblNewLabel_1.setBounds(204, 241, 83, 34); lblNewLabel_1.setBounds(185, 241, 83, 34); indexPanelA.add(lblNewLabel_1); param1 = new JTextField(); param1.setHorizontalAlignment(SwingConstants.CENTER); param1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); param1.setText("0"); param1.setEditable(false); // param1.setBounds(288, 241, 83, 34); param1.setBounds(269, 241, 83, 34); indexPanelA.add(param1); param1.setColumns(10); JLabel lblNewLabel_1_1 = new JLabel("\u5B8C\u6210\u6570\u91CF"); lblNewLabel_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); // lblNewLabel_1_1.setBounds(204, 285, 83, 34); lblNewLabel_1_1.setBounds(185, 285, 83, 34); indexPanelA.add(lblNewLabel_1_1); param2 = new JTextField(); param2.setHorizontalAlignment(SwingConstants.CENTER); param2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); param2.setText("0"); param2.setEditable(false); param2.setColumns(10); // param2.setBounds(288, 285, 83, 34); param2.setBounds(269, 285, 83, 34); indexPanelA.add(param2); JLabel lblB = new JLabel("B\u67AA"); lblB.setHorizontalAlignment(SwingConstants.CENTER); lblB.setFont(new Font("微软雅黑", Font.PLAIN, 20)); lblB.setBounds(525, 186, 166, 28); // indexPanelA.add(lblB); JLabel lblNewLabel_1_2 = new JLabel("\u9884\u8BBE\u6570\u91CF"); lblNewLabel_1_2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); lblNewLabel_1_2.setBounds(525, 241, 83, 34); // indexPanelA.add(lblNewLabel_1_2); param3 = new JTextField(); param3.setText("0"); param3.setHorizontalAlignment(SwingConstants.CENTER); param3.setFont(new Font("微软雅黑", Font.PLAIN, 18)); param3.setEditable(false); param3.setColumns(10); param3.setBounds(608, 241, 83, 34); indexPanelA.add(param3); MesClient.param3.setVisible(false); JLabel lblNewLabel_1_1_1 = new JLabel("\u5B8C\u6210\u6570\u91CF"); lblNewLabel_1_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); lblNewLabel_1_1_1.setBounds(525, 285, 83, 34); // indexPanelA.add(lblNewLabel_1_1_1); param4 = new JTextField(); param4.setText("0"); param4.setHorizontalAlignment(SwingConstants.CENTER); param4.setFont(new Font("微软雅黑", Font.PLAIN, 18)); param4.setEditable(false); param4.setColumns(10); param4.setBounds(608, 285, 83, 34); indexPanelA.add(param4); MesClient.param4.setVisible(false); resetResultA = new JButton("重拉"); resetResultA.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { log.info("【A枪操作】点击了 [重拉] 按钮,当前产品SN: {}", product_sn.getText()); if(zdStatusA == 1){ log.info("【NG审核检查】准备校验 A枪 NG中断数据, SN: {}", product_sn.getText()); JSONObject retObj = DataUtil.checkNgData(product_sn.getText(),"A"); log.info("【NG审核检查】校验返回结果: {}", retObj != null ? retObj.toJSONString() : "null"); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { MesClient.zdStatusA = 0; MesClient.resetResultA.setEnabled(false); MesClient.okResultA.setEnabled(false); MesClient.ngResultA.setEnabled(false); MesClient.sortA = (short) (MesClient.sortA - 1); MesClient.param2.setText(MesClient.sortA+""); plcA.writeInt16(1136,MesClient.sortA); MesClient.foutA = ""; MesClient.soutA = ""; MesClient.fmaxA = ""; MesClient.fminA = ""; MesClient.smaxA = ""; MesClient.sminA = ""; MesClient.curA = ""; ModbusUtil.setPowerOn(plcA); }else{ MesClient.setMenuStatus("NG中断未审核",-1); } } } }); resetResultA.setEnabled(false); resetResultA.setFont(new Font("微软雅黑", Font.PLAIN, 16)); resetResultA.setBounds(508, 285, 100, 40); indexPanelA.add(resetResultA); okResultA = new JButton("OK"); okResultA.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { log.info("【A枪操作】点击了 [OK] 按钮,当前产品SN: {}", product_sn.getText()); if(zdStatusA == 1){ log.info("【NG审核检查】准备校验 A枪 NG中断数据, SN: {}", product_sn.getText()); JSONObject retObj = DataUtil.checkNgData(product_sn.getText(),"A"); log.info("【NG审核检查】校验返回结果: {}", retObj != null ? retObj.toJSONString() : "null"); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { if(!MesClient.product_sn.getText().isEmpty()){ MesClient.aFinish++; JdbcUtils.insertProdData(MesClient.mes_gw, MesClient.mes_line_sn, MesClient.product_sn.getText(),"A",foutA,soutA,fminA,sminA,fmaxA,smaxA,"1",curA+"", MesClient.user_menu.getText()); } MesClient.zdStatusA = 0; MesClient.resetResultA.setEnabled(false); MesClient.okResultA.setEnabled(false); MesClient.ngResultA.setEnabled(false); MesClient.foutA = ""; MesClient.soutA = ""; MesClient.fmaxA = ""; MesClient.fminA = ""; MesClient.smaxA = ""; MesClient.sminA = ""; MesClient.curA = ""; ModbusUtil.upResult(); ModbusUtil.setPowerOn(plcA); }else{ MesClient.setMenuStatus("NG中断未审核",-1); } } } }); okResultA.setEnabled(false); okResultA.setForeground(new Color(0, 128, 0)); okResultA.setFont(new Font("微软雅黑", Font.PLAIN, 16)); okResultA.setBounds(632, 285, 100, 40); indexPanelA.add(okResultA); ngResultA = new JButton("NG"); ngResultA.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { log.info("【A枪操作】点击了 [NG] 按钮,当前产品SN: {}", product_sn.getText()); if(zdStatusA == 1){ log.info("【NG审核检查】准备校验 A枪 NG中断数据, SN: {}", product_sn.getText()); JSONObject retObj = DataUtil.checkNgData(product_sn.getText(),"A"); log.info("【NG审核检查】校验返回结果: {}", retObj != null ? retObj.toJSONString() : "null"); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { if(!MesClient.product_sn.getText().isEmpty()){ JdbcUtils.insertProdData(MesClient.mes_gw, MesClient.mes_line_sn, MesClient.product_sn.getText(),"A",foutA,soutA,fminA,sminA,fmaxA,smaxA,"0",curA+"", MesClient.user_menu.getText()); } MesClient.zdStatusA = 0; MesClient.resetResultA.setEnabled(false); MesClient.okResultA.setEnabled(false); MesClient.ngResultA.setEnabled(false); MesClient.foutA = ""; MesClient.soutA = ""; MesClient.fmaxA = ""; MesClient.fminA = ""; MesClient.smaxA = ""; MesClient.sminA = ""; MesClient.curA = ""; ModbusUtil.upResult(); ModbusUtil.setPowerOn(plcA); }else{ MesClient.setMenuStatus("NG中断未审核",-1); } } } }); ngResultA.setEnabled(false); ngResultA.setForeground(new Color(255, 0, 0)); ngResultA.setFont(new Font("微软雅黑", Font.PLAIN, 16)); ngResultA.setBounds(759, 285, 100, 40); indexPanelA.add(ngResultA); fxMode = new JButton("返修模式"); fxMode.setFont(new Font("微软雅黑", Font.PLAIN, 16)); fxMode.setBounds(161, 10, 107, 40); indexPanelA.add(fxMode); fxMode.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fxModeVal = !fxModeVal; if(fxModeVal){ fxMode.setBackground(Color.GREEN); fxMode.setForeground(Color.WHITE); fxMaxNum.setText(""); fxMaxNum.setEditable(true); fxMaxBtn.setEnabled(true); }else{ fxMode.setBackground(null); fxMode.setForeground(null); fxMaxNum.setText(""); fxMaxNum.setEditable(false); fxMaxBtn.setEnabled(false); } } }); fxMaxNum = new JTextField(); fxMaxNum.setBounds(286, 10, 83, 40); indexPanelA.add(fxMaxNum); fxMaxNum.setColumns(10); fxMaxBtn = new JButton("确定数量"); fxMaxBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16)); fxMaxBtn.setBounds(379, 10, 107, 40); indexPanelA.add(fxMaxBtn); fxMaxBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int maxNum = Integer.parseInt(fxMaxNum.getText()); if(maxNum > 0){ ModbusUtil.setTask(MesClient.plcA,(short)maxNum); } } }); tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null); tabbedPane.setEnabledAt(0, true); // searchScrollPane = new JScrollPane((Component) null); indexPanelC = new JPanel(); searchScrollPaneDj = new JScrollPane(indexPanelC); indexPanelC.setLayout(null); tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null); indexPanelB = new JPanel(); searchScrollPane = new JScrollPane(indexPanelB); indexPanelB.setLayout(null); // tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null); tabbedPane.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { JTabbedPane tabbedPane = (JTabbedPane) e.getSource(); int selectedIndex = tabbedPane.getSelectedIndex(); System.out.println("selectedIndex:"+selectedIndex); // if(selectedIndex == 1){ // // } // 创建一个单线程的ExecutorService ExecutorService executorService = Executors.newSingleThreadExecutor(); // 提交一个异步任务 executorService.submit(() -> { System.out.println("异步线程正在运行..."); // 在这里执行异步操作 // getCheckResult(); }); // 关闭ExecutorService,以优雅的方式 executorService.shutdown(); } }); } public static void setMenuStatus(String msg,int error){ if(error == 0){ MesClient.status_menu.setForeground(Color.GREEN); }else{ MesClient.status_menu.setForeground(Color.RED); } MesClient.status_menu.setText(msg); } // 获取开班点检状态 public static void getCheckResult(){ try{ log.info("【开班点检】准备获取点检状态..."); JSONObject retObj = DataUtil.getCheckResult(); log.info("【开班点检】获取结果: {}", retObj != null ? retObj.toJSONString() : "null"); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { // 已点检 MesClient.checkState = true; }else{ // 未点检 MesClient.checkState = false; } }catch (Exception e){ log.error("【开班点检】获取点检状态异常: ", e); MesClient.checkState = false; } MesClient.formatDeviceState(); } // 格式化设备启动状态 public static void formatDeviceState(){ try{ if(MesClient.checkState){ // 检查物料是否已绑定 if(rowData == null){ System.out.println("openDevice1"); ModbusRtu.openDevice(serialPort); }else{ // 判断物料是否可启动 int flag = 0; int i = 0; for (Object object:rowData){ Integer times = Integer.valueOf(String.valueOf(rowData[i][2])); if(times < 1){ flag = 1; } i++; } if(flag == 1){ // 不能启动 System.out.println("closeDevice2"); ModbusRtu.closeDevice(serialPort); }else{ System.out.println("openDevice2"); // 可启动 ModbusRtu.openDevice(serialPort); } } }else{ System.out.println("closeDevice1"); //不能启动 ModbusRtu.closeDevice(serialPort); } }catch (Exception e){ e.printStackTrace(); } } public static void getMaterailData(){ JSONObject retObj = DataUtil.getBindMaterail(); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { java.util.List arrs = retObj.getList("data",BindMaterialResp.class); int i = 0; rowData = new Object[arrs.size()][7]; for (BindMaterialResp bindMaterialResp:arrs){ rowData[i][0] = bindMaterialResp.getMaterialTitle(); rowData[i][1] = bindMaterialResp.getBatchSn(); rowData[i][2] = bindMaterialResp.getLastTimes(); rowData[i][3] = ""; rowData[i][4] = bindMaterialResp.getCraft(); rowData[i][5] = bindMaterialResp.getMaterialId(); rowData[i][6] = bindMaterialResp.getType(); i++; } bindBatchPanel(); } } public static void updateMaterailData(){ JSONObject retObj = DataUtil.getBindMaterail(); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { List arrs = retObj.getList("data",BindMaterialResp.class); int i = 0; for (BindMaterialResp bindMaterialResp:arrs){ rowData[i][0] = bindMaterialResp.getMaterialTitle(); rowData[i][1] = bindMaterialResp.getBatchSn(); rowData[i][2] = bindMaterialResp.getLastTimes(); rowData[i][3] = ""; rowData[i][4] = bindMaterialResp.getCraft(); rowData[i][5] = bindMaterialResp.getMaterialId(); rowData[i][6] = bindMaterialResp.getType(); i++; } getCheckResult(); MesClient.table.repaint(); } } // 绑定物料批次码 public static void scanBatchSn(BindMaterialResp bindMaterialResp) { //弹窗扫工件码 String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle(); String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle); if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) { log.info("【物料绑定】扫描物料: {}, 批次码: {}", bindMaterialResp.getMaterialTitle(), scanBarcode); JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType()); log.info("【物料绑定】返回结果: {}", retObj != null ? retObj.toJSONString() : "null"); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { MesClient.setMenuStatus("扫物料:"+bindMaterialResp.getMaterialTitle()+"成功",0); updateMaterailData(); }else{ if(retObj.get("result")==null){ MesClient.setMenuStatus("请求失败,请重试",-1); }else{ if(retObj.get("result").toString().equalsIgnoreCase("false")){ MesClient.setMenuStatus(retObj.getString("message"),-1); } } } } } public static void bindBatchPanel(){ JPanel indexPanelBB = new JPanel(); JPanel panel = new JPanel(); panel.setBounds(0, 0, 990, 550); // panel.setBounds(81, 50, 810, 479); indexPanelBB.add(panel); panel.setLayout(new GridLayout(0, 1, 0, 0)); table = new JTable(rowData, columnNames){ public boolean isCellEditable(int row, int column) { if(column == 3){ return true; } return false; } }; table.setRowHeight(40); table.setEnabled(true); table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton()); table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton()); JScrollPane scrollPane = new JScrollPane(table); panel.add(scrollPane); JScrollPane indexScrollPaneB = new JScrollPane(indexPanelBB); indexPanelBB.setLayout(null); tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null); } public static void scanBarcodeLb() { String scanBarcodeTitle = ""; product_sn_lb.setText(""); // resetLb(); scanBarcodeTitle = "请扫冷板码"; //弹窗扫工件码 String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle); System.out.println("冷板码="+scanBarcode); if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) { //获取用户 getUser(); //获取扫码内容36位 String warehouseSn = getBarcode(scanBarcode);//处理36为码 //工位号 String gw = ""; product_sn_lb.setText(scanBarcode); //刷新界面 mesClientFrame.repaint(); if(!scanBarcode.isEmpty()) { // 检查工件质量 if(work_status == 1) { // 直接调用绑定 String sn = getBarcode(product_sn.getText()); getUser(); log.info("【冷板绑定】准备调用绑定接口, SN: {}, 冷板码: {}", sn, warehouseSn); JSONObject retObj = DataUtil.bindWarehouse(sn, warehouseSn, user20, "400006"); log.info("【冷板绑定】绑定返回结果: {}", retObj != null ? retObj.toJSONString() : "null"); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { MesClient.setMenuStatus("绑定成功",0); }else{ if(retObj.get("result")==null){ MesClient.setMenuStatus("请求失败,请重试",-1); }else{ if(retObj.get("result").toString().equalsIgnoreCase("false")){ MesClient.setMenuStatus(retObj.getString("message"),-1); } } } }else{ setMenuStatus("请先扫工件码",1); return; } }else { setMenuStatus("冷板码为空,请重新扫码",1); return; } }else { setMenuStatus("冷板码为空,请重新扫码",1); return; } } }