MesClient.java 36 KB

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