package com.mes.ui; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.mes.component.MesRadio; import com.mes.component.MesWebView; import com.mes.component.ProductTypePanel; import com.mes.netty.NettyClient; import com.mes.util.*; 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.List; import java.util.Properties; import java.util.Timer; import java.util.TimerTask; public class MesClient extends JFrame { 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 = ""; // 产线编号 public static String mes_serial_port = "COM1"; // 称重仪串口号 public static int mes_serial_baud = 9600; // 称重仪波特率 //新增字段 public static ProductTypePanel productTypePanel; public static JLabel productTypeCurrentLabel; public static final int PRODUCT_SN_LENGTH = 32; public static final String PREFIX_ZJ = "000020021815-01000016"; //智界 public static final String PREFIX_SJ = "000020022029-01000016"; //尚界 public static final boolean PRODUCT_TYPE_SWITCH_ENABLED = false; public static final String PRODUCT_TYPE_SWITCH_PASSWORD = "503833"; public static final String PRODUCT_TYPE_ZJ = ProductTypePanel.TYPE_ZJ; public static final String PRODUCT_TYPE_SJ = ProductTypePanel.TYPE_SJ; //TCP连接 public static NettyClient nettyClient; //TCP连接状态 public static boolean tcp_connect_flag = false; //TCP连接请求 public static boolean connect_request_flag = false; public static boolean com_connect_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 SerialPortUtils serialPortUtils; public static BluetoothUtils bluetoothUtils; public static ParamConfig paramConfig; 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 JButton batch_sn_bt; public static JTextField product_sn; public static JButton f_scan_data_bt_1; public static JButton is_js_btn; public static String comboBoxValue = "COM1"; public static String user20 = ""; public static String is_js_btn_val = "1"; 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 JTable table; public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" }; public static Object[][] rowData = null; public static Timer cjTimer; //串口定时器 public static Timer cjTimer2; //串口定时器2 private static final String TARGET_DEVICE_ADDRESS = "001135390238"; public static void main(String[] args) { //EventQueue.invokeLater(new Runnable() )是SwingUtilities类中的一个静态方法, // 用于在事件调度线程中执行指定的Runnable。 //事件调度线程是Swing的核心线程,负责处理所有Swing组件的事件。 EventQueue.invokeLater(new Runnable() { @Override public void run() { try{ //读文件配置(新增串口号和波特率) readProperty(); //检查串口连接(定时器定时检查串口连接) checkComPort(); // 显示界面 mesClientFrame = new MesClient(); mesClientFrame.setVisible(false); //连接数据库 JdbcUtils.getConn(); //显示登录界面 welcomeWin = new LoginFarme(); welcomeWin.setVisible(true); // getMaterailData(); }catch (Exception e){ e.printStackTrace(); } } }); } public static void initComPort() { // try { if (serialPortUtils != null) { serialPortUtils.closeSerialPort(); serialPortUtils = null; } //初始化串口 serialPortUtils = new SerialPortUtils(); //创建串口参数配置 paramConfig = new ParamConfig(mes_serial_port, mes_serial_baud, 0, 8, 1); //初始化串口 serialPortUtils.init(paramConfig); System.out.println("初始化称重串口: " + mes_serial_port + ", 波特率: " + mes_serial_baud); } catch (Throwable e) { //串口连接失败标志 com_connect_flag = false; //打印错误信息 System.err.println("称重串口初始化失败: " + e.getMessage()); e.printStackTrace(); } } // 串口断开时定时重连 public static void checkComPort() { //如果定时器不为空,则取消定时器 if (MesClient.cjTimer2 != null) { MesClient.cjTimer2.cancel(); } //创建新的定时器 MesClient.cjTimer2 = new Timer(); //定时器执行任务 MesClient.cjTimer2.schedule(new TimerTask() { @Override public void run() { try { //如果串口连接标志为false,则重连串口 if (!com_connect_flag) { System.out.println("称重串口断开,正在重连"); initComPort(); //重连串口 } } catch (Exception e) { e.printStackTrace(); } } }, 2000, 2000); //线程本身并不阻塞 initComPort(); //初始化串口 } //读配置文件 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_serial_port = pro.getProperty("mes.serial.port", "COM1"); mes_serial_baud = Integer.parseInt(pro.getProperty("mes.serial.baud", "9600")); mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw); System.out.println(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle); } // 初始化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) { // TODO Auto-generated catch block e.printStackTrace(); } } //启动心跳包程序 public static java.util.Timer heartBeatTimer; public static java.util.Timer heartBeatIconTimer; public static boolean iconREDFlag = true; public static JTextField weight_text; public static JTextField batch_sn_text; public static JLabel lblNewLabel; public static JLabel lblNewLabel_1; private static JComboBox comboBox; public static JButton finish_ok_bt_1; 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) { //System.out.println("发送心跳报文"); 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){ System.out.println("TCP已断开"); //TCP重新同步连接 if(!connect_request_flag) { //System.out.println("TCP重新同步连接"); //设置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(){ loadProductTypeFromDb(); //新增 resetScanA(); } public static void resetScanA() { work_status = 0; check_quality_result = false; // MesClient.start_work_bt.setVisible(false); // MesClient.finish_ng_bt.setVisible(false); // MesClient.finish_ok_bt.setVisible(false); MesClient.finish_ok_bt.setEnabled(false); product_sn.setText(""); batch_sn_text.setText(""); weight_text.setText(""); batch_sn_bt.setEnabled(false); weight_text.setEnabled(false); is_js_btn.setText("最后一台"); is_js_btn_val = "1"; is_js_btn.setBackground(null); finish_ok_bt.setEnabled(false); MesClient.finish_ok_bt_1.setText("称重"); // MesClient.f_scan_data_bt_1.setEnabled(false); MesClient.f_scan_data_bt_1.setEnabled(true); // DataUtil.stopWork(sessionid); updateProductTypePanelEnabled(true); if (PRODUCT_TYPE_SWITCH_ENABLED) { MesClient.setMenuStatus("当前生产类型:" + getProductTypeName() + ",请扫工件码",0); } else { MesClient.setMenuStatus("请扫工件码",0); } // product_result_text.setText(""); // work_status_text.setText(""); // MesClient.setMenuStatus("涂胶已超过10分钟,已失效",-1); // 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 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("")) { scanBarcode = scanBarcode.trim(); //生产类型新增 //获取用户 getUser(); //获取扫码内容36位 String barcode36 = getBarcode(scanBarcode);//处理36为码 //工位号 String gw = ""; switch(scan_type) { case 1: product_sn.setText(scanBarcode); break; } //刷新界面 mesClientFrame.repaint(); //生产类型新增 String validateMsg = validateProductSnMessage(scanBarcode); if (validateMsg != null) { check_quality_result = false; work_status = 0; MesClient.finish_ok_bt.setEnabled(false); // MesClient.finish_ng_bt.setEnabled(false); MesClient.f_scan_data_bt_1.setEnabled(true); updateProductTypePanelEnabled(true); MesClient.setMenuStatus(validateMsg, -1); return; } updateProductTypePanelEnabled(false); if(!tcp_connect_flag) { JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } // 查询工件质量 Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20); if(!sendret){ JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } }else { JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } } public static void logoff() { // welcomeWin.setVisible(true); mesClientFrame.setVisible(false); //退出程序 退出登录 System.exit(0); 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); //重连mes 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); 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); //删除 /* comboBox = new JComboBox(); comboBox.setBackground(Color.WHITE); comboBox.setFont(new Font("微软雅黑", Font.PLAIN, 18)); comboBox.setBounds(50, 10, 80, 21); comboBox.setMaximumSize(new Dimension(100, 40)); comboBox.setMinimumSize(new Dimension(100, 30)); comboBox.setPreferredSize(new Dimension(100, 30)); comboBox.setModel(new DefaultComboBoxModel(new String[] {"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8"})); comboBox.setSelectedItem("COM4"); toolBar.add(comboBox); // 串口发生改变 comboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (bluetoothUtils != null) { bluetoothUtils.closeConnection(); } MesClient.initComPort(); } });*/ tabbedPane = new JTabbedPane(JTabbedPane.TOP); tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); 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) { 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); finish_ok_bt = new JButton("打包"); finish_ok_bt.setEnabled(false); finish_ok_bt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e ) { if(work_status == 1 && check_quality_result) { if ( batch_sn_text.getText().equals("")){ JOptionPane.showMessageDialog(mesClientFrame,"请先获取包装码!","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } if (weight_text.getText().isEmpty()) { JOptionPane.showMessageDialog(mesClientFrame,"请先称重!","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } String wig = weight_text.getText(); String is_js = is_js_btn_val; String result = HttpUtils.sendRequest( "http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesProductRecord/dp350?" + "sn=" + product_sn.getText() + "&oprno=" + mes_gw + "&batchSn=" + batch_sn_text.getText() + "&weight=" + wig + "&isJs=" + is_js + "&ucode=system"+"&lineSn="+mes_line_sn); weight_text.setText(""); if (result != null && !result.equals("") && !result.equals("false")) { JSONObject ret = JSON.parseObject(result); if (ret.get("result").equals("true")) { MesClient.setMenuStatus("结果提交成功,请扫下一件",0); resetScanA(); // cjTimer.cancel(); // MesClient.weight_text.setText(""); }else { JOptionPane.showMessageDialog(mesClientFrame, "结果提交失败,"+ret.get("message")+"!", "提示窗口", JOptionPane.INFORMATION_MESSAGE); MesClient.setMenuStatus("结果提交失败,请重试!",1); } } // String sn = getBarcode(product_sn.getText()); // getUser(); // // String qret = "OK"; // Boolean sendret = DataUtil.sendQuality(nettyClient, sn, qret, user20); // if (!sendret) { // 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, 40)); finish_ok_bt.setBounds(578, 405, 212, 92); indexPanelA.add(finish_ok_bt); //重量输入框(由串口定时器定时获取重量) weight_text = new JTextField(); weight_text.setHorizontalAlignment(SwingConstants.CENTER); weight_text.setFont(new Font("微软雅黑", Font.PLAIN, 28)); weight_text.setEditable(false); weight_text.setColumns(10); weight_text.setBounds(403, 300, 181, 70); weight_text.setText(""); indexPanelA.add(weight_text); //包装码输入框 batch_sn_text = new JTextField(); batch_sn_text.setEditable(false); batch_sn_text.setHorizontalAlignment(SwingConstants.CENTER); batch_sn_text.setFont(new Font("微软雅黑", Font.PLAIN, 28)); batch_sn_text.setColumns(10); batch_sn_text.setBounds(403, 192, 280, 70); indexPanelA.add(batch_sn_text); batch_sn_bt = new JButton("生成包装码"); batch_sn_bt.setEnabled(false); batch_sn_bt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //生成包装码 try { String result = HttpUtils.sendRequest("http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesBatchSn/createBatchSn?lineSn="+mes_line_sn); JSONObject ret = JSON.parseObject(result); String TF = ret.getString("result"); if (Boolean.parseBoolean(TF)){ String batchSn = ret.getJSONObject("data").getString("batchSn"); //赋值 禁用生成按钮 batch_sn_text.setText(batchSn); MesClient.batch_sn_bt.setEnabled(false); MesClient.finish_ok_bt.setEnabled(true); setMenuStatus("生成包装码成功",0); // 打印包装码标签(文字 + 二维码) PrintUtil.printLabel(batchSn); }else{ JOptionPane.showMessageDialog(mesClientFrame,"生成包装码失败!,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE); } } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(mesClientFrame,"生成包装码失败!,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE); } // if (result!= null&&!result.equals("")&&!result.equals("false")) { // JSONObject ret = JSON.parseObject(result); // if (ret != null && ret.containsKey("data")) { // String batchSn = ret.getJSONObject("data").getString("batchSn"); // if (batchSn == null || batchSn.equals("")) { // 包装码为null,激活生成包装码按钮 // JOptionPane.showMessageDialog(mesClientFrame,"生成包装码失败!,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE); // }else{ // //赋值 禁用生成按钮 // batch_sn_text.setText(batchSn); // MesClient.batch_sn_bt.setEnabled(false); // } // } // // }else{ // JOptionPane.showMessageDialog(mesClientFrame,"生成包装码失败!,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE); // } } }); batch_sn_bt.setFont(new Font("微软雅黑", Font.PLAIN, 30)); batch_sn_bt.setBounds(693, 192, 198, 70); indexPanelA.add(batch_sn_bt); //重量标签 lblNewLabel = new JLabel("重量"); lblNewLabel.setEnabled(false); lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT); lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 30)); lblNewLabel.setBounds(238, 300, 117, 70); indexPanelA.add(lblNewLabel); lblNewLabel_1 = new JLabel("包装码"); lblNewLabel_1.setEnabled(false); lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT); lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 30)); lblNewLabel_1.setBounds(238, 192, 117, 70); indexPanelA.add(lblNewLabel_1); is_js_btn = new JButton("最后一台"); is_js_btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // if (is_js_btn.getText().equals("结束")){ // is_js_btn.setText("开始"); // }else if (is_js_btn.getText().equals("开始")){ // is_js_btn.setText("结束"); // } if (is_js_btn_val.equals("2")){ is_js_btn.setBackground(null); is_js_btn_val = "1"; }else{ // is_js_btn.setText("结束"); is_js_btn.setBackground(Color.GREEN); is_js_btn_val = "2"; } } }); is_js_btn.setFont(new Font("微软雅黑", Font.PLAIN, 32)); is_js_btn.setBounds(693, 300, 198, 70); indexPanelA.add(is_js_btn); //称重按钮,触发称重操作 finish_ok_bt_1 = new JButton("称重"); finish_ok_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png"))); finish_ok_bt_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!com_connect_flag || serialPortUtils == null) { JOptionPane.showMessageDialog(mesClientFrame, "称重串口未连接", "提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } new Thread(() -> serialPortUtils.requestWeight()).start(); } }); finish_ok_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 40)); finish_ok_bt_1.setEnabled(true); finish_ok_bt_1.setBounds(207, 405, 212, 92); indexPanelA.add(finish_ok_bt_1); 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(new BorderLayout()); if (PRODUCT_TYPE_SWITCH_ENABLED) { //新增的面板 JPanel indexPanelProductType = new JPanel(); indexPanelProductType.setLayout(null); JScrollPane productTypeScrollPane = new JScrollPane(indexPanelProductType); JLabel productTypeTitle = new JLabel("生产类型设置"); productTypeTitle.setFont(new Font("微软雅黑", Font.BOLD, 28)); productTypeTitle.setForeground(new Color(64, 64, 64)); productTypeTitle.setHorizontalAlignment(SwingConstants.CENTER); productTypeTitle.setBounds(81, 80, 810, 50); indexPanelProductType.add(productTypeTitle); JLabel productTypeHint = new JLabel("请选择当前生产的工件类型,切换后将保存到本地并用于扫码校验"); productTypeHint.setFont(new Font("微软雅黑", Font.PLAIN, 20)); productTypeHint.setForeground(new Color(140, 140, 140)); productTypeHint.setHorizontalAlignment(SwingConstants.CENTER); productTypeHint.setBounds(81, 140, 810, 40); indexPanelProductType.add(productTypeHint); productTypePanel = new ProductTypePanel(PRODUCT_TYPE_SWITCH_PASSWORD); productTypePanel.setBounds(276, 210, 420, 72); productTypePanel.setOnChangeListener(() -> { if (work_status == 0) { saveProductTypeToDb(); if (PRODUCT_TYPE_SWITCH_ENABLED) { setMenuStatus("当前生产类型:" + getProductTypeName() + ",请扫工件码",0); } else { setMenuStatus("请扫工件码",0); } } refreshProductTypeCurrentLabel(); }); indexPanelProductType.add(productTypePanel); productTypeCurrentLabel = new JLabel(); productTypeCurrentLabel.setFont(new Font("微软雅黑", Font.PLAIN, 22)); productTypeCurrentLabel.setForeground(new Color(64, 64, 64)); productTypeCurrentLabel.setHorizontalAlignment(SwingConstants.CENTER); productTypeCurrentLabel.setBounds(81, 310, 810, 40); refreshProductTypeCurrentLabel(); indexPanelProductType.add(productTypeCurrentLabel); tabbedPane.addTab("生产类型", new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")), productTypeScrollPane, null); } tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexPanelC, 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){ } } }); } private static String validateProductSnMessage(String sn) { if (sn == null || sn.isEmpty()) { return "工件码为空,请重试"; } if (sn.length() != PRODUCT_SN_LENGTH) { return "工件码长度须为32位,当前" + sn.length() + "位"; } if (!PRODUCT_TYPE_SWITCH_ENABLED) { return null; } String prefix = getProductSnPrefix(); if (!sn.startsWith(prefix)) { return "工件码前缀与当前类型(" + getProductTypeName() + ")不匹配,当前工件码为" + getProductTypeNameBySn(sn); } return null; } private static void updateProductTypePanelEnabled(boolean enabled) { if (productTypePanel != null) { productTypePanel.setEnabled(enabled); } } public static void loadProductTypeFromDb() { if (productTypePanel == null) { return; } String savedType = JdbcUtils.getClientConfig( JdbcUtils.getProductTypeConfigKey(mes_gw), PRODUCT_TYPE_ZJ ); if (PRODUCT_TYPE_SJ.equals(savedType)) { productTypePanel.setResult(PRODUCT_TYPE_SJ); } else { productTypePanel.setResult(PRODUCT_TYPE_ZJ); } refreshProductTypeCurrentLabel(); } public static void refreshProductTypeCurrentLabel() { if (productTypeCurrentLabel != null) { productTypeCurrentLabel.setText("当前选择:" + getProductTypeName() + "(前缀 " + getProductSnPrefix() + ")"); } } public static void saveProductTypeToDb() { if (productTypePanel == null) { return; } JdbcUtils.saveClientConfig( JdbcUtils.getProductTypeConfigKey(mes_gw), productTypePanel.getResult() ); } public static String getProductSnPrefix() { if (productTypePanel != null && PRODUCT_TYPE_SJ.equals(productTypePanel.getResult())) { return PREFIX_SJ; } return PREFIX_ZJ; } public static String getProductTypeName() { if (productTypePanel != null) { return productTypePanel.getTypeName(); } return "智界"; } private static String getProductTypeNameBySn(String sn) { if (sn.startsWith(PREFIX_SJ)) { return "尚界"; } if (sn.startsWith(PREFIX_ZJ)) { return "智界"; } return "未知"; } 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 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++; } MesClient.table.repaint(); } } // 绑定物料批次码 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); } }