| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004 |
- package com.mes.ui;
- import com.alibaba.fastjson2.JSONObject;
- import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
- import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
- import com.mes.component.MesRadio;
- import com.mes.component.MesWebView;
- import com.mes.device.DeviceStateReporter;
- import com.mes.update.ClientAutoUpdater;
- import com.mes.util.*;
- 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.util.*;
- import java.util.List;
- import java.util.Timer;
- 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 = ""; // 产线编号
- public static String login_title = "MES管理系统"; // 登录页顶部标题,来自配置文件
- public static int mes_device_heartbeat_minutes = 5; // 设备状态上报间隔(分钟)
- public static String csyq = "";
- //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; // 已弃用,改为使用 WorkRecordPanel
- public static JScrollPane searchScrollPaneDj;
- public static Boolean check_quality_result = false;
- public static Integer work_status = 0;
- public static Integer plc_status = 0;
- public static List<String> hj_params = new ArrayList<>();
- 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 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 JLabel fxlabel;
- public static Timer getSnBeatTimer;
- // S7plc
- public static S7PLC plc = new S7PLC(EPlcType.S1200,"192.168.2.1");
- 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{//读文件配置
- Properties config = readProperty();
- // 程序启动时先检查是否有当前工位的新 jar;如需升级,会退出并交给外部 bat 替换 jar。
- if (ClientAutoUpdater.checkAndUpdate(config)) {
- return;
- }
- // 显示界面
- mesClientFrame = new MesClient();
- mesClientFrame.setVisible(false);
- JdbcUtils.getConn();
- welcomeWin = new LoginFarme();
- welcomeWin.setVisible(true);
- getMaterailData();
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- });
- }
- }
- //读配置文件
- private static Properties 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");
- String loginTitleFromCfg = pro.getProperty("mes.login.title");
- if (loginTitleFromCfg == null) {
- login_title = "MES管理系统";
- } else {
- login_title = loginTitleFromCfg.trim();
- }
- csyq = pro.getProperty("mes.csyq");
- mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
- try {
- mes_device_heartbeat_minutes = Integer.parseInt(pro.getProperty("mes.device.heartbeat.minutes", "5").trim());
- } catch (Exception e) {
- mes_device_heartbeat_minutes = 5;
- }
- if (mes_device_heartbeat_minutes <= 0) {
- mes_device_heartbeat_minutes = 5;
- }
- log.info(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";");
- return pro;
- }
- //启动心跳包程序
- public static java.util.Timer heartBeatTimer;
- public static java.util.Timer heartBeatIconTimer;
- // 设备状态定时上报
- public static java.util.Timer deviceReportTimer;
- 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 void startDeviceReportTimer() {
- stopDeviceReportTimer();
- long intervalMs = mes_device_heartbeat_minutes * 60L * 1000L;
- deviceReportTimer = new Timer("device-report-timer", true);
- deviceReportTimer.schedule(new TimerTask() {
- @Override
- public void run() {
- DeviceStateReporter.getInstance().reportAsync(DeviceStateReporter.STATE_RUNNING, "运行心跳");
- }
- }, intervalMs, intervalMs);
- log.info("设备状态定时上报已启动,间隔 {} 分钟", mes_device_heartbeat_minutes);
- }
- /**
- * 停止设备状态定时上报
- */
- public static void stopDeviceReportTimer() {
- if (deviceReportTimer != null) {
- deviceReportTimer.cancel();
- deviceReportTimer = null;
- }
- }
- /**
- * 登录成功后上报设备开机状态
- */
- public static void reportDeviceRunning() {
- DeviceStateReporter.getInstance().init(mes_server_ip, mes_gw, mes_line_sn);
- DeviceStateReporter.getInstance().reportAsync(DeviceStateReporter.STATE_RUNNING, "工位登录,设备开机");
- startDeviceReportTimer();
- }
- /**
- * 退出或关闭时上报设备停机状态
- */
- public static void reportDeviceStopped() {
- stopDeviceReportTimer();
- DeviceStateReporter.getInstance().reportSync(DeviceStateReporter.STATE_STOP, "工位退出,设备停机");
- }
- //设置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() {
- JSONObject retObj = DataUtil.delCurSn(MesClient.mes_gw, mes_server_ip, mes_line_sn);
- if (retObj.get("result")!=null && retObj.get("result").toString().equalsIgnoreCase("true")) {
- work_status = 0;
- check_quality_result = false;
- MesClient.finish_ok_bt.setEnabled(false);
- MesClient.finish_ng_bt.setEnabled(false);
- product_sn.setText("");
- MesClient.fxlabel.setVisible(false);
- MesClient.f_scan_data_bt_1.setEnabled(true);
- MesClient.setMenuStatus("请扫工件码",0);
- // MesClient.setMenuStatus("开班点检,请先进行OKNG样件测试",-1);
- updateMaterailData();
- // shiftUserCheck();
- } else {
- MesClient.setMenuStatus("刷新失败",0);
- }
- }
- 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("")) {
- //获取用户
- getUser();
- //获取扫码内容36位
- String barcode36 = getBarcode(scanBarcode);//处理36为码
- //工位号
- String gw = "";
- switch(scan_type) {
- case 1:
- product_sn.setText(scanBarcode);
- break;
- }
- //刷新界面
- mesClientFrame.repaint();
- // 查询工件质量
- JSONObject retObj = DataUtil.checkQuality(scanBarcode,user20);
- if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
- MesClient.status_menu.setForeground(Color.GREEN);
- MesClient.check_quality_result = true;//质量合格,可以绑定加工
- MesClient.status_menu.setText("该工件可以加工");
- MesClient.work_status = 1;
- MesClient.f_scan_data_bt_1.setEnabled(false);
- MesClient.finish_ok_bt.setEnabled(true);
- MesClient.finish_ng_bt.setEnabled(true);
- }else{
- MesClient.check_quality_result = false;
- MesClient.work_status = 0;
- if(retObj.get("result")==null){
- MesClient.setMenuStatus("请求失败,请重试",-1);
- }else{
- if(retObj.get("result").toString().equalsIgnoreCase("false")){
- MesClient.setMenuStatus(retObj.getString("message"),-1);
- }
- }
- }
- }else {
- MesClient.setMenuStatus("请扫工件码,请重试",-1);
- // JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
- return;
- }
- }
- public static void logoff() {
- reportDeviceStopped();
- if (getSnBeatTimer != null) {
- getSnBeatTimer.cancel();
- getSnBeatTimer = null;
- }
- welcomeWin.setVisible(true);
- mesClientFrame.setVisible(false);
- }
- public MesClient() {
- setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png")));
- setTitle("MES系统客户端BE14:"+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) {
- reportDeviceStopped();
- 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 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) {
- 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);
- fxlabel = new JLabel("该工件为返修件,请仔细检查");
- fxlabel.setFont(new Font("微软雅黑", Font.PLAIN, 38));
- fxlabel.setBounds(81, 170, 810, 70);
- fxlabel.setForeground(Color.RED);
- fxlabel.setHorizontalAlignment(SwingConstants.CENTER);
- fxlabel.setVisible(false);
- indexPanelA.add(fxlabel);
- finish_ok_bt = new JButton("OK");
- finish_ok_bt.setEnabled(false);
- finish_ok_bt.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(work_status == 1 && check_quality_result){
- getUser();
- String sn = MesClient.product_sn.getText().trim();
- if(sn.isEmpty()){
- MesClient.setMenuStatus("工件码为空,请重试",-1);
- return;
- }
- JSONObject retObj = DataUtil.sendQuality(sn,"OK",user20);
- System.out.println(retObj);
- if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
- resetState();
- MesClient.setMenuStatus("结果提交成功,请扫下一件",0);
- MesClient.scan_type = 1;
- }else{
- if(retObj.get("result")==null){
- MesClient.setMenuStatus("请求失败,请重试",-1);
- }else{
- if(retObj.get("result").toString().equalsIgnoreCase("false")){
- MesClient.setMenuStatus(retObj.getString("message"),-1);
- }
- }
- }
- }
- }
- });
- 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.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) {
- if(work_status == 1 && check_quality_result){
- getUser();
- String sn = MesClient.product_sn.getText().trim();
- if(sn.isEmpty()){
- MesClient.setMenuStatus("工件码为空,请重试",-1);
- return;
- }
- JSONObject retObj = DataUtil.sendQuality(sn,"NG",user20);
- if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
- MesClient.resetScanA();
- MesClient.setMenuStatus("结果提交成功,请扫下一件",0);
- MesClient.scan_type = 1;
- MesClient.scanBarcode();
- }else{
- if(retObj.get("result")==null){
- MesClient.setMenuStatus("请求失败,请重试",-1);
- }else{
- if(retObj.get("result").toString().equalsIgnoreCase("false")){
- MesClient.setMenuStatus(retObj.getString("message"),-1);
- }
- }
- }
- }
- }
- });
- 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, 291, 240, 80);
- finish_ng_bt.setEnabled(false);
- indexPanelA.add(finish_ng_bt);
- 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);
- // ========== 工作记录 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 startGetCurSn(){
- if (getSnBeatTimer != null) {
- getSnBeatTimer.cancel();
- }
- getSnBeatTimer = new Timer();
- getSnBeatTimer.schedule(new TimerTask() {
- @Override
- public void run() {
- if (work_status == 1) {
- // if (plc_status == 0){
- // // 等待扫码
- // String sn = product_sn.getText().trim();
- // if (sn.isEmpty()) {
- // plc_status = 1;
- // }
- // } else if (plc_status == 1) {
- // // 发送允许启动信号
- // Boolean ret = PLCUtils.writeStartMethod(plc);
- // if (ret) {
- // plc_status = 2;
- // }
- // } else if (plc_status == 2) {
- // // 等待加工结束信号
- // Boolean ret = PLCUtils.readStopMethod(plc);
- // if (ret){
- // // 将允许启动置空
- // PLCUtils.writeStopMethod(plc);
- // // 获取参数
- // QmParam qmParam = PLCUtils.getParameter(plc);
- // // 发送参数
- // JSONObject retObj1 = DataUtil.sendQualityParam( user20, product_sn.getText().trim(), MesClient.mes_gw, MesClient.mes_line_sn,qmParam);
- // if(retObj1 != null && retObj1.get("result") != null && retObj1.get("result").toString().equalsIgnoreCase("true")) {
- // MesClient.resetScanA();
- // MesClient.setMenuStatus("测试结果上传成功,请扫下一件",0);
- // }else{
- // MesClient.setMenuStatus("测试结果上传失败,请重试",-1);
- // return;
- // }
- // // 获取结果
- // String result = PLCUtils.getResult(plc);
- // // 发送结果
- // String sn = product_sn.getText().trim();
- // JSONObject retObj = DataUtil.sendQuality( sn,result,user20);
- // if (retObj != null && "true".equalsIgnoreCase(retObj.getString("result"))) {
- // resetState();
- // status_menu.setForeground(Color.GREEN);
- // status_menu.setText("结果提交成功,请扫下一件");
- // finish_ng_bt.setEnabled(false);
- // } else {
- // status_menu.setForeground(Color.RED);
- // String msg = (retObj != null && retObj.containsKey("message")) ? retObj.getString("message") : "提交失败";
- // status_menu.setText(msg);
- // }
- // }
- // }
- // 获取结果
- String result = PLCUtils.getResult(plc);
- System.out.println(result);
- // 如果等于OK或者NG的时候才处理结果
- if("OK".equals(result)){
- // 获取参数
- QmParam qmParam = PLCUtils.getParameter(plc);
- // 发送参数
- JSONObject retObj1 = DataUtil.sendQualityParam( user20, product_sn.getText().trim(), MesClient.mes_gw, MesClient.mes_line_sn,qmParam);
- System.out.println(qmParam);
- if(retObj1 != null && retObj1.get("result") != null && retObj1.get("result").toString().equalsIgnoreCase("true")) {
- //MesClient.resetScanA();
- MesClient.setMenuStatus("气密数据上传成功,请扫下一件",0);
- }else{
- MesClient.setMenuStatus("测试结果上传失败,请重试",-1);
- }
- String sn = product_sn.getText().trim();
- JSONObject retObj = DataUtil.sendQuality(sn,result,user20);
- if (retObj != null && "true".equalsIgnoreCase(retObj.getString("result"))) {
- resetState();
- status_menu.setForeground(Color.GREEN);
- status_menu.setText("结果提交成功,请扫下一件");
- finish_ng_bt.setEnabled(false);
- } else {
- status_menu.setForeground(Color.RED);
- String msg = (retObj != null && retObj.containsKey("message")) ? retObj.getString("message") : "提交失败";
- status_menu.setText(msg);
- }
- } else if ("NG".equals(result)) {
- String sn = product_sn.getText().trim();
- JSONObject retObj = DataUtil.sendQuality( sn,result,user20);
- if (retObj != null && "true".equalsIgnoreCase(retObj.getString("result"))) {
- resetState();
- status_menu.setForeground(Color.GREEN);
- status_menu.setText("结果提交成功,请扫下一件");
- finish_ng_bt.setEnabled(false);
- } else {
- status_menu.setForeground(Color.RED);
- String msg = (retObj != null && retObj.containsKey("message")) ? retObj.getString("message") : "提交失败";
- status_menu.setText(msg);
- }
- }
- } else if (work_status == 0) {
- JSONObject result = DataUtil.getCurSn(mes_gw, mes_server_ip, mes_line_sn);
- // 正常处理结果
- if (result.containsKey("data")) {
- String data = result.getString("data");
- if (data != null && !data.isEmpty()) {
- product_sn.setText(data);
- JSONObject jsonObject = DataUtil.checkQuality(data, user20);
- String msg = jsonObject.getString("message");
- if (jsonObject.getString("result").equalsIgnoreCase("true")) {
- /* finish_ng_bt.setEnabled(true);
- finish_ok_bt.setEnabled(true);*/
- check_quality_result = true;
- work_status = 1;
- MesClient.setMenuStatus(msg,0);
- } else {
- work_status = 0;
- String msg2 = (jsonObject != null && jsonObject.containsKey("message")) ? jsonObject.getString("message") : "检查失败";
- MesClient.setMenuStatus(msg2,-1);
- check_quality_result = false;
- }
- }
- }
- String abc = PLCUtils.getResult(plc);
- System.out.println(abc);
- }
- }
- }, 100, 1000);
- }
- 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(){
- try{
- JSONObject retObj = DataUtil.getBindMaterail();
- if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
- java.util.List<BindMaterialResp> 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<BindMaterialResp> 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);
- }
- }
- }
- }
- }
- private static void resetState() {
- work_status = 0;
- plc_status = 0;
- check_quality_result = false;
- product_sn.setText("");
- finish_ok_bt.setEnabled(false);
- finish_ng_bt.setEnabled(false);
- fxlabel.setVisible(false);
- MesClient.setMenuStatus("请扫工件码",0);
- }
- 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("异步任务执行完毕");
- });
- }
- }
|