| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446 |
- package com.mes.ui;
- import com.alibaba.fastjson2.JSONObject;
- import com.mes.component.GaugeNgPointDialog;
- import com.mes.component.MesRadio;
- import com.mes.component.ProductTypePanel;
- import com.mes.component.MesWebView;
- import com.mes.util.DateLocalUtils;
- import com.mes.util.HttpUtils;
- import com.mes.util.JdbcUtils;
- import com.mes.util.PlcService;
- 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.net.InetSocketAddress;
- import java.net.Socket;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.time.LocalDateTime;
- 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 String mes_plc_ip = "192.168.2.1"; // PLC IP地址
- public static int mes_heart_icon_cycle = 1;
- public static String mes_line_sn = ""; // 产线编号
- //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 JButton heart_beat_menu;
- public static JButton status_menu;
- public static JButton user_menu;
- // “软件设置”页签索引(用于判断当前是否切换到该页签)
- public static int softwareSettingTabIndex = -1;
- // 软件设置页面中的输入控件与操作按钮
- private JTextField softwareIpField;
- private JTextField softwareOpField;
- private JTextField softwareOpNameField;
- private JButton softwareEditBtn;
- private JButton softwareSaveBtn;
- // 记录上一次非“软件设置”页签,用于密码校验失败时回退
- private int lastNonSoftwareTabIndex = 0;
- // 防止页签回退时触发重复校验导致递归切换
- private boolean softwareTabReverting = false;
- 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 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;
- public static JTable table;
- public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" };
- public static Object[][] rowData = null;
- public static JLabel fxlabel;
- public static void main(String[] args) {
- // if (LockUtil.getInstance().isAppActive() == false){
- //// JOptionPane.showMessageDialog(null, "已有一个程序在运行,程序退出");
- // return;
- // }else{
- EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- try{
- JdbcUtils.getConn();
- //读文件配置
- readProperty();
- // 显示界面
- mesClientFrame = new MesClient();
- mesClientFrame.setVisible(false);
- welcomeWin = new LoginFarme();
- welcomeWin.setVisible(true);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- });
- }
- // }
- //读配置文件
- private static void readProperty() throws IOException{
- String enconding = "UTF-8";
- InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
- if (is == null) {
- mes_gw = "";
- mes_line_sn = "";
- mes_gw_des = "";
- log.info("未找到config/config.properties,工位信息将由登录界面输入");
- return;
- }
- Properties pro = new Properties();
- BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
- pro.load(br);
- mes_gw = pro.getProperty("mes.gw");
- mes_line_sn = pro.getProperty("mes.line_sn");
- if (mes_gw == null) {
- mes_gw = "";
- }
- if (mes_line_sn == null) {
- mes_line_sn = "";
- }
- String plcIp = pro.getProperty("mes.plc_ip");
- if (plcIp != null && !plcIp.trim().isEmpty()) {
- mes_plc_ip = plcIp.trim();
- }
- // SQLite 优先;没有本地配置时再读 config.properties
- String ipFromDb = JdbcUtils.getServerIpByOp(mes_gw);
- if (ipFromDb != null && !ipFromDb.trim().isEmpty()) {
- mes_server_ip = ipFromDb.trim();
- } else {
- String serverIp = pro.getProperty("mes.server_ip");
- if (serverIp != null && !serverIp.trim().isEmpty()) {
- mes_server_ip = serverIp.trim();
- }
- }
- String nameFromDb = JdbcUtils.getOpNameByOp(mes_gw);
- if (nameFromDb != null && !nameFromDb.trim().isEmpty()) {
- mes_gw_des = nameFromDb.trim();
- } else if (!mes_gw.trim().isEmpty() && !mes_line_sn.trim().isEmpty()) {
- mes_gw_des = OprnoUtil.getGwDes(mes_line_sn, mes_gw);
- } else {
- mes_gw_des = "";
- }
- log.info(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";");
- }
- public static void applyOpConfig(String op, String opName, String ip) {
- if (op != null) {
- mes_gw = op.trim();
- }
- if (opName != null) {
- mes_gw_des = opName.trim();
- }
- if (ip != null) {
- mes_server_ip = ip.trim();
- }
- try {
- // 清除旧配置,确保单条记录(本地工位只保留当前配置)
- JdbcUtils.clearOpIpConfig();
- JdbcUtils.upsertOpIpConfig(mes_gw, mes_gw_des, mes_server_ip);
- } catch (Exception e) {
- log.info("保存工位配置失败: {}", e.getMessage());
- }
- if (mesClientFrame != null) {
- mesClientFrame.setTitle("MES系统客户端:" + mes_gw + "- " + mes_gw_des);
- }
- if (welcomeWin != null) {
- welcomeWin.setTitle("MES系统客户端:" + mes_gw + " - " + mes_gw_des);
- }
- }
- // 打开并切换到“软件设置”页签(实际访问权限由页签切换监听统一控制)
- public static void openSoftwareSettings() {
- if (mesClientFrame != null) {
- mesClientFrame.setVisible(true);
- }
- if (tabbedPane != null && softwareSettingTabIndex >= 0 && softwareSettingTabIndex < tabbedPane.getTabCount()) {
- tabbedPane.setSelectedIndex(softwareSettingTabIndex);
- }
- }
- public static boolean ensureOpConfig(Component parent) {
- // 1) 先尝试从本地SQLite读取一条配置(本地工位一般只会配置一次)
- String[] cfg = JdbcUtils.getAnyOpIpConfig();
- if (cfg != null && cfg.length >= 3) {
- String op = cfg[0];
- String opName = cfg[1];
- String ip = cfg[2];
- if (!ip.isEmpty() && !op.isEmpty()) {
- applyOpConfig(op, opName, ip);
- }
- }
- // 2) 如果已有配置则直接放行
- boolean missing = (mes_server_ip == null || mes_server_ip.trim().isEmpty())
- || (mes_gw == null || mes_gw.trim().isEmpty());
- if (!missing) {
- return true;
- }
- // 3) 没有配置才弹窗让用户输入并写入SQLite
- return showOpConfigDialog(parent);
- }
- public static boolean showOpConfigDialog(Component parent) {
- String currentOp = mes_gw == null ? "" : mes_gw.trim();
- String currentIp = mes_server_ip == null ? "" : mes_server_ip.trim();
- String currentName = mes_gw_des == null ? "" : mes_gw_des.trim();
- if (!currentOp.isEmpty()) {
- if (currentIp.isEmpty()) {
- String ipFromDb = JdbcUtils.getServerIpByOp(currentOp);
- if (ipFromDb != null && !ipFromDb.trim().isEmpty()) {
- currentIp = ipFromDb.trim();
- }
- }
- if (currentName.isEmpty()) {
- String nameFromDb = JdbcUtils.getOpNameByOp(currentOp);
- if (nameFromDb != null && !nameFromDb.trim().isEmpty()) {
- currentName = nameFromDb.trim();
- }
- }
- }
- JTextField ipField = new JTextField(currentIp);
- JTextField opField = new JTextField(currentOp);
- JTextField nameField = new JTextField(currentName);
- JPanel panel = new JPanel(new GridLayout(0, 1));
- panel.add(new JLabel("服务器IP/域名:"));
- panel.add(ipField);
- panel.add(new JLabel("工位号:"));
- panel.add(opField);
- panel.add(new JLabel("工位名称:"));
- panel.add(nameField);
- while (true) {
- int result = JOptionPane.showConfirmDialog(parent, panel, "软件设置", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
- if (result != JOptionPane.OK_OPTION) {
- return false;
- }
- String ip = ipField.getText() == null ? "" : ipField.getText().trim();
- String op = opField.getText() == null ? "" : opField.getText().trim();
- String opName = nameField.getText() == null ? "" : nameField.getText().trim();
- if (ip.isEmpty() || op.isEmpty()) {
- JOptionPane.showMessageDialog(parent, "服务器IP和工位号不能为空", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
- continue;
- }
- if (opName.isEmpty()) {
- opName = OprnoUtil.getGwDes(mes_line_sn, op);
- }
- if (!testMesServer(ip)) {
- JOptionPane.showMessageDialog(parent, "无法连接到MES服务器:" + ip + ":8980", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
- continue;
- }
- applyOpConfig(op, opName, ip);
- return true;
- }
- }
- private static void initServerIp() {
- String op = mes_gw;
- String ip = JdbcUtils.getServerIpByOp(op);
- while (true) {
- // 1) DB里有IP,先校验连通性(防止历史错误IP导致永远登录不了)
- if (ip != null && !ip.trim().isEmpty()) {
- ip = ip.trim();
- if (testMesServer(ip)) {
- break;
- }
- JOptionPane.showMessageDialog(null,
- "当前工位已配置的MES服务器IP/域名无法连接:" + ip + "\n请重新输入正确的IP/域名",
- "MES服务器设置",
- JOptionPane.WARNING_MESSAGE);
- }
- // 2) 让用户输入
- String input = JOptionPane.showInputDialog(null, "请输入MES服务器IP/域名", "MES服务器设置", JOptionPane.QUESTION_MESSAGE);
- if (input == null) {
- System.exit(0);
- return;
- }
- input = input.trim();
- if (input.isEmpty()) {
- continue;
- }
- // 3) 校验输入可连通,再写库
- if (!testMesServer(input)) {
- JOptionPane.showMessageDialog(null,
- "无法连接到MES服务器:" + input + ":8980\n请检查IP/域名是否正确,或网络是否可达",
- "MES服务器设置",
- JOptionPane.WARNING_MESSAGE);
- continue;
- }
- boolean ok = JdbcUtils.upsertOpIpConfig(op, mes_gw_des, input);
- if (ok) {
- ip = input;
- break;
- } else {
- JOptionPane.showMessageDialog(null, "保存失败,请重试", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
- }
- }
- mes_server_ip = ip;
- log.info(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";");
- }
- public static boolean testMesServer(String host) {
- if (host == null || host.trim().isEmpty()) {
- return false;
- }
- try (Socket socket = new Socket()) {
- socket.connect(new InetSocketAddress(host.trim(), 8980), 2500);
- return true;
- } catch (Exception e) {
- return false;
- }
- }
- //启动心跳包程序
- 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);
- }
- //设置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(){
- try {
- PlcService.init(mes_plc_ip);
- } catch (Exception e) {
- log.error("PLC 初始化失败", e);
- setMenuStatus("PLC 连接失败: " + e.getMessage(), -1);
- }
- loadProductTypeFromDb();
- resetScanA();
- }
- public static void resetScanA() {
- PlcService.stopWatchProcessDone();
- 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);
- updateProductTypePanelEnabled(true);
- if (PRODUCT_TYPE_SWITCH_ENABLED) {
- MesClient.setMenuStatus("当前生产类型:" + getProductTypeName() + ",请扫工件码",0);
- } else {
- MesClient.setMenuStatus("请扫工件码",0);
- }
- // MesClient.setMenuStatus("开班点检,请先进行OKNG样件测试",-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 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 "未知";
- }
- 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 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);
- // 查询工件质量
- JSONObject retObj = DataUtil.checkQuality(scanBarcode,user20);
- if(retObj == null){
- MesClient.check_quality_result = false;
- MesClient.work_status = 0;
- updateProductTypePanelEnabled(true);
- MesClient.setMenuStatus("请求失败,请重试",-1);
- return;
- }
- if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
- MesClient.status_menu.setForeground(Color.GREEN);
- MesClient.check_quality_result = true;
- MesClient.work_status = 1;
- MesClient.f_scan_data_bt_1.setEnabled(false);
- MesClient.finish_ok_bt.setEnabled(false);
- MesClient.finish_ng_bt.setEnabled(false);
- MesClient.setMenuStatus("校验通过,正在通知PLC允许加工...", 0);
- startPlcWorkFlow();
- }else{
- MesClient.check_quality_result = false;
- MesClient.work_status = 0;
- updateProductTypePanelEnabled(true);
- 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;
- }
- }
- private static void startPlcWorkFlow() {
- try {
- if (!PlcService.isInitialized()) {
- PlcService.init(mes_plc_ip);
- }
- String sn = MesClient.product_sn.getText().trim();
- PlcService.writeBarcode(sn);
- PlcService.writeAllowStart();
- MesClient.status_menu.setText("该工件可以加工,等待设备完成...");
- PlcService.startWatchProcessDone(() -> {
- MesClient.finish_ok_bt.setEnabled(true);
- MesClient.finish_ng_bt.setEnabled(true);
- MesClient.setMenuStatus("加工完成,请选择OK或NG", 0);
- });
- } catch (Exception e) {
- log.error("PLC 允许加工信号发送失败", e);
- MesClient.check_quality_result = false;
- MesClient.work_status = 0;
- MesClient.f_scan_data_bt_1.setEnabled(true);
- updateProductTypePanelEnabled(true);
- MesClient.setMenuStatus("PLC通信失败: " + e.getMessage(), -1);
- }
- }
- private static void submitQualityAndNotifyPlc(String quality) {
- submitQualityAndNotifyPlc(quality, null);
- }
- /**
- * @param ngPoints NG 时勾选的点位(逗号分隔),OK 时传 null
- */
- private static void submitQualityAndNotifyPlc(String quality, String ngPoints) {
- if (work_status != 1 || !check_quality_result) {
- return;
- }
- getUser();
- String sn = MesClient.product_sn.getText().trim();
- if (sn.isEmpty()) {
- MesClient.setMenuStatus("工件码为空,请重试", -1);
- return;
- }
- String validateMsg = validateProductSnMessage(sn);
- if (validateMsg != null) {
- MesClient.setMenuStatus(validateMsg, -1);
- return;
- }
- MesClient.finish_ok_bt.setEnabled(false);
- MesClient.finish_ng_bt.setEnabled(false);
- PlcService.stopWatchProcessDone();
- if ("NG".equalsIgnoreCase(quality) && ngPoints != null && !ngPoints.trim().isEmpty()) {
- JSONObject ngObj = DataUtil.sendGaugeNgPoints(sn, ngPoints.trim(), user20);
- if (ngObj == null) {
- MesClient.setMenuStatus("NG点位上传失败,请重试", -1);
- MesClient.finish_ok_bt.setEnabled(true);
- MesClient.finish_ng_bt.setEnabled(true);
- return;
- }
- if (ngObj.get("result") == null || !ngObj.get("result").toString().equalsIgnoreCase("true")) {
- String msg = ngObj.getString("message");
- MesClient.setMenuStatus(msg == null || msg.isEmpty() ? "NG点位上传失败" : msg, -1);
- MesClient.finish_ok_bt.setEnabled(true);
- MesClient.finish_ng_bt.setEnabled(true);
- return;
- }
- }
- JSONObject retObj = DataUtil.sendQuality(sn, quality, user20);
- if (retObj == null) {
- MesClient.setMenuStatus("请求失败,请重试", -1);
- MesClient.finish_ok_bt.setEnabled(true);
- MesClient.finish_ng_bt.setEnabled(true);
- return;
- }
- System.out.println(retObj);
- if (retObj.get("result") != null && retObj.get("result").toString().equalsIgnoreCase("true")) {
- MesClient.setMenuStatus("结果上传成功,正在通知PLC...", 0);
- new Thread(() -> {
- try {
- PlcService.writeUploadComplete();
- Thread.sleep(3000);
- PlcService.resetAllowAndUpload();
- SwingUtilities.invokeLater(() -> {
- MesClient.resetScanA();
- MesClient.setMenuStatus("结果提交成功,请扫下一件", 0);
- MesClient.scan_type = 1;
- MesClient.scanBarcode();
- });
- } catch (Exception e) {
- log.error("PLC 上传完成信号失败", e);
- SwingUtilities.invokeLater(() -> {
- MesClient.setMenuStatus("PLC信号发送失败: " + e.getMessage(), -1);
- MesClient.finish_ok_bt.setEnabled(true);
- MesClient.finish_ng_bt.setEnabled(true);
- });
- }
- }, "plc-after-upload").start();
- } else {
- if (retObj.get("result") == null) {
- MesClient.setMenuStatus("请求失败,请重试", -1);
- } else if (retObj.get("result").toString().equalsIgnoreCase("false")) {
- MesClient.setMenuStatus(retObj.getString("message"), -1);
- }
- MesClient.finish_ok_bt.setEnabled(true);
- MesClient.finish_ng_bt.setEnabled(true);
- }
- }
- 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) {
- PlcService.close();
- 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);
- JMenuItem manualSubmitMenu = new JMenuItem("手动提交");
- manualSubmitMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
- manualSubmitMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- manualSubmitMenu.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- super.mouseClicked(e);
- finish_ok_bt.setEnabled(true);
- finish_ng_bt.setEnabled(true);
- setMenuStatus("手动提交已开启,请选择OK或NG", 0);
- }
- });
- settingMenu.add(manualSubmitMenu);
- 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.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);
- fxlabel = new JLabel("该工件为返修件,请仔细检查");
- fxlabel.setFont(new Font("微软雅黑", Font.PLAIN, 38));
- fxlabel.setBounds(81, 330, 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) {
- submitQualityAndNotifyPlc("OK");
- }
- });
- 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, 420, 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) {
- String ngPoints = GaugeNgPointDialog.showDialog(MesClient.mesClientFrame);
- if (ngPoints == null || ngPoints.trim().isEmpty()) {
- return;
- }
- submitQualityAndNotifyPlc("NG", ngPoints.trim());
- }
- });
- 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, 420, 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);
- 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);
- }
- // searchScrollPane = new JScrollPane((Component) null);
- indexPanelC = new JPanel(new BorderLayout());
- tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexPanelC, 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);
- // 创建“软件设置”页面,并加入主Tab页签
- JPanel softwareSettingPanel = buildSoftwareSettingPanel();
- JScrollPane softwareSettingScrollPane = new JScrollPane(softwareSettingPanel);
- tabbedPane.addTab("软件设置", new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")), softwareSettingScrollPane, null);
- softwareSettingTabIndex = tabbedPane.getTabCount() - 1;
- // 页面初始化时先从本地配置加载软件设置
- loadSoftwareSettingsFromDb();
- // // 创建工作记录的子 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 (softwareTabReverting) {
- return;
- }
- if (selectedIndex == softwareSettingTabIndex) {
- // 每次点击进入“软件设置”页签都进行密码验证(固定密码:mes)
- if (!verifySoftwareSettingPassword()) {
- softwareTabReverting = true;
- tabbedPane.setSelectedIndex(lastNonSoftwareTabIndex);
- softwareTabReverting = false;
- return;
- }
- loadSoftwareSettingsFromDb();
- } else {
- // 只记录非“软件设置”页签,便于校验失败时回退
- lastNonSoftwareTabIndex = selectedIndex;
- }
- }
- });
- }
- // 软件设置页面UI构建(含编辑/保存/重载按钮)
- private JPanel buildSoftwareSettingPanel() {
- JPanel panel = new JPanel();
- panel.setLayout(null);
- JLabel title = new JLabel("软件设置");
- title.setFont(new Font("微软雅黑", Font.BOLD, 26));
- title.setBounds(40, 30, 300, 40);
- panel.add(title);
- JLabel ipLabel = new JLabel("服务器IP/域名:");
- ipLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- ipLabel.setBounds(40, 100, 200, 30);
- panel.add(ipLabel);
- softwareIpField = new JTextField("");
- softwareIpField.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- softwareIpField.setBounds(220, 100, 360, 34);
- softwareIpField.setEditable(false);
- panel.add(softwareIpField);
- JLabel opLabel = new JLabel("工位号:");
- opLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- opLabel.setBounds(40, 150, 200, 30);
- panel.add(opLabel);
- softwareOpField = new JTextField("");
- softwareOpField.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- softwareOpField.setBounds(220, 150, 360, 34);
- softwareOpField.setEditable(false);
- panel.add(softwareOpField);
- JLabel nameLabel = new JLabel("工位名称:");
- nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- nameLabel.setBounds(40, 200, 200, 30);
- panel.add(nameLabel);
- softwareOpNameField = new JTextField("");
- softwareOpNameField.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- softwareOpNameField.setBounds(220, 200, 360, 34);
- softwareOpNameField.setEditable(false);
- panel.add(softwareOpNameField);
- softwareEditBtn = new JButton("编辑");
- softwareEditBtn.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- softwareEditBtn.setBounds(220, 270, 140, 44);
- softwareEditBtn.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // 点击“编辑”后允许修改输入框,并开放保存按钮
- softwareIpField.setEditable(true);
- softwareOpField.setEditable(true);
- softwareOpNameField.setEditable(true);
- if (softwareSaveBtn != null) {
- softwareSaveBtn.setEnabled(true);
- }
- }
- });
- panel.add(softwareEditBtn);
- softwareSaveBtn = new JButton("保存");
- softwareSaveBtn.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- softwareSaveBtn.setBounds(380, 270, 140, 44);
- softwareSaveBtn.setEnabled(false);
- softwareSaveBtn.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // 保存前读取并校验输入内容
- String ip = softwareIpField.getText() == null ? "" : softwareIpField.getText().trim();
- String op = softwareOpField.getText() == null ? "" : softwareOpField.getText().trim();
- String opName = softwareOpNameField.getText() == null ? "" : softwareOpNameField.getText().trim();
- if (ip.isEmpty() || op.isEmpty()) {
- JOptionPane.showMessageDialog(mesClientFrame, "服务器IP和工位号不能为空", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
- return;
- }
- if (opName.isEmpty()) {
- opName = OprnoUtil.getGwDes(mes_line_sn, op);
- }
- if (!testMesServer(ip)) {
- JOptionPane.showMessageDialog(mesClientFrame, "无法连接到MES服务器:" + ip + ":8980", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
- return;
- }
- // 写入本地配置并刷新显示
- applyOpConfig(op, opName, ip);
- loadSoftwareSettingsFromDb();
- softwareIpField.setEditable(false);
- softwareOpField.setEditable(false);
- softwareOpNameField.setEditable(false);
- softwareSaveBtn.setEnabled(false);
- JOptionPane.showMessageDialog(mesClientFrame, "保存成功,请重启软件", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
- }
- });
- panel.add(softwareSaveBtn);
- JButton reloadBtn = new JButton("从本地配置读取");
- reloadBtn.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- reloadBtn.setBounds(540, 270, 200, 44);
- reloadBtn.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // 从本地数据库读取配置并回填页面
- loadSoftwareSettingsFromDb();
- }
- });
- panel.add(reloadBtn);
- panel.setPreferredSize(new Dimension(990, 550));
- return panel;
- }
- // 读取并回填“软件设置”页面数据
- private void loadSoftwareSettingsFromDb() {
- String[] cfg = JdbcUtils.getAnyOpIpConfig();
- if (cfg != null && cfg.length >= 3) {
- applyOpConfig(cfg[0], cfg[1], cfg[2]);
- softwareOpField.setText(cfg[0]);
- softwareOpNameField.setText(cfg[1]);
- softwareIpField.setText(cfg[2]);
- } else {
- softwareOpField.setText(mes_gw == null ? "" : mes_gw);
- softwareOpNameField.setText(mes_gw_des == null ? "" : mes_gw_des);
- softwareIpField.setText(mes_server_ip == null ? "" : mes_server_ip);
- }
- if (softwareSaveBtn != null) {
- softwareSaveBtn.setEnabled(false);
- }
- if (softwareIpField != null) {
- softwareIpField.setEditable(false);
- }
- if (softwareOpField != null) {
- softwareOpField.setEditable(false);
- }
- if (softwareOpNameField != null) {
- softwareOpNameField.setEditable(false);
- }
- }
- // 进入“软件设置”页签前的密码校验(固定密码:mes)
- private boolean verifySoftwareSettingPassword() {
- String input = JOptionPane.showInputDialog(this, "请输入软件设置密码:", "软件设置权限验证", JOptionPane.QUESTION_MESSAGE);
- if (input == null) {
- return false;
- }
- if ("mes".equals(input.trim())) {
- return true;
- }
- JOptionPane.showMessageDialog(this, "密码错误,无法进入软件设置页面", "提示窗口", JOptionPane.WARNING_MESSAGE);
- return false;
- }
- 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 == null){
- return;
- }
- 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 == null){
- return;
- }
- 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);
- }
- }
- }
- }
- }
- 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("异步任务执行完毕");
- });
- }
- }
|