MesClient.java 34 KB

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