MesClient.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.fazecast.jSerialComm.SerialPort;
  4. import com.mes.component.MesRadio;
  5. import com.mes.component.MesWebView;
  6. import com.mes.netty.NettyClient;
  7. import com.mes.util.DateLocalUtils;
  8. import com.mes.util.HttpUtils;
  9. import com.mes.util.JdbcUtils;
  10. import com.mes.util.ModbusRtu;
  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.sql.ResultSet;
  28. import java.sql.SQLException;
  29. import java.sql.Statement;
  30. import java.time.LocalDateTime;
  31. import java.util.*;
  32. import java.util.List;
  33. import java.util.Timer;
  34. public class MesClient extends JFrame {
  35. public static final Logger log = LoggerFactory.getLogger(MesClient.class);
  36. private static String Drivde = "org.sqlite.JDBC";
  37. public static int mes_auth = 0; // 权限级别 0=无权限 1=操作工人 2=管理员
  38. public static String mes_gw = ""; // 工位号
  39. public static String mes_gw_des = ""; // 工位名称
  40. public static String mes_server_ip = ""; // 服务器IP地址
  41. public static int mes_tcp_port = 3000; // TCP服务端口
  42. public static int mes_heart_beat_cycle = 10; // 心跳周期
  43. public static int mes_heart_icon_cycle = 1;
  44. public static String mes_line_sn = ""; // 产线编号
  45. //TCP连接
  46. public static NettyClient nettyClient;
  47. //TCP连接状态
  48. public static boolean tcp_connect_flag = false;
  49. //TCP连接请求
  50. public static boolean connect_request_flag = false;
  51. //session
  52. public static String sessionid = "";
  53. public static JPanel contentPane;
  54. public static MesClient mesClientFrame;
  55. public static JTabbedPane tabbedPane;
  56. public static JScrollPane indexScrollPaneA;
  57. public static JScrollPane searchScrollPane;
  58. public static JScrollPane searchScrollPaneD;
  59. public static JScrollPane searchScrollPaneDj;
  60. public static JScrollPane searchScrollPaneJs;
  61. public static JPanel indexPanelJs;
  62. public static MesWebView jfxPanelJs = null;
  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 String user20 = "";
  74. public static JFrame welcomeWin;
  75. public static JPanel indexPanelB;
  76. public static MesWebView jfxPanel = null;
  77. public static JPanel indexPanelC;
  78. public static JPanel indexPanelD;
  79. public static MesWebView jfxPanel2 = null;
  80. public static MesWebView jfxPanel3 = 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 SerialPort serialPort; // 串口对象
  86. public static void main(String[] args) {
  87. EventQueue.invokeLater(new Runnable() {
  88. @Override
  89. public void run() {
  90. try{
  91. //读文件配置
  92. readProperty();
  93. // 显示界面
  94. mesClientFrame = new MesClient();
  95. mesClientFrame.setVisible(false);
  96. JdbcUtils.getConn();
  97. welcomeWin = new LoginFarme();
  98. welcomeWin.setVisible(true);
  99. getMaterailData();
  100. serialPort = ModbusRtu.connect();
  101. // startJsTimer();
  102. startUpdateQualityTimer(); // 开启提交结果的定时任务
  103. }catch (Exception e){
  104. log.error(e.getMessage());
  105. }
  106. }
  107. });
  108. }
  109. //读配置文件
  110. private static void readProperty() throws IOException{
  111. String enconding = "UTF-8";
  112. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  113. Properties pro = new Properties();
  114. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  115. pro.load(br);
  116. mes_gw = pro.getProperty("mes.gw");
  117. // mes_gw_des = pro.getProperty("mes.gw_des");
  118. mes_server_ip = pro.getProperty("mes.server_ip");
  119. mes_tcp_port = Integer.parseInt(pro.getProperty("mes.tcp_port"));
  120. mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle"));
  121. mes_line_sn = pro.getProperty("mes.line_sn");
  122. mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
  123. log.info(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle);
  124. }
  125. // 初始化TCP
  126. public static void initTcpConnection() {
  127. try {
  128. if(nettyClient==null) {
  129. //初始化TCP连接
  130. nettyClient = new NettyClient();
  131. //TCP连接状态
  132. tcp_connect_flag = false;
  133. //设置TCP请求状态
  134. connect_request_flag = true;
  135. DataUtil.synrTcp(nettyClient,mes_gw);
  136. }
  137. } catch (Exception e) {
  138. // TODO Auto-generated catch block
  139. e.printStackTrace();
  140. }
  141. }
  142. public static java.util.Timer jsTimer;
  143. public static Integer jsCount = 0;
  144. public static Integer jsFlag = 0; // 0=未关闭 1=已关闭
  145. public static void startJsTimer() { // 定时关闭继电器
  146. if(jsTimer!=null) {
  147. jsTimer.cancel();
  148. }
  149. jsTimer = new java.util.Timer();
  150. jsTimer.schedule(new TimerTask() {
  151. public void run() {
  152. if(work_status == 1) {
  153. jsCount++;
  154. if(jsCount > 2*60 && jsFlag == 0){
  155. ModbusRtu.closeDevice(serialPort);
  156. jsFlag = 1;
  157. }
  158. }
  159. }
  160. }, 100,1000);
  161. //心跳显示图标
  162. if(heartBeatIconTimer!=null) {
  163. heartBeatIconTimer.cancel();
  164. }
  165. heartBeatIconTimer = new Timer();
  166. heartBeatIconTimer.schedule(new TimerTask() {
  167. public void run() {
  168. if(tcp_connect_flag) {
  169. if(iconREDFlag) {
  170. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  171. iconREDFlag = false;
  172. }else {
  173. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  174. iconREDFlag = true;
  175. }
  176. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  177. heart_beat_menu.repaint();
  178. }else {
  179. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  180. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  181. heart_beat_menu.repaint();
  182. //若未连接则尝试连接
  183. if(nettyClient!=null&&!tcp_connect_flag){
  184. System.out.println("TCP已断开");
  185. //TCP重新同步连接
  186. if(!connect_request_flag) {
  187. //System.out.println("TCP重新同步连接");
  188. //设置TCP请求状态,只重新同步连接一次
  189. connect_request_flag = true;
  190. DataUtil.synrTcp(nettyClient,mes_gw);
  191. }
  192. }
  193. }
  194. }
  195. }, 100,mes_heart_icon_cycle*1000);
  196. }
  197. //启动心跳包程序
  198. public static java.util.Timer heartBeatTimer;
  199. public static java.util.Timer heartBeatIconTimer;
  200. public static boolean iconREDFlag = true;
  201. public static void startHeartBeatTimer() {
  202. if(heartBeatTimer!=null) {
  203. heartBeatTimer.cancel();
  204. }
  205. heartBeatTimer = new java.util.Timer();
  206. heartBeatTimer.schedule(new TimerTask() {
  207. public void run() {
  208. if(nettyClient!=null&&tcp_connect_flag) {
  209. //log.info("发送心跳报文");
  210. DataUtil.heartBeat(nettyClient,mes_gw);
  211. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  212. }
  213. }
  214. }, 100,mes_heart_beat_cycle*1000);
  215. //心跳显示图标
  216. if(heartBeatIconTimer!=null) {
  217. heartBeatIconTimer.cancel();
  218. }
  219. heartBeatIconTimer = new Timer();
  220. heartBeatIconTimer.schedule(new TimerTask() {
  221. public void run() {
  222. if(tcp_connect_flag) {
  223. if(iconREDFlag) {
  224. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  225. iconREDFlag = false;
  226. }else {
  227. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  228. iconREDFlag = true;
  229. }
  230. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  231. heart_beat_menu.repaint();
  232. }else {
  233. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  234. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  235. heart_beat_menu.repaint();
  236. //若未连接则尝试连接
  237. if(nettyClient!=null&&!tcp_connect_flag){
  238. log.info("TCP已断开");
  239. //TCP重新同步连接
  240. if(!connect_request_flag) {
  241. //log.info("TCP重新同步连接");
  242. //设置TCP请求状态,只重新同步连接一次
  243. connect_request_flag = true;
  244. DataUtil.synrTcp(nettyClient,mes_gw);
  245. }
  246. }
  247. }
  248. }
  249. }, 100,mes_heart_icon_cycle*1000);
  250. }
  251. //设置tcp连接状态显示
  252. public static void setTcpStatus() {
  253. if(tcp_connect_flag) {
  254. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  255. heart_beat_menu.repaint();
  256. }else {
  257. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  258. heart_beat_menu.repaint();
  259. }
  260. }
  261. public static void initWarehouseData(){
  262. resetScanA();
  263. }
  264. public static void resetScanA() {
  265. work_status = 0;
  266. check_quality_result = false;
  267. MesClient.finish_ok_bt.setEnabled(false);
  268. MesClient.finish_ng_bt.setEnabled(false);
  269. product_sn.setText("");
  270. MesClient.f_scan_data_bt_1.setEnabled(true);
  271. MesClient.setMenuStatus("请扫工件码",0);
  272. //继电器
  273. MesClient.jsFlag = 0;
  274. MesClient.jsCount = 0;
  275. try {
  276. ModbusRtu.closeDevice(serialPort);
  277. } catch (Exception e) {
  278. e.printStackTrace();
  279. }
  280. updateMaterailData();
  281. // shiftUserCheck();
  282. }
  283. public static int userLoginHours;//用户登录所处小时
  284. //换班用户信息检查
  285. private static void shiftUserCheck() {
  286. LocalDateTime now = LocalDateTime.now();
  287. // 判断时间范围
  288. if (userLoginHours >= 8 && userLoginHours < 20) {
  289. int hour = now.getHour();
  290. if (hour >= 20 || hour < 8) {
  291. logoff();
  292. }
  293. } else {
  294. int hour = now.getHour();
  295. if (hour >= 8 && hour < 20) {
  296. logoff();
  297. }
  298. }
  299. }
  300. public static void resetScanB() {
  301. }
  302. //获取用户20位
  303. public static void getUser() {
  304. user20 = user_menu.getText().toString();
  305. String space_tmp1 = "";
  306. if(user20.length()<20) {
  307. for(int i=0;i<20-user20.length();i++) {
  308. space_tmp1 = space_tmp1 + " ";
  309. }
  310. }
  311. user20 = user20 + space_tmp1;
  312. }
  313. //获取barcode内容36位
  314. public static String getBarcode(String barcodeTmp) {
  315. String barcodeRet = barcodeTmp;
  316. if(barcodeTmp.equalsIgnoreCase("")) {
  317. return "";
  318. }else {
  319. if(barcodeTmp.length()<36) {
  320. String space = "";
  321. for(int i=0;i<36-barcodeTmp.length();i++) {
  322. space = space + " ";
  323. }
  324. barcodeRet = barcodeTmp + space;
  325. }
  326. }
  327. return barcodeRet;
  328. }
  329. public static void scanBarcode() {
  330. if(work_status == 1){
  331. JOptionPane.showMessageDialog(mesClientFrame,"工作中,勿扫码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  332. return;
  333. }
  334. String scanBarcodeTitle = "";
  335. switch(scan_type) {
  336. case 1:
  337. product_sn.setText("");
  338. scanBarcodeTitle = "请扫工件码";
  339. break;
  340. }
  341. //弹窗扫工件码
  342. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  343. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  344. //获取用户
  345. getUser();
  346. //获取扫码内容36位
  347. String barcode36 = getBarcode(scanBarcode);//处理36为码
  348. //工位号
  349. String gw = "";
  350. switch(scan_type) {
  351. case 1:
  352. product_sn.setText(scanBarcode);
  353. break;
  354. }
  355. //刷新界面
  356. mesClientFrame.repaint();
  357. if(!tcp_connect_flag) {
  358. JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  359. return;
  360. }
  361. // 查询工件质量
  362. Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20);
  363. if(!sendret){
  364. JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  365. return;
  366. }
  367. }else {
  368. JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  369. return;
  370. }
  371. }
  372. public static void logoff() {
  373. // welcomeWin.setVisible(true);
  374. mesClientFrame.setVisible(false);
  375. //退出程序 退出登录
  376. System.exit(0);
  377. nettyClient = null;
  378. // if(heartBeatTimer!=null) {
  379. // heartBeatTimer.cancel();
  380. // }
  381. // if(heartBeatIconTimer!=null) {
  382. // heartBeatIconTimer.cancel();
  383. // }
  384. tcp_connect_flag = false;
  385. connect_request_flag = false;
  386. }
  387. public MesClient() {
  388. setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png")));
  389. setTitle("MES系统客户端:"+mes_gw + "- " + mes_gw_des);
  390. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  391. setBounds(0, 0, 1024, 768);
  392. JMenuBar menuBar = new JMenuBar();
  393. menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26));
  394. setJMenuBar(menuBar);
  395. JMenu fileMenu = new JMenu("用户");
  396. fileMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png")));
  397. fileMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  398. menuBar.add(fileMenu);
  399. JMenuItem exitMenuItem = new JMenuItem("退出");
  400. exitMenuItem.setIcon(new ImageIcon(MesClient.class.getResource("/bg/logoff.png")));
  401. exitMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  402. fileMenu.add(exitMenuItem);
  403. exitMenuItem.addMouseListener(new MouseAdapter() {
  404. @Override
  405. public void mousePressed(MouseEvent e) {//只能检测到mousePressed事件
  406. super.mouseClicked(e);
  407. //dispose();
  408. logoff();
  409. }
  410. });
  411. JMenu settingMenu = new JMenu("设置");
  412. //settingMenu.setVisible(false);
  413. settingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
  414. settingMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  415. menuBar.add(settingMenu);
  416. JMenuItem resetTcpMenu = new JMenuItem("重连MES");
  417. resetTcpMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  418. resetTcpMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  419. resetTcpMenu.addMouseListener(new MouseAdapter() {
  420. @Override
  421. public void mousePressed(MouseEvent e) {
  422. super.mouseClicked(e);
  423. //重连mes
  424. nettyClient.future.channel().close();
  425. }
  426. });
  427. settingMenu.add(resetTcpMenu);
  428. JMenuItem resetTcpMenu_1 = new JMenuItem("刷新工件");
  429. resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  430. resetTcpMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  431. resetTcpMenu_1.addMouseListener(new MouseAdapter() {
  432. @Override
  433. public void mousePressed(MouseEvent e) {
  434. super.mouseClicked(e);
  435. resetScanA();
  436. }
  437. });
  438. settingMenu.add(resetTcpMenu_1);
  439. JMenuItem reset1 = new JMenuItem("MES屏蔽");
  440. reset1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  441. reset1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  442. reset1.addMouseListener(new MouseAdapter() {
  443. @Override
  444. public void mousePressed(MouseEvent e) {
  445. super.mouseClicked(e);
  446. ModbusRtu.openDevice(MesClient.serialPort);
  447. setMenuStatus("MES屏蔽成功",0);
  448. }
  449. });
  450. settingMenu.add(reset1);
  451. contentPane = new JPanel();
  452. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  453. setContentPane(contentPane);
  454. contentPane.setLayout(new BorderLayout(0, 0));
  455. JToolBar toolBar = new JToolBar();
  456. contentPane.add(toolBar, BorderLayout.NORTH);
  457. JLabel equipment_statu_label = new JLabel("状态:");
  458. equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER);
  459. equipment_statu_label.setForeground(Color.BLACK);
  460. equipment_statu_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  461. equipment_statu_label.setBackground(Color.LIGHT_GRAY);
  462. toolBar.add(equipment_statu_label);
  463. status_menu = new JButton("设备未连接MES服务器");
  464. if(tcp_connect_flag) {
  465. MesClient.setMenuStatus("已连接MES服务器",0);
  466. status_menu.setForeground(Color.GREEN);
  467. }else {
  468. MesClient.setMenuStatus("未连接MES服务器",-1);
  469. status_menu.setForeground(Color.DARK_GRAY);
  470. }
  471. status_menu.addActionListener(new ActionListener() {
  472. public void actionPerformed(ActionEvent e) {
  473. }
  474. });
  475. status_menu.setForeground(Color.GREEN);
  476. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  477. status_menu.setBackground(Color.BLACK);
  478. toolBar.add(status_menu);
  479. JLabel space_1 = new JLabel(" ");
  480. toolBar.add(space_1);
  481. JLabel heart_beat_status_label = new JLabel("心跳:");
  482. heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  483. heart_beat_status_label.setForeground(Color.BLACK);
  484. heart_beat_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  485. heart_beat_status_label.setBackground(Color.LIGHT_GRAY);
  486. toolBar.add(heart_beat_status_label);
  487. heart_beat_menu = new JButton("");
  488. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  489. heart_beat_menu.setForeground(Color.GREEN);
  490. heart_beat_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  491. heart_beat_menu.setBackground(Color.BLACK);
  492. toolBar.add(heart_beat_menu);
  493. JLabel space_2 = new JLabel(" ");
  494. toolBar.add(space_2);
  495. JLabel user_status_label = new JLabel("登录用户:");
  496. user_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  497. user_status_label.setForeground(Color.BLACK);
  498. user_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  499. user_status_label.setBackground(Color.LIGHT_GRAY);
  500. toolBar.add(user_status_label);
  501. user_menu = new JButton("");
  502. user_menu.setForeground(Color.GREEN);
  503. user_menu.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  504. user_menu.setBackground(Color.BLACK);
  505. toolBar.add(user_menu);
  506. JLabel space_3 = new JLabel(" ");
  507. toolBar.add(space_3);
  508. JLabel space_4 = new JLabel(" ");
  509. toolBar.add(space_4);
  510. tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  511. tabbedPane.setMinimumSize(new Dimension(400, 50));
  512. tabbedPane.setFont(new Font("宋体", Font.BOLD, 22));
  513. contentPane.add(tabbedPane);
  514. //首页
  515. JPanel indexPanelA = new JPanel();
  516. indexScrollPaneA = new JScrollPane(indexPanelA);
  517. indexPanelA.setLayout(null);
  518. product_sn = new JTextField();
  519. product_sn.setHorizontalAlignment(SwingConstants.CENTER);
  520. product_sn.setEditable(false);
  521. product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  522. product_sn.setBounds(81, 70, 602, 70);
  523. indexPanelA.add(product_sn);
  524. product_sn.setColumns(10);
  525. f_scan_data_bt_1 = new JButton("扫码");
  526. f_scan_data_bt_1.addActionListener(new ActionListener() {
  527. public void actionPerformed(ActionEvent e) {
  528. scan_type = 1;
  529. scanBarcode();
  530. }
  531. });
  532. f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  533. f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  534. f_scan_data_bt_1.setBounds(693, 70, 198, 70);
  535. indexPanelA.add(f_scan_data_bt_1);
  536. finish_ok_bt = new JButton("OK");
  537. finish_ok_bt.setEnabled(false);
  538. finish_ok_bt.addActionListener(new ActionListener() {
  539. public void actionPerformed(ActionEvent e) {
  540. if(work_status == 1 && check_quality_result){
  541. String sn = getBarcode(product_sn.getText());
  542. getUser();
  543. String qret = "OK";
  544. Boolean result = DataUtil.sendQuality(sn,qret,user20);
  545. if(result) {
  546. MesClient.resetScanA();
  547. MesClient.setMenuStatus("结果提交成功,请扫下一件", 0);
  548. MesClient.scan_type = 1;
  549. MesClient.scanBarcode();
  550. } else {
  551. MesClient.setMenuStatus("结果提交失败,请重试",-1);
  552. }
  553. }
  554. }
  555. });
  556. finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
  557. finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  558. finish_ok_bt.setBounds(185, 291, 240, 80);
  559. finish_ok_bt.setEnabled(false);
  560. indexPanelA.add(finish_ok_bt);
  561. finish_ng_bt = new JButton("NG");
  562. finish_ng_bt.setEnabled(false);
  563. finish_ng_bt.addActionListener(new ActionListener() {
  564. public void actionPerformed(ActionEvent e) {
  565. if(work_status == 1 && check_quality_result){
  566. String sn = getBarcode(product_sn.getText());
  567. getUser();
  568. String qret = "NG";
  569. Boolean result = DataUtil.sendQuality(sn,qret,user20);
  570. if(result) {
  571. MesClient.resetScanA();
  572. MesClient.setMenuStatus("结果提交成功,请扫下一件", 0);
  573. MesClient.scan_type = 1;
  574. MesClient.scanBarcode();
  575. } else {
  576. MesClient.setMenuStatus("结果提交失败,请重试",-1);
  577. }
  578. }
  579. }
  580. });
  581. finish_ng_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ng_bg.png")));
  582. finish_ng_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  583. finish_ng_bt.setBounds(508, 291, 240, 80);
  584. finish_ng_bt.setEnabled(false);
  585. indexPanelA.add(finish_ng_bt);
  586. tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null);
  587. tabbedPane.setEnabledAt(0, true);
  588. indexPanelC = new JPanel();
  589. searchScrollPaneDj = new JScrollPane(indexPanelC);
  590. indexPanelC.setLayout(null);
  591. tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null);
  592. indexPanelB = new JPanel();
  593. searchScrollPane = new JScrollPane(indexPanelB);
  594. indexPanelB.setLayout(null);
  595. tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
  596. indexPanelJs = new JPanel();
  597. searchScrollPaneJs = new JScrollPane(indexPanelJs);
  598. indexPanelJs.setLayout(null);
  599. // tabbedPane.addTab("胶水寿命", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneJs, null);
  600. indexPanelD = new JPanel();
  601. searchScrollPaneD = new JScrollPane(indexPanelD);
  602. indexPanelD.setLayout(null);
  603. // tabbedPane.addTab("工具寿命", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneD, null);
  604. tabbedPane.addChangeListener(new ChangeListener() {
  605. @Override
  606. public void stateChanged(ChangeEvent e) {
  607. JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
  608. int selectedIndex = tabbedPane.getSelectedIndex();
  609. if(selectedIndex == 1){
  610. }
  611. }
  612. });
  613. }
  614. public static void setMenuStatus(String msg,int error){
  615. if(error == 0){
  616. MesClient.status_menu.setForeground(Color.GREEN);
  617. }else{
  618. MesClient.status_menu.setForeground(Color.RED);
  619. }
  620. MesClient.status_menu.setText(msg);
  621. }
  622. public static void getMaterailData(){
  623. JSONObject retObj = DataUtil.getBindMaterail();
  624. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  625. java.util.List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  626. int i = 0;
  627. rowData = new Object[arrs.size()][7];
  628. for (BindMaterialResp bindMaterialResp:arrs){
  629. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  630. rowData[i][1] = bindMaterialResp.getBatchSn();
  631. rowData[i][2] = bindMaterialResp.getLastTimes();
  632. rowData[i][3] = "";
  633. rowData[i][4] = bindMaterialResp.getCraft();
  634. rowData[i][5] = bindMaterialResp.getMaterialId();
  635. rowData[i][6] = bindMaterialResp.getType();
  636. i++;
  637. }
  638. bindBatchPanel();
  639. }
  640. }
  641. public static void updateMaterailData(){
  642. JSONObject retObj = DataUtil.getBindMaterail();
  643. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  644. List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  645. int i = 0;
  646. for (BindMaterialResp bindMaterialResp:arrs){
  647. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  648. rowData[i][1] = bindMaterialResp.getBatchSn();
  649. rowData[i][2] = bindMaterialResp.getLastTimes();
  650. rowData[i][3] = "";
  651. rowData[i][4] = bindMaterialResp.getCraft();
  652. rowData[i][5] = bindMaterialResp.getMaterialId();
  653. rowData[i][6] = bindMaterialResp.getType();
  654. i++;
  655. }
  656. MesClient.table.repaint();
  657. }
  658. }
  659. // 绑定物料批次码
  660. public static void scanBatchSn(BindMaterialResp bindMaterialResp) {
  661. //弹窗扫工件码
  662. String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle();
  663. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  664. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  665. JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType());
  666. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  667. MesClient.setMenuStatus("扫物料:"+bindMaterialResp.getMaterialTitle()+"成功",0);
  668. updateMaterailData();
  669. }else{
  670. if(retObj.get("result")==null){
  671. MesClient.setMenuStatus("请求失败,请重试",-1);
  672. }else{
  673. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  674. MesClient.setMenuStatus(retObj.getString("message"),-1);
  675. }
  676. }
  677. }
  678. }
  679. }
  680. public static void bindBatchPanel(){
  681. JPanel indexPanelBB = new JPanel();
  682. JPanel panel = new JPanel();
  683. panel.setBounds(0, 0, 990, 550);
  684. // panel.setBounds(81, 50, 810, 479);
  685. indexPanelBB.add(panel);
  686. panel.setLayout(new GridLayout(0, 1, 0, 0));
  687. table = new JTable(rowData, columnNames){
  688. public boolean isCellEditable(int row, int column) {
  689. if(column == 3){
  690. return true;
  691. }
  692. return false;
  693. }
  694. };
  695. table.setRowHeight(40);
  696. table.setEnabled(true);
  697. table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  698. table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton());
  699. table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton());
  700. JScrollPane scrollPane = new JScrollPane(table);
  701. panel.add(scrollPane);
  702. JScrollPane indexScrollPaneB = new JScrollPane(indexPanelBB);
  703. indexPanelBB.setLayout(null);
  704. tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null);
  705. }
  706. // 开启发送结果定时任务
  707. public static void startUpdateQualityTimer(){
  708. new Timer().schedule(new TimerTask() {
  709. @Override
  710. public void run() {
  711. try {
  712. Statement statement = JdbcUtils.conn.createStatement();
  713. String selectSql = "SELECT id, bw,fail_count FROM submit_record\n" +
  714. "WHERE state = 0 AND fail_status = 0";
  715. String updateSql = "UPDATE submit_record \n" +
  716. "SET state = '1'\n" +
  717. "WHERE id = ";
  718. ResultSet rs = statement.executeQuery(selectSql);
  719. while(rs.next()){
  720. int id = rs.getInt("id");
  721. String bw = rs.getString("bw");
  722. String failCount = rs.getString("fail_count");
  723. log.info("submit_record表取数据: id: " + id + " bw:" + bw);
  724. // 发送请求
  725. String url = "http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesProductRecord/updateQualityByTiming";
  726. String s = HttpUtils.sendPostRequestJson(url, bw );
  727. Boolean result = JSONObject.parseObject(s).getBoolean("result");
  728. if(result) {
  729. // 更改状态为 1
  730. statement.executeUpdate(updateSql + id);
  731. } else {
  732. log.error("定时任务提交失败, 报文: " + bw);
  733. int newFailCount = Integer.parseInt(failCount);
  734. String updateFailSql;
  735. //当提交次数超过10次,则将状态置为失败,不再进行提交
  736. if (newFailCount >= 9) {
  737. updateFailSql = "UPDATE submit_record SET fail_count = fail_count + 1, fail_status = 1 WHERE id = " + id;
  738. } else {
  739. updateFailSql = "UPDATE submit_record SET fail_count = fail_count + 1 WHERE id = " + id;
  740. }
  741. statement.executeUpdate(updateFailSql);
  742. }
  743. }
  744. rs.close();
  745. statement.close();
  746. } catch (SQLException | IOException e) {
  747. // throw new RuntimeException(e);
  748. log.info(e.getMessage());
  749. }
  750. }
  751. },1000, 5000);
  752. }
  753. }