MesClient.java 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.mes.component.GaugeNgPointDialog;
  4. import com.mes.component.MesRadio;
  5. import com.mes.component.ProductTypePanel;
  6. import com.mes.component.MesWebView;
  7. import com.mes.util.DateLocalUtils;
  8. import com.mes.util.HttpUtils;
  9. import com.mes.util.JdbcUtils;
  10. import com.mes.util.PlcService;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import javax.swing.*;
  14. import javax.swing.border.EmptyBorder;
  15. import javax.swing.event.ChangeEvent;
  16. import javax.swing.event.ChangeListener;
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import java.io.*;
  20. import java.net.InetSocketAddress;
  21. import java.net.Socket;
  22. import java.sql.ResultSet;
  23. import java.sql.Statement;
  24. import java.time.LocalDateTime;
  25. import java.util.List;
  26. import java.util.Properties;
  27. import java.util.Timer;
  28. import java.util.TimerTask;
  29. import java.util.concurrent.ExecutorService;
  30. import java.util.concurrent.Executors;
  31. public class MesClient extends JFrame {
  32. public static final Logger log = LoggerFactory.getLogger(MesClient.class);
  33. public static int mes_auth = 0; // 权限级别 0=无权限 1=操作工人 2=管理员
  34. public static String mes_gw = ""; // 工位号
  35. public static String mes_gw_des = ""; // 工位名称
  36. public static String mes_server_ip = ""; // 服务器IP地址
  37. public static String mes_plc_ip = "192.168.2.1"; // PLC IP地址
  38. public static int mes_heart_icon_cycle = 1;
  39. public static String mes_line_sn = ""; // 产线编号
  40. //session
  41. public static String sessionid = "";
  42. public static JPanel contentPane;
  43. public static MesClient mesClientFrame;
  44. public static JTabbedPane tabbedPane;
  45. public static JScrollPane indexScrollPaneA;
  46. public static JScrollPane searchScrollPane; // 已弃用,改为使用 WorkRecordPanel
  47. public static JScrollPane searchScrollPaneDj;
  48. public static Boolean check_quality_result = false;
  49. public static Integer work_status = 0;
  50. public static JButton heart_beat_menu;
  51. public static JButton status_menu;
  52. public static JButton user_menu;
  53. // “软件设置”页签索引(用于判断当前是否切换到该页签)
  54. public static int softwareSettingTabIndex = -1;
  55. // 软件设置页面中的输入控件与操作按钮
  56. private JTextField softwareIpField;
  57. private JTextField softwareOpField;
  58. private JTextField softwareOpNameField;
  59. private JButton softwareEditBtn;
  60. private JButton softwareSaveBtn;
  61. // 记录上一次非“软件设置”页签,用于密码校验失败时回退
  62. private int lastNonSoftwareTabIndex = 0;
  63. // 防止页签回退时触发重复校验导致递归切换
  64. private boolean softwareTabReverting = false;
  65. public static int scan_type = 0;
  66. public static JButton finish_ok_bt;
  67. public static JButton finish_ng_bt;
  68. public static JTextField product_sn;
  69. public static JButton f_scan_data_bt_1;
  70. public static String user20 = "";
  71. public static JFrame welcomeWin;
  72. public static JPanel indexPanelB; // 已弃用,改为使用 WorkRecordPanel
  73. public static MesWebView jfxPanel = null;
  74. public static JPanel indexPanelC;
  75. public static MesWebView jfxPanel2 = null;
  76. public static MesRadio mesRadioHj;
  77. public static ProductTypePanel productTypePanel;
  78. public static JLabel productTypeCurrentLabel;
  79. public static final int PRODUCT_SN_LENGTH = 32;
  80. public static final String PREFIX_ZJ = "000020021815-01000016"; //智界
  81. public static final String PREFIX_SJ = "000020022029-01000016"; //尚界
  82. public static final boolean PRODUCT_TYPE_SWITCH_ENABLED = false;
  83. public static final String PRODUCT_TYPE_SWITCH_PASSWORD = "503833";
  84. public static final String PRODUCT_TYPE_ZJ = ProductTypePanel.TYPE_ZJ;
  85. public static final String PRODUCT_TYPE_SJ = ProductTypePanel.TYPE_SJ;
  86. public static JTable table;
  87. public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" };
  88. public static Object[][] rowData = null;
  89. public static JLabel fxlabel;
  90. public static void main(String[] args) {
  91. // if (LockUtil.getInstance().isAppActive() == false){
  92. //// JOptionPane.showMessageDialog(null, "已有一个程序在运行,程序退出");
  93. // return;
  94. // }else{
  95. EventQueue.invokeLater(new Runnable() {
  96. @Override
  97. public void run() {
  98. try{
  99. JdbcUtils.getConn();
  100. //读文件配置
  101. readProperty();
  102. // 显示界面
  103. mesClientFrame = new MesClient();
  104. mesClientFrame.setVisible(false);
  105. welcomeWin = new LoginFarme();
  106. welcomeWin.setVisible(true);
  107. }catch (Exception e){
  108. e.printStackTrace();
  109. }
  110. }
  111. });
  112. }
  113. // }
  114. //读配置文件
  115. private static void readProperty() throws IOException{
  116. String enconding = "UTF-8";
  117. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  118. if (is == null) {
  119. mes_gw = "";
  120. mes_line_sn = "";
  121. mes_gw_des = "";
  122. log.info("未找到config/config.properties,工位信息将由登录界面输入");
  123. return;
  124. }
  125. Properties pro = new Properties();
  126. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  127. pro.load(br);
  128. mes_gw = pro.getProperty("mes.gw");
  129. mes_line_sn = pro.getProperty("mes.line_sn");
  130. if (mes_gw == null) {
  131. mes_gw = "";
  132. }
  133. if (mes_line_sn == null) {
  134. mes_line_sn = "";
  135. }
  136. String plcIp = pro.getProperty("mes.plc_ip");
  137. if (plcIp != null && !plcIp.trim().isEmpty()) {
  138. mes_plc_ip = plcIp.trim();
  139. }
  140. // SQLite 优先;没有本地配置时再读 config.properties
  141. String ipFromDb = JdbcUtils.getServerIpByOp(mes_gw);
  142. if (ipFromDb != null && !ipFromDb.trim().isEmpty()) {
  143. mes_server_ip = ipFromDb.trim();
  144. } else {
  145. String serverIp = pro.getProperty("mes.server_ip");
  146. if (serverIp != null && !serverIp.trim().isEmpty()) {
  147. mes_server_ip = serverIp.trim();
  148. }
  149. }
  150. String nameFromDb = JdbcUtils.getOpNameByOp(mes_gw);
  151. if (nameFromDb != null && !nameFromDb.trim().isEmpty()) {
  152. mes_gw_des = nameFromDb.trim();
  153. } else if (!mes_gw.trim().isEmpty() && !mes_line_sn.trim().isEmpty()) {
  154. mes_gw_des = OprnoUtil.getGwDes(mes_line_sn, mes_gw);
  155. } else {
  156. mes_gw_des = "";
  157. }
  158. log.info(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";");
  159. }
  160. public static void applyOpConfig(String op, String opName, String ip) {
  161. if (op != null) {
  162. mes_gw = op.trim();
  163. }
  164. if (opName != null) {
  165. mes_gw_des = opName.trim();
  166. }
  167. if (ip != null) {
  168. mes_server_ip = ip.trim();
  169. }
  170. try {
  171. // 清除旧配置,确保单条记录(本地工位只保留当前配置)
  172. JdbcUtils.clearOpIpConfig();
  173. JdbcUtils.upsertOpIpConfig(mes_gw, mes_gw_des, mes_server_ip);
  174. } catch (Exception e) {
  175. log.info("保存工位配置失败: {}", e.getMessage());
  176. }
  177. if (mesClientFrame != null) {
  178. mesClientFrame.setTitle("MES系统客户端:" + mes_gw + "- " + mes_gw_des);
  179. }
  180. if (welcomeWin != null) {
  181. welcomeWin.setTitle("MES系统客户端:" + mes_gw + " - " + mes_gw_des);
  182. }
  183. }
  184. // 打开并切换到“软件设置”页签(实际访问权限由页签切换监听统一控制)
  185. public static void openSoftwareSettings() {
  186. if (mesClientFrame != null) {
  187. mesClientFrame.setVisible(true);
  188. }
  189. if (tabbedPane != null && softwareSettingTabIndex >= 0 && softwareSettingTabIndex < tabbedPane.getTabCount()) {
  190. tabbedPane.setSelectedIndex(softwareSettingTabIndex);
  191. }
  192. }
  193. public static boolean ensureOpConfig(Component parent) {
  194. // 1) 先尝试从本地SQLite读取一条配置(本地工位一般只会配置一次)
  195. String[] cfg = JdbcUtils.getAnyOpIpConfig();
  196. if (cfg != null && cfg.length >= 3) {
  197. String op = cfg[0];
  198. String opName = cfg[1];
  199. String ip = cfg[2];
  200. if (!ip.isEmpty() && !op.isEmpty()) {
  201. applyOpConfig(op, opName, ip);
  202. }
  203. }
  204. // 2) 如果已有配置则直接放行
  205. boolean missing = (mes_server_ip == null || mes_server_ip.trim().isEmpty())
  206. || (mes_gw == null || mes_gw.trim().isEmpty());
  207. if (!missing) {
  208. return true;
  209. }
  210. // 3) 没有配置才弹窗让用户输入并写入SQLite
  211. return showOpConfigDialog(parent);
  212. }
  213. public static boolean showOpConfigDialog(Component parent) {
  214. String currentOp = mes_gw == null ? "" : mes_gw.trim();
  215. String currentIp = mes_server_ip == null ? "" : mes_server_ip.trim();
  216. String currentName = mes_gw_des == null ? "" : mes_gw_des.trim();
  217. if (!currentOp.isEmpty()) {
  218. if (currentIp.isEmpty()) {
  219. String ipFromDb = JdbcUtils.getServerIpByOp(currentOp);
  220. if (ipFromDb != null && !ipFromDb.trim().isEmpty()) {
  221. currentIp = ipFromDb.trim();
  222. }
  223. }
  224. if (currentName.isEmpty()) {
  225. String nameFromDb = JdbcUtils.getOpNameByOp(currentOp);
  226. if (nameFromDb != null && !nameFromDb.trim().isEmpty()) {
  227. currentName = nameFromDb.trim();
  228. }
  229. }
  230. }
  231. JTextField ipField = new JTextField(currentIp);
  232. JTextField opField = new JTextField(currentOp);
  233. JTextField nameField = new JTextField(currentName);
  234. JPanel panel = new JPanel(new GridLayout(0, 1));
  235. panel.add(new JLabel("服务器IP/域名:"));
  236. panel.add(ipField);
  237. panel.add(new JLabel("工位号:"));
  238. panel.add(opField);
  239. panel.add(new JLabel("工位名称:"));
  240. panel.add(nameField);
  241. while (true) {
  242. int result = JOptionPane.showConfirmDialog(parent, panel, "软件设置", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  243. if (result != JOptionPane.OK_OPTION) {
  244. return false;
  245. }
  246. String ip = ipField.getText() == null ? "" : ipField.getText().trim();
  247. String op = opField.getText() == null ? "" : opField.getText().trim();
  248. String opName = nameField.getText() == null ? "" : nameField.getText().trim();
  249. if (ip.isEmpty() || op.isEmpty()) {
  250. JOptionPane.showMessageDialog(parent, "服务器IP和工位号不能为空", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
  251. continue;
  252. }
  253. if (opName.isEmpty()) {
  254. opName = OprnoUtil.getGwDes(mes_line_sn, op);
  255. }
  256. if (!testMesServer(ip)) {
  257. JOptionPane.showMessageDialog(parent, "无法连接到MES服务器:" + ip + ":8980", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
  258. continue;
  259. }
  260. applyOpConfig(op, opName, ip);
  261. return true;
  262. }
  263. }
  264. private static void initServerIp() {
  265. String op = mes_gw;
  266. String ip = JdbcUtils.getServerIpByOp(op);
  267. while (true) {
  268. // 1) DB里有IP,先校验连通性(防止历史错误IP导致永远登录不了)
  269. if (ip != null && !ip.trim().isEmpty()) {
  270. ip = ip.trim();
  271. if (testMesServer(ip)) {
  272. break;
  273. }
  274. JOptionPane.showMessageDialog(null,
  275. "当前工位已配置的MES服务器IP/域名无法连接:" + ip + "\n请重新输入正确的IP/域名",
  276. "MES服务器设置",
  277. JOptionPane.WARNING_MESSAGE);
  278. }
  279. // 2) 让用户输入
  280. String input = JOptionPane.showInputDialog(null, "请输入MES服务器IP/域名", "MES服务器设置", JOptionPane.QUESTION_MESSAGE);
  281. if (input == null) {
  282. System.exit(0);
  283. return;
  284. }
  285. input = input.trim();
  286. if (input.isEmpty()) {
  287. continue;
  288. }
  289. // 3) 校验输入可连通,再写库
  290. if (!testMesServer(input)) {
  291. JOptionPane.showMessageDialog(null,
  292. "无法连接到MES服务器:" + input + ":8980\n请检查IP/域名是否正确,或网络是否可达",
  293. "MES服务器设置",
  294. JOptionPane.WARNING_MESSAGE);
  295. continue;
  296. }
  297. boolean ok = JdbcUtils.upsertOpIpConfig(op, mes_gw_des, input);
  298. if (ok) {
  299. ip = input;
  300. break;
  301. } else {
  302. JOptionPane.showMessageDialog(null, "保存失败,请重试", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
  303. }
  304. }
  305. mes_server_ip = ip;
  306. log.info(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";");
  307. }
  308. public static boolean testMesServer(String host) {
  309. if (host == null || host.trim().isEmpty()) {
  310. return false;
  311. }
  312. try (Socket socket = new Socket()) {
  313. socket.connect(new InetSocketAddress(host.trim(), 8980), 2500);
  314. return true;
  315. } catch (Exception e) {
  316. return false;
  317. }
  318. }
  319. //启动心跳包程序
  320. public static java.util.Timer heartBeatTimer;
  321. public static java.util.Timer heartBeatIconTimer;
  322. public static boolean iconREDFlag = true;
  323. public static void startHeartBeatTimer() {
  324. if(heartBeatTimer!=null) {
  325. heartBeatTimer.cancel();
  326. }
  327. //心跳显示图标
  328. if(heartBeatIconTimer!=null) {
  329. heartBeatIconTimer.cancel();
  330. }
  331. heartBeatIconTimer = new Timer();
  332. heartBeatIconTimer.schedule(new TimerTask() {
  333. public void run() {
  334. if(iconREDFlag) {
  335. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  336. iconREDFlag = false;
  337. }else {
  338. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  339. iconREDFlag = true;
  340. }
  341. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  342. heart_beat_menu.repaint();
  343. }
  344. }, 100,mes_heart_icon_cycle*1000);
  345. }
  346. //设置tcp连接状态显示
  347. public static void setTcpStatus() {
  348. status_menu.setText("已连接MES服务器");
  349. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  350. heart_beat_menu.repaint();
  351. }
  352. public static void initWarehouseData(){
  353. try {
  354. PlcService.init(mes_plc_ip);
  355. } catch (Exception e) {
  356. log.error("PLC 初始化失败", e);
  357. setMenuStatus("PLC 连接失败: " + e.getMessage(), -1);
  358. }
  359. loadProductTypeFromDb();
  360. resetScanA();
  361. }
  362. public static void resetScanA() {
  363. PlcService.stopWatchProcessDone();
  364. work_status = 0;
  365. check_quality_result = false;
  366. MesClient.finish_ok_bt.setEnabled(false);
  367. MesClient.finish_ng_bt.setEnabled(false);
  368. product_sn.setText("");
  369. MesClient.fxlabel.setVisible(false);
  370. MesClient.f_scan_data_bt_1.setEnabled(true);
  371. updateProductTypePanelEnabled(true);
  372. if (PRODUCT_TYPE_SWITCH_ENABLED) {
  373. MesClient.setMenuStatus("当前生产类型:" + getProductTypeName() + ",请扫工件码",0);
  374. } else {
  375. MesClient.setMenuStatus("请扫工件码",0);
  376. }
  377. // MesClient.setMenuStatus("开班点检,请先进行OKNG样件测试",-1);
  378. updateMaterailData();
  379. // shiftUserCheck();
  380. }
  381. public static int userLoginHours;//用户登录所处小时
  382. //换班用户信息检查
  383. private static void shiftUserCheck() {
  384. LocalDateTime now = LocalDateTime.now();
  385. // 判断时间范围
  386. if (userLoginHours >= 8 && userLoginHours < 20) {
  387. int hour = now.getHour();
  388. if (hour >= 20 || hour < 8) {
  389. logoff();
  390. }
  391. } else {
  392. int hour = now.getHour();
  393. if (hour >= 8 && hour < 20) {
  394. logoff();
  395. }
  396. }
  397. }
  398. public static void resetScanB() {
  399. }
  400. //获取用户20位
  401. public static void getUser() {
  402. user20 = user_menu.getText().toString();
  403. // String space_tmp1 = "";
  404. // if(user20.length()<20) {
  405. // for(int i=0;i<20-user20.length();i++) {
  406. // space_tmp1 = space_tmp1 + " ";
  407. // }
  408. // }
  409. // user20 = user20 + space_tmp1;
  410. }
  411. //获取barcode内容36位
  412. public static String getBarcode(String barcodeTmp) {
  413. String barcodeRet = barcodeTmp;
  414. if(barcodeTmp.equalsIgnoreCase("")) {
  415. return "";
  416. }else {
  417. if(barcodeTmp.length()<36) {
  418. String space = "";
  419. for(int i=0;i<36-barcodeTmp.length();i++) {
  420. space = space + " ";
  421. }
  422. barcodeRet = barcodeTmp + space;
  423. }
  424. }
  425. return barcodeRet;
  426. }
  427. public static void loadProductTypeFromDb() {
  428. if (productTypePanel == null) {
  429. return;
  430. }
  431. String savedType = JdbcUtils.getClientConfig(
  432. JdbcUtils.getProductTypeConfigKey(mes_gw),
  433. PRODUCT_TYPE_ZJ
  434. );
  435. if (PRODUCT_TYPE_SJ.equals(savedType)) {
  436. productTypePanel.setResult(PRODUCT_TYPE_SJ);
  437. } else {
  438. productTypePanel.setResult(PRODUCT_TYPE_ZJ);
  439. }
  440. refreshProductTypeCurrentLabel();
  441. }
  442. public static void refreshProductTypeCurrentLabel() {
  443. if (productTypeCurrentLabel != null) {
  444. productTypeCurrentLabel.setText("当前选择:" + getProductTypeName() + "(前缀 " + getProductSnPrefix() + ")");
  445. }
  446. }
  447. public static void saveProductTypeToDb() {
  448. if (productTypePanel == null) {
  449. return;
  450. }
  451. JdbcUtils.saveClientConfig(
  452. JdbcUtils.getProductTypeConfigKey(mes_gw),
  453. productTypePanel.getResult()
  454. );
  455. }
  456. public static String getProductSnPrefix() {
  457. if (productTypePanel != null && PRODUCT_TYPE_SJ.equals(productTypePanel.getResult())) {
  458. return PREFIX_SJ;
  459. }
  460. return PREFIX_ZJ;
  461. }
  462. public static String getProductTypeName() {
  463. if (productTypePanel != null) {
  464. return productTypePanel.getTypeName();
  465. }
  466. return "智界";
  467. }
  468. private static String getProductTypeNameBySn(String sn) {
  469. if (sn.startsWith(PREFIX_SJ)) {
  470. return "尚界";
  471. }
  472. if (sn.startsWith(PREFIX_ZJ)) {
  473. return "智界";
  474. }
  475. return "未知";
  476. }
  477. private static String validateProductSnMessage(String sn) {
  478. if (sn == null || sn.isEmpty()) {
  479. return "工件码为空,请重试";
  480. }
  481. if (sn.length() != PRODUCT_SN_LENGTH) {
  482. return "工件码长度须为32位,当前" + sn.length() + "位";
  483. }
  484. if (!PRODUCT_TYPE_SWITCH_ENABLED) {
  485. return null;
  486. }
  487. String prefix = getProductSnPrefix();
  488. if (!sn.startsWith(prefix)) {
  489. return "工件码前缀与当前类型(" + getProductTypeName() + ")不匹配,当前工件码为" + getProductTypeNameBySn(sn);
  490. }
  491. return null;
  492. }
  493. private static void updateProductTypePanelEnabled(boolean enabled) {
  494. if (productTypePanel != null) {
  495. productTypePanel.setEnabled(enabled);
  496. }
  497. }
  498. public static void scanBarcode() {
  499. if(work_status == 1){
  500. JOptionPane.showMessageDialog(mesClientFrame,"工作中,勿扫码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  501. return;
  502. }
  503. String scanBarcodeTitle = "";
  504. switch(scan_type) {
  505. case 1:
  506. product_sn.setText("");
  507. scanBarcodeTitle = "请扫工件码";
  508. break;
  509. }
  510. //弹窗扫工件码
  511. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  512. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  513. scanBarcode = scanBarcode.trim();
  514. //获取用户
  515. getUser();
  516. //获取扫码内容36位
  517. String barcode36 = getBarcode(scanBarcode);//处理36为码
  518. //工位号
  519. String gw = "";
  520. switch(scan_type) {
  521. case 1:
  522. product_sn.setText(scanBarcode);
  523. break;
  524. }
  525. //刷新界面
  526. mesClientFrame.repaint();
  527. String validateMsg = validateProductSnMessage(scanBarcode);
  528. if (validateMsg != null) {
  529. check_quality_result = false;
  530. work_status = 0;
  531. MesClient.finish_ok_bt.setEnabled(false);
  532. MesClient.finish_ng_bt.setEnabled(false);
  533. MesClient.f_scan_data_bt_1.setEnabled(true);
  534. updateProductTypePanelEnabled(true);
  535. MesClient.setMenuStatus(validateMsg, -1);
  536. return;
  537. }
  538. updateProductTypePanelEnabled(false);
  539. // 查询工件质量
  540. JSONObject retObj = DataUtil.checkQuality(scanBarcode,user20);
  541. if(retObj == null){
  542. MesClient.check_quality_result = false;
  543. MesClient.work_status = 0;
  544. updateProductTypePanelEnabled(true);
  545. MesClient.setMenuStatus("请求失败,请重试",-1);
  546. return;
  547. }
  548. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  549. MesClient.status_menu.setForeground(Color.GREEN);
  550. MesClient.check_quality_result = true;
  551. MesClient.work_status = 1;
  552. MesClient.f_scan_data_bt_1.setEnabled(false);
  553. MesClient.finish_ok_bt.setEnabled(false);
  554. MesClient.finish_ng_bt.setEnabled(false);
  555. MesClient.setMenuStatus("校验通过,正在通知PLC允许加工...", 0);
  556. startPlcWorkFlow();
  557. }else{
  558. MesClient.check_quality_result = false;
  559. MesClient.work_status = 0;
  560. updateProductTypePanelEnabled(true);
  561. if(retObj.get("result")==null){
  562. MesClient.setMenuStatus("请求失败,请重试",-1);
  563. }else{
  564. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  565. MesClient.setMenuStatus(retObj.getString("message"),-1);
  566. }
  567. }
  568. }
  569. }else {
  570. MesClient.setMenuStatus("请扫工件码,请重试",-1);
  571. // JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  572. return;
  573. }
  574. }
  575. private static void startPlcWorkFlow() {
  576. try {
  577. if (!PlcService.isInitialized()) {
  578. PlcService.init(mes_plc_ip);
  579. }
  580. String sn = MesClient.product_sn.getText().trim();
  581. PlcService.writeBarcode(sn);
  582. PlcService.writeAllowStart();
  583. MesClient.status_menu.setText("该工件可以加工,等待设备完成...");
  584. PlcService.startWatchProcessDone(() -> {
  585. MesClient.finish_ok_bt.setEnabled(true);
  586. MesClient.finish_ng_bt.setEnabled(true);
  587. MesClient.setMenuStatus("加工完成,请选择OK或NG", 0);
  588. });
  589. } catch (Exception e) {
  590. log.error("PLC 允许加工信号发送失败", e);
  591. MesClient.check_quality_result = false;
  592. MesClient.work_status = 0;
  593. MesClient.f_scan_data_bt_1.setEnabled(true);
  594. updateProductTypePanelEnabled(true);
  595. MesClient.setMenuStatus("PLC通信失败: " + e.getMessage(), -1);
  596. }
  597. }
  598. private static void submitQualityAndNotifyPlc(String quality) {
  599. submitQualityAndNotifyPlc(quality, null);
  600. }
  601. /**
  602. * @param ngPoints NG 时勾选的点位(逗号分隔),OK 时传 null
  603. */
  604. private static void submitQualityAndNotifyPlc(String quality, String ngPoints) {
  605. if (work_status != 1 || !check_quality_result) {
  606. return;
  607. }
  608. getUser();
  609. String sn = MesClient.product_sn.getText().trim();
  610. if (sn.isEmpty()) {
  611. MesClient.setMenuStatus("工件码为空,请重试", -1);
  612. return;
  613. }
  614. String validateMsg = validateProductSnMessage(sn);
  615. if (validateMsg != null) {
  616. MesClient.setMenuStatus(validateMsg, -1);
  617. return;
  618. }
  619. MesClient.finish_ok_bt.setEnabled(false);
  620. MesClient.finish_ng_bt.setEnabled(false);
  621. PlcService.stopWatchProcessDone();
  622. if ("NG".equalsIgnoreCase(quality) && ngPoints != null && !ngPoints.trim().isEmpty()) {
  623. JSONObject ngObj = DataUtil.sendGaugeNgPoints(sn, ngPoints.trim(), user20);
  624. if (ngObj == null) {
  625. MesClient.setMenuStatus("NG点位上传失败,请重试", -1);
  626. MesClient.finish_ok_bt.setEnabled(true);
  627. MesClient.finish_ng_bt.setEnabled(true);
  628. return;
  629. }
  630. if (ngObj.get("result") == null || !ngObj.get("result").toString().equalsIgnoreCase("true")) {
  631. String msg = ngObj.getString("message");
  632. MesClient.setMenuStatus(msg == null || msg.isEmpty() ? "NG点位上传失败" : msg, -1);
  633. MesClient.finish_ok_bt.setEnabled(true);
  634. MesClient.finish_ng_bt.setEnabled(true);
  635. return;
  636. }
  637. }
  638. JSONObject retObj = DataUtil.sendQuality(sn, quality, user20);
  639. if (retObj == null) {
  640. MesClient.setMenuStatus("请求失败,请重试", -1);
  641. MesClient.finish_ok_bt.setEnabled(true);
  642. MesClient.finish_ng_bt.setEnabled(true);
  643. return;
  644. }
  645. System.out.println(retObj);
  646. if (retObj.get("result") != null && retObj.get("result").toString().equalsIgnoreCase("true")) {
  647. MesClient.setMenuStatus("结果上传成功,正在通知PLC...", 0);
  648. new Thread(() -> {
  649. try {
  650. PlcService.writeUploadComplete();
  651. Thread.sleep(3000);
  652. PlcService.resetAllowAndUpload();
  653. SwingUtilities.invokeLater(() -> {
  654. MesClient.resetScanA();
  655. MesClient.setMenuStatus("结果提交成功,请扫下一件", 0);
  656. MesClient.scan_type = 1;
  657. MesClient.scanBarcode();
  658. });
  659. } catch (Exception e) {
  660. log.error("PLC 上传完成信号失败", e);
  661. SwingUtilities.invokeLater(() -> {
  662. MesClient.setMenuStatus("PLC信号发送失败: " + e.getMessage(), -1);
  663. MesClient.finish_ok_bt.setEnabled(true);
  664. MesClient.finish_ng_bt.setEnabled(true);
  665. });
  666. }
  667. }, "plc-after-upload").start();
  668. } else {
  669. if (retObj.get("result") == null) {
  670. MesClient.setMenuStatus("请求失败,请重试", -1);
  671. } else if (retObj.get("result").toString().equalsIgnoreCase("false")) {
  672. MesClient.setMenuStatus(retObj.getString("message"), -1);
  673. }
  674. MesClient.finish_ok_bt.setEnabled(true);
  675. MesClient.finish_ng_bt.setEnabled(true);
  676. }
  677. }
  678. public static void logoff() {
  679. welcomeWin.setVisible(true);
  680. mesClientFrame.setVisible(false);
  681. }
  682. public MesClient() {
  683. setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png")));
  684. setTitle("MES系统客户端:"+mes_gw + "- " + mes_gw_des);
  685. // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  686. setBounds(0, 0, 1024, 768);
  687. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  688. // 添加窗口监听器
  689. addWindowListener(new WindowAdapter() {
  690. @Override
  691. public void windowClosing(WindowEvent e) {
  692. int choice = JOptionPane.showConfirmDialog(null, "确定要关闭窗口吗?", "关闭确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  693. if (choice == JOptionPane.YES_OPTION) {
  694. PlcService.close();
  695. dispose(); // 释放窗口资源并关闭窗口
  696. System.exit(0);
  697. }
  698. }
  699. });
  700. JMenuBar menuBar = new JMenuBar();
  701. menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26));
  702. setJMenuBar(menuBar);
  703. JMenu fileMenu = new JMenu("用户");
  704. fileMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png")));
  705. fileMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  706. menuBar.add(fileMenu);
  707. JMenuItem exitMenuItem = new JMenuItem("退出");
  708. exitMenuItem.setIcon(new ImageIcon(MesClient.class.getResource("/bg/logoff.png")));
  709. exitMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  710. fileMenu.add(exitMenuItem);
  711. exitMenuItem.addMouseListener(new MouseAdapter() {
  712. @Override
  713. public void mousePressed(MouseEvent e) {//只能检测到mousePressed事件
  714. super.mouseClicked(e);
  715. //dispose();
  716. logoff();
  717. }
  718. });
  719. JMenu settingMenu = new JMenu("设置");
  720. //settingMenu.setVisible(false);
  721. settingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
  722. settingMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  723. menuBar.add(settingMenu);
  724. JMenuItem resetTcpMenu = new JMenuItem("\u91CD\u8FDEMES");
  725. resetTcpMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  726. resetTcpMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  727. resetTcpMenu.addMouseListener(new MouseAdapter() {
  728. @Override
  729. public void mousePressed(MouseEvent e) {
  730. super.mouseClicked(e);
  731. }
  732. });
  733. // settingMenu.add(resetTcpMenu);
  734. JMenuItem resetTcpMenu_1 = new JMenuItem("刷新工件");
  735. resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  736. resetTcpMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  737. resetTcpMenu_1.addMouseListener(new MouseAdapter() {
  738. @Override
  739. public void mousePressed(MouseEvent e) {
  740. super.mouseClicked(e);
  741. resetScanA();
  742. }
  743. });
  744. settingMenu.add(resetTcpMenu_1);
  745. JMenuItem manualSubmitMenu = new JMenuItem("手动提交");
  746. manualSubmitMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
  747. manualSubmitMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  748. manualSubmitMenu.addMouseListener(new MouseAdapter() {
  749. @Override
  750. public void mousePressed(MouseEvent e) {
  751. super.mouseClicked(e);
  752. finish_ok_bt.setEnabled(true);
  753. finish_ng_bt.setEnabled(true);
  754. setMenuStatus("手动提交已开启,请选择OK或NG", 0);
  755. }
  756. });
  757. settingMenu.add(manualSubmitMenu);
  758. contentPane = new JPanel();
  759. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  760. setContentPane(contentPane);
  761. contentPane.setLayout(new BorderLayout(0, 0));
  762. JToolBar toolBar = new JToolBar();
  763. contentPane.add(toolBar, BorderLayout.NORTH);
  764. JLabel equipment_statu_label = new JLabel("状态:");
  765. equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER);
  766. equipment_statu_label.setForeground(Color.BLACK);
  767. equipment_statu_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  768. equipment_statu_label.setBackground(Color.LIGHT_GRAY);
  769. toolBar.add(equipment_statu_label);
  770. status_menu = new JButton("设备未连接MES服务器");
  771. status_menu.setText("已连接MES服务器");
  772. status_menu.setForeground(Color.GREEN);
  773. status_menu.addActionListener(new ActionListener() {
  774. public void actionPerformed(ActionEvent e) {
  775. }
  776. });
  777. status_menu.setForeground(Color.GREEN);
  778. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  779. status_menu.setBackground(Color.BLACK);
  780. toolBar.add(status_menu);
  781. JLabel space_1 = new JLabel(" ");
  782. toolBar.add(space_1);
  783. JLabel heart_beat_status_label = new JLabel("心跳:");
  784. heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  785. heart_beat_status_label.setForeground(Color.BLACK);
  786. heart_beat_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  787. heart_beat_status_label.setBackground(Color.LIGHT_GRAY);
  788. toolBar.add(heart_beat_status_label);
  789. heart_beat_menu = new JButton("2024-02-20 23:20:10");
  790. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  791. heart_beat_menu.setForeground(Color.GREEN);
  792. heart_beat_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  793. heart_beat_menu.setBackground(Color.BLACK);
  794. toolBar.add(heart_beat_menu);
  795. JLabel space_2 = new JLabel(" ");
  796. toolBar.add(space_2);
  797. JLabel user_status_label = new JLabel("登录用户:");
  798. user_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  799. user_status_label.setForeground(Color.BLACK);
  800. user_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  801. user_status_label.setBackground(Color.LIGHT_GRAY);
  802. toolBar.add(user_status_label);
  803. user_menu = new JButton("JinJuShi");
  804. user_menu.setForeground(Color.GREEN);
  805. user_menu.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  806. user_menu.setBackground(Color.BLACK);
  807. toolBar.add(user_menu);
  808. JLabel space_3 = new JLabel(" ");
  809. toolBar.add(space_3);
  810. JLabel space_4 = new JLabel(" ");
  811. toolBar.add(space_4);
  812. tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  813. tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
  814. tabbedPane.setMinimumSize(new Dimension(400, 50));
  815. tabbedPane.setFont(new Font("宋体", Font.BOLD, 22));
  816. contentPane.add(tabbedPane);
  817. //首页
  818. JPanel indexPanelA = new JPanel();
  819. indexScrollPaneA = new JScrollPane(indexPanelA);
  820. indexPanelA.setLayout(null);
  821. product_sn = new JTextField();
  822. product_sn.setHorizontalAlignment(SwingConstants.CENTER);
  823. product_sn.setEditable(false);
  824. product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  825. product_sn.setBounds(81, 70, 602, 70);
  826. indexPanelA.add(product_sn);
  827. product_sn.setColumns(10);
  828. f_scan_data_bt_1 = new JButton("扫码");
  829. f_scan_data_bt_1.addActionListener(new ActionListener() {
  830. public void actionPerformed(ActionEvent e) {
  831. scan_type = 1;
  832. scanBarcode();
  833. }
  834. });
  835. f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  836. f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  837. f_scan_data_bt_1.setBounds(693, 70, 198, 70);
  838. indexPanelA.add(f_scan_data_bt_1);
  839. // String[] hjtitles = new String[]{"焊机1","焊机2","焊机3"};
  840. // String[] hjvals = new String[]{"HJ001","HJ002","HJ003"};
  841. // mesRadioHj = new MesRadio(hjtitles,hjvals);
  842. // mesRadioHj.setSize(500,50);
  843. // mesRadioHj.setBounds(190,170,500,50);
  844. // indexPanelA.add(mesRadioHj);
  845. fxlabel = new JLabel("该工件为返修件,请仔细检查");
  846. fxlabel.setFont(new Font("微软雅黑", Font.PLAIN, 38));
  847. fxlabel.setBounds(81, 330, 810, 70);
  848. fxlabel.setForeground(Color.RED);
  849. fxlabel.setHorizontalAlignment(SwingConstants.CENTER);
  850. fxlabel.setVisible(false);
  851. indexPanelA.add(fxlabel);
  852. finish_ok_bt = new JButton("OK");
  853. finish_ok_bt.setEnabled(false);
  854. finish_ok_bt.addActionListener(new ActionListener() {
  855. public void actionPerformed(ActionEvent e) {
  856. submitQualityAndNotifyPlc("OK");
  857. }
  858. });
  859. finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
  860. finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  861. finish_ok_bt.setBounds(185, 420, 240, 80);
  862. finish_ok_bt.setEnabled(false);
  863. indexPanelA.add(finish_ok_bt);
  864. finish_ng_bt = new JButton("NG");
  865. finish_ng_bt.setEnabled(false);
  866. finish_ng_bt.addActionListener(new ActionListener() {
  867. public void actionPerformed(ActionEvent e) {
  868. String ngPoints = GaugeNgPointDialog.showDialog(MesClient.mesClientFrame);
  869. if (ngPoints == null || ngPoints.trim().isEmpty()) {
  870. return;
  871. }
  872. submitQualityAndNotifyPlc("NG", ngPoints.trim());
  873. }
  874. });
  875. finish_ng_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ng_bg.png")));
  876. finish_ng_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  877. finish_ng_bt.setBounds(508, 420, 240, 80);
  878. finish_ng_bt.setEnabled(false);
  879. indexPanelA.add(finish_ng_bt);
  880. tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null);
  881. tabbedPane.setEnabledAt(0, true);
  882. if (PRODUCT_TYPE_SWITCH_ENABLED) {
  883. JPanel indexPanelProductType = new JPanel();
  884. indexPanelProductType.setLayout(null);
  885. JScrollPane productTypeScrollPane = new JScrollPane(indexPanelProductType);
  886. JLabel productTypeTitle = new JLabel("生产类型设置");
  887. productTypeTitle.setFont(new Font("微软雅黑", Font.BOLD, 28));
  888. productTypeTitle.setForeground(new Color(64, 64, 64));
  889. productTypeTitle.setHorizontalAlignment(SwingConstants.CENTER);
  890. productTypeTitle.setBounds(81, 80, 810, 50);
  891. indexPanelProductType.add(productTypeTitle);
  892. JLabel productTypeHint = new JLabel("请选择当前生产的工件类型,切换后将保存到本地并用于扫码校验");
  893. productTypeHint.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  894. productTypeHint.setForeground(new Color(140, 140, 140));
  895. productTypeHint.setHorizontalAlignment(SwingConstants.CENTER);
  896. productTypeHint.setBounds(81, 140, 810, 40);
  897. indexPanelProductType.add(productTypeHint);
  898. productTypePanel = new ProductTypePanel(PRODUCT_TYPE_SWITCH_PASSWORD);
  899. productTypePanel.setBounds(276, 210, 420, 72);
  900. productTypePanel.setOnChangeListener(() -> {
  901. if (work_status == 0) {
  902. saveProductTypeToDb();
  903. if (PRODUCT_TYPE_SWITCH_ENABLED) {
  904. setMenuStatus("当前生产类型:" + getProductTypeName() + ",请扫工件码",0);
  905. } else {
  906. setMenuStatus("请扫工件码",0);
  907. }
  908. }
  909. refreshProductTypeCurrentLabel();
  910. });
  911. indexPanelProductType.add(productTypePanel);
  912. productTypeCurrentLabel = new JLabel();
  913. productTypeCurrentLabel.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  914. productTypeCurrentLabel.setForeground(new Color(64, 64, 64));
  915. productTypeCurrentLabel.setHorizontalAlignment(SwingConstants.CENTER);
  916. productTypeCurrentLabel.setBounds(81, 310, 810, 40);
  917. refreshProductTypeCurrentLabel();
  918. indexPanelProductType.add(productTypeCurrentLabel);
  919. tabbedPane.addTab("生产类型", new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")), productTypeScrollPane, null);
  920. }
  921. // searchScrollPane = new JScrollPane((Component) null);
  922. indexPanelC = new JPanel(new BorderLayout());
  923. tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexPanelC, null);
  924. // ========== 工作记录 Tab(改造:当前工位Tab + 所有工位Tab) ==========
  925. // 旧代码已注释:
  926. indexPanelB = new JPanel();
  927. searchScrollPane = new JScrollPane(indexPanelB);
  928. indexPanelB.setLayout(null);
  929. tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
  930. // 创建“软件设置”页面,并加入主Tab页签
  931. JPanel softwareSettingPanel = buildSoftwareSettingPanel();
  932. JScrollPane softwareSettingScrollPane = new JScrollPane(softwareSettingPanel);
  933. tabbedPane.addTab("软件设置", new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")), softwareSettingScrollPane, null);
  934. softwareSettingTabIndex = tabbedPane.getTabCount() - 1;
  935. // 页面初始化时先从本地配置加载软件设置
  936. loadSoftwareSettingsFromDb();
  937. // // 创建工作记录的子 TabbedPane
  938. // JTabbedPane workRecordTabbedPane = new JTabbedPane(JTabbedPane.TOP);
  939. // workRecordTabbedPane.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  940. //
  941. // // OKNG 工位:当前工位 + 所有工位
  942. // WorkRecordPanel[] workRecordPanels = new WorkRecordPanel[2];
  943. //
  944. // // 当前工位Tab(使用配置文件中的 mes.gw)
  945. // workRecordPanels[0] = new WorkRecordPanel(mes_gw, mes_line_sn);
  946. // workRecordTabbedPane.addTab(mes_gw, null, workRecordPanels[0], "查询" + mes_gw + "的工作记录");
  947. //
  948. // // 所有工位Tab(查询整条产线所有工位)
  949. // workRecordPanels[1] = new WorkRecordPanel(null, mes_line_sn);
  950. // workRecordTabbedPane.addTab("所有工位", null, workRecordPanels[1], "查询整条产线所有工位的工作记录");
  951. //
  952. // // 切换子Tab时自动刷新数据
  953. // workRecordTabbedPane.addChangeListener(e -> {
  954. // int selectedIndex = workRecordTabbedPane.getSelectedIndex();
  955. // if (selectedIndex >= 0 && selectedIndex < workRecordPanels.length) {
  956. // workRecordPanels[selectedIndex].refreshData();
  957. // }
  958. // });
  959. //
  960. // // 将子 TabbedPane 添加到主 TabbedPane
  961. // tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), workRecordTabbedPane, null);
  962. //
  963. tabbedPane.addChangeListener(new ChangeListener() {
  964. @Override
  965. public void stateChanged(ChangeEvent e) {
  966. JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
  967. int selectedIndex = tabbedPane.getSelectedIndex();
  968. System.out.println("selectedIndex:"+selectedIndex);
  969. if (softwareTabReverting) {
  970. return;
  971. }
  972. if (selectedIndex == softwareSettingTabIndex) {
  973. // 每次点击进入“软件设置”页签都进行密码验证(固定密码:mes)
  974. if (!verifySoftwareSettingPassword()) {
  975. softwareTabReverting = true;
  976. tabbedPane.setSelectedIndex(lastNonSoftwareTabIndex);
  977. softwareTabReverting = false;
  978. return;
  979. }
  980. loadSoftwareSettingsFromDb();
  981. } else {
  982. // 只记录非“软件设置”页签,便于校验失败时回退
  983. lastNonSoftwareTabIndex = selectedIndex;
  984. }
  985. }
  986. });
  987. }
  988. // 软件设置页面UI构建(含编辑/保存/重载按钮)
  989. private JPanel buildSoftwareSettingPanel() {
  990. JPanel panel = new JPanel();
  991. panel.setLayout(null);
  992. JLabel title = new JLabel("软件设置");
  993. title.setFont(new Font("微软雅黑", Font.BOLD, 26));
  994. title.setBounds(40, 30, 300, 40);
  995. panel.add(title);
  996. JLabel ipLabel = new JLabel("服务器IP/域名:");
  997. ipLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  998. ipLabel.setBounds(40, 100, 200, 30);
  999. panel.add(ipLabel);
  1000. softwareIpField = new JTextField("");
  1001. softwareIpField.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  1002. softwareIpField.setBounds(220, 100, 360, 34);
  1003. softwareIpField.setEditable(false);
  1004. panel.add(softwareIpField);
  1005. JLabel opLabel = new JLabel("工位号:");
  1006. opLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  1007. opLabel.setBounds(40, 150, 200, 30);
  1008. panel.add(opLabel);
  1009. softwareOpField = new JTextField("");
  1010. softwareOpField.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  1011. softwareOpField.setBounds(220, 150, 360, 34);
  1012. softwareOpField.setEditable(false);
  1013. panel.add(softwareOpField);
  1014. JLabel nameLabel = new JLabel("工位名称:");
  1015. nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  1016. nameLabel.setBounds(40, 200, 200, 30);
  1017. panel.add(nameLabel);
  1018. softwareOpNameField = new JTextField("");
  1019. softwareOpNameField.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  1020. softwareOpNameField.setBounds(220, 200, 360, 34);
  1021. softwareOpNameField.setEditable(false);
  1022. panel.add(softwareOpNameField);
  1023. softwareEditBtn = new JButton("编辑");
  1024. softwareEditBtn.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  1025. softwareEditBtn.setBounds(220, 270, 140, 44);
  1026. softwareEditBtn.addActionListener(new ActionListener() {
  1027. @Override
  1028. public void actionPerformed(ActionEvent e) {
  1029. // 点击“编辑”后允许修改输入框,并开放保存按钮
  1030. softwareIpField.setEditable(true);
  1031. softwareOpField.setEditable(true);
  1032. softwareOpNameField.setEditable(true);
  1033. if (softwareSaveBtn != null) {
  1034. softwareSaveBtn.setEnabled(true);
  1035. }
  1036. }
  1037. });
  1038. panel.add(softwareEditBtn);
  1039. softwareSaveBtn = new JButton("保存");
  1040. softwareSaveBtn.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  1041. softwareSaveBtn.setBounds(380, 270, 140, 44);
  1042. softwareSaveBtn.setEnabled(false);
  1043. softwareSaveBtn.addActionListener(new ActionListener() {
  1044. @Override
  1045. public void actionPerformed(ActionEvent e) {
  1046. // 保存前读取并校验输入内容
  1047. String ip = softwareIpField.getText() == null ? "" : softwareIpField.getText().trim();
  1048. String op = softwareOpField.getText() == null ? "" : softwareOpField.getText().trim();
  1049. String opName = softwareOpNameField.getText() == null ? "" : softwareOpNameField.getText().trim();
  1050. if (ip.isEmpty() || op.isEmpty()) {
  1051. JOptionPane.showMessageDialog(mesClientFrame, "服务器IP和工位号不能为空", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
  1052. return;
  1053. }
  1054. if (opName.isEmpty()) {
  1055. opName = OprnoUtil.getGwDes(mes_line_sn, op);
  1056. }
  1057. if (!testMesServer(ip)) {
  1058. JOptionPane.showMessageDialog(mesClientFrame, "无法连接到MES服务器:" + ip + ":8980", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
  1059. return;
  1060. }
  1061. // 写入本地配置并刷新显示
  1062. applyOpConfig(op, opName, ip);
  1063. loadSoftwareSettingsFromDb();
  1064. softwareIpField.setEditable(false);
  1065. softwareOpField.setEditable(false);
  1066. softwareOpNameField.setEditable(false);
  1067. softwareSaveBtn.setEnabled(false);
  1068. JOptionPane.showMessageDialog(mesClientFrame, "保存成功,请重启软件", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
  1069. }
  1070. });
  1071. panel.add(softwareSaveBtn);
  1072. JButton reloadBtn = new JButton("从本地配置读取");
  1073. reloadBtn.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  1074. reloadBtn.setBounds(540, 270, 200, 44);
  1075. reloadBtn.addActionListener(new ActionListener() {
  1076. @Override
  1077. public void actionPerformed(ActionEvent e) {
  1078. // 从本地数据库读取配置并回填页面
  1079. loadSoftwareSettingsFromDb();
  1080. }
  1081. });
  1082. panel.add(reloadBtn);
  1083. panel.setPreferredSize(new Dimension(990, 550));
  1084. return panel;
  1085. }
  1086. // 读取并回填“软件设置”页面数据
  1087. private void loadSoftwareSettingsFromDb() {
  1088. String[] cfg = JdbcUtils.getAnyOpIpConfig();
  1089. if (cfg != null && cfg.length >= 3) {
  1090. applyOpConfig(cfg[0], cfg[1], cfg[2]);
  1091. softwareOpField.setText(cfg[0]);
  1092. softwareOpNameField.setText(cfg[1]);
  1093. softwareIpField.setText(cfg[2]);
  1094. } else {
  1095. softwareOpField.setText(mes_gw == null ? "" : mes_gw);
  1096. softwareOpNameField.setText(mes_gw_des == null ? "" : mes_gw_des);
  1097. softwareIpField.setText(mes_server_ip == null ? "" : mes_server_ip);
  1098. }
  1099. if (softwareSaveBtn != null) {
  1100. softwareSaveBtn.setEnabled(false);
  1101. }
  1102. if (softwareIpField != null) {
  1103. softwareIpField.setEditable(false);
  1104. }
  1105. if (softwareOpField != null) {
  1106. softwareOpField.setEditable(false);
  1107. }
  1108. if (softwareOpNameField != null) {
  1109. softwareOpNameField.setEditable(false);
  1110. }
  1111. }
  1112. // 进入“软件设置”页签前的密码校验(固定密码:mes)
  1113. private boolean verifySoftwareSettingPassword() {
  1114. String input = JOptionPane.showInputDialog(this, "请输入软件设置密码:", "软件设置权限验证", JOptionPane.QUESTION_MESSAGE);
  1115. if (input == null) {
  1116. return false;
  1117. }
  1118. if ("mes".equals(input.trim())) {
  1119. return true;
  1120. }
  1121. JOptionPane.showMessageDialog(this, "密码错误,无法进入软件设置页面", "提示窗口", JOptionPane.WARNING_MESSAGE);
  1122. return false;
  1123. }
  1124. public static void setMenuStatus(String msg,int error){
  1125. if(error == 0){
  1126. MesClient.status_menu.setForeground(Color.GREEN);
  1127. }else{
  1128. MesClient.status_menu.setForeground(Color.RED);
  1129. }
  1130. MesClient.status_menu.setText(msg);
  1131. }
  1132. public static void getMaterailData(){
  1133. try{
  1134. JSONObject retObj = DataUtil.getBindMaterail();
  1135. if(retObj == null){
  1136. return;
  1137. }
  1138. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  1139. java.util.List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  1140. int i = 0;
  1141. rowData = new Object[arrs.size()][7];
  1142. for (BindMaterialResp bindMaterialResp:arrs){
  1143. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  1144. rowData[i][1] = bindMaterialResp.getBatchSn();
  1145. rowData[i][2] = bindMaterialResp.getLastTimes();
  1146. rowData[i][3] = "";
  1147. rowData[i][4] = bindMaterialResp.getCraft();
  1148. rowData[i][5] = bindMaterialResp.getMaterialId();
  1149. rowData[i][6] = bindMaterialResp.getType();
  1150. i++;
  1151. }
  1152. bindBatchPanel();
  1153. }
  1154. }catch (Exception e){
  1155. log.info(e.getMessage());
  1156. }
  1157. }
  1158. public static void updateMaterailData(){
  1159. try{
  1160. JSONObject retObj = DataUtil.getBindMaterail();
  1161. if(retObj == null){
  1162. return;
  1163. }
  1164. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  1165. List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  1166. int i = 0;
  1167. for (BindMaterialResp bindMaterialResp:arrs){
  1168. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  1169. rowData[i][1] = bindMaterialResp.getBatchSn();
  1170. rowData[i][2] = bindMaterialResp.getLastTimes();
  1171. rowData[i][3] = "";
  1172. rowData[i][4] = bindMaterialResp.getCraft();
  1173. rowData[i][5] = bindMaterialResp.getMaterialId();
  1174. rowData[i][6] = bindMaterialResp.getType();
  1175. i++;
  1176. }
  1177. MesClient.table.repaint();
  1178. }
  1179. }catch (Exception e){
  1180. log.info(e.getMessage());
  1181. }
  1182. }
  1183. // 绑定物料批次码
  1184. public static void scanBatchSn(BindMaterialResp bindMaterialResp) {
  1185. //弹窗扫工件码
  1186. String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle();
  1187. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  1188. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  1189. JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType());
  1190. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  1191. MesClient.setMenuStatus("扫物料:"+bindMaterialResp.getMaterialTitle()+"成功",0);
  1192. updateMaterailData();
  1193. }else{
  1194. if(retObj.get("result")==null){
  1195. MesClient.setMenuStatus("请求失败,请重试",-1);
  1196. }else{
  1197. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  1198. MesClient.setMenuStatus(retObj.getString("message"),-1);
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. public static void bindBatchPanel(){
  1205. JPanel indexPanelBB = new JPanel();
  1206. JPanel panel = new JPanel();
  1207. panel.setBounds(0, 0, 990, 550);
  1208. // panel.setBounds(81, 50, 810, 479);
  1209. indexPanelBB.add(panel);
  1210. panel.setLayout(new GridLayout(0, 1, 0, 0));
  1211. table = new JTable(rowData, columnNames){
  1212. public boolean isCellEditable(int row, int column) {
  1213. if(column == 3){
  1214. return true;
  1215. }
  1216. return false;
  1217. }
  1218. };
  1219. table.setRowHeight(40);
  1220. table.setEnabled(true);
  1221. table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  1222. table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton());
  1223. table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton());
  1224. JScrollPane scrollPane = new JScrollPane(table);
  1225. panel.add(scrollPane);
  1226. JScrollPane indexScrollPaneB = new JScrollPane(indexPanelBB);
  1227. indexPanelBB.setLayout(null);
  1228. tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null);
  1229. }
  1230. // 开启发送结果定时任务
  1231. public static void startUpdateQualityTimer(){
  1232. // 创建异步线程池
  1233. ExecutorService executorService = Executors.newSingleThreadExecutor();
  1234. // 提交任务
  1235. executorService.submit(() -> {
  1236. log.info("异步任务执行中...");
  1237. // 模拟耗时操作
  1238. try {
  1239. while (true){
  1240. try {
  1241. // 确保连接已经打开
  1242. if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
  1243. JdbcUtils.openConnection();
  1244. }
  1245. Statement statement = JdbcUtils.conn.createStatement();
  1246. String selectSql = "SELECT id, bw FROM submit_record\n" +
  1247. "WHERE state = 0 ORDER BY id DESC LIMIT 10";
  1248. String updateSql = "UPDATE submit_record \n" +
  1249. "SET state = '1'\n" +
  1250. "WHERE id = ";
  1251. ResultSet rs = statement.executeQuery(selectSql);
  1252. while(rs.next()){
  1253. int id = rs.getInt("id");
  1254. String bw = rs.getString("bw");
  1255. // 发送请求
  1256. String url = "http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesProductRecord/updateQualityByTiming";
  1257. log.info("质量:"+bw);
  1258. String s = HttpUtils.sendPostRequestJson(url, bw );
  1259. log.info("结果:"+s);
  1260. Boolean result = JSONObject.parseObject(s).getBoolean("result");
  1261. if(result) {
  1262. // 更改状态为 1
  1263. statement.executeUpdate(updateSql + id);
  1264. }
  1265. }
  1266. rs.close();
  1267. statement.close();
  1268. } catch (Exception e) {
  1269. log.info(e.getMessage());
  1270. }
  1271. try {
  1272. Thread.sleep(2000);
  1273. }catch (Exception e){
  1274. log.info(e.getMessage());
  1275. }
  1276. }
  1277. } catch (Exception e) {
  1278. log.info(e.getMessage());
  1279. }
  1280. log.info("异步任务执行完毕");
  1281. });
  1282. }
  1283. }