MesClient.java 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.fazecast.jSerialComm.SerialPort;
  4. import com.fazecast.jSerialComm.SerialPortEvent;
  5. import com.mes.component.MesRadio;
  6. import com.mes.component.MesWebView;
  7. import com.mes.laser.LaserFlowManager;
  8. import com.mes.netty.NettyClient;
  9. import com.mes.util.DateLocalUtils;
  10. import com.mes.util.JdbcUtils;
  11. import javafx.embed.swing.JFXPanel;
  12. import javax.swing.*;
  13. import javax.swing.border.EmptyBorder;
  14. import javax.swing.event.ChangeEvent;
  15. import javax.swing.event.ChangeListener;
  16. import javax.swing.event.DocumentEvent;
  17. import javax.swing.event.DocumentListener;
  18. import java.awt.*;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ActionListener;
  21. import java.awt.event.ComponentAdapter;
  22. import java.awt.event.ComponentEvent;
  23. import java.awt.event.MouseAdapter;
  24. import java.awt.event.MouseEvent;
  25. import java.awt.event.WindowAdapter;
  26. import java.awt.event.WindowEvent;
  27. import java.io.BufferedReader;
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.io.InputStreamReader;
  31. import java.time.LocalDateTime;
  32. import java.util.List;
  33. import java.util.Properties;
  34. import java.util.Timer;
  35. import java.util.TimerTask;
  36. import java.util.concurrent.ExecutorService;
  37. import java.util.concurrent.Executors;
  38. public class MesClient extends JFrame {
  39. private static String Drivde = "org.sqlite.JDBC";
  40. public static int mes_auth = 0; // 权限级别 0=无权限 1=操作工人 2=管理员
  41. public static String mes_gw = ""; // 工位号
  42. public static String mes_gw_des = ""; // 工位名称
  43. public static String mes_server_ip = ""; // 服务器IP地址
  44. public static int mes_tcp_port = 3000; // TCP服务端口
  45. public static int mes_heart_beat_cycle = 10; // 心跳周期
  46. public static int mes_heart_icon_cycle = 1;
  47. public static String mes_line_sn = ""; // 产线编号
  48. public static String ui_design_password = "mes123"; // UI面板密码
  49. //TCP连接
  50. public static NettyClient nettyClient;
  51. //TCP连接状态
  52. public static boolean tcp_connect_flag = false;
  53. //TCP连接请求
  54. public static boolean connect_request_flag = false;
  55. //session
  56. public static String sessionid = "";
  57. public static JPanel contentPane;
  58. public static MesClient mesClientFrame;
  59. public static JTabbedPane tabbedPane;
  60. public static JScrollPane indexScrollPaneA;
  61. public static JScrollPane searchScrollPane;
  62. public static JScrollPane searchScrollPaneDj;
  63. public static Boolean check_quality_result = false;
  64. public static Integer work_status = 0;
  65. public static JButton heart_beat_menu;
  66. public static JButton status_menu;
  67. public static JButton user_menu;
  68. public static int scan_type = 0;
  69. public static JButton finish_ok_bt;
  70. public static JButton finish_ng_bt;
  71. public static JTextField product_sn;
  72. public static JButton f_scan_data_bt_1;
  73. public static JTextField batch_sn; // 物料批次码
  74. public static JButton batch_sn_btn;
  75. public static String user20 = "";
  76. public static JFrame welcomeWin;
  77. public static JPanel indexPanelB;
  78. public static MesWebView jfxPanel = null;
  79. public static JPanel indexPanelC;
  80. public static MesWebView jfxPanel2 = null;
  81. public static MesRadio mesRadioHj;
  82. public static JTable table;
  83. public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" };
  84. public static Object[][] rowData = null;
  85. public static Boolean checkState = false; // 点检状态 false=未点检 true=已点检
  86. public static SerialPort serialPort; // 串口对象
  87. // 镭雕相关
  88. public static JButton laser_get_code_bt; // 获取镭雕码按钮
  89. public static JButton laser_reset_bt; // 人工复位按钮
  90. public static LaserFlowManager laserFlowManager;
  91. public static JTextField manualSteelSnInput;
  92. public static JButton manualGetCodeBt;
  93. public static JButton manualFinishOkBt;
  94. public static JButton manualUnbindBt;
  95. private static boolean manualWaitingComplete = false;
  96. private static boolean manualCompleting = false;
  97. private static final ExecutorService laserActionExecutor = Executors.newSingleThreadExecutor();
  98. public static void main(String[] args) {
  99. if (LockUtil.getInstance().isAppActive() == true){
  100. // JOptionPane.showMessageDialog(null, "已有一个程序在运行,程序退出");
  101. return;
  102. }else{
  103. // 设置支持高DPI
  104. System.setProperty("sun.java2d.uiScale", "1.0");
  105. System.setProperty("sun.java2d.dpiaware", "true");
  106. EventQueue.invokeLater(new Runnable() {
  107. @Override
  108. public void run() {
  109. try{
  110. //读文件配置
  111. readProperty();
  112. // 显示界面
  113. mesClientFrame = new MesClient();
  114. mesClientFrame.setVisible(false);
  115. JdbcUtils.getConn();
  116. welcomeWin = new LoginFarme();
  117. welcomeWin.setVisible(true);
  118. getMaterailData();
  119. serialPort = ModbusRtu.connect();
  120. // checkTimer()已移除:原用于每3秒驱动继电器开关(formatDeviceState),
  121. // 继电器功能已不再使用,保留会导致无意义的轮询打印
  122. // 启动镭雕流程管理器(含本地HTTP服务,接收镭雕软件完成上报)
  123. initLaserFlow();
  124. }catch (Exception e){
  125. e.printStackTrace();
  126. }
  127. }
  128. });
  129. }
  130. }
  131. public static java.util.Timer checkTimer;
  132. public static void checkTimer() {
  133. if(checkTimer!=null) {
  134. checkTimer.cancel();
  135. }
  136. checkTimer = new java.util.Timer();
  137. checkTimer.schedule(new TimerTask() {
  138. public void run() {
  139. if(!MesClient.sessionid.isEmpty()){
  140. getCheckResult();
  141. }
  142. }
  143. }, 100,3*1000);
  144. }
  145. //读配置文件
  146. private static void readProperty() throws IOException{
  147. String enconding = "UTF-8";
  148. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  149. Properties pro = new Properties();
  150. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  151. pro.load(br);
  152. mes_gw = pro.getProperty("mes.gw");
  153. // mes_gw_des = pro.getProperty("mes.gw_des");
  154. mes_server_ip = pro.getProperty("mes.server_ip");
  155. mes_tcp_port = Integer.parseInt(pro.getProperty("mes.tcp_port"));
  156. mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle"));
  157. mes_line_sn = pro.getProperty("mes.line_sn");
  158. ui_design_password = pro.getProperty("mes.ui.design.password", "123456").trim();
  159. mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
  160. System.out.println(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle);
  161. }
  162. // 初始化TCP
  163. public static void initTcpConnection() {
  164. try {
  165. if(nettyClient==null) {
  166. //初始化TCP连接
  167. nettyClient = new NettyClient();
  168. //TCP连接状态
  169. tcp_connect_flag = false;
  170. //设置TCP请求状态
  171. connect_request_flag = true;
  172. DataUtil.synrTcp(nettyClient,mes_gw);
  173. }
  174. } catch (Exception e) {
  175. // TODO Auto-generated catch block
  176. e.printStackTrace();
  177. }
  178. }
  179. //启动心跳包程序
  180. public static java.util.Timer heartBeatTimer;
  181. public static java.util.Timer heartBeatIconTimer;
  182. public static boolean iconREDFlag = true;
  183. public static void startHeartBeatTimer() {
  184. if(heartBeatTimer!=null) {
  185. heartBeatTimer.cancel();
  186. }
  187. heartBeatTimer = new java.util.Timer();
  188. heartBeatTimer.schedule(new TimerTask() {
  189. public void run() {
  190. if(nettyClient!=null&&tcp_connect_flag) {
  191. //System.out.println("发送心跳报文");
  192. DataUtil.heartBeat(nettyClient,mes_gw);
  193. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  194. }
  195. }
  196. }, 100,mes_heart_beat_cycle*1000);
  197. //心跳显示图标
  198. if(heartBeatIconTimer!=null) {
  199. heartBeatIconTimer.cancel();
  200. }
  201. heartBeatIconTimer = new Timer();
  202. heartBeatIconTimer.schedule(new TimerTask() {
  203. public void run() {
  204. if(tcp_connect_flag) {
  205. if(iconREDFlag) {
  206. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  207. iconREDFlag = false;
  208. }else {
  209. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  210. iconREDFlag = true;
  211. }
  212. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  213. heart_beat_menu.repaint();
  214. }else {
  215. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  216. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  217. heart_beat_menu.repaint();
  218. //若未连接则尝试连接
  219. if(nettyClient!=null&&!tcp_connect_flag){
  220. System.out.println("TCP已断开");
  221. //TCP重新同步连接
  222. if(!connect_request_flag) {
  223. //System.out.println("TCP重新同步连接");
  224. //设置TCP请求状态,只重新同步连接一次
  225. connect_request_flag = true;
  226. DataUtil.synrTcp(nettyClient,mes_gw);
  227. }
  228. }
  229. }
  230. }
  231. }, 100,mes_heart_icon_cycle*1000);
  232. }
  233. //设置tcp连接状态显示
  234. public static void setTcpStatus() {
  235. if(tcp_connect_flag) {
  236. status_menu.setText("已连接MES服务器");
  237. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  238. heart_beat_menu.repaint();
  239. }else {
  240. status_menu.setText("未连接MES服务器");
  241. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  242. heart_beat_menu.repaint();
  243. }
  244. }
  245. public static void initWarehouseData(){
  246. resetScanA();
  247. }
  248. public static void resetScanA() {
  249. work_status = 0;
  250. check_quality_result = false;
  251. MesClient.finish_ok_bt.setEnabled(false);
  252. MesClient.finish_ng_bt.setEnabled(false);
  253. product_sn.setText("");
  254. batch_sn.setText("");
  255. // MesClient.f_scan_data_bt_1.setEnabled(true);
  256. MesClient.setMenuStatus("请扫工件码",0);
  257. updateMaterailData();
  258. // getCheckResult()调用已移除:原用于驱动继电器(formatDeviceState),继电器功能已不再使用
  259. // 注释掉换班检查,避免自动退出
  260. // shiftUserCheck();
  261. }
  262. public static int userLoginHours;//用户登录所处小时
  263. //换班用户信息检查(已注释,避免早上8点自动关闭)
  264. /*
  265. private static void shiftUserCheck() {
  266. LocalDateTime now = LocalDateTime.now();
  267. // 判断时间范围
  268. if (userLoginHours >= 8 && userLoginHours < 20) {
  269. int hour = now.getHour();
  270. if (hour >= 20 || hour < 8) {
  271. logoff();
  272. }
  273. } else {
  274. int hour = now.getHour();
  275. if (hour >= 8 && hour < 20) {
  276. logoff();
  277. }
  278. }
  279. }
  280. */
  281. public static void resetScanB() {
  282. }
  283. //获取用户20位
  284. public static void getUser() {
  285. user20 = user_menu.getText().toString();
  286. String space_tmp1 = "";
  287. if(user20.length()<20) {
  288. for(int i=0;i<20-user20.length();i++) {
  289. space_tmp1 = space_tmp1 + " ";
  290. }
  291. }
  292. user20 = user20 + space_tmp1;
  293. }
  294. //获取barcode内容36位
  295. public static String getBarcode(String barcodeTmp) {
  296. String barcodeRet = barcodeTmp;
  297. if(barcodeTmp.equalsIgnoreCase("")) {
  298. return "";
  299. }else {
  300. if(barcodeTmp.length()<36) {
  301. String space = "";
  302. for(int i=0;i<36-barcodeTmp.length();i++) {
  303. space = space + " ";
  304. }
  305. barcodeRet = barcodeTmp + space;
  306. }
  307. }
  308. return barcodeRet;
  309. }
  310. public static void setMenuStatus(String msg,int error){
  311. if(error == 0){
  312. MesClient.status_menu.setForeground(Color.GREEN);
  313. }else{
  314. MesClient.status_menu.setForeground(Color.RED);
  315. }
  316. MesClient.status_menu.setText(msg);
  317. }
  318. private static boolean isManualSteelSnReady() {
  319. return manualSteelSnInput != null && manualSteelSnInput.getText().trim().length() == 11;
  320. }
  321. private static void updateManualSubmitButtons() {
  322. boolean ready = isManualSteelSnReady();
  323. if (manualGetCodeBt != null) {
  324. manualGetCodeBt.setEnabled(!manualWaitingComplete && ready);
  325. }
  326. if (manualFinishOkBt != null) {
  327. manualFinishOkBt.setEnabled(manualWaitingComplete && !manualCompleting);
  328. }
  329. if (manualUnbindBt != null) {
  330. manualUnbindBt.setEnabled(!manualWaitingComplete && !manualCompleting && ready);
  331. }
  332. }
  333. private static void resetManualSubmitPanel(boolean clearInput) {
  334. manualWaitingComplete = false;
  335. manualCompleting = false;
  336. if (clearInput && manualSteelSnInput != null) {
  337. manualSteelSnInput.setText("");
  338. }
  339. updateManualSubmitButtons();
  340. }
  341. private static void requestUnbindSteelSn(String steelSn) {
  342. requestUnbindSteelSn(steelSn, manualSteelSnInput);
  343. }
  344. private static void requestUnbindSteelSn(String steelSn, final JTextField steelSnInputToClear) {
  345. if (steelSn == null || steelSn.trim().length() != 11) {
  346. setMenuStatus("请输入11位钢印码", 1);
  347. updateManualSubmitButtons();
  348. return;
  349. }
  350. if (laserFlowManager != null && laserFlowManager.isWaitingComplete()) {
  351. setMenuStatus("镭雕进行中,不能解绑", 1);
  352. return;
  353. }
  354. manualCompleting = true;
  355. updateManualSubmitButtons();
  356. final String steelSnTrim = steelSn.trim();
  357. laserActionExecutor.submit(new Runnable() {
  358. @Override
  359. public void run() {
  360. JSONObject retObj = DataUtil.unbindSteelSn(steelSnTrim);
  361. SwingUtilities.invokeLater(new Runnable() {
  362. @Override
  363. public void run() {
  364. manualCompleting = false;
  365. if (retObj != null && retObj.get("result") != null
  366. && "true".equalsIgnoreCase(retObj.get("result").toString())) {
  367. setMenuStatus(retObj.getString("message"), 0);
  368. if (steelSnInputToClear != null) {
  369. steelSnInputToClear.setText("");
  370. }
  371. product_sn.setText("");
  372. } else {
  373. String msg = retObj != null && retObj.get("message") != null
  374. ? retObj.get("message").toString() : "解绑失败,请重试";
  375. setMenuStatus(msg, 1);
  376. }
  377. updateManualSubmitButtons();
  378. }
  379. });
  380. }
  381. });
  382. }
  383. public static void scanBarcode() {
  384. String scanBarcodeTitle = "";
  385. switch(scan_type) {
  386. case 1:
  387. product_sn.setText("");
  388. scanBarcodeTitle = "请扫工件码";
  389. break;
  390. case 2:
  391. batch_sn.setText("");
  392. scanBarcodeTitle = "请扫物料批次码";
  393. break;
  394. }
  395. //弹窗扫工件码
  396. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  397. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  398. //获取用户
  399. getUser();
  400. //获取扫码内容36位
  401. String barcode36 = getBarcode(scanBarcode);//处理36为码
  402. //工位号
  403. String gw = "";
  404. switch(scan_type) {
  405. case 1:
  406. product_sn.setText(scanBarcode);
  407. break;
  408. case 2:
  409. batch_sn.setText(scanBarcode);
  410. break;
  411. }
  412. //刷新界面
  413. mesClientFrame.repaint();
  414. if(!tcp_connect_flag) {
  415. MesClient.setMenuStatus("设备未连接Mes服务器",1);
  416. // JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  417. return;
  418. }
  419. // if(!product_sn.getText().isEmpty() && !batch_sn.getText().isEmpty()){
  420. // finish_ok_bt.setEnabled(true);
  421. // finish_ng_bt.setEnabled(true);
  422. // }
  423. if(!product_sn.getText().isEmpty()){
  424. // 扫码后自动提交OK
  425. String sn = getBarcode(product_sn.getText());
  426. if(!sn.isEmpty()){
  427. String qret = "OK";
  428. Boolean sendret = DataUtil.sendCreate(nettyClient,sn,qret,user20);
  429. if(!sendret){
  430. MesClient.setMenuStatus("消息发送失败,请重试",1);
  431. return;
  432. }
  433. // 提交成功后,弹出新的扫码框
  434. SwingUtilities.invokeLater(new Runnable() {
  435. @Override
  436. public void run() {
  437. scan_type = 1;
  438. scanBarcode();
  439. }
  440. });
  441. }
  442. }
  443. // 查询工件质量
  444. // Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20);
  445. // if(!sendret){
  446. // JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  447. // return;
  448. // }
  449. }else {
  450. JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  451. return;
  452. }
  453. }
  454. // 复用op040既有建档提交逻辑(原finish_ok_bt/finish_ng_bt里的sendCreate调用抽出来)
  455. // sn: 用于提交的编码(客户编码),qret: OK/NG
  456. public static boolean submitLaserResult(String sn, String qret){
  457. if(sn == null || sn.isEmpty()){
  458. setMenuStatus("客户编码为空,无法提交",1);
  459. return false;
  460. }
  461. getUser();
  462. String snPad = getBarcode(sn);
  463. Boolean sendret = DataUtil.sendCreate(nettyClient, snPad, qret, user20);
  464. if(!sendret){
  465. setMenuStatus("结果提交MES失败,请重试",1);
  466. return false;
  467. }
  468. return true;
  469. }
  470. // 初始化镭雕流程管理器
  471. public static void initLaserFlow(){
  472. laserFlowManager = new LaserFlowManager(new LaserFlowManager.UiCallback() {
  473. @Override
  474. public void onStatus(String msg, boolean error) {
  475. SwingUtilities.invokeLater(() -> setMenuStatus(msg, error ? 1 : 0));
  476. }
  477. @Override
  478. public void onLockButton(boolean lock) {
  479. SwingUtilities.invokeLater(() -> {
  480. if(laser_get_code_bt != null){
  481. laser_get_code_bt.setEnabled(!lock);
  482. }
  483. if (!lock && manualWaitingComplete) {
  484. resetManualSubmitPanel(true);
  485. }
  486. });
  487. }
  488. @Override
  489. public void onCodeReceived(String code) {
  490. SwingUtilities.invokeLater(() -> product_sn.setText(code));
  491. }
  492. @Override
  493. public boolean onSubmitResult(String customerSn, String qret) {
  494. return submitLaserResult(customerSn, qret);
  495. }
  496. });
  497. laserFlowManager.startCompleteServer();
  498. }
  499. public static void logoff() {
  500. // welcomeWin.setVisible(true);
  501. mesClientFrame.setVisible(false);
  502. if(laserFlowManager != null){
  503. laserFlowManager.stopCompleteServer();
  504. }
  505. nettyClient = null;
  506. // if(heartBeatTimer!=null) {
  507. // heartBeatTimer.cancel();
  508. // }
  509. // if(heartBeatIconTimer!=null) {
  510. // heartBeatIconTimer.cancel();
  511. // }
  512. tcp_connect_flag = false;
  513. connect_request_flag = false;
  514. }
  515. public static boolean confirmClose(Component parent) {
  516. return JOptionPane.showConfirmDialog(parent, "确定要关闭MES系统客户端吗?", "确认关闭",
  517. JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION;
  518. }
  519. public MesClient() {
  520. setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png")));
  521. setTitle("MES系统客户端:"+mes_gw + "- " + mes_gw_des);
  522. setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  523. addWindowListener(new WindowAdapter() {
  524. @Override
  525. public void windowClosing(WindowEvent e) {
  526. if (confirmClose(MesClient.this)) {
  527. System.exit(0);
  528. }
  529. }
  530. });
  531. setBounds(0, 0, 1024, 768);
  532. // 确保标题栏正确显示
  533. validate();
  534. JMenuBar menuBar = new JMenuBar();
  535. menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26));
  536. setJMenuBar(menuBar);
  537. JMenu fileMenu = new JMenu("用户");
  538. fileMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png")));
  539. fileMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  540. menuBar.add(fileMenu);
  541. JMenuItem exitMenuItem = new JMenuItem("退出");
  542. exitMenuItem.setIcon(new ImageIcon(MesClient.class.getResource("/bg/logoff.png")));
  543. exitMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  544. fileMenu.add(exitMenuItem);
  545. exitMenuItem.addMouseListener(new MouseAdapter() {
  546. @Override
  547. public void mousePressed(MouseEvent e) {//只能检测到mousePressed事件
  548. super.mouseClicked(e);
  549. //dispose();
  550. logoff();
  551. }
  552. });
  553. JMenu settingMenu = new JMenu("设置");
  554. //settingMenu.setVisible(false);
  555. settingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
  556. settingMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  557. menuBar.add(settingMenu);
  558. JMenuItem resetTcpMenu = new JMenuItem("\u91CD\u8FDEMES");
  559. resetTcpMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  560. resetTcpMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  561. resetTcpMenu.addMouseListener(new MouseAdapter() {
  562. @Override
  563. public void mousePressed(MouseEvent e) {
  564. super.mouseClicked(e);
  565. //重连mes
  566. nettyClient.future.channel().close();
  567. }
  568. });
  569. settingMenu.add(resetTcpMenu);
  570. JMenuItem resetTcpMenu_1 = new JMenuItem("刷新工件");
  571. resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  572. resetTcpMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  573. resetTcpMenu_1.addMouseListener(new MouseAdapter() {
  574. @Override
  575. public void mousePressed(MouseEvent e) {
  576. super.mouseClicked(e);
  577. resetScanA();
  578. }
  579. });
  580. settingMenu.add(resetTcpMenu_1);
  581. contentPane = new JPanel();
  582. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  583. setContentPane(contentPane);
  584. contentPane.setLayout(new BorderLayout(0, 0));
  585. JToolBar toolBar = new JToolBar();
  586. contentPane.add(toolBar, BorderLayout.NORTH);
  587. JLabel equipment_statu_label = new JLabel("状态:");
  588. equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER);
  589. equipment_statu_label.setForeground(Color.BLACK);
  590. equipment_statu_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  591. equipment_statu_label.setBackground(Color.LIGHT_GRAY);
  592. toolBar.add(equipment_statu_label);
  593. status_menu = new JButton("设备未连接MES服务器");
  594. if(tcp_connect_flag) {
  595. status_menu.setText("已连接MES服务器");
  596. status_menu.setForeground(Color.GREEN);
  597. }else {
  598. status_menu.setText("未连接MES服务器");
  599. status_menu.setForeground(Color.DARK_GRAY);
  600. }
  601. status_menu.addActionListener(new ActionListener() {
  602. public void actionPerformed(ActionEvent e) {
  603. }
  604. });
  605. status_menu.setForeground(Color.GREEN);
  606. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  607. status_menu.setBackground(Color.BLACK);
  608. toolBar.add(status_menu);
  609. JLabel space_1 = new JLabel(" ");
  610. toolBar.add(space_1);
  611. JLabel heart_beat_status_label = new JLabel("心跳:");
  612. heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  613. heart_beat_status_label.setForeground(Color.BLACK);
  614. heart_beat_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  615. heart_beat_status_label.setBackground(Color.LIGHT_GRAY);
  616. toolBar.add(heart_beat_status_label);
  617. heart_beat_menu = new JButton("2024-02-20 23:20:10");
  618. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  619. heart_beat_menu.setForeground(Color.GREEN);
  620. heart_beat_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  621. heart_beat_menu.setBackground(Color.BLACK);
  622. toolBar.add(heart_beat_menu);
  623. JLabel space_2 = new JLabel(" ");
  624. toolBar.add(space_2);
  625. JLabel user_status_label = new JLabel("登录用户:");
  626. user_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  627. user_status_label.setForeground(Color.BLACK);
  628. user_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  629. user_status_label.setBackground(Color.LIGHT_GRAY);
  630. toolBar.add(user_status_label);
  631. user_menu = new JButton("JinJuShi");
  632. user_menu.setForeground(Color.GREEN);
  633. user_menu.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  634. user_menu.setBackground(Color.BLACK);
  635. toolBar.add(user_menu);
  636. JLabel space_3 = new JLabel(" ");
  637. toolBar.add(space_3);
  638. JLabel space_4 = new JLabel(" ");
  639. toolBar.add(space_4);
  640. tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  641. tabbedPane.setMinimumSize(new Dimension(400, 50));
  642. tabbedPane.setFont(new Font("宋体", Font.BOLD, 22));
  643. contentPane.add(tabbedPane);
  644. //首页
  645. JPanel indexPanelA = new JPanel();
  646. indexScrollPaneA = new JScrollPane(indexPanelA);
  647. indexPanelA.setLayout(null);
  648. // 镭雕:获取镭雕码 / 复位按钮,放在扫码框(回显框)上方
  649. laser_get_code_bt = new JButton("获取镭雕码");
  650. laser_get_code_bt.addActionListener(new ActionListener() {
  651. public void actionPerformed(ActionEvent e) {
  652. laserFlowManager.startLaserFlow(mes_gw, mes_line_sn);
  653. }
  654. });
  655. laser_get_code_bt.setFont(new Font("微软雅黑", Font.PLAIN, 24));
  656. laser_get_code_bt.setBounds(81, 40, 300, 50);
  657. indexPanelA.add(laser_get_code_bt);
  658. // 镭雕:人工复位按钮(完成信号超时或异常时使用,强制解锁并作废当前请求)
  659. laser_reset_bt = new JButton("复位");
  660. laser_reset_bt.addActionListener(new ActionListener() {
  661. public void actionPerformed(ActionEvent e) {
  662. int result = JOptionPane.showConfirmDialog(MesClient.this,
  663. "复位会取消当前镭雕流程,确定要继续吗?", "确认复位",
  664. JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
  665. if (result == JOptionPane.YES_OPTION) {
  666. laserFlowManager.manualReset();
  667. resetManualSubmitPanel(true);
  668. }
  669. }
  670. });
  671. laser_reset_bt.setFont(new Font("微软雅黑", Font.PLAIN, 24));
  672. laser_reset_bt.setBounds(400, 40, 150, 50);
  673. indexPanelA.add(laser_reset_bt);
  674. product_sn = new JTextField();
  675. product_sn.setHorizontalAlignment(SwingConstants.CENTER);
  676. product_sn.setEditable(false);
  677. product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  678. product_sn.setBounds(81, 100, 602, 70);
  679. indexPanelA.add(product_sn);
  680. product_sn.setColumns(10);
  681. // 原扫码按钮不再需要,隐藏但保留逻辑(scanBarcode等方法仍被其他流程复用)
  682. f_scan_data_bt_1 = new JButton("扫码");
  683. f_scan_data_bt_1.addActionListener(new ActionListener() {
  684. public void actionPerformed(ActionEvent e) {
  685. scan_type = 1;
  686. scanBarcode();
  687. }
  688. });
  689. f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  690. f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  691. f_scan_data_bt_1.setBounds(693, 100, 198, 70);
  692. f_scan_data_bt_1.setVisible(false);
  693. indexPanelA.add(f_scan_data_bt_1);
  694. JLabel batch_label = new JLabel("绑定物料批次码");
  695. batch_label.setHorizontalAlignment(SwingConstants.CENTER);
  696. batch_label.setForeground(Color.BLACK);
  697. batch_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  698. batch_label.setBackground(Color.LIGHT_GRAY);
  699. batch_label.setBounds(81, 140, 602, 70);
  700. // indexPanelA.add(batch_label);
  701. batch_sn = new JTextField();
  702. batch_sn.setHorizontalAlignment(SwingConstants.CENTER);
  703. batch_sn.setEditable(false);
  704. batch_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  705. batch_sn.setBounds(81, 210, 602, 70);
  706. // indexPanelA.add(batch_sn);
  707. batch_sn.setColumns(10);
  708. batch_sn_btn = new JButton("扫码");
  709. batch_sn_btn.addActionListener(new ActionListener() {
  710. public void actionPerformed(ActionEvent e) {
  711. scan_type = 2;
  712. scanBarcode();
  713. }
  714. });
  715. batch_sn_btn.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  716. batch_sn_btn.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  717. batch_sn_btn.setBounds(693, 210, 198, 70);
  718. // indexPanelA.add(batch_sn_btn);
  719. // String[] hjtitles = new String[]{"焊机1","焊机2","焊机3"};
  720. // String[] hjvals = new String[]{"HJ001","HJ002","HJ003"};
  721. // mesRadioHj = new MesRadio(hjtitles,hjvals);
  722. // mesRadioHj.setSize(500,50);
  723. // mesRadioHj.setBounds(190,170,500,50);
  724. // indexPanelA.add(mesRadioHj);
  725. finish_ok_bt = new JButton("OK");
  726. finish_ok_bt.setEnabled(false);
  727. finish_ok_bt.addActionListener(new ActionListener() {
  728. public void actionPerformed(ActionEvent e) {
  729. String sn = getBarcode(product_sn.getText());
  730. // String batchsn = getBarcode(batch_sn.getText());
  731. if(sn.isEmpty()){
  732. MesClient.setMenuStatus("请扫工件码",1);
  733. return;
  734. }
  735. // if(batchsn.isEmpty()){
  736. // MesClient.setMenuStatus("请扫物料批次码",1);
  737. // return;
  738. // }
  739. getUser();
  740. String qret = "OK";
  741. Boolean sendret = DataUtil.sendCreate(nettyClient,sn,qret,user20);
  742. if(!sendret){
  743. MesClient.setMenuStatus("消息发送失败,请重试",1);
  744. // JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  745. return;
  746. }
  747. }
  748. });
  749. finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
  750. finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  751. finish_ok_bt.setBounds(185, 341, 240, 80);
  752. finish_ok_bt.setEnabled(false);
  753. // 镭雕工位不需要手动点OK,完成信号收到后自动提交,隐藏但保留逻辑复用
  754. finish_ok_bt.setVisible(false);
  755. indexPanelA.add(finish_ok_bt);
  756. finish_ng_bt = new JButton("NG");
  757. finish_ng_bt.setEnabled(false);
  758. finish_ng_bt.addActionListener(new ActionListener() {
  759. public void actionPerformed(ActionEvent e) {
  760. String sn = getBarcode(product_sn.getText());
  761. // String batchsn = getBarcode(batch_sn.getText());
  762. if(sn.isEmpty()){
  763. MesClient.setMenuStatus("请扫工件码",1);
  764. return;
  765. }
  766. // if(batchsn.isEmpty()){
  767. // MesClient.setMenuStatus("请扫物料批次码",1);
  768. // return;
  769. // }
  770. getUser();
  771. String qret = "NG";
  772. Boolean sendret = DataUtil.sendCreate(nettyClient,sn,qret,user20);
  773. if(!sendret){
  774. MesClient.setMenuStatus("消息发送失败,请重试",1);
  775. // JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  776. return;
  777. }
  778. }
  779. });
  780. finish_ng_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ng_bg.png")));
  781. finish_ng_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  782. finish_ng_bt.setBounds(508, 341, 240, 80);
  783. finish_ng_bt.setEnabled(false);
  784. // 镭雕工位不需要手动点NG,隐藏但保留逻辑复用
  785. finish_ng_bt.setVisible(false);
  786. indexPanelA.add(finish_ng_bt);
  787. // ===== 手动模式面板 =====
  788. JLabel debugTitle = new JLabel("手动模式");
  789. debugTitle.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  790. debugTitle.setForeground(Color.GRAY);
  791. debugTitle.setHorizontalAlignment(SwingConstants.CENTER);
  792. debugTitle.setVisible(true);
  793. debugTitle.setBounds(760, 40, 220, 30);
  794. indexPanelA.add(debugTitle);
  795. manualSteelSnInput = new JTextField();
  796. manualSteelSnInput.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  797. manualSteelSnInput.setBounds(760, 80, 220, 40);
  798. manualSteelSnInput.setToolTipText("输入11位钢印码");
  799. manualSteelSnInput.getDocument().addDocumentListener(new DocumentListener() {
  800. public void insertUpdate(DocumentEvent e) {
  801. updateManualSubmitButtons();
  802. }
  803. public void removeUpdate(DocumentEvent e) {
  804. updateManualSubmitButtons();
  805. }
  806. public void changedUpdate(DocumentEvent e) {
  807. updateManualSubmitButtons();
  808. }
  809. });
  810. indexPanelA.add(manualSteelSnInput);
  811. manualGetCodeBt = new JButton("模拟获取镭雕码");
  812. manualGetCodeBt.setEnabled(false);
  813. manualGetCodeBt.addActionListener(new ActionListener() {
  814. public void actionPerformed(ActionEvent e) {
  815. if (!isManualSteelSnReady()) {
  816. MesClient.setMenuStatus("请输入11位钢印码", 1);
  817. updateManualSubmitButtons();
  818. return;
  819. }
  820. if (laserFlowManager.isWaitingComplete()) {
  821. MesClient.setMenuStatus("镭雕进行中,请先完成或复位", 1);
  822. return;
  823. }
  824. manualWaitingComplete = true;
  825. manualCompleting = false;
  826. laserFlowManager.simulateGetCode(manualSteelSnInput.getText());
  827. updateManualSubmitButtons();
  828. }
  829. });
  830. manualGetCodeBt.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  831. manualGetCodeBt.setBounds(760, 130, 220, 50);
  832. indexPanelA.add(manualGetCodeBt);
  833. manualFinishOkBt = new JButton("模拟完成信号(OK)");
  834. manualFinishOkBt.setEnabled(false);
  835. manualFinishOkBt.addActionListener(new ActionListener() {
  836. public void actionPerformed(ActionEvent e) {
  837. manualCompleting = true;
  838. updateManualSubmitButtons();
  839. laserFlowManager.simulateComplete("OK", "");
  840. }
  841. });
  842. manualFinishOkBt.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  843. manualFinishOkBt.setBounds(760, 190, 220, 50);
  844. indexPanelA.add(manualFinishOkBt);
  845. manualUnbindBt = new JButton("解绑钢印码");
  846. manualUnbindBt.setEnabled(false);
  847. manualUnbindBt.addActionListener(new ActionListener() {
  848. public void actionPerformed(ActionEvent e) {
  849. requestUnbindSteelSn(manualSteelSnInput.getText());
  850. }
  851. });
  852. manualUnbindBt.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  853. manualUnbindBt.setBounds(760, 250, 220, 50);
  854. indexPanelA.add(manualUnbindBt);
  855. JButton debugSimNgBtn = new JButton("模拟完成信号(NG)");
  856. debugSimNgBtn.setVisible(false);
  857. debugSimNgBtn.addActionListener(new ActionListener() {
  858. public void actionPerformed(ActionEvent e) {
  859. laserFlowManager.simulateComplete("NG", "模拟打码失败");
  860. }
  861. });
  862. indexPanelA.add(debugSimNgBtn);
  863. JButton debugGenSnBtn = new JButton("测试生成客户编码");
  864. debugGenSnBtn.setVisible(false);
  865. debugGenSnBtn.addActionListener(new ActionListener() {
  866. public void actionPerformed(ActionEvent e) {
  867. laserFlowManager.manualGenCustomerSn(manualSteelSnInput.getText());
  868. }
  869. });
  870. indexPanelA.add(debugGenSnBtn);
  871. // ===== 手动模式面板结束 =====
  872. JButton modbusBtn = new JButton("关");
  873. modbusBtn.addActionListener(new ActionListener() {
  874. public void actionPerformed(ActionEvent e) {
  875. ModbusRtu.closeDevice(serialPort);
  876. }
  877. });
  878. modbusBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  879. modbusBtn.setBounds(208, 241, 160, 60);
  880. // indexPanelA.add(modbusBtn);
  881. JButton modbusBtn2 = new JButton("开");
  882. modbusBtn2.addActionListener(new ActionListener() {
  883. public void actionPerformed(ActionEvent e) {
  884. ModbusRtu.openDevice(serialPort);
  885. }
  886. });
  887. modbusBtn2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  888. modbusBtn2.setBounds(388, 241, 160, 60);
  889. // indexPanelA.add(modbusBtn2);
  890. JButton modbusBtn3 = new JButton("查询");
  891. modbusBtn3.addActionListener(new ActionListener() {
  892. public void actionPerformed(ActionEvent e) {
  893. ModbusRtu.readCoil(serialPort,0);
  894. }
  895. });
  896. modbusBtn3.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  897. modbusBtn3.setBounds(568, 241, 160, 60);
  898. // indexPanelA.add(modbusBtn3);
  899. VisualUiEditorPanel.applySavedDesign(indexPanelA);
  900. tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null);
  901. tabbedPane.setEnabledAt(0, true);
  902. JPanel unbindPanel = new JPanel();
  903. JScrollPane unbindScrollPane = new JScrollPane(unbindPanel);
  904. unbindPanel.setLayout(null);
  905. JLabel unbindTitle = new JLabel("钢印码解绑");
  906. unbindTitle.setHorizontalAlignment(SwingConstants.CENTER);
  907. unbindTitle.setForeground(Color.BLACK);
  908. unbindTitle.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  909. unbindTitle.setBounds(260, 80, 420, 50);
  910. unbindPanel.add(unbindTitle);
  911. JLabel unbindInputLabel = new JLabel("钢印码");
  912. unbindInputLabel.setHorizontalAlignment(SwingConstants.RIGHT);
  913. unbindInputLabel.setForeground(Color.BLACK);
  914. unbindInputLabel.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  915. unbindInputLabel.setBounds(170, 170, 120, 50);
  916. unbindPanel.add(unbindInputLabel);
  917. final JTextField unbindSteelSnInput = new JTextField();
  918. unbindSteelSnInput.setHorizontalAlignment(SwingConstants.CENTER);
  919. unbindSteelSnInput.setFont(new Font("微软雅黑", Font.PLAIN, 24));
  920. unbindSteelSnInput.setToolTipText("输入11位钢印码");
  921. unbindSteelSnInput.setBounds(320, 170, 340, 50);
  922. unbindPanel.add(unbindSteelSnInput);
  923. final JButton unbindSteelSnBt = new JButton("解绑钢印码");
  924. unbindSteelSnBt.setEnabled(false);
  925. unbindSteelSnBt.setFont(new Font("微软雅黑", Font.PLAIN, 24));
  926. unbindSteelSnBt.setBounds(320, 250, 220, 60);
  927. unbindSteelSnBt.addActionListener(new ActionListener() {
  928. public void actionPerformed(ActionEvent e) {
  929. requestUnbindSteelSn(unbindSteelSnInput.getText(), unbindSteelSnInput);
  930. }
  931. });
  932. unbindPanel.add(unbindSteelSnBt);
  933. unbindSteelSnInput.getDocument().addDocumentListener(new DocumentListener() {
  934. private void update() {
  935. boolean ready = unbindSteelSnInput.getText().trim().length() == 11;
  936. boolean laserBusy = laserFlowManager != null && laserFlowManager.isWaitingComplete();
  937. unbindSteelSnBt.setEnabled(ready && !laserBusy);
  938. }
  939. public void insertUpdate(DocumentEvent e) {
  940. update();
  941. }
  942. public void removeUpdate(DocumentEvent e) {
  943. update();
  944. }
  945. public void changedUpdate(DocumentEvent e) {
  946. update();
  947. }
  948. });
  949. tabbedPane.addTab("钢印解绑", new ImageIcon(MesClient.class.getResource("/bg/delete.png")), unbindScrollPane, null);
  950. // searchScrollPane = new JScrollPane((Component) null);
  951. indexPanelC = new JPanel();
  952. searchScrollPaneDj = new JScrollPane(indexPanelC);
  953. indexPanelC.setLayout(null);
  954. tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null);
  955. indexPanelB = new JPanel();
  956. searchScrollPane = new JScrollPane(indexPanelB);
  957. indexPanelB.setLayout(null);
  958. tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
  959. VisualUiEditorPanel visualEditorPanel = new VisualUiEditorPanel(indexPanelA);
  960. final JDialog visualEditorToolWindow = new JDialog(this, false);
  961. visualEditorToolWindow.setTitle("UI");
  962. visualEditorToolWindow.setContentPane(visualEditorPanel.createExternalToolPanel());
  963. visualEditorToolWindow.pack();
  964. addComponentListener(new ComponentAdapter() {
  965. public void componentMoved(ComponentEvent e) {
  966. if (visualEditorToolWindow.isVisible()) {
  967. positionVisualEditorToolWindow(visualEditorToolWindow);
  968. }
  969. }
  970. public void componentResized(ComponentEvent e) {
  971. if (visualEditorToolWindow.isVisible()) {
  972. positionVisualEditorToolWindow(visualEditorToolWindow);
  973. }
  974. }
  975. });
  976. // UI面板默认隐藏,三击时间状态栏并输入密码后再显示。
  977. heart_beat_menu.addMouseListener(new MouseAdapter() {
  978. @Override
  979. public void mouseClicked(MouseEvent e) {
  980. if (e.getClickCount() != 3) {
  981. return;
  982. }
  983. JPasswordField passwordField = new JPasswordField();
  984. int ret = JOptionPane.showConfirmDialog(
  985. MesClient.this,
  986. passwordField,
  987. "请输入UI密码",
  988. JOptionPane.OK_CANCEL_OPTION,
  989. JOptionPane.PLAIN_MESSAGE);
  990. if (ret != JOptionPane.OK_OPTION) {
  991. return;
  992. }
  993. String password = new String(passwordField.getPassword());
  994. if (!ui_design_password.equals(password)) {
  995. setMenuStatus("UI密码错误", 1);
  996. return;
  997. }
  998. int index = tabbedPane.indexOfComponent(visualEditorPanel);
  999. if (index < 0) {
  1000. tabbedPane.addTab("UI", null, visualEditorPanel, null);
  1001. index = tabbedPane.indexOfComponent(visualEditorPanel);
  1002. }
  1003. tabbedPane.setSelectedIndex(index);
  1004. positionVisualEditorToolWindow(visualEditorToolWindow);
  1005. visualEditorToolWindow.setVisible(true);
  1006. }
  1007. });
  1008. tabbedPane.addChangeListener(new ChangeListener() {
  1009. @Override
  1010. public void stateChanged(ChangeEvent e) {
  1011. JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
  1012. int selectedIndex = tabbedPane.getSelectedIndex();
  1013. System.out.println("selectedIndex:"+selectedIndex);
  1014. boolean visualEditorSelected = selectedIndex >= 0 && tabbedPane.getComponentAt(selectedIndex) == visualEditorPanel;
  1015. if (visualEditorSelected) {
  1016. positionVisualEditorToolWindow(visualEditorToolWindow);
  1017. visualEditorToolWindow.setVisible(true);
  1018. } else {
  1019. visualEditorToolWindow.setVisible(false);
  1020. }
  1021. // getCheckResult()调用已移除:原用于驱动继电器(formatDeviceState),
  1022. // 继电器功能已不再使用
  1023. }
  1024. });
  1025. // 确保窗口布局正确
  1026. pack();
  1027. setSize(1024, 768);
  1028. validate();
  1029. repaint();
  1030. }
  1031. private void positionVisualEditorToolWindow(JDialog toolWindow) {
  1032. Point location = getLocationOnScreen();
  1033. int x = location.x + getWidth() - 1;
  1034. int y = location.y + 110;
  1035. toolWindow.pack();
  1036. toolWindow.setLocation(x, y);
  1037. }
  1038. // 获取开班点检状态
  1039. public static void getCheckResult(){
  1040. try{
  1041. JSONObject retObj = DataUtil.getCheckResult();
  1042. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { // 已点检
  1043. MesClient.checkState = true;
  1044. }else{ // 未点检
  1045. MesClient.checkState = false;
  1046. }
  1047. }catch (Exception e){
  1048. e.printStackTrace();
  1049. MesClient.checkState = false;
  1050. }
  1051. MesClient.formatDeviceState();
  1052. }
  1053. // 格式化设备启动状态
  1054. public static void formatDeviceState(){
  1055. try{
  1056. if(MesClient.checkState){ // 检查物料是否已绑定
  1057. if(rowData == null){
  1058. ModbusRtu.openDevice(serialPort);
  1059. }else{
  1060. // 判断物料是否可启动
  1061. int flag = 0;
  1062. int i = 0;
  1063. for (Object object:rowData){
  1064. Integer times = Integer.valueOf(String.valueOf(rowData[i][2]));
  1065. if(times < 1){
  1066. flag = 1;
  1067. }
  1068. i++;
  1069. }
  1070. if(flag == 1){ // 不能启动
  1071. ModbusRtu.closeDevice(serialPort);
  1072. }else{
  1073. // 可启动
  1074. ModbusRtu.openDevice(serialPort);
  1075. }
  1076. }
  1077. }else{
  1078. //不能启动
  1079. ModbusRtu.closeDevice(serialPort);
  1080. }
  1081. }catch (Exception e){
  1082. e.printStackTrace();
  1083. }
  1084. }
  1085. public static void getMaterailData(){
  1086. JSONObject retObj = DataUtil.getBindMaterail();
  1087. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  1088. java.util.List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  1089. int i = 0;
  1090. rowData = new Object[arrs.size()][7];
  1091. for (BindMaterialResp bindMaterialResp:arrs){
  1092. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  1093. rowData[i][1] = bindMaterialResp.getBatchSn();
  1094. rowData[i][2] = bindMaterialResp.getLastTimes();
  1095. rowData[i][3] = "";
  1096. rowData[i][4] = bindMaterialResp.getCraft();
  1097. rowData[i][5] = bindMaterialResp.getMaterialId();
  1098. rowData[i][6] = bindMaterialResp.getType();
  1099. i++;
  1100. }
  1101. bindBatchPanel();
  1102. }
  1103. }
  1104. public static void updateMaterailData(){
  1105. JSONObject retObj = DataUtil.getBindMaterail();
  1106. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  1107. List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  1108. int i = 0;
  1109. for (BindMaterialResp bindMaterialResp:arrs){
  1110. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  1111. rowData[i][1] = bindMaterialResp.getBatchSn();
  1112. rowData[i][2] = bindMaterialResp.getLastTimes();
  1113. rowData[i][3] = "";
  1114. rowData[i][4] = bindMaterialResp.getCraft();
  1115. rowData[i][5] = bindMaterialResp.getMaterialId();
  1116. rowData[i][6] = bindMaterialResp.getType();
  1117. i++;
  1118. }
  1119. MesClient.table.repaint();
  1120. }
  1121. }
  1122. // 绑定物料批次码
  1123. public static void scanBatchSn(BindMaterialResp bindMaterialResp) {
  1124. //弹窗扫工件码
  1125. String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle();
  1126. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  1127. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  1128. JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType());
  1129. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  1130. MesClient.setMenuStatus("扫物料:"+bindMaterialResp.getMaterialTitle()+"成功",0);
  1131. updateMaterailData();
  1132. }else{
  1133. if(retObj.get("result")==null){
  1134. MesClient.setMenuStatus("请求失败,请重试",-1);
  1135. }else{
  1136. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  1137. MesClient.setMenuStatus(retObj.getString("message"),-1);
  1138. }
  1139. }
  1140. }
  1141. }
  1142. }
  1143. public static void bindBatchPanel(){
  1144. JPanel indexPanelBB = new JPanel();
  1145. JPanel panel = new JPanel();
  1146. panel.setBounds(0, 0, 990, 550);
  1147. // panel.setBounds(81, 50, 810, 479);
  1148. indexPanelBB.add(panel);
  1149. panel.setLayout(new GridLayout(0, 1, 0, 0));
  1150. table = new JTable(rowData, columnNames){
  1151. public boolean isCellEditable(int row, int column) {
  1152. if(column == 3){
  1153. return true;
  1154. }
  1155. return false;
  1156. }
  1157. };
  1158. table.setRowHeight(40);
  1159. table.setEnabled(true);
  1160. table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  1161. table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton());
  1162. table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton());
  1163. JScrollPane scrollPane = new JScrollPane(table);
  1164. panel.add(scrollPane);
  1165. JScrollPane indexScrollPaneB = new JScrollPane(indexPanelBB);
  1166. indexPanelBB.setLayout(null);
  1167. tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null);
  1168. }
  1169. }