MesClient.java 37 KB

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