MesClient.java 42 KB

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