MesClient.java 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.fazecast.jSerialComm.SerialPort;
  5. import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
  6. import com.mes.component.MesRadio;
  7. import com.mes.component.MesWebView;
  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 org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import javax.swing.*;
  15. import javax.swing.border.EmptyBorder;
  16. import javax.swing.event.ChangeEvent;
  17. import javax.swing.event.ChangeListener;
  18. import java.awt.*;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ActionListener;
  21. import java.awt.event.MouseAdapter;
  22. import java.awt.event.MouseEvent;
  23. import java.io.BufferedReader;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.InputStreamReader;
  27. import java.time.LocalDateTime;
  28. import java.util.*;
  29. import java.util.List;
  30. import java.util.Timer;
  31. import java.util.concurrent.ExecutorService;
  32. import java.util.concurrent.Executors;
  33. public class MesClient extends JFrame {
  34. private static final Logger log = LoggerFactory.getLogger(MesClient.class);
  35. private static String Drivde = "org.sqlite.JDBC";
  36. public static int mes_auth = 0; // 权限级别 0=无权限 1=操作工人 2=管理员
  37. public static String mes_gw = ""; // 工位号
  38. public static String mes_gw_des = ""; // 工位名称
  39. public static String mes_server_ip = ""; // 服务器IP地址
  40. public static int mes_tcp_port = 3000; // TCP服务端口
  41. public static int mes_heart_beat_cycle = 10; // 心跳周期
  42. public static int mes_heart_icon_cycle = 1;
  43. public static String mes_line_sn = ""; // 产线编号
  44. //TCP连接
  45. public static NettyClient nettyClient;
  46. //TCP连接状态
  47. public static boolean tcp_connect_flag = false;
  48. //TCP连接请求
  49. public static boolean connect_request_flag = false;
  50. //session
  51. public static String sessionid = "";
  52. public static JPanel contentPane;
  53. public static MesClient mesClientFrame;
  54. public static JTabbedPane tabbedPane;
  55. public static JScrollPane indexScrollPaneA;
  56. public static JScrollPane searchScrollPane;
  57. public static JScrollPane searchScrollPaneDj;
  58. public static Boolean check_quality_result = false;
  59. public static Integer work_status = 0;
  60. public static JButton heart_beat_menu;
  61. public static JButton status_menu;
  62. public static JButton user_menu;
  63. public static int scan_type = 0;
  64. public static JButton finish_ok_bt;
  65. public static JButton finish_ng_bt;
  66. public static JTextField product_sn;
  67. public static JButton f_scan_data_bt_1;
  68. public static JTextField product_sn_lb;
  69. public static JButton f_scan_data_bt_2;
  70. public static String user20 = "";
  71. public static JFrame welcomeWin;
  72. public static JPanel indexPanelB;
  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 JTextField param1;
  78. public static JTextField param2;
  79. public static JTextField param3;
  80. public static JTextField param4;
  81. public static ModbusTcp plcA = new ModbusTcp(1, "192.168.1.7");
  82. // public static ModbusTcp plcB = new ModbusTcp(1, "192.168.1.8");
  83. public static Timer cjTimer;
  84. public static Timer cjTimer2;
  85. public static Timer cjTimer3;
  86. public static Short aSetNum = 48;
  87. public static Short sortA = 0;
  88. public static Short aMax = 0;
  89. public static Short aFinish = 0; //合格数量
  90. public static List<Map> alist = new ArrayList<>();
  91. public static Short bSetNum = 10;
  92. public static Short sortB = 0;
  93. public static Short bMax = 0;
  94. public static List<Map> blist = new ArrayList<>();
  95. public static Short deviceControl = 0; // 0=本地 1=远程
  96. public static Integer tjStatus = 0; // 1=提交失败
  97. public static String tjFlagTextErr = "结果上传MES失败,请重试";
  98. public static String curSn = "";
  99. public static Integer zdStatusA = 0;
  100. public static String foutA = "";
  101. public static String fmaxA = "";
  102. public static String fminA = "";
  103. public static String soutA = "";
  104. public static String smaxA = "";
  105. public static String sminA = "";
  106. public static String curA = "";
  107. public static JButton resetResultA;
  108. public static JButton okResultA;
  109. public static JButton ngResultA;
  110. public static JTable table;
  111. public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" };
  112. public static Object[][] rowData = null;
  113. public static Boolean checkState = false; // 点检状态 false=未点检 true=已点检
  114. public static SerialPort serialPort; // 串口对象
  115. public static JLabel lblNewLabel;
  116. public static java.util.Timer deviceHeartTimer = null;
  117. public static Short curInit = 0; // 初始拉铆数量
  118. public static Boolean fxModeVal = false;
  119. public static JButton fxMode;
  120. public static JButton fxMaxBtn;
  121. public static void main(String[] args) {
  122. EventQueue.invokeLater(new Runnable() {
  123. @Override
  124. public void run() {
  125. try{
  126. //读文件配置
  127. readProperty();
  128. // 显示界面
  129. mesClientFrame = new MesClient();
  130. mesClientFrame.setVisible(false);
  131. JdbcUtils.getConn();
  132. welcomeWin = new LoginFarme();
  133. welcomeWin.setVisible(true);
  134. getPlcParam();
  135. upParams();
  136. setDeviceStart();
  137. getMaterailData();
  138. deviceOnline();
  139. }catch (Exception e){
  140. log.error("系统初始化异常: ", e);
  141. e.printStackTrace();
  142. }
  143. }
  144. });
  145. }
  146. public static java.util.Timer cjTimer4;
  147. public static void setDeviceStart() {
  148. if(cjTimer4!=null) {
  149. cjTimer4.cancel();
  150. }
  151. cjTimer4 = new Timer();
  152. cjTimer4.schedule(new TimerTask() {
  153. public void run() {
  154. try{
  155. if(work_status == 1 && zdStatusA == 0){
  156. List<Boolean> yxstatus = plcA.readCoil(456,1);
  157. if(yxstatus.size() >= 1 && !yxstatus.get(0)){
  158. ModbusUtil.setPowerOn(MesClient.plcA); // 远程关机
  159. }
  160. }
  161. }catch (Exception e){
  162. log.error("设备启动监控异常: ", e);
  163. e.printStackTrace();
  164. }
  165. }
  166. }, 1000,1000);
  167. }
  168. //读配置文件
  169. private static void readProperty() throws IOException{
  170. String enconding = "UTF-8";
  171. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  172. Properties pro = new Properties();
  173. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  174. pro.load(br);
  175. mes_gw = pro.getProperty("mes.gw");
  176. // mes_gw_des = pro.getProperty("mes.gw_des");
  177. mes_server_ip = pro.getProperty("mes.server_ip");
  178. mes_tcp_port = Integer.parseInt(pro.getProperty("mes.tcp_port"));
  179. mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle"));
  180. mes_line_sn = pro.getProperty("mes.line_sn");
  181. mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
  182. log.info("加载配置: gw={}, gw_des={}, server_ip={}, tcp_port={}, heart_beat_cycle={}",
  183. mes_gw, mes_gw_des, mes_server_ip, mes_tcp_port, mes_heart_beat_cycle);
  184. }
  185. public static void getPlcParam() {
  186. if(cjTimer!=null) {
  187. cjTimer.cancel();
  188. }
  189. cjTimer = new Timer();
  190. cjTimer.schedule(new TimerTask() {
  191. public void run() {
  192. try{
  193. ModbusUtil.getDataA(plcA);
  194. }catch (Exception e){
  195. e.printStackTrace();
  196. }
  197. // try{
  198. // ModbusUtil.getDataB(plcB);
  199. // }catch (Exception e){
  200. // e.printStackTrace();
  201. // }
  202. }
  203. }, 1000,500);
  204. }
  205. public static java.util.Timer cjTimer5;
  206. public static void deviceOnline() {
  207. if(cjTimer5!=null) {
  208. cjTimer5.cancel();
  209. }
  210. cjTimer5 = new Timer();
  211. cjTimer5.schedule(new TimerTask() {
  212. public void run() {
  213. try{
  214. boolean lamaoret = ModbusUtil.getOnOff(plcA);
  215. if(lamaoret){
  216. lblNewLabel.setText("A枪:在线");
  217. lblNewLabel.setForeground(Color.GREEN);
  218. }else{
  219. lblNewLabel.setText("A枪:离线");
  220. lblNewLabel.setForeground(Color.RED);
  221. }
  222. if(work_status == 1){
  223. List<Boolean> yxstatus = plcA.readCoil(3128,1);
  224. if(yxstatus.size() >= 1 && !yxstatus.get(0)){
  225. if(!MesClient.curSn.isEmpty() && MesClient.aMax > 0 && MesClient.aMax == MesClient.sortA){
  226. ModbusUtil.setPowerOff(plcA);
  227. }else{
  228. ModbusUtil.setPowerOn(plcA);
  229. }
  230. }
  231. }else{
  232. List<Boolean> yxstatus = plcA.readCoil(3128,1);
  233. if(yxstatus.size() >= 1 && yxstatus.get(0)){
  234. ModbusUtil.setPowerOff(plcA);
  235. }
  236. }
  237. }catch (Exception e){
  238. e.printStackTrace();
  239. }
  240. }
  241. }, 1000,1000);
  242. }
  243. public static void upParams() {
  244. if(cjTimer2!=null) {
  245. cjTimer2.cancel();
  246. }
  247. cjTimer2 = new Timer();
  248. cjTimer2.schedule(new TimerTask() {
  249. public void run() {
  250. try{
  251. List<ProdReq> prods = JdbcUtils.getProds();
  252. log.info("【参数定时上传】获取到待同步的生产数据,记录数: {}", prods.size());
  253. if(prods.size() > 0){
  254. log.debug("【参数定时上传】请求数据详情: {}", JSON.toJSONString(prods));
  255. // 定时上传参数
  256. JSONObject retObj = DataUtil.upParams(JSON.toJSONString(prods));
  257. log.info("【参数定时上传】服务端返回结果: {}", retObj != null ? retObj.toJSONString() : "null");
  258. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  259. // 更新sqlite已同步
  260. for(ProdReq prodReq:prods){
  261. JdbcUtils.updateProdSync(prodReq.getId(),1);
  262. }
  263. log.info("【参数定时上传】本地 SQLite 状态更新完成");
  264. } else {
  265. log.warn("【参数定时上传】接口返回非 true,未更新本地状态");
  266. }
  267. }
  268. }catch (Exception e){
  269. log.error("【参数定时上传】发生异常: ", e);
  270. e.printStackTrace();
  271. }
  272. }
  273. }, 1000,30*1000);
  274. }
  275. // 初始化TCP
  276. public static void initTcpConnection() {
  277. try {
  278. if(nettyClient==null) {
  279. //初始化TCP连接
  280. nettyClient = new NettyClient();
  281. //TCP连接状态
  282. tcp_connect_flag = false;
  283. //设置TCP请求状态
  284. connect_request_flag = true;
  285. DataUtil.synrTcp(nettyClient,mes_gw);
  286. }
  287. } catch (Exception e) {
  288. log.error("初始化TCP连接异常: ", e);
  289. e.printStackTrace();
  290. }
  291. }
  292. //启动心跳包程序
  293. public static java.util.Timer heartBeatTimer;
  294. public static java.util.Timer heartBeatIconTimer;
  295. public static boolean iconREDFlag = true;
  296. public static void startHeartBeatTimer() {
  297. if(heartBeatTimer!=null) {
  298. heartBeatTimer.cancel();
  299. }
  300. heartBeatTimer = new java.util.Timer();
  301. heartBeatTimer.schedule(new TimerTask() {
  302. public void run() {
  303. if(nettyClient!=null&&tcp_connect_flag) {
  304. DataUtil.heartBeat(nettyClient,mes_gw);
  305. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  306. }
  307. }
  308. }, 100,mes_heart_beat_cycle*1000);
  309. //心跳显示图标
  310. if(heartBeatIconTimer!=null) {
  311. heartBeatIconTimer.cancel();
  312. }
  313. heartBeatIconTimer = new Timer();
  314. heartBeatIconTimer.schedule(new TimerTask() {
  315. public void run() {
  316. if(tcp_connect_flag) {
  317. if(iconREDFlag) {
  318. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  319. iconREDFlag = false;
  320. }else {
  321. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  322. iconREDFlag = true;
  323. }
  324. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  325. heart_beat_menu.repaint();
  326. }else {
  327. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  328. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  329. heart_beat_menu.repaint();
  330. //若未连接则尝试连接
  331. if(nettyClient!=null&&!tcp_connect_flag){
  332. log.warn("TCP已断开,尝试重新连接");
  333. //TCP重新同步连接
  334. if(!connect_request_flag) {
  335. //设置TCP请求状态,只重新同步连接一次
  336. connect_request_flag = true;
  337. DataUtil.synrTcp(nettyClient,mes_gw);
  338. }
  339. }
  340. }
  341. }
  342. }, 100,mes_heart_icon_cycle*1000);
  343. }
  344. //设置tcp连接状态显示
  345. public static void setTcpStatus() {
  346. if(tcp_connect_flag) {
  347. status_menu.setText("已连接MES服务器");
  348. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  349. heart_beat_menu.repaint();
  350. }else {
  351. status_menu.setText("未连接MES服务器");
  352. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  353. heart_beat_menu.repaint();
  354. }
  355. }
  356. public static void initWarehouseData(){
  357. resetScanA();
  358. }
  359. public static void resetScanA() {
  360. log.info("【系统操作】执行产品及面板状态重置 (resetScanA)");
  361. work_status = 0;
  362. check_quality_result = false;
  363. MesClient.finish_ok_bt.setEnabled(false);
  364. MesClient.finish_ng_bt.setEnabled(false);
  365. product_sn.setText("");
  366. curSn = "";
  367. product_sn_lb.setText("");
  368. f_scan_data_bt_2.setEnabled(false);
  369. MesClient.f_scan_data_bt_1.setEnabled(true);
  370. MesClient.setMenuStatus("请扫工件码",0);
  371. MesClient.aMax = 0;
  372. MesClient.aFinish = 0;
  373. MesClient.bMax = 0;
  374. MesClient.alist = new ArrayList<>();
  375. MesClient.blist = new ArrayList<>();
  376. MesClient.sortB = 0;
  377. MesClient.sortA = 0;
  378. MesClient.param1.setText("");
  379. MesClient.param2.setText("");
  380. MesClient.param3.setText("");
  381. MesClient.param4.setText("");
  382. MesClient.param3.setVisible(false);
  383. MesClient.param4.setVisible(false);
  384. ModbusUtil.setPowerOff(MesClient.plcA); // 远程关机
  385. // ModbusUtil.setMesUnAllow(MesClient.plcA, MesClient.deviceHeartTimer);
  386. ModbusUtil.setTask(MesClient.plcA,aSetNum);
  387. updateMaterailData();
  388. zdStatusA = 0;
  389. resetResultA.setEnabled(false);
  390. okResultA.setEnabled(false);
  391. ngResultA.setEnabled(false);
  392. // shiftUserCheck();
  393. }
  394. public static int userLoginHours;//用户登录所处小时
  395. public static JTextField fxMaxNum;
  396. //换班用户信息检查
  397. private static void shiftUserCheck() {
  398. LocalDateTime now = LocalDateTime.now();
  399. // 判断时间范围
  400. if (userLoginHours >= 8 && userLoginHours < 20) {
  401. int hour = now.getHour();
  402. if (hour >= 20 || hour < 8) {
  403. logoff();
  404. }
  405. } else {
  406. int hour = now.getHour();
  407. if (hour >= 8 && hour < 20) {
  408. logoff();
  409. }
  410. }
  411. }
  412. public static void resetScanB() {
  413. }
  414. //获取用户20位
  415. public static void getUser() {
  416. user20 = user_menu.getText().toString();
  417. String space_tmp1 = "";
  418. if(user20.length()<20) {
  419. for(int i=0;i<20-user20.length();i++) {
  420. space_tmp1 = space_tmp1 + " ";
  421. }
  422. }
  423. user20 = user20 + space_tmp1;
  424. }
  425. //获取barcode内容36位
  426. public static String getBarcode(String barcodeTmp) {
  427. String barcodeRet = barcodeTmp;
  428. if(barcodeTmp.equalsIgnoreCase("")) {
  429. return "";
  430. }else {
  431. if(barcodeTmp.length()<36) {
  432. String space = "";
  433. for(int i=0;i<36-barcodeTmp.length();i++) {
  434. space = space + " ";
  435. }
  436. barcodeRet = barcodeTmp + space;
  437. }
  438. }
  439. return barcodeRet;
  440. }
  441. public static void scanBarcode() {
  442. // if(work_status == 1){
  443. // JOptionPane.showMessageDialog(mesClientFrame,"工作中,勿扫码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  444. // return;
  445. // }
  446. String scanBarcodeTitle = "";
  447. switch(scan_type) {
  448. case 1:
  449. product_sn.setText("");
  450. scanBarcodeTitle = "请扫工件码";
  451. break;
  452. }
  453. //弹窗扫工件码
  454. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  455. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  456. //获取用户
  457. getUser();
  458. //获取扫码内容36位
  459. String barcode36 = getBarcode(scanBarcode);//处理36为码
  460. //工位号
  461. String gw = "";
  462. switch(scan_type) {
  463. case 1:
  464. product_sn.setText(scanBarcode);
  465. MesClient.curSn = scanBarcode;
  466. break;
  467. }
  468. //刷新界面
  469. mesClientFrame.repaint();
  470. if(!tcp_connect_flag) {
  471. JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  472. return;
  473. }
  474. if(!fxModeVal){
  475. // 查询工件质量
  476. log.info("【质量检查-扫码触发】准备发送扫码质量询问报文, SN: {}, User: {}", barcode36, user20);
  477. Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20);
  478. log.info("【质量检查-扫码触发】请求发送结果: {}", sendret);
  479. if(!sendret){
  480. JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  481. return;
  482. }
  483. }else{
  484. MesClient.check_quality_result = true;//质量合格,可以绑定加工
  485. MesClient.setMenuStatus("该工件可以加工",0);
  486. MesClient.getUser();
  487. DataUtil.startWork(MesClient.nettyClient,barcode36,MesClient.user20);
  488. MesClient.f_scan_data_bt_2.setEnabled(true);
  489. }
  490. }else {
  491. JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  492. return;
  493. }
  494. }
  495. public static void logoff() {
  496. log.info("【系统操作】用户执行退出登录 (logoff)");
  497. // welcomeWin.setVisible(true);
  498. mesClientFrame.setVisible(false);
  499. nettyClient = null;
  500. // if(heartBeatTimer!=null) {
  501. // heartBeatTimer.cancel();
  502. // }
  503. // if(heartBeatIconTimer!=null) {
  504. // heartBeatIconTimer.cancel();
  505. // }
  506. tcp_connect_flag = false;
  507. connect_request_flag = false;
  508. }
  509. public MesClient() {
  510. setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png")));
  511. setTitle("MES系统客户端:"+mes_gw + "- " + mes_gw_des);
  512. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  513. setBounds(0, 0, 1024, 768);
  514. JMenuBar menuBar = new JMenuBar();
  515. menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26));
  516. setJMenuBar(menuBar);
  517. JMenu fileMenu = new JMenu("用户");
  518. fileMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png")));
  519. fileMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  520. menuBar.add(fileMenu);
  521. JMenuItem exitMenuItem = new JMenuItem("退出");
  522. exitMenuItem.setIcon(new ImageIcon(MesClient.class.getResource("/bg/logoff.png")));
  523. exitMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  524. fileMenu.add(exitMenuItem);
  525. exitMenuItem.addMouseListener(new MouseAdapter() {
  526. @Override
  527. public void mousePressed(MouseEvent e) {//只能检测到mousePressed事件
  528. super.mouseClicked(e);
  529. //dispose();
  530. logoff();
  531. }
  532. });
  533. JMenu settingMenu = new JMenu("设置");
  534. //settingMenu.setVisible(false);
  535. settingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
  536. settingMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  537. menuBar.add(settingMenu);
  538. JMenuItem resetTcpMenu = new JMenuItem("\u91CD\u8FDEMES");
  539. resetTcpMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  540. resetTcpMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  541. resetTcpMenu.addMouseListener(new MouseAdapter() {
  542. @Override
  543. public void mousePressed(MouseEvent e) {
  544. super.mouseClicked(e);
  545. log.info("【系统设置】用户点击了 [重连MES] 菜单");
  546. //重连mes
  547. if(nettyClient != null && nettyClient.future != null && nettyClient.future.channel() != null) {
  548. nettyClient.future.channel().close();
  549. }
  550. }
  551. });
  552. settingMenu.add(resetTcpMenu);
  553. JMenuItem resetTcpMenu_1 = new JMenuItem("刷新工件");
  554. resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  555. resetTcpMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  556. resetTcpMenu_1.addMouseListener(new MouseAdapter() {
  557. @Override
  558. public void mousePressed(MouseEvent e) {
  559. super.mouseClicked(e);
  560. log.info("【系统设置】用户点击了 [刷新工件] 菜单");
  561. resetScanA();
  562. }
  563. });
  564. settingMenu.add(resetTcpMenu_1);
  565. contentPane = new JPanel();
  566. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  567. setContentPane(contentPane);
  568. contentPane.setLayout(new BorderLayout(0, 0));
  569. JToolBar toolBar = new JToolBar();
  570. contentPane.add(toolBar, BorderLayout.NORTH);
  571. JLabel equipment_statu_label = new JLabel("状态:");
  572. equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER);
  573. equipment_statu_label.setForeground(Color.BLACK);
  574. equipment_statu_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  575. equipment_statu_label.setBackground(Color.LIGHT_GRAY);
  576. toolBar.add(equipment_statu_label);
  577. status_menu = new JButton("设备未连接MES服务器");
  578. if(tcp_connect_flag) {
  579. status_menu.setText("已连接MES服务器");
  580. status_menu.setForeground(Color.GREEN);
  581. }else {
  582. status_menu.setText("未连接MES服务器");
  583. status_menu.setForeground(Color.DARK_GRAY);
  584. }
  585. status_menu.addActionListener(new ActionListener() {
  586. public void actionPerformed(ActionEvent e) {
  587. }
  588. });
  589. status_menu.setForeground(Color.GREEN);
  590. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  591. status_menu.setBackground(Color.BLACK);
  592. toolBar.add(status_menu);
  593. JLabel space_1 = new JLabel(" ");
  594. toolBar.add(space_1);
  595. JLabel heart_beat_status_label = new JLabel("心跳:");
  596. heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  597. heart_beat_status_label.setForeground(Color.BLACK);
  598. heart_beat_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  599. heart_beat_status_label.setBackground(Color.LIGHT_GRAY);
  600. toolBar.add(heart_beat_status_label);
  601. heart_beat_menu = new JButton("2024-02-20 23:20:10");
  602. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  603. heart_beat_menu.setForeground(Color.GREEN);
  604. heart_beat_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  605. heart_beat_menu.setBackground(Color.BLACK);
  606. toolBar.add(heart_beat_menu);
  607. JLabel space_2 = new JLabel(" ");
  608. toolBar.add(space_2);
  609. JLabel user_status_label = new JLabel("登录用户:");
  610. user_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  611. user_status_label.setForeground(Color.BLACK);
  612. user_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  613. user_status_label.setBackground(Color.LIGHT_GRAY);
  614. toolBar.add(user_status_label);
  615. user_menu = new JButton("JinJuShi");
  616. user_menu.setForeground(Color.GREEN);
  617. user_menu.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  618. user_menu.setBackground(Color.BLACK);
  619. toolBar.add(user_menu);
  620. JLabel space_3 = new JLabel(" ");
  621. toolBar.add(space_3);
  622. JLabel space_4 = new JLabel(" ");
  623. toolBar.add(space_4);
  624. tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  625. tabbedPane.setMinimumSize(new Dimension(400, 50));
  626. tabbedPane.setFont(new Font("宋体", Font.BOLD, 22));
  627. contentPane.add(tabbedPane);
  628. //首页
  629. JPanel indexPanelA = new JPanel();
  630. indexScrollPaneA = new JScrollPane(indexPanelA);
  631. indexPanelA.setLayout(null);
  632. product_sn = new JTextField();
  633. product_sn.setHorizontalAlignment(SwingConstants.CENTER);
  634. product_sn.setEditable(false);
  635. product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  636. product_sn.setBounds(81, 70, 602, 70);
  637. indexPanelA.add(product_sn);
  638. product_sn.setColumns(10);
  639. f_scan_data_bt_1 = new JButton("扫码");
  640. f_scan_data_bt_1.addActionListener(new ActionListener() {
  641. public void actionPerformed(ActionEvent e) {
  642. log.info("【总控操作】点击了 [扫码(工件)] 按钮");
  643. scan_type = 1;
  644. scanBarcode();
  645. }
  646. });
  647. f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  648. f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  649. f_scan_data_bt_1.setBounds(693, 70, 198, 70);
  650. indexPanelA.add(f_scan_data_bt_1);
  651. // String[] hjtitles = new String[]{"焊机1","焊机2","焊机3"};
  652. // String[] hjvals = new String[]{"HJ001","HJ002","HJ003"};
  653. // mesRadioHj = new MesRadio(hjtitles,hjvals);
  654. // mesRadioHj.setSize(500,50);
  655. // mesRadioHj.setBounds(190,170,500,50);
  656. // indexPanelA.add(mesRadioHj);
  657. JLabel lbLabel = new JLabel("冷板码:");
  658. lbLabel.setFont(new Font("微软雅黑", Font.PLAIN, 24));
  659. lbLabel.setBounds(20, 165, 100, 50);
  660. indexPanelA.add(lbLabel);
  661. product_sn_lb = new JTextField();
  662. product_sn_lb.setHorizontalAlignment(SwingConstants.CENTER);
  663. product_sn_lb.setEditable(false);
  664. product_sn_lb.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  665. product_sn_lb.setBounds(100, 160, 580, 70);
  666. indexPanelA.add(product_sn_lb);
  667. product_sn_lb.setColumns(10);
  668. f_scan_data_bt_2 = new JButton("扫码");
  669. f_scan_data_bt_2.addActionListener(new ActionListener() {
  670. public void actionPerformed(ActionEvent e) {
  671. // scanBarcodeLb();
  672. // scan_type = 1;
  673. log.info("【总控操作】点击了 [扫码(冷板)] 按钮");
  674. if (product_sn.getText().isEmpty()){
  675. setMenuStatus("请在工件上料完成后扫码!",1);
  676. return;
  677. }
  678. if (work_status == 1&&check_quality_result){
  679. scanBarcodeLb();
  680. }else{
  681. setMenuStatus("请在工件质量检测合格后扫码!",1);
  682. }
  683. }
  684. });
  685. f_scan_data_bt_2.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  686. f_scan_data_bt_2.setFont(new Font("微软雅黑", Font.PLAIN, 30));
  687. f_scan_data_bt_2.setBounds(693, 160, 140, 70);
  688. f_scan_data_bt_2.setEnabled(false);
  689. indexPanelA.add(f_scan_data_bt_2);
  690. finish_ok_bt = new JButton("OK");
  691. finish_ok_bt.setEnabled(false);
  692. finish_ok_bt.addActionListener(new ActionListener() {
  693. public void actionPerformed(ActionEvent e) {
  694. log.info("【总控操作】点击了 [OK] 按钮,当前产品SN: {}, 操作用户: {}", product_sn.getText(), user_menu.getText());
  695. if(work_status == 1 && check_quality_result){
  696. if (product_sn_lb.getText().equals("")){
  697. JOptionPane.showMessageDialog(MesClient.mesClientFrame,"请绑定冷板码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  698. return;
  699. }
  700. String sn = getBarcode(product_sn.getText());
  701. getUser();
  702. String qret = "OK";
  703. log.info("【判定上传】准备发送质量判定结果, SN: {}, 结果: {}", sn, qret);
  704. Boolean sendret = DataUtil.sendQuality(nettyClient,sn,qret,user20);
  705. log.info("【判定上传】发送结果: {}", sendret);
  706. if(!sendret){
  707. MesClient.setMenuStatus("消息发送失败,请重试",-1);
  708. // JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  709. return;
  710. }
  711. }
  712. }
  713. });
  714. finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
  715. finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  716. // finish_ok_bt.setBounds(185, 291, 240, 80);
  717. finish_ok_bt.setBounds(185, 371, 240, 80);
  718. finish_ok_bt.setEnabled(false);
  719. indexPanelA.add(finish_ok_bt);
  720. finish_ng_bt = new JButton("NG");
  721. finish_ng_bt.setEnabled(false);
  722. finish_ng_bt.addActionListener(new ActionListener() {
  723. public void actionPerformed(ActionEvent e) {
  724. log.info("【总控操作】点击了 [NG] 按钮,当前产品SN: {}, 操作用户: {}", product_sn.getText(), user_menu.getText());
  725. if(work_status == 1 && check_quality_result){
  726. String sn = getBarcode(product_sn.getText());
  727. getUser();
  728. String qret = "NG";
  729. log.info("【判定上传】准备发送质量判定结果, SN: {}, 结果: {}", sn, qret);
  730. Boolean sendret = DataUtil.sendQuality(nettyClient,sn,qret,user20);
  731. log.info("【判定上传】发送结果: {}", sendret);
  732. if(!sendret){
  733. MesClient.setMenuStatus("消息发送失败,请重试",-1);
  734. // JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  735. return;
  736. }
  737. }
  738. }
  739. });
  740. finish_ng_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ng_bg.png")));
  741. finish_ng_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  742. finish_ng_bt.setBounds(508, 371, 240, 80);
  743. finish_ng_bt.setEnabled(false);
  744. indexPanelA.add(finish_ng_bt);
  745. lblNewLabel = new JLabel("A枪");
  746. lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
  747. lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  748. // lblNewLabel.setBounds(204, 186, 166, 28);
  749. lblNewLabel.setBounds(304, 186, 166, 28);
  750. indexPanelA.add(lblNewLabel);
  751. JLabel lblNewLabel_1 = new JLabel("\u9884\u8BBE\u6570\u91CF");
  752. lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  753. // lblNewLabel_1.setBounds(204, 241, 83, 34);
  754. lblNewLabel_1.setBounds(185, 241, 83, 34);
  755. indexPanelA.add(lblNewLabel_1);
  756. param1 = new JTextField();
  757. param1.setHorizontalAlignment(SwingConstants.CENTER);
  758. param1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  759. param1.setText("0");
  760. param1.setEditable(false);
  761. // param1.setBounds(288, 241, 83, 34);
  762. param1.setBounds(269, 241, 83, 34);
  763. indexPanelA.add(param1);
  764. param1.setColumns(10);
  765. JLabel lblNewLabel_1_1 = new JLabel("\u5B8C\u6210\u6570\u91CF");
  766. lblNewLabel_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  767. // lblNewLabel_1_1.setBounds(204, 285, 83, 34);
  768. lblNewLabel_1_1.setBounds(185, 285, 83, 34);
  769. indexPanelA.add(lblNewLabel_1_1);
  770. param2 = new JTextField();
  771. param2.setHorizontalAlignment(SwingConstants.CENTER);
  772. param2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  773. param2.setText("0");
  774. param2.setEditable(false);
  775. param2.setColumns(10);
  776. // param2.setBounds(288, 285, 83, 34);
  777. param2.setBounds(269, 285, 83, 34);
  778. indexPanelA.add(param2);
  779. JLabel lblB = new JLabel("B\u67AA");
  780. lblB.setHorizontalAlignment(SwingConstants.CENTER);
  781. lblB.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  782. lblB.setBounds(525, 186, 166, 28);
  783. // indexPanelA.add(lblB);
  784. JLabel lblNewLabel_1_2 = new JLabel("\u9884\u8BBE\u6570\u91CF");
  785. lblNewLabel_1_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  786. lblNewLabel_1_2.setBounds(525, 241, 83, 34);
  787. // indexPanelA.add(lblNewLabel_1_2);
  788. param3 = new JTextField();
  789. param3.setText("0");
  790. param3.setHorizontalAlignment(SwingConstants.CENTER);
  791. param3.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  792. param3.setEditable(false);
  793. param3.setColumns(10);
  794. param3.setBounds(608, 241, 83, 34);
  795. indexPanelA.add(param3);
  796. MesClient.param3.setVisible(false);
  797. JLabel lblNewLabel_1_1_1 = new JLabel("\u5B8C\u6210\u6570\u91CF");
  798. lblNewLabel_1_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  799. lblNewLabel_1_1_1.setBounds(525, 285, 83, 34);
  800. // indexPanelA.add(lblNewLabel_1_1_1);
  801. param4 = new JTextField();
  802. param4.setText("0");
  803. param4.setHorizontalAlignment(SwingConstants.CENTER);
  804. param4.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  805. param4.setEditable(false);
  806. param4.setColumns(10);
  807. param4.setBounds(608, 285, 83, 34);
  808. indexPanelA.add(param4);
  809. MesClient.param4.setVisible(false);
  810. resetResultA = new JButton("重拉");
  811. resetResultA.addActionListener(new ActionListener() {
  812. public void actionPerformed(ActionEvent e) {
  813. log.info("【A枪操作】点击了 [重拉] 按钮,当前产品SN: {}", product_sn.getText());
  814. if(zdStatusA == 1){
  815. log.info("【NG审核检查】准备校验 A枪 NG中断数据, SN: {}", product_sn.getText());
  816. JSONObject retObj = DataUtil.checkNgData(product_sn.getText(),"A");
  817. log.info("【NG审核检查】校验返回结果: {}", retObj != null ? retObj.toJSONString() : "null");
  818. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  819. MesClient.zdStatusA = 0;
  820. MesClient.resetResultA.setEnabled(false);
  821. MesClient.okResultA.setEnabled(false);
  822. MesClient.ngResultA.setEnabled(false);
  823. MesClient.sortA = (short) (MesClient.sortA - 1);
  824. MesClient.param2.setText(MesClient.sortA+"");
  825. plcA.writeInt16(1136,MesClient.sortA);
  826. MesClient.foutA = "";
  827. MesClient.soutA = "";
  828. MesClient.fmaxA = "";
  829. MesClient.fminA = "";
  830. MesClient.smaxA = "";
  831. MesClient.sminA = "";
  832. MesClient.curA = "";
  833. ModbusUtil.setPowerOn(plcA);
  834. }else{
  835. MesClient.setMenuStatus("NG中断未审核",-1);
  836. }
  837. }
  838. }
  839. });
  840. resetResultA.setEnabled(false);
  841. resetResultA.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  842. resetResultA.setBounds(508, 285, 100, 40);
  843. indexPanelA.add(resetResultA);
  844. okResultA = new JButton("OK");
  845. okResultA.addActionListener(new ActionListener() {
  846. public void actionPerformed(ActionEvent e) {
  847. log.info("【A枪操作】点击了 [OK] 按钮,当前产品SN: {}", product_sn.getText());
  848. if(zdStatusA == 1){
  849. log.info("【NG审核检查】准备校验 A枪 NG中断数据, SN: {}", product_sn.getText());
  850. JSONObject retObj = DataUtil.checkNgData(product_sn.getText(),"A");
  851. log.info("【NG审核检查】校验返回结果: {}", retObj != null ? retObj.toJSONString() : "null");
  852. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  853. if(!MesClient.product_sn.getText().isEmpty()){
  854. MesClient.aFinish++;
  855. JdbcUtils.insertProdData(MesClient.mes_gw, MesClient.mes_line_sn, MesClient.product_sn.getText(),"A",foutA,soutA,fminA,sminA,fmaxA,smaxA,"1",curA+"", MesClient.user_menu.getText());
  856. }
  857. MesClient.zdStatusA = 0;
  858. MesClient.resetResultA.setEnabled(false);
  859. MesClient.okResultA.setEnabled(false);
  860. MesClient.ngResultA.setEnabled(false);
  861. MesClient.foutA = "";
  862. MesClient.soutA = "";
  863. MesClient.fmaxA = "";
  864. MesClient.fminA = "";
  865. MesClient.smaxA = "";
  866. MesClient.sminA = "";
  867. MesClient.curA = "";
  868. ModbusUtil.upResult();
  869. ModbusUtil.setPowerOn(plcA);
  870. }else{
  871. MesClient.setMenuStatus("NG中断未审核",-1);
  872. }
  873. }
  874. }
  875. });
  876. okResultA.setEnabled(false);
  877. okResultA.setForeground(new Color(0, 128, 0));
  878. okResultA.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  879. okResultA.setBounds(632, 285, 100, 40);
  880. indexPanelA.add(okResultA);
  881. ngResultA = new JButton("NG");
  882. ngResultA.addActionListener(new ActionListener() {
  883. public void actionPerformed(ActionEvent e) {
  884. log.info("【A枪操作】点击了 [NG] 按钮,当前产品SN: {}", product_sn.getText());
  885. if(zdStatusA == 1){
  886. log.info("【NG审核检查】准备校验 A枪 NG中断数据, SN: {}", product_sn.getText());
  887. JSONObject retObj = DataUtil.checkNgData(product_sn.getText(),"A");
  888. log.info("【NG审核检查】校验返回结果: {}", retObj != null ? retObj.toJSONString() : "null");
  889. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  890. if(!MesClient.product_sn.getText().isEmpty()){
  891. JdbcUtils.insertProdData(MesClient.mes_gw, MesClient.mes_line_sn, MesClient.product_sn.getText(),"A",foutA,soutA,fminA,sminA,fmaxA,smaxA,"0",curA+"", MesClient.user_menu.getText());
  892. }
  893. MesClient.zdStatusA = 0;
  894. MesClient.resetResultA.setEnabled(false);
  895. MesClient.okResultA.setEnabled(false);
  896. MesClient.ngResultA.setEnabled(false);
  897. MesClient.foutA = "";
  898. MesClient.soutA = "";
  899. MesClient.fmaxA = "";
  900. MesClient.fminA = "";
  901. MesClient.smaxA = "";
  902. MesClient.sminA = "";
  903. MesClient.curA = "";
  904. ModbusUtil.upResult();
  905. ModbusUtil.setPowerOn(plcA);
  906. }else{
  907. MesClient.setMenuStatus("NG中断未审核",-1);
  908. }
  909. }
  910. }
  911. });
  912. ngResultA.setEnabled(false);
  913. ngResultA.setForeground(new Color(255, 0, 0));
  914. ngResultA.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  915. ngResultA.setBounds(759, 285, 100, 40);
  916. indexPanelA.add(ngResultA);
  917. fxMode = new JButton("返修模式");
  918. fxMode.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  919. fxMode.setBounds(161, 10, 107, 40);
  920. indexPanelA.add(fxMode);
  921. fxMode.addActionListener(new ActionListener() {
  922. public void actionPerformed(ActionEvent e) {
  923. fxModeVal = !fxModeVal;
  924. if(fxModeVal){
  925. fxMode.setBackground(Color.GREEN);
  926. fxMode.setForeground(Color.WHITE);
  927. fxMaxNum.setText("");
  928. fxMaxNum.setEditable(true);
  929. fxMaxBtn.setEnabled(true);
  930. }else{
  931. fxMode.setBackground(null);
  932. fxMode.setForeground(null);
  933. fxMaxNum.setText("");
  934. fxMaxNum.setEditable(false);
  935. fxMaxBtn.setEnabled(false);
  936. }
  937. }
  938. });
  939. fxMaxNum = new JTextField();
  940. fxMaxNum.setBounds(286, 10, 83, 40);
  941. indexPanelA.add(fxMaxNum);
  942. fxMaxNum.setColumns(10);
  943. fxMaxBtn = new JButton("确定数量");
  944. fxMaxBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  945. fxMaxBtn.setBounds(379, 10, 107, 40);
  946. indexPanelA.add(fxMaxBtn);
  947. fxMaxBtn.addActionListener(new ActionListener() {
  948. public void actionPerformed(ActionEvent e) {
  949. int maxNum = Integer.parseInt(fxMaxNum.getText());
  950. if(maxNum > 0){
  951. ModbusUtil.setTask(MesClient.plcA,(short)maxNum);
  952. }
  953. }
  954. });
  955. tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null);
  956. tabbedPane.setEnabledAt(0, true);
  957. // searchScrollPane = new JScrollPane((Component) null);
  958. indexPanelC = new JPanel();
  959. searchScrollPaneDj = new JScrollPane(indexPanelC);
  960. indexPanelC.setLayout(null);
  961. tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null);
  962. indexPanelB = new JPanel();
  963. searchScrollPane = new JScrollPane(indexPanelB);
  964. indexPanelB.setLayout(null);
  965. // tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
  966. tabbedPane.addChangeListener(new ChangeListener() {
  967. @Override
  968. public void stateChanged(ChangeEvent e) {
  969. JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
  970. int selectedIndex = tabbedPane.getSelectedIndex();
  971. System.out.println("selectedIndex:"+selectedIndex);
  972. // if(selectedIndex == 1){
  973. //
  974. // }
  975. // 创建一个单线程的ExecutorService
  976. ExecutorService executorService = Executors.newSingleThreadExecutor();
  977. // 提交一个异步任务
  978. executorService.submit(() -> {
  979. System.out.println("异步线程正在运行...");
  980. // 在这里执行异步操作
  981. // getCheckResult();
  982. });
  983. // 关闭ExecutorService,以优雅的方式
  984. executorService.shutdown();
  985. }
  986. });
  987. }
  988. public static void setMenuStatus(String msg,int error){
  989. if(error == 0){
  990. MesClient.status_menu.setForeground(Color.GREEN);
  991. }else{
  992. MesClient.status_menu.setForeground(Color.RED);
  993. }
  994. MesClient.status_menu.setText(msg);
  995. }
  996. // 获取开班点检状态
  997. public static void getCheckResult(){
  998. try{
  999. log.info("【开班点检】准备获取点检状态...");
  1000. JSONObject retObj = DataUtil.getCheckResult();
  1001. log.info("【开班点检】获取结果: {}", retObj != null ? retObj.toJSONString() : "null");
  1002. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { // 已点检
  1003. MesClient.checkState = true;
  1004. }else{ // 未点检
  1005. MesClient.checkState = false;
  1006. }
  1007. }catch (Exception e){
  1008. log.error("【开班点检】获取点检状态异常: ", e);
  1009. MesClient.checkState = false;
  1010. }
  1011. MesClient.formatDeviceState();
  1012. }
  1013. // 格式化设备启动状态
  1014. public static void formatDeviceState(){
  1015. try{
  1016. if(MesClient.checkState){ // 检查物料是否已绑定
  1017. if(rowData == null){
  1018. System.out.println("openDevice1");
  1019. ModbusRtu.openDevice(serialPort);
  1020. }else{
  1021. // 判断物料是否可启动
  1022. int flag = 0;
  1023. int i = 0;
  1024. for (Object object:rowData){
  1025. Integer times = Integer.valueOf(String.valueOf(rowData[i][2]));
  1026. if(times < 1){
  1027. flag = 1;
  1028. }
  1029. i++;
  1030. }
  1031. if(flag == 1){ // 不能启动
  1032. System.out.println("closeDevice2");
  1033. ModbusRtu.closeDevice(serialPort);
  1034. }else{
  1035. System.out.println("openDevice2");
  1036. // 可启动
  1037. ModbusRtu.openDevice(serialPort);
  1038. }
  1039. }
  1040. }else{
  1041. System.out.println("closeDevice1");
  1042. //不能启动
  1043. ModbusRtu.closeDevice(serialPort);
  1044. }
  1045. }catch (Exception e){
  1046. e.printStackTrace();
  1047. }
  1048. }
  1049. public static void getMaterailData(){
  1050. JSONObject retObj = DataUtil.getBindMaterail();
  1051. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  1052. java.util.List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  1053. int i = 0;
  1054. rowData = new Object[arrs.size()][7];
  1055. for (BindMaterialResp bindMaterialResp:arrs){
  1056. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  1057. rowData[i][1] = bindMaterialResp.getBatchSn();
  1058. rowData[i][2] = bindMaterialResp.getLastTimes();
  1059. rowData[i][3] = "";
  1060. rowData[i][4] = bindMaterialResp.getCraft();
  1061. rowData[i][5] = bindMaterialResp.getMaterialId();
  1062. rowData[i][6] = bindMaterialResp.getType();
  1063. i++;
  1064. }
  1065. bindBatchPanel();
  1066. }
  1067. }
  1068. public static void updateMaterailData(){
  1069. JSONObject retObj = DataUtil.getBindMaterail();
  1070. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  1071. List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  1072. int i = 0;
  1073. for (BindMaterialResp bindMaterialResp:arrs){
  1074. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  1075. rowData[i][1] = bindMaterialResp.getBatchSn();
  1076. rowData[i][2] = bindMaterialResp.getLastTimes();
  1077. rowData[i][3] = "";
  1078. rowData[i][4] = bindMaterialResp.getCraft();
  1079. rowData[i][5] = bindMaterialResp.getMaterialId();
  1080. rowData[i][6] = bindMaterialResp.getType();
  1081. i++;
  1082. }
  1083. getCheckResult();
  1084. MesClient.table.repaint();
  1085. }
  1086. }
  1087. // 绑定物料批次码
  1088. public static void scanBatchSn(BindMaterialResp bindMaterialResp) {
  1089. //弹窗扫工件码
  1090. String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle();
  1091. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  1092. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  1093. log.info("【物料绑定】扫描物料: {}, 批次码: {}", bindMaterialResp.getMaterialTitle(), scanBarcode);
  1094. JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType());
  1095. log.info("【物料绑定】返回结果: {}", retObj != null ? retObj.toJSONString() : "null");
  1096. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  1097. MesClient.setMenuStatus("扫物料:"+bindMaterialResp.getMaterialTitle()+"成功",0);
  1098. updateMaterailData();
  1099. }else{
  1100. if(retObj.get("result")==null){
  1101. MesClient.setMenuStatus("请求失败,请重试",-1);
  1102. }else{
  1103. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  1104. MesClient.setMenuStatus(retObj.getString("message"),-1);
  1105. }
  1106. }
  1107. }
  1108. }
  1109. }
  1110. public static void bindBatchPanel(){
  1111. JPanel indexPanelBB = new JPanel();
  1112. JPanel panel = new JPanel();
  1113. panel.setBounds(0, 0, 990, 550);
  1114. // panel.setBounds(81, 50, 810, 479);
  1115. indexPanelBB.add(panel);
  1116. panel.setLayout(new GridLayout(0, 1, 0, 0));
  1117. table = new JTable(rowData, columnNames){
  1118. public boolean isCellEditable(int row, int column) {
  1119. if(column == 3){
  1120. return true;
  1121. }
  1122. return false;
  1123. }
  1124. };
  1125. table.setRowHeight(40);
  1126. table.setEnabled(true);
  1127. table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  1128. table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton());
  1129. table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton());
  1130. JScrollPane scrollPane = new JScrollPane(table);
  1131. panel.add(scrollPane);
  1132. JScrollPane indexScrollPaneB = new JScrollPane(indexPanelBB);
  1133. indexPanelBB.setLayout(null);
  1134. tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null);
  1135. }
  1136. public static void scanBarcodeLb() {
  1137. String scanBarcodeTitle = "";
  1138. product_sn_lb.setText("");
  1139. // resetLb();
  1140. scanBarcodeTitle = "请扫冷板码";
  1141. //弹窗扫工件码
  1142. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  1143. System.out.println("冷板码="+scanBarcode);
  1144. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  1145. //获取用户
  1146. getUser();
  1147. //获取扫码内容36位
  1148. String warehouseSn = getBarcode(scanBarcode);//处理36为码
  1149. //工位号
  1150. String gw = "";
  1151. product_sn_lb.setText(scanBarcode);
  1152. //刷新界面
  1153. mesClientFrame.repaint();
  1154. if(!scanBarcode.isEmpty()) {
  1155. // 检查工件质量
  1156. if(work_status == 1) {
  1157. // 直接调用绑定
  1158. String sn = getBarcode(product_sn.getText());
  1159. getUser();
  1160. log.info("【冷板绑定】准备调用绑定接口, SN: {}, 冷板码: {}", sn, warehouseSn);
  1161. JSONObject retObj = DataUtil.bindWarehouse(sn, warehouseSn, user20, "400006");
  1162. log.info("【冷板绑定】绑定返回结果: {}", retObj != null ? retObj.toJSONString() : "null");
  1163. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  1164. MesClient.setMenuStatus("绑定成功",0);
  1165. }else{
  1166. if(retObj.get("result")==null){
  1167. MesClient.setMenuStatus("请求失败,请重试",-1);
  1168. }else{
  1169. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  1170. MesClient.setMenuStatus(retObj.getString("message"),-1);
  1171. }
  1172. }
  1173. }
  1174. }else{
  1175. setMenuStatus("请先扫工件码",1);
  1176. return;
  1177. }
  1178. }else {
  1179. setMenuStatus("冷板码为空,请重新扫码",1);
  1180. return;
  1181. }
  1182. }else {
  1183. setMenuStatus("冷板码为空,请重新扫码",1);
  1184. return;
  1185. }
  1186. }
  1187. }