MesClient.java 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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.device.DeviceStateReporter;
  8. import com.mes.util.*;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import javax.swing.*;
  12. import javax.swing.border.EmptyBorder;
  13. import javax.swing.event.ChangeEvent;
  14. import javax.swing.event.ChangeListener;
  15. import java.awt.*;
  16. import java.awt.event.*;
  17. import java.io.*;
  18. import java.sql.ResultSet;
  19. import java.sql.Statement;
  20. import java.time.LocalDateTime;
  21. import java.util.*;
  22. import java.util.List;
  23. import java.util.Timer;
  24. import java.util.concurrent.ExecutorService;
  25. import java.util.concurrent.Executors;
  26. public class MesClient extends JFrame {
  27. public static final Logger log = LoggerFactory.getLogger(MesClient.class);
  28. public static int mes_auth = 0; // 权限级别 0=无权限 1=操作工人 2=管理员
  29. public static String mes_gw = ""; // 工位号
  30. public static String mes_gw_des = ""; // 工位名称
  31. public static String mes_server_ip = ""; // 服务器IP地址
  32. public static int mes_heart_icon_cycle = 1;
  33. public static String mes_line_sn = ""; // 产线编号
  34. public static int mes_device_heartbeat_minutes = 5; // 设备状态上报间隔(分钟)
  35. public static String csyq = "";
  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; // 已弃用,改为使用 WorkRecordPanel
  43. public static JScrollPane searchScrollPaneDj;
  44. public static Boolean check_quality_result = false;
  45. public static Integer work_status = 0;
  46. public static Integer plc_status = 0;
  47. public static List<String> hj_params = new ArrayList<>();
  48. public static JButton heart_beat_menu;
  49. public static JButton status_menu;
  50. public static JButton user_menu;
  51. public static int scan_type = 0;
  52. public static JButton finish_ok_bt;
  53. public static JButton finish_ng_bt;
  54. public static JTextField product_sn;
  55. public static JButton f_scan_data_bt_1;
  56. public static String user20 = "";
  57. public static JFrame welcomeWin;
  58. public static JPanel indexPanelB; // 已弃用,改为使用 WorkRecordPanel
  59. public static MesWebView jfxPanel = null;
  60. public static JPanel indexPanelC;
  61. public static MesWebView jfxPanel2 = null;
  62. public static MesRadio mesRadioHj;
  63. public static JTable table;
  64. public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" };
  65. public static Object[][] rowData = null;
  66. public static JLabel fxlabel;
  67. public static Timer getSnBeatTimer;
  68. // S7plc
  69. public static S7PLC plc = new S7PLC(EPlcType.S1200,"192.168.2.1");
  70. public static void main(String[] args) {
  71. if (LockUtil.getInstance().isAppActive() == true){
  72. // JOptionPane.showMessageDialog(null, "已有一个程序在运行,程序退出");
  73. return;
  74. }else{
  75. EventQueue.invokeLater(new Runnable() {
  76. @Override
  77. public void run() {
  78. try{//读文件配置
  79. readProperty();
  80. // 显示界面
  81. mesClientFrame = new MesClient();
  82. mesClientFrame.setVisible(false);
  83. JdbcUtils.getConn();
  84. welcomeWin = new LoginFarme();
  85. welcomeWin.setVisible(true);
  86. getMaterailData();
  87. }catch (Exception e){
  88. e.printStackTrace();
  89. }
  90. }
  91. });
  92. }
  93. }
  94. //读配置文件
  95. private static void readProperty() throws IOException{
  96. String enconding = "UTF-8";
  97. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  98. Properties pro = new Properties();
  99. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  100. pro.load(br);
  101. mes_gw = pro.getProperty("mes.gw");
  102. mes_server_ip = pro.getProperty("mes.server_ip");
  103. mes_line_sn = pro.getProperty("mes.line_sn");
  104. csyq = pro.getProperty("mes.csyq");
  105. mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
  106. try {
  107. mes_device_heartbeat_minutes = Integer.parseInt(pro.getProperty("mes.device.heartbeat.minutes", "5").trim());
  108. } catch (Exception e) {
  109. mes_device_heartbeat_minutes = 5;
  110. }
  111. if (mes_device_heartbeat_minutes <= 0) {
  112. mes_device_heartbeat_minutes = 5;
  113. }
  114. log.info(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";");
  115. }
  116. //启动心跳包程序
  117. public static java.util.Timer heartBeatTimer;
  118. public static java.util.Timer heartBeatIconTimer;
  119. // 设备状态定时上报
  120. public static java.util.Timer deviceReportTimer;
  121. public static boolean iconREDFlag = true;
  122. public static void startHeartBeatTimer() {
  123. if(heartBeatTimer!=null) {
  124. heartBeatTimer.cancel();
  125. }
  126. //心跳显示图标
  127. if(heartBeatIconTimer!=null) {
  128. heartBeatIconTimer.cancel();
  129. }
  130. heartBeatIconTimer = new Timer();
  131. heartBeatIconTimer.schedule(new TimerTask() {
  132. public void run() {
  133. if(iconREDFlag) {
  134. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  135. iconREDFlag = false;
  136. }else {
  137. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  138. iconREDFlag = true;
  139. }
  140. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  141. heart_beat_menu.repaint();
  142. }
  143. }, 100,mes_heart_icon_cycle*1000);
  144. }
  145. /**
  146. * 启动设备状态定时上报
  147. */
  148. public static void startDeviceReportTimer() {
  149. stopDeviceReportTimer();
  150. long intervalMs = mes_device_heartbeat_minutes * 60L * 1000L;
  151. deviceReportTimer = new Timer("device-report-timer", true);
  152. deviceReportTimer.schedule(new TimerTask() {
  153. @Override
  154. public void run() {
  155. DeviceStateReporter.getInstance().reportAsync(DeviceStateReporter.STATE_RUNNING, "运行心跳");
  156. }
  157. }, intervalMs, intervalMs);
  158. log.info("设备状态定时上报已启动,间隔 {} 分钟", mes_device_heartbeat_minutes);
  159. }
  160. /**
  161. * 停止设备状态定时上报
  162. */
  163. public static void stopDeviceReportTimer() {
  164. if (deviceReportTimer != null) {
  165. deviceReportTimer.cancel();
  166. deviceReportTimer = null;
  167. }
  168. }
  169. /**
  170. * 登录成功后上报设备开机状态
  171. */
  172. public static void reportDeviceRunning() {
  173. DeviceStateReporter.getInstance().init(mes_server_ip, mes_gw, mes_line_sn);
  174. DeviceStateReporter.getInstance().reportAsync(DeviceStateReporter.STATE_RUNNING, "工位登录,设备开机");
  175. startDeviceReportTimer();
  176. }
  177. /**
  178. * 退出或关闭时上报设备停机状态
  179. */
  180. public static void reportDeviceStopped() {
  181. stopDeviceReportTimer();
  182. DeviceStateReporter.getInstance().reportSync(DeviceStateReporter.STATE_STOP, "工位退出,设备停机");
  183. }
  184. //设置tcp连接状态显示
  185. public static void setTcpStatus() {
  186. status_menu.setText("已连接MES服务器");
  187. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  188. heart_beat_menu.repaint();
  189. }
  190. public static void initWarehouseData(){
  191. resetScanA();
  192. }
  193. public static void resetScanA() {
  194. JSONObject retObj = DataUtil.delCurSn(MesClient.mes_gw, mes_server_ip, mes_line_sn);
  195. if (retObj.get("result")!=null && retObj.get("result").toString().equalsIgnoreCase("true")) {
  196. work_status = 0;
  197. check_quality_result = false;
  198. MesClient.finish_ok_bt.setEnabled(false);
  199. MesClient.finish_ng_bt.setEnabled(false);
  200. product_sn.setText("");
  201. MesClient.fxlabel.setVisible(false);
  202. MesClient.f_scan_data_bt_1.setEnabled(true);
  203. MesClient.setMenuStatus("请扫工件码",0);
  204. // MesClient.setMenuStatus("开班点检,请先进行OKNG样件测试",-1);
  205. updateMaterailData();
  206. // shiftUserCheck();
  207. } else {
  208. MesClient.setMenuStatus("刷新失败",0);
  209. }
  210. }
  211. public static int userLoginHours;//用户登录所处小时
  212. //换班用户信息检查
  213. private static void shiftUserCheck() {
  214. LocalDateTime now = LocalDateTime.now();
  215. // 判断时间范围
  216. if (userLoginHours >= 8 && userLoginHours < 20) {
  217. int hour = now.getHour();
  218. if (hour >= 20 || hour < 8) {
  219. logoff();
  220. }
  221. } else {
  222. int hour = now.getHour();
  223. if (hour >= 8 && hour < 20) {
  224. logoff();
  225. }
  226. }
  227. }
  228. public static void resetScanB() {
  229. }
  230. //获取用户20位
  231. public static void getUser() {
  232. user20 = user_menu.getText().toString();
  233. // String space_tmp1 = "";
  234. // if(user20.length()<20) {
  235. // for(int i=0;i<20-user20.length();i++) {
  236. // space_tmp1 = space_tmp1 + " ";
  237. // }
  238. // }
  239. // user20 = user20 + space_tmp1;
  240. }
  241. //获取barcode内容36位
  242. public static String getBarcode(String barcodeTmp) {
  243. String barcodeRet = barcodeTmp;
  244. if(barcodeTmp.equalsIgnoreCase("")) {
  245. return "";
  246. }else {
  247. if(barcodeTmp.length()<36) {
  248. String space = "";
  249. for(int i=0;i<36-barcodeTmp.length();i++) {
  250. space = space + " ";
  251. }
  252. barcodeRet = barcodeTmp + space;
  253. }
  254. }
  255. return barcodeRet;
  256. }
  257. public static void scanBarcode() {
  258. if(work_status == 1){
  259. JOptionPane.showMessageDialog(mesClientFrame,"工作中,勿扫码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  260. return;
  261. }
  262. String scanBarcodeTitle = "";
  263. switch(scan_type) {
  264. case 1:
  265. product_sn.setText("");
  266. scanBarcodeTitle = "请扫工件码";
  267. break;
  268. }
  269. //弹窗扫工件码
  270. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  271. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  272. //获取用户
  273. getUser();
  274. //获取扫码内容36位
  275. String barcode36 = getBarcode(scanBarcode);//处理36为码
  276. //工位号
  277. String gw = "";
  278. switch(scan_type) {
  279. case 1:
  280. product_sn.setText(scanBarcode);
  281. break;
  282. }
  283. //刷新界面
  284. mesClientFrame.repaint();
  285. // 查询工件质量
  286. JSONObject retObj = DataUtil.checkQuality(scanBarcode,user20);
  287. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  288. MesClient.status_menu.setForeground(Color.GREEN);
  289. MesClient.check_quality_result = true;//质量合格,可以绑定加工
  290. MesClient.status_menu.setText("该工件可以加工");
  291. MesClient.work_status = 1;
  292. MesClient.f_scan_data_bt_1.setEnabled(false);
  293. MesClient.finish_ok_bt.setEnabled(true);
  294. MesClient.finish_ng_bt.setEnabled(true);
  295. }else{
  296. MesClient.check_quality_result = false;
  297. MesClient.work_status = 0;
  298. if(retObj.get("result")==null){
  299. MesClient.setMenuStatus("请求失败,请重试",-1);
  300. }else{
  301. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  302. MesClient.setMenuStatus(retObj.getString("message"),-1);
  303. }
  304. }
  305. }
  306. }else {
  307. MesClient.setMenuStatus("请扫工件码,请重试",-1);
  308. // JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  309. return;
  310. }
  311. }
  312. public static void logoff() {
  313. reportDeviceStopped();
  314. if (getSnBeatTimer != null) {
  315. getSnBeatTimer.cancel();
  316. getSnBeatTimer = null;
  317. }
  318. welcomeWin.setVisible(true);
  319. mesClientFrame.setVisible(false);
  320. }
  321. public MesClient() {
  322. setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png")));
  323. setTitle("MES系统客户端115:"+mes_gw + "- " + mes_gw_des);
  324. // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  325. setBounds(0, 0, 1024, 768);
  326. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  327. // 添加窗口监听器
  328. addWindowListener(new WindowAdapter() {
  329. @Override
  330. public void windowClosing(WindowEvent e) {
  331. int choice = JOptionPane.showConfirmDialog(null, "确定要关闭窗口吗?", "关闭确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  332. if (choice == JOptionPane.YES_OPTION) {
  333. reportDeviceStopped();
  334. dispose();
  335. System.exit(0);
  336. }
  337. }
  338. });
  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. }
  371. });
  372. // settingMenu.add(resetTcpMenu);
  373. JMenuItem resetTcpMenu_1 = new JMenuItem("刷新工件");
  374. resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  375. resetTcpMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  376. resetTcpMenu_1.addMouseListener(new MouseAdapter() {
  377. @Override
  378. public void mousePressed(MouseEvent e) {
  379. super.mouseClicked(e);
  380. resetScanA();
  381. }
  382. });
  383. settingMenu.add(resetTcpMenu_1);
  384. contentPane = new JPanel();
  385. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  386. setContentPane(contentPane);
  387. contentPane.setLayout(new BorderLayout(0, 0));
  388. JToolBar toolBar = new JToolBar();
  389. contentPane.add(toolBar, BorderLayout.NORTH);
  390. JLabel equipment_statu_label = new JLabel("状态:");
  391. equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER);
  392. equipment_statu_label.setForeground(Color.BLACK);
  393. equipment_statu_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  394. equipment_statu_label.setBackground(Color.LIGHT_GRAY);
  395. toolBar.add(equipment_statu_label);
  396. status_menu = new JButton("设备未连接MES服务器");
  397. status_menu.setText("已连接MES服务器");
  398. status_menu.setForeground(Color.GREEN);
  399. status_menu.addActionListener(new ActionListener() {
  400. public void actionPerformed(ActionEvent e) {
  401. }
  402. });
  403. status_menu.setForeground(Color.GREEN);
  404. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  405. status_menu.setBackground(Color.BLACK);
  406. toolBar.add(status_menu);
  407. JLabel space_1 = new JLabel(" ");
  408. toolBar.add(space_1);
  409. JLabel heart_beat_status_label = new JLabel("心跳:");
  410. heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  411. heart_beat_status_label.setForeground(Color.BLACK);
  412. heart_beat_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  413. heart_beat_status_label.setBackground(Color.LIGHT_GRAY);
  414. toolBar.add(heart_beat_status_label);
  415. heart_beat_menu = new JButton("2024-02-20 23:20:10");
  416. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  417. heart_beat_menu.setForeground(Color.GREEN);
  418. heart_beat_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  419. heart_beat_menu.setBackground(Color.BLACK);
  420. toolBar.add(heart_beat_menu);
  421. JLabel space_2 = new JLabel(" ");
  422. toolBar.add(space_2);
  423. JLabel user_status_label = new JLabel("登录用户:");
  424. user_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  425. user_status_label.setForeground(Color.BLACK);
  426. user_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  427. user_status_label.setBackground(Color.LIGHT_GRAY);
  428. toolBar.add(user_status_label);
  429. user_menu = new JButton("JinJuShi");
  430. user_menu.setForeground(Color.GREEN);
  431. user_menu.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  432. user_menu.setBackground(Color.BLACK);
  433. toolBar.add(user_menu);
  434. JLabel space_3 = new JLabel(" ");
  435. toolBar.add(space_3);
  436. JLabel space_4 = new JLabel(" ");
  437. toolBar.add(space_4);
  438. tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  439. tabbedPane.setMinimumSize(new Dimension(400, 50));
  440. tabbedPane.setFont(new Font("宋体", Font.BOLD, 22));
  441. contentPane.add(tabbedPane);
  442. //首页
  443. JPanel indexPanelA = new JPanel();
  444. indexScrollPaneA = new JScrollPane(indexPanelA);
  445. indexPanelA.setLayout(null);
  446. product_sn = new JTextField();
  447. product_sn.setHorizontalAlignment(SwingConstants.CENTER);
  448. product_sn.setEditable(false);
  449. product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  450. product_sn.setBounds(81, 70, 602, 70);
  451. indexPanelA.add(product_sn);
  452. product_sn.setColumns(10);
  453. f_scan_data_bt_1 = new JButton("扫码");
  454. f_scan_data_bt_1.addActionListener(new ActionListener() {
  455. public void actionPerformed(ActionEvent e) {
  456. scan_type = 1;
  457. scanBarcode();
  458. }
  459. });
  460. f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  461. f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  462. f_scan_data_bt_1.setBounds(693, 70, 198, 70);
  463. indexPanelA.add(f_scan_data_bt_1);
  464. // String[] hjtitles = new String[]{"焊机1","焊机2","焊机3"};
  465. // String[] hjvals = new String[]{"HJ001","HJ002","HJ003"};
  466. // mesRadioHj = new MesRadio(hjtitles,hjvals);
  467. // mesRadioHj.setSize(500,50);
  468. // mesRadioHj.setBounds(190,170,500,50);
  469. // indexPanelA.add(mesRadioHj);
  470. fxlabel = new JLabel("该工件为返修件,请仔细检查");
  471. fxlabel.setFont(new Font("微软雅黑", Font.PLAIN, 38));
  472. fxlabel.setBounds(81, 170, 810, 70);
  473. fxlabel.setForeground(Color.RED);
  474. fxlabel.setHorizontalAlignment(SwingConstants.CENTER);
  475. fxlabel.setVisible(false);
  476. indexPanelA.add(fxlabel);
  477. finish_ok_bt = new JButton("OK");
  478. finish_ok_bt.setEnabled(false);
  479. finish_ok_bt.addActionListener(new ActionListener() {
  480. public void actionPerformed(ActionEvent e) {
  481. if(work_status == 1 && check_quality_result){
  482. getUser();
  483. String sn = MesClient.product_sn.getText().trim();
  484. if(sn.isEmpty()){
  485. MesClient.setMenuStatus("工件码为空,请重试",-1);
  486. return;
  487. }
  488. JSONObject retObj = DataUtil.sendQuality(sn,"OK",user20);
  489. System.out.println(retObj);
  490. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  491. resetState();
  492. MesClient.setMenuStatus("结果提交成功,请扫下一件",0);
  493. MesClient.scan_type = 1;
  494. }else{
  495. if(retObj.get("result")==null){
  496. MesClient.setMenuStatus("请求失败,请重试",-1);
  497. }else{
  498. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  499. MesClient.setMenuStatus(retObj.getString("message"),-1);
  500. }
  501. }
  502. }
  503. }
  504. }
  505. });
  506. finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
  507. finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  508. finish_ok_bt.setBounds(185, 291, 240, 80);
  509. finish_ok_bt.setEnabled(false);
  510. indexPanelA.add(finish_ok_bt);
  511. finish_ng_bt = new JButton("NG");
  512. finish_ng_bt.setEnabled(false);
  513. finish_ng_bt.addActionListener(new ActionListener() {
  514. public void actionPerformed(ActionEvent e) {
  515. if(work_status == 1 && check_quality_result){
  516. getUser();
  517. String sn = MesClient.product_sn.getText().trim();
  518. if(sn.isEmpty()){
  519. MesClient.setMenuStatus("工件码为空,请重试",-1);
  520. return;
  521. }
  522. JSONObject retObj = DataUtil.sendQuality(sn,"NG",user20);
  523. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  524. MesClient.resetScanA();
  525. MesClient.setMenuStatus("结果提交成功,请扫下一件",0);
  526. MesClient.scan_type = 1;
  527. MesClient.scanBarcode();
  528. }else{
  529. if(retObj.get("result")==null){
  530. MesClient.setMenuStatus("请求失败,请重试",-1);
  531. }else{
  532. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  533. MesClient.setMenuStatus(retObj.getString("message"),-1);
  534. }
  535. }
  536. }
  537. }
  538. }
  539. });
  540. finish_ng_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ng_bg.png")));
  541. finish_ng_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  542. finish_ng_bt.setBounds(508, 291, 240, 80);
  543. finish_ng_bt.setEnabled(false);
  544. indexPanelA.add(finish_ng_bt);
  545. tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null);
  546. tabbedPane.setEnabledAt(0, true);
  547. // searchScrollPane = new JScrollPane((Component) null);
  548. indexPanelC = new JPanel();
  549. searchScrollPaneDj = new JScrollPane(indexPanelC);
  550. indexPanelC.setLayout(null);
  551. tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null);
  552. // ========== 工作记录 Tab(改造:当前工位Tab + 所有工位Tab) ==========
  553. // 旧代码已注释:
  554. indexPanelB = new JPanel();
  555. searchScrollPane = new JScrollPane(indexPanelB);
  556. indexPanelB.setLayout(null);
  557. tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
  558. // // 创建工作记录的子 TabbedPane
  559. // JTabbedPane workRecordTabbedPane = new JTabbedPane(JTabbedPane.TOP);
  560. // workRecordTabbedPane.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  561. //
  562. // // OKNG 工位:当前工位 + 所有工位
  563. // WorkRecordPanel[] workRecordPanels = new WorkRecordPanel[2];
  564. //
  565. // // 当前工位Tab(使用配置文件中的 mes.gw)
  566. // workRecordPanels[0] = new WorkRecordPanel(mes_gw, mes_line_sn);
  567. // workRecordTabbedPane.addTab(mes_gw, null, workRecordPanels[0], "查询" + mes_gw + "的工作记录");
  568. //
  569. // // 所有工位Tab(查询整条产线所有工位)
  570. // workRecordPanels[1] = new WorkRecordPanel(null, mes_line_sn);
  571. // workRecordTabbedPane.addTab("所有工位", null, workRecordPanels[1], "查询整条产线所有工位的工作记录");
  572. //
  573. // // 切换子Tab时自动刷新数据
  574. // workRecordTabbedPane.addChangeListener(e -> {
  575. // int selectedIndex = workRecordTabbedPane.getSelectedIndex();
  576. // if (selectedIndex >= 0 && selectedIndex < workRecordPanels.length) {
  577. // workRecordPanels[selectedIndex].refreshData();
  578. // }
  579. // });
  580. //
  581. // // 将子 TabbedPane 添加到主 TabbedPane
  582. // tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), workRecordTabbedPane, null);
  583. //
  584. tabbedPane.addChangeListener(new ChangeListener() {
  585. @Override
  586. public void stateChanged(ChangeEvent e) {
  587. JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
  588. int selectedIndex = tabbedPane.getSelectedIndex();
  589. System.out.println("selectedIndex:"+selectedIndex);
  590. if(selectedIndex == 1){
  591. }
  592. }
  593. });
  594. }
  595. public static void startGetCurSn(){
  596. if (getSnBeatTimer != null) {
  597. getSnBeatTimer.cancel();
  598. }
  599. getSnBeatTimer = new Timer();
  600. getSnBeatTimer.schedule(new TimerTask() {
  601. @Override
  602. public void run() {
  603. if (work_status == 1) {
  604. // if (plc_status == 0){
  605. // // 等待扫码
  606. // String sn = product_sn.getText().trim();
  607. // if (sn.isEmpty()) {
  608. // plc_status = 1;
  609. // }
  610. // } else if (plc_status == 1) {
  611. // // 发送允许启动信号
  612. // Boolean ret = PLCUtils.writeStartMethod(plc);
  613. // if (ret) {
  614. // plc_status = 2;
  615. // }
  616. // } else if (plc_status == 2) {
  617. // // 等待加工结束信号
  618. // Boolean ret = PLCUtils.readStopMethod(plc);
  619. // if (ret){
  620. // // 将允许启动置空
  621. // PLCUtils.writeStopMethod(plc);
  622. // // 获取参数
  623. // QmParam qmParam = PLCUtils.getParameter(plc);
  624. // // 发送参数
  625. // JSONObject retObj1 = DataUtil.sendQualityParam( user20, product_sn.getText().trim(), MesClient.mes_gw, MesClient.mes_line_sn,qmParam);
  626. // if(retObj1 != null && retObj1.get("result") != null && retObj1.get("result").toString().equalsIgnoreCase("true")) {
  627. // MesClient.resetScanA();
  628. // MesClient.setMenuStatus("测试结果上传成功,请扫下一件",0);
  629. // }else{
  630. // MesClient.setMenuStatus("测试结果上传失败,请重试",-1);
  631. // return;
  632. // }
  633. // // 获取结果
  634. // String result = PLCUtils.getResult(plc);
  635. // // 发送结果
  636. // String sn = product_sn.getText().trim();
  637. // JSONObject retObj = DataUtil.sendQuality( sn,result,user20);
  638. // if (retObj != null && "true".equalsIgnoreCase(retObj.getString("result"))) {
  639. // resetState();
  640. // status_menu.setForeground(Color.GREEN);
  641. // status_menu.setText("结果提交成功,请扫下一件");
  642. // finish_ng_bt.setEnabled(false);
  643. // } else {
  644. // status_menu.setForeground(Color.RED);
  645. // String msg = (retObj != null && retObj.containsKey("message")) ? retObj.getString("message") : "提交失败";
  646. // status_menu.setText(msg);
  647. // }
  648. // }
  649. // }
  650. // 获取结果
  651. String result = PLCUtils.getResult(plc);
  652. System.out.println(result);
  653. // 如果等于OK或者NG的时候才处理结果
  654. if("OK".equals(result)){
  655. // 获取参数
  656. QmParam qmParam = PLCUtils.getParameter(plc);
  657. // 发送参数
  658. JSONObject retObj1 = DataUtil.sendQualityParam( user20, product_sn.getText().trim(), MesClient.mes_gw, MesClient.mes_line_sn,qmParam);
  659. System.out.println(qmParam);
  660. if(retObj1 != null && retObj1.get("result") != null && retObj1.get("result").toString().equalsIgnoreCase("true")) {
  661. //MesClient.resetScanA();
  662. MesClient.setMenuStatus("气密数据上传成功,请扫下一件",0);
  663. }else{
  664. MesClient.setMenuStatus("测试结果上传失败,请重试",-1);
  665. }
  666. String sn = product_sn.getText().trim();
  667. JSONObject retObj = DataUtil.sendQuality(sn,result,user20);
  668. if (retObj != null && "true".equalsIgnoreCase(retObj.getString("result"))) {
  669. resetState();
  670. status_menu.setForeground(Color.GREEN);
  671. status_menu.setText("结果提交成功,请扫下一件");
  672. finish_ng_bt.setEnabled(false);
  673. } else {
  674. status_menu.setForeground(Color.RED);
  675. String msg = (retObj != null && retObj.containsKey("message")) ? retObj.getString("message") : "提交失败";
  676. status_menu.setText(msg);
  677. }
  678. } else if ("NG".equals(result)) {
  679. String sn = product_sn.getText().trim();
  680. JSONObject retObj = DataUtil.sendQuality( sn,result,user20);
  681. if (retObj != null && "true".equalsIgnoreCase(retObj.getString("result"))) {
  682. resetState();
  683. status_menu.setForeground(Color.GREEN);
  684. status_menu.setText("结果提交成功,请扫下一件");
  685. finish_ng_bt.setEnabled(false);
  686. } else {
  687. status_menu.setForeground(Color.RED);
  688. String msg = (retObj != null && retObj.containsKey("message")) ? retObj.getString("message") : "提交失败";
  689. status_menu.setText(msg);
  690. }
  691. }
  692. } else if (work_status == 0) {
  693. JSONObject result = DataUtil.getCurSn(mes_gw, mes_server_ip, mes_line_sn);
  694. // 正常处理结果
  695. if (result.containsKey("data")) {
  696. String data = result.getString("data");
  697. if (data != null && !data.isEmpty()) {
  698. product_sn.setText(data);
  699. JSONObject jsonObject = DataUtil.checkQuality(data, user20);
  700. String msg = jsonObject.getString("message");
  701. if (jsonObject.getString("result").equalsIgnoreCase("true")) {
  702. /* finish_ng_bt.setEnabled(true);
  703. finish_ok_bt.setEnabled(true);*/
  704. check_quality_result = true;
  705. work_status = 1;
  706. MesClient.setMenuStatus(msg,0);
  707. } else {
  708. work_status = 0;
  709. String msg2 = (jsonObject != null && jsonObject.containsKey("message")) ? jsonObject.getString("message") : "检查失败";
  710. MesClient.setMenuStatus(msg2,-1);
  711. check_quality_result = false;
  712. }
  713. }
  714. }
  715. String abc = PLCUtils.getResult(plc);
  716. System.out.println(abc);
  717. }
  718. }
  719. }, 100, 1000);
  720. }
  721. public static void setMenuStatus(String msg,int error){
  722. if(error == 0){
  723. MesClient.status_menu.setForeground(Color.GREEN);
  724. }else{
  725. MesClient.status_menu.setForeground(Color.RED);
  726. }
  727. MesClient.status_menu.setText(msg);
  728. }
  729. public static void getMaterailData(){
  730. try{
  731. JSONObject retObj = DataUtil.getBindMaterail();
  732. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  733. java.util.List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  734. int i = 0;
  735. rowData = new Object[arrs.size()][7];
  736. for (BindMaterialResp bindMaterialResp:arrs){
  737. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  738. rowData[i][1] = bindMaterialResp.getBatchSn();
  739. rowData[i][2] = bindMaterialResp.getLastTimes();
  740. rowData[i][3] = "";
  741. rowData[i][4] = bindMaterialResp.getCraft();
  742. rowData[i][5] = bindMaterialResp.getMaterialId();
  743. rowData[i][6] = bindMaterialResp.getType();
  744. i++;
  745. }
  746. bindBatchPanel();
  747. }
  748. }catch (Exception e){
  749. log.info(e.getMessage());
  750. }
  751. }
  752. public static void updateMaterailData(){
  753. try{
  754. JSONObject retObj = DataUtil.getBindMaterail();
  755. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  756. List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  757. int i = 0;
  758. for (BindMaterialResp bindMaterialResp:arrs){
  759. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  760. rowData[i][1] = bindMaterialResp.getBatchSn();
  761. rowData[i][2] = bindMaterialResp.getLastTimes();
  762. rowData[i][3] = "";
  763. rowData[i][4] = bindMaterialResp.getCraft();
  764. rowData[i][5] = bindMaterialResp.getMaterialId();
  765. rowData[i][6] = bindMaterialResp.getType();
  766. i++;
  767. }
  768. MesClient.table.repaint();
  769. }
  770. }catch (Exception e){
  771. log.info(e.getMessage());
  772. }
  773. }
  774. // 绑定物料批次码
  775. public static void scanBatchSn(BindMaterialResp bindMaterialResp) {
  776. //弹窗扫工件码
  777. String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle();
  778. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  779. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  780. JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType());
  781. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  782. MesClient.setMenuStatus("扫物料:"+bindMaterialResp.getMaterialTitle()+"成功",0);
  783. updateMaterailData();
  784. }else{
  785. if(retObj.get("result")==null){
  786. MesClient.setMenuStatus("请求失败,请重试",-1);
  787. }else{
  788. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  789. MesClient.setMenuStatus(retObj.getString("message"),-1);
  790. }
  791. }
  792. }
  793. }
  794. }
  795. private static void resetState() {
  796. work_status = 0;
  797. plc_status = 0;
  798. check_quality_result = false;
  799. product_sn.setText("");
  800. finish_ok_bt.setEnabled(false);
  801. finish_ng_bt.setEnabled(false);
  802. fxlabel.setVisible(false);
  803. MesClient.setMenuStatus("请扫工件码",0);
  804. }
  805. public static void bindBatchPanel(){
  806. JPanel indexPanelBB = new JPanel();
  807. JPanel panel = new JPanel();
  808. panel.setBounds(0, 0, 990, 550);
  809. // panel.setBounds(81, 50, 810, 479);
  810. indexPanelBB.add(panel);
  811. panel.setLayout(new GridLayout(0, 1, 0, 0));
  812. table = new JTable(rowData, columnNames){
  813. public boolean isCellEditable(int row, int column) {
  814. if(column == 3){
  815. return true;
  816. }
  817. return false;
  818. }
  819. };
  820. table.setRowHeight(40);
  821. table.setEnabled(true);
  822. table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  823. table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton());
  824. table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton());
  825. JScrollPane scrollPane = new JScrollPane(table);
  826. panel.add(scrollPane);
  827. JScrollPane indexScrollPaneB = new JScrollPane(indexPanelBB);
  828. indexPanelBB.setLayout(null);
  829. tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null);
  830. }
  831. // 开启发送结果定时任务
  832. public static void startUpdateQualityTimer(){
  833. // 创建异步线程池
  834. ExecutorService executorService = Executors.newSingleThreadExecutor();
  835. // 提交任务
  836. executorService.submit(() -> {
  837. log.info("异步任务执行中...");
  838. // 模拟耗时操作
  839. try {
  840. while (true){
  841. try {
  842. // 确保连接已经打开
  843. if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
  844. JdbcUtils.openConnection();
  845. }
  846. Statement statement = JdbcUtils.conn.createStatement();
  847. String selectSql = "SELECT id, bw FROM submit_record\n" +
  848. "WHERE state = 0 ORDER BY id DESC LIMIT 10";
  849. String updateSql = "UPDATE submit_record \n" +
  850. "SET state = '1'\n" +
  851. "WHERE id = ";
  852. ResultSet rs = statement.executeQuery(selectSql);
  853. while(rs.next()){
  854. int id = rs.getInt("id");
  855. String bw = rs.getString("bw");
  856. // 发送请求
  857. String url = "http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesProductRecord/updateQualityByTiming";
  858. log.info("质量:"+bw);
  859. String s = HttpUtils.sendPostRequestJson(url, bw );
  860. log.info("结果:"+s);
  861. Boolean result = JSONObject.parseObject(s).getBoolean("result");
  862. if(result) {
  863. // 更改状态为 1
  864. statement.executeUpdate(updateSql + id);
  865. }
  866. }
  867. rs.close();
  868. statement.close();
  869. } catch (Exception e) {
  870. log.info(e.getMessage());
  871. }
  872. try {
  873. Thread.sleep(2000);
  874. }catch (Exception e){
  875. log.info(e.getMessage());
  876. }
  877. }
  878. } catch (Exception e) {
  879. log.info(e.getMessage());
  880. }
  881. log.info("异步任务执行完毕");
  882. });
  883. }
  884. }