package com.mes.ui; import com.alibaba.fastjson2.JSONObject; import com.mes.component.MesRadio; import com.mes.component.MesWebView; import com.mes.component.StationPanel; import com.mes.plc.HeliumFlow; import com.mes.plc.McPlcClient; import com.mes.util.DateLocalUtils; import com.mes.util.HttpUtils; import com.mes.util.JdbcUtils; 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.*; import java.io.*; import java.sql.ResultSet; import java.sql.Statement; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Properties; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class MesClient extends JFrame { public static final Logger log = LoggerFactory.getLogger(MesClient.class); 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_heart_icon_cycle = 1; public static String mes_line_sn = ""; // 产线编号 // PLC(三菱 MC 3E) public static String plc_ip = ""; public static int plc_port = 2500; public static String plc_a_code_addr = "D5000"; public static String plc_b_code_addr = "D6000"; public static int plc_code_len = 24; //session public static String sessionid = ""; public static JPanel contentPane; public static MesClient mesClientFrame; public static JTabbedPane tabbedPane; // public static JScrollPane searchScrollPane; // 已弃用,改为使用 WorkRecordPanel 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 JButton plc_menu; // 氦检 A/B 双工位 public static StationPanel stationA; public static StationPanel stationB; public static JTextArea logArea; public static String user20 = ""; public static JFrame welcomeWin; // public static JPanel indexPanelB; // 已弃用,改为使用 WorkRecordPanel public static MesWebView jfxPanel = null; public static JPanel indexPanelC; public static MesWebView jfxPanel2 = null; public static MesRadio mesRadioHj; public static JTable table; public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" }; public static Object[][] rowData = null; public static void main(String[] args) { if (LockUtil.getInstance().isAppActive() == true){ // JOptionPane.showMessageDialog(null, "已有一个程序在运行,程序退出"); return; }else{ EventQueue.invokeLater(new Runnable() { @Override public void run() { try{ //读文件配置 readProperty(); // 显示界面 mesClientFrame = new MesClient(); mesClientFrame.setVisible(false); JdbcUtils.getConn(); welcomeWin = new LoginFarme(); welcomeWin.setVisible(true); getMaterailData(); }catch (Exception e){ e.printStackTrace(); } } }); } } //读配置文件 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_server_ip = pro.getProperty("mes.server_ip"); mes_line_sn = pro.getProperty("mes.line_sn"); mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw); plc_ip = pro.getProperty("plc.ip", "").trim(); plc_port = Integer.parseInt(pro.getProperty("plc.port", "2500").trim()); plc_a_code_addr = pro.getProperty("plc.a.code_addr", "D5000").trim(); plc_b_code_addr = pro.getProperty("plc.b.code_addr", "D6000").trim(); plc_code_len = Integer.parseInt(pro.getProperty("plc.code_len", "24").trim()); log.info(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + plc_ip + ":" + plc_port); } //启动心跳包程序 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(); } //心跳显示图标 if(heartBeatIconTimer!=null) { heartBeatIconTimer.cancel(); } heartBeatIconTimer = new Timer(); heartBeatIconTimer.schedule(new TimerTask() { public void run() { // 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(); } }, 100,mes_heart_icon_cycle*1000); } public static java.util.Timer onlineTimer; public static void checkOnline() { if(onlineTimer!=null) { onlineTimer.cancel(); } onlineTimer = new Timer(); onlineTimer.schedule(new TimerTask() { public void run() { Boolean retObj = DataUtil.isServerOnlineBySocket(); if(retObj) { heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png"))); }else{ heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png"))); } heart_beat_menu.repaint(); } }, 100,5000); } //设置tcp连接状态显示 public static void setTcpStatus() { 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() { work_status = 0; check_quality_result = false; if (stationA != null) { stationA.reset(); } if (stationB != null) { stationB.reset(); } MesClient.setMenuStatus("等待 PLC 扫码信号",0); updateMaterailData(); // shiftUserCheck(); } public static int userLoginHours;//用户登录所处小时 //换班用户信息检查 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 logoff() { welcomeWin.setVisible(true); mesClientFrame.setVisible(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); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); // 添加窗口监听器 addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { int choice = JOptionPane.showConfirmDialog(null, "确定要关闭窗口吗?", "关闭确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (choice == JOptionPane.YES_OPTION) { // 如果用户点击“是”,则关闭窗口 dispose(); // 释放窗口资源并关闭窗口 System.exit(0); } } }); 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); } }); // 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); 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服务器"); status_menu.setText("已连接MES服务器"); status_menu.setForeground(Color.GREEN); 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 plc_status_label = new JLabel("PLC:"); plc_status_label.setHorizontalAlignment(SwingConstants.CENTER); plc_status_label.setForeground(Color.BLACK); plc_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20)); toolBar.add(plc_status_label); plc_menu = new JButton("未连接"); plc_menu.setForeground(Color.RED); plc_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20)); plc_menu.setBackground(Color.BLACK); plc_menu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { showPlcSignals(); } }); toolBar.add(plc_menu); 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(); indexPanelA.setLayout(null); indexPanelA.setBackground(Color.WHITE); // 氦检双工位面板:A/B 并排卡片 + 底部日志 // 点位顺序:扫码成功、MES允许、检测开始、检测结束、泄露、本底、二维码 // 本底、二维码点位待 PLC 确认,先留空 stationA = new StationPanel("A 工位", new String[]{"M205", "M206", "M207", "M208", "D200", "--", "D5000"}); stationA.setBounds(10, 10, 460, 375); indexPanelA.add(stationA); stationB = new StationPanel("B 工位", new String[]{"M215", "M216", "M217", "M218", "D210", "--", "D6000"}); stationB.setBounds(485, 10, 460, 375); indexPanelA.add(stationB); // A/B 工位之间的分隔竖线 final JPanel divider = new JPanel(); divider.setBackground(new Color(0xBD, 0xBD, 0xBD)); divider.setBounds(472, 10, 2, 375); indexPanelA.add(divider); logArea = new JTextArea(); logArea.setEditable(false); logArea.setFont(new Font("微软雅黑", Font.PLAIN, 16)); logArea.setBackground(Color.WHITE); logArea.setForeground(Color.BLACK); logArea.setMargin(new Insets(6, 8, 6, 8)); final JScrollPane logScrollPane = new JScrollPane(logArea); logScrollPane.setBounds(10, 390, 935, 150); indexPanelA.add(logScrollPane); // 面板宽度变化时整体居中,日志高度跟随窗口 indexPanelA.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { int w = indexPanelA.getWidth(); int h = indexPanelA.getHeight(); int offset = Math.max(10, (w - 935) / 2); stationA.setBounds(offset, 10, 460, 375); stationB.setBounds(offset + 475, 10, 460, 375); divider.setBounds(offset + 472, 10, 2, 375); logScrollPane.setBounds(offset, 390, 935, Math.max(60, h - 400)); } }); tabbedPane.addTab("检测面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexPanelA, 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); // ========== 工作记录 Tab(改造:当前工位Tab + 所有工位Tab) ========== // 旧代码已注释: // 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 JTabbedPane workRecordTabbedPane = new JTabbedPane(JTabbedPane.TOP); workRecordTabbedPane.setFont(new Font("微软雅黑", Font.PLAIN, 14)); // OKNG 工位:当前工位 + 所有工位 WorkRecordPanel[] workRecordPanels = new WorkRecordPanel[2]; // 当前工位Tab(使用配置文件中的 mes.gw) workRecordPanels[0] = new WorkRecordPanel(mes_gw, mes_line_sn); workRecordTabbedPane.addTab(mes_gw, null, workRecordPanels[0], "查询" + mes_gw + "的工作记录"); // 所有工位Tab(查询整条产线所有工位) workRecordPanels[1] = new WorkRecordPanel(null, mes_line_sn); workRecordTabbedPane.addTab("所有工位", null, workRecordPanels[1], "查询整条产线所有工位的工作记录"); // 切换子Tab时自动刷新数据 workRecordTabbedPane.addChangeListener(e -> { int selectedIndex = workRecordTabbedPane.getSelectedIndex(); if (selectedIndex >= 0 && selectedIndex < workRecordPanels.length) { workRecordPanels[selectedIndex].refreshData(); } }); // 将子 TabbedPane 添加到主 TabbedPane tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), workRecordTabbedPane, 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){ } } }); } 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); } // ===== 氦检工位 UI 更新(PLC 线程可直接调用,内部已切到 EDT) ===== /** 追加一条运行日志,自动滚到底 */ public static void addLog(String msg) { if (logArea == null) { return; } SwingUtilities.invokeLater(() -> { logArea.append(LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")) + " " + msg + "\n"); logArea.setCaretPosition(logArea.getDocument().getLength()); }); } /** PLC 连接状态 */ public static void setPlcStatus(boolean connected) { if (plc_menu == null) { return; } SwingUtilities.invokeLater(() -> { plc_menu.setText(connected ? "已连接" : "未连接"); plc_menu.setForeground(connected ? Color.GREEN : Color.RED); }); } /** 取工位卡片:1=A,2=B */ public static StationPanel getStation(int station) { return station == 2 ? stationB : stationA; } // ===== PLC 连接 ===== public static McPlcClient plcClient; public static java.util.Timer plcTimer; /** 点顶栏 PLC 状态,读取并显示所有点位当前值 */ public static void showPlcSignals() { if (plcClient == null) { plcClient = new McPlcClient(plc_ip, plc_port); } new Thread(() -> { if (!plcClient.isConnected()) { plcClient.connect(); } if (!plcClient.isConnected()) { SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(mesClientFrame, "PLC 未连接 " + plc_ip + ":" + plc_port, "PLC 信号", JOptionPane.WARNING_MESSAGE)); return; } StringBuilder sb = new StringBuilder(); appendSignals(sb, "A", "M205", "M206", "M207", "M208", "D200", plc_a_code_addr); sb.append("\n"); appendSignals(sb, "B", "M215", "M216", "M217", "M218", "D210", plc_b_code_addr); log.info("PLC 信号:\n" + sb); JTextArea area = new JTextArea(sb.toString()); area.setEditable(false); area.setFont(new Font("微软雅黑", Font.PLAIN, 18)); area.setMargin(new Insets(8, 10, 8, 10)); SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(mesClientFrame, new JScrollPane(area), "PLC 信号 " + plc_ip + ":" + plc_port, JOptionPane.INFORMATION_MESSAGE)); }).start(); } private static void appendSignals(StringBuilder sb, String name, String scan, String allow, String start, String end, String leak, String code) { sb.append(name).append(" 扫码成功(").append(scan).append(") = ").append(plcClient.readBit(scan)).append("\n"); sb.append(name).append(" MES允许(").append(allow).append(") = ").append(plcClient.readBit(allow)).append("\n"); sb.append(name).append(" 检测开始(").append(start).append(") = ").append(plcClient.readBit(start)).append("\n"); sb.append(name).append(" 检测结束(").append(end).append(") = ").append(plcClient.readBit(end)).append("\n"); sb.append(name).append(" 泄露(").append(leak).append(") = ").append(plcClient.readString(leak, 8)).append("\n"); sb.append(name).append(" 条码(").append(code).append(") = ").append(plcClient.readString(code, plc_code_len)).append("\n"); } /** 启动 PLC 连接/重连,登录成功后调用 */ public static void startPlcTimer() { if (plcTimer != null) { plcTimer.cancel(); } plcClient = new McPlcClient(plc_ip, plc_port); plcTimer = new java.util.Timer(true); plcTimer.schedule(new TimerTask() { @Override public void run() { if (!plcClient.isConnected()) { plcClient.connect(); if (plcClient.isConnected()) { setPlcStatus(true); addLog("PLC 已连接 " + plc_ip + ":" + plc_port); String sig = "M205=" + plcClient.readBit("M205") + " M215=" + plcClient.readBit("M215"); addLog(sig); log.info("PLC 点位: " + sig); startFlows(); } else { setPlcStatus(false); } } else { // 心跳检测:主动读一次,断线时读失败会自动标记断开,下一轮重连 plcClient.readBit("M205"); if (!plcClient.isConnected()) { setPlcStatus(false); addLog("PLC 连接断开,等待重连"); } } } }, 0, 3000); } public static HeliumFlow flowA; public static HeliumFlow flowB; public static java.util.Timer flowTimer; /** PLC 连接成功后启动 A/B 工位流程轮询 */ private static void startFlows() { if (flowTimer != null) { return; } flowA = new HeliumFlow(1, plcClient, stationA, "M205", "M206", "M207", "M208", "D200", plc_a_code_addr); flowB = new HeliumFlow(2, plcClient, stationB, "M215", "M216", "M217", "M218", "D210", plc_b_code_addr); flowTimer = new java.util.Timer(true); flowTimer.schedule(new TimerTask() { @Override public void run() { try { flowA.poll(); flowB.poll(); } catch (Exception e) { log.info("氦检流程异常: " + e.getMessage()); } } }, 500, 500); addLog("氦检流程已启动"); } public static void getMaterailData(){ try{ 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(); } }catch (Exception e){ log.info(e.getMessage()); } } public static void updateMaterailData(){ try{ 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++; } MesClient.table.repaint(); } }catch (Exception e){ log.info(e.getMessage()); } } // 绑定物料批次码 public static void scanBatchSn(BindMaterialResp bindMaterialResp) { //弹窗扫工件码 String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle(); String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle); if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) { JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType()); 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 startUpdateQualityTimer(){ // 创建异步线程池 ExecutorService executorService = Executors.newSingleThreadExecutor(); // 提交任务 executorService.submit(() -> { log.info("异步任务执行中..."); // 模拟耗时操作 try { while (true){ try { // 确保连接已经打开 if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) { JdbcUtils.openConnection(); } Statement statement = JdbcUtils.conn.createStatement(); String selectSql = "SELECT id, bw FROM submit_record\n" + "WHERE state = 0 ORDER BY id DESC LIMIT 10"; String updateSql = "UPDATE submit_record \n" + "SET state = '1'\n" + "WHERE id = "; ResultSet rs = statement.executeQuery(selectSql); while(rs.next()){ int id = rs.getInt("id"); String bw = rs.getString("bw"); // 发送请求 String url = "http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesProductRecord/updateQualityByTiming"; log.info("质量:"+bw); String s = HttpUtils.sendPostRequestJson(url, bw ); log.info("结果:"+s); Boolean result = JSONObject.parseObject(s).getBoolean("result"); if(result) { // 更改状态为 1 statement.executeUpdate(updateSql + id); } } rs.close(); statement.close(); } catch (Exception e) { log.info(e.getMessage()); } try { Thread.sleep(2000); }catch (Exception e){ log.info(e.getMessage()); } } } catch (Exception e) { log.info(e.getMessage()); } log.info("异步任务执行完毕"); }); } }