MesClient.java 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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.google.zxing.BarcodeFormat;
  6. import com.google.zxing.client.j2se.MatrixToImageWriter;
  7. import com.google.zxing.common.BitMatrix;
  8. import com.google.zxing.qrcode.QRCodeWriter;
  9. import com.mes.component.MesClientComponent;
  10. import com.mes.component.MesRadio;
  11. import com.mes.component.MesWebView;
  12. import com.mes.netty.NettyClient;
  13. import com.mes.netty.ProtocolParam;
  14. import com.mes.util.*;
  15. import javafx.embed.swing.JFXPanel;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import javax.swing.*;
  19. import javax.swing.border.EmptyBorder;
  20. import javax.swing.event.ChangeEvent;
  21. import javax.swing.event.ChangeListener;
  22. import java.awt.*;
  23. import java.awt.event.ActionEvent;
  24. import java.awt.event.ActionListener;
  25. import java.awt.event.MouseAdapter;
  26. import java.awt.event.MouseEvent;
  27. import java.awt.image.BufferedImage;
  28. import java.io.*;
  29. import java.lang.reflect.Field;
  30. import java.nio.channels.FileChannel;
  31. import java.nio.channels.FileLock;
  32. import java.nio.channels.OverlappingFileLockException;
  33. import java.sql.ResultSet;
  34. import java.sql.SQLException;
  35. import java.sql.Statement;
  36. import java.time.LocalDateTime;
  37. import java.util.*;
  38. import java.util.List;
  39. import java.util.Timer;
  40. import java.util.concurrent.ExecutorService;
  41. import java.util.concurrent.Executors;
  42. import java.util.concurrent.ScheduledExecutorService;
  43. import java.util.concurrent.ScheduledFuture;
  44. import java.util.concurrent.TimeUnit;
  45. import java.util.regex.Matcher;
  46. import java.util.regex.Pattern;
  47. public class MesClient extends JFrame {
  48. public static final Logger log = LoggerFactory.getLogger(MesClient.class);
  49. public static int mes_auth = 0; // 权限级别 0=无权限 1=操作工人 2=管理员
  50. public static String mes_gw = ""; // 工位号
  51. public static String mes_gw_des = ""; // 工位名称
  52. public static String mes_server_ip = ""; // 服务器IP地址
  53. public static int mes_tcp_port = 3000; // TCP服务端口
  54. public static int mes_heart_beat_cycle = 10; // 心跳周期
  55. public static int mes_heart_icon_cycle = 1;
  56. public static String mes_line_sn = ""; // 产线编号
  57. //TCP连接
  58. public static NettyClient nettyClient;
  59. //TCP连接状态
  60. public static boolean tcp_connect_flag = false;
  61. //TCP连接请求
  62. public static boolean connect_request_flag = false;
  63. //session
  64. public static String sessionid = "";
  65. public static JPanel contentPane;
  66. public static MesClient mesClientFrame;
  67. public static JTabbedPane tabbedPane;
  68. public static JScrollPane indexScrollPaneA;
  69. public static JScrollPane searchScrollPane;
  70. public static JScrollPane searchScrollPaneDj;
  71. // 存储工位号和对应的MesClientComponent组件的映射
  72. public static Map<String, MesClientComponent> componentMap = new HashMap<>();
  73. public static Boolean check_quality_result = false;
  74. public static Integer work_status = 0;
  75. public static JButton heart_beat_menu;
  76. public static JButton status_menu;
  77. public static JButton user_menu;
  78. public static int scan_type = 0;
  79. public static JButton finish_ok_bt;
  80. public static JButton finish_ng_bt;
  81. public static JTextField product_sn;
  82. public static JButton f_scan_data_bt_1;
  83. public static String user20 = "";
  84. public static JFrame welcomeWin;
  85. public static JPanel indexPanelB;
  86. public static MesWebView jfxPanel = null;
  87. public static JPanel indexPanelC;
  88. public static MesWebView jfxPanel2 = null;
  89. /** 工作记录机台分标签(点哪个加载哪个) */
  90. public static JTabbedPane workRecordTabs = null;
  91. public static boolean workRecordInited = false;
  92. public static MesRadio mesRadioHj;
  93. public static JTable table;
  94. public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" };
  95. public static Object[][] rowData = null;
  96. public static JLabel fxlabel;
  97. /**
  98. * 业务字段
  99. *
  100. */
  101. // 存储工位信息的 Map:key=工位号,value=Oprno 对象
  102. public static Map<String, Oprno> oprnoMap = new HashMap<>();
  103. // 正则表达式:匹配 mes.oprno.${工位号}.${属性名}
  104. private static final Pattern OPRNO_PATTERN = Pattern.compile("mes\\.oprno\\.(\\w+)\\.(\\w+)");
  105. // 需要跳过的属性名(不读入 Map)
  106. private static final String[] SKIP_PROPERTIES = {"hgA_qr_code", "hgB_qr_code"};
  107. // 新增:存储动态生成的工位输入框(key=工位号,value=输入框)
  108. private static Map<String, JTextField> oprnoTextFieldMap = new HashMap<>();
  109. //点检字段
  110. public static Boolean check_info = false;
  111. public static S7PLC s7PLC;
  112. /** 人工点击 PLC 反馈信号时的确认密码 */
  113. public static final String MANUAL_PLC_PASSWORD = "503833";
  114. /**
  115. * 人工改 PLC 反馈前校验密码;取消或错误则返回 false。
  116. */
  117. public static boolean verifyManualPlcPassword(Component parent) {
  118. JPasswordField pwdField = new JPasswordField(12);
  119. int opt = JOptionPane.showConfirmDialog(
  120. parent != null ? parent : mesClientFrame,
  121. pwdField,
  122. "请输入操作密码",
  123. JOptionPane.OK_CANCEL_OPTION,
  124. JOptionPane.PLAIN_MESSAGE);
  125. if (opt != JOptionPane.OK_OPTION) {
  126. return false;
  127. }
  128. String input = new String(pwdField.getPassword());
  129. if (!MANUAL_PLC_PASSWORD.equals(input)) {
  130. JOptionPane.showMessageDialog(
  131. parent != null ? parent : mesClientFrame,
  132. "密码错误",
  133. "提示窗口",
  134. JOptionPane.WARNING_MESSAGE);
  135. return false;
  136. }
  137. return true;
  138. }
  139. public static void main(String[] args) {
  140. if (LockUtil.getInstance().isAppActive() == true){
  141. // JOptionPane.showMessageDialog(null, "已有一个程序在运行,程序退出");
  142. return;
  143. }else{
  144. EventQueue.invokeLater(new Runnable() {
  145. @Override
  146. public void run() {
  147. try{
  148. //读文件配置
  149. readProperty();
  150. // 显示界面
  151. mesClientFrame = new MesClient();
  152. mesClientFrame.setVisible(false);
  153. JdbcUtils.getConn();
  154. welcomeWin = new LoginFarme();
  155. welcomeWin.setVisible(true);
  156. getMaterailData();
  157. }catch (Exception e){
  158. e.printStackTrace();
  159. }
  160. }
  161. });
  162. }
  163. }
  164. //读配置文件
  165. private static void readProperty() throws IOException{
  166. String enconding = "UTF-8";
  167. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  168. Properties pro = new Properties();
  169. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  170. pro.load(br);
  171. mes_gw = pro.getProperty("mes.gw");
  172. mes_server_ip = pro.getProperty("mes.server_ip");
  173. mes_tcp_port = Integer.parseInt(pro.getProperty("mes.tcp_port"));
  174. mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle"));
  175. mes_line_sn = pro.getProperty("mes.line_sn");
  176. mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
  177. // PLC
  178. String plcIp = pro.getProperty("mes.plc_ip", "10.88.10.80");
  179. s7PLC = new S7PLC(EPlcType.S1200, plcIp);
  180. log.info(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle + ";plc=" + plcIp);
  181. // 2. 读取工位信息并填充到 oprnoMap(自动跳过二维码字段)
  182. loadOprnoMap(pro);
  183. // 循环打印工位信息
  184. for (Map.Entry<String, Oprno> entry : oprnoMap.entrySet()) {
  185. Oprno oprno = entry.getValue();
  186. log.info("工位信息" + oprno.toString());
  187. }
  188. //DataUtil.getCheckInfo();
  189. }
  190. // 初始化TCP
  191. public static void initTcpConnection() {
  192. try {
  193. if(nettyClient==null) {
  194. //初始化TCP连接
  195. nettyClient = new NettyClient();
  196. //TCP连接状态
  197. tcp_connect_flag = false;
  198. //设置TCP请求状态
  199. connect_request_flag = true;
  200. DataUtil.synrTcp(nettyClient,mes_gw);
  201. }
  202. } catch (Exception e) {
  203. // TODO Auto-generated catch block
  204. e.printStackTrace();
  205. }
  206. }
  207. /**
  208. * 创建工作面板:OP180A-D 四台 CNC(R3 无上料架统一读码区)
  209. */
  210. private void createOprnoPanel(JPanel parentPanel){
  211. List<String> oprnoCodes = new ArrayList<>(oprnoMap.keySet());
  212. Collections.sort(oprnoCodes);
  213. int fieldY = 0;
  214. for (String oprnoCode : oprnoCodes) {
  215. int columnIndex = fieldY % 4;
  216. int rowIndex = fieldY / 4;
  217. MesClientComponent mesClientComponent = new MesClientComponent(oprnoMap.get(oprnoCode));
  218. mesClientComponent.setBounds(
  219. columnIndex * MesClientComponent.workWidth,
  220. rowIndex * MesClientComponent.workHeight,
  221. MesClientComponent.workWidth,
  222. MesClientComponent.workHeight);
  223. parentPanel.add(mesClientComponent);
  224. componentMap.put(oprnoCode, mesClientComponent);
  225. fieldY++;
  226. }
  227. }
  228. //启动心跳包程序
  229. public static java.util.Timer heartBeatTimer;
  230. public static java.util.Timer heartBeatIconTimer;
  231. public static boolean iconREDFlag = true;
  232. // 心跳程序包
  233. public static void startHeartBeatTimer() {
  234. if(heartBeatTimer!=null) {
  235. heartBeatTimer.cancel();
  236. }
  237. heartBeatTimer = new java.util.Timer();
  238. heartBeatTimer.schedule(new TimerTask() {
  239. public void run() {
  240. if(nettyClient!=null&&tcp_connect_flag) {
  241. //System.out.println("发送心跳报文");
  242. DataUtil.heartBeat(nettyClient,mes_gw);
  243. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  244. }
  245. }
  246. }, 100,mes_heart_beat_cycle*1000);
  247. //心跳显示图标
  248. if(heartBeatIconTimer!=null) {
  249. heartBeatIconTimer.cancel();
  250. }
  251. heartBeatIconTimer = new Timer();
  252. heartBeatIconTimer.schedule(new TimerTask() {
  253. public void run() {
  254. if(tcp_connect_flag) {
  255. if(iconREDFlag) {
  256. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  257. iconREDFlag = false;
  258. }else {
  259. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  260. iconREDFlag = true;
  261. }
  262. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  263. heart_beat_menu.repaint();
  264. }else {
  265. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  266. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  267. heart_beat_menu.repaint();
  268. //若未连接则尝试连接
  269. if(nettyClient!=null&&!tcp_connect_flag){
  270. System.out.println("TCP已断开");
  271. //TCP重新同步连接
  272. if(!connect_request_flag) {
  273. //System.out.println("TCP重新同步连接");
  274. //设置TCP请求状态,只重新同步连接一次
  275. connect_request_flag = true;
  276. DataUtil.synrTcp(nettyClient,mes_gw);
  277. }
  278. }
  279. }
  280. }
  281. }, 100,mes_heart_icon_cycle*1000);
  282. }
  283. //设置tcp连接状态显示
  284. public static void setTcpStatus() {
  285. if(tcp_connect_flag) {
  286. status_menu.setText("已连接MES服务器");
  287. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  288. heart_beat_menu.repaint();
  289. }else {
  290. status_menu.setText("未连接MES服务器");
  291. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  292. heart_beat_menu.repaint();
  293. }
  294. }
  295. public static void initWarehouseData(){
  296. resetScanA();
  297. }
  298. public static void resetScanA() {
  299. work_status = 0;
  300. check_quality_result = false;
  301. MesClient.finish_ok_bt.setEnabled(false);
  302. MesClient.finish_ng_bt.setEnabled(false);
  303. product_sn.setText("");
  304. MesClient.fxlabel.setVisible(false);
  305. MesClient.f_scan_data_bt_1.setEnabled(true);
  306. MesClient.status_menu.setText("请扫工件码");
  307. updateMaterailData();
  308. shiftUserCheck();
  309. }
  310. public static int userLoginHours;//用户登录所处小时
  311. //换班用户信息检查
  312. private static void shiftUserCheck() {
  313. LocalDateTime now = LocalDateTime.now();
  314. // 判断时间范围
  315. if (userLoginHours >= 8 && userLoginHours < 20) {
  316. int hour = now.getHour();
  317. if (hour >= 20 || hour < 8) {
  318. logoff();
  319. }
  320. } else {
  321. int hour = now.getHour();
  322. if (hour >= 8 && hour < 20) {
  323. logoff();
  324. }
  325. }
  326. }
  327. //获取用户20位
  328. public static void getUser() {
  329. user20 = user_menu.getText().toString();
  330. String space_tmp1 = "";
  331. if(user20.length()<20) {
  332. for(int i=0;i<20-user20.length();i++) {
  333. space_tmp1 = space_tmp1 + " ";
  334. }
  335. }
  336. user20 = user20 + space_tmp1;
  337. }
  338. //获取barcode内容36位
  339. public static String getBarcode(String barcodeTmp) {
  340. String barcodeRet = barcodeTmp;
  341. if(barcodeTmp.equalsIgnoreCase("")) {
  342. return "";
  343. }else {
  344. if(barcodeTmp.length()<36) {
  345. String space = "";
  346. for(int i=0;i<36-barcodeTmp.length();i++) {
  347. space = space + " ";
  348. }
  349. barcodeRet = barcodeTmp + space;
  350. }
  351. }
  352. return barcodeRet;
  353. }
  354. public static void scanBarcode() {
  355. if(work_status == 1){
  356. JOptionPane.showMessageDialog(mesClientFrame,"工作中,勿扫码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  357. return;
  358. }
  359. String scanBarcodeTitle = "";
  360. switch(scan_type) {
  361. case 1:
  362. product_sn.setText("");
  363. scanBarcodeTitle = "请扫工件码";
  364. break;
  365. }
  366. //弹窗扫工件码
  367. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  368. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  369. //获取用户
  370. getUser();
  371. //获取扫码内容36位
  372. String barcode36 = getBarcode(scanBarcode);//处理36为码
  373. //工位号
  374. String gw = "";
  375. switch(scan_type) {
  376. case 1:
  377. product_sn.setText(scanBarcode);
  378. break;
  379. }
  380. //刷新界面
  381. mesClientFrame.repaint();
  382. if(!tcp_connect_flag) {
  383. MesClient.setMenuStatus("设备未连接Mes服务器",-1);
  384. // JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  385. return;
  386. }
  387. // 查询工件质量
  388. Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20);
  389. if(!sendret){
  390. MesClient.setMenuStatus("消息发送失败,请重试",-1);
  391. return;
  392. }
  393. }else {
  394. MesClient.setMenuStatus("请扫工件码,请重试",-1);
  395. return;
  396. }
  397. }
  398. /** 点击工件码弹窗显示二维码 */
  399. public static void createQRCode(String s, String title) {
  400. if (s == null || s.trim().isEmpty()) {
  401. return;
  402. }
  403. s = s.trim();
  404. try {
  405. BitMatrix bitMatrix = new QRCodeWriter().encode(s, BarcodeFormat.QR_CODE, 200, 200);
  406. BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
  407. JDialog qrDialog = new JDialog(mesClientFrame, title == null ? "" : title, Dialog.ModalityType.APPLICATION_MODAL);
  408. qrDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  409. qrDialog.setSize(250, 250);
  410. qrDialog.setLocationRelativeTo(mesClientFrame);
  411. qrDialog.setResizable(false);
  412. qrDialog.getContentPane().add(new JLabel(new ImageIcon(qrImage)), BorderLayout.CENTER);
  413. qrDialog.setVisible(true);
  414. } catch (Exception e) {
  415. log.error("生成二维码失败", e);
  416. }
  417. }
  418. public static void logoff() {
  419. welcomeWin.setVisible(true);
  420. mesClientFrame.setVisible(false);
  421. nettyClient = null;
  422. // if(heartBeatTimer!=null) {
  423. // heartBeatTimer.cancel();
  424. // }
  425. // if(heartBeatIconTimer!=null) {
  426. // heartBeatIconTimer.cancel();
  427. // }
  428. tcp_connect_flag = false;
  429. connect_request_flag = false;
  430. }
  431. public MesClient() {
  432. setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png")));
  433. setTitle("MES系统客户端:"+mes_gw + "- " + mes_gw_des);
  434. setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  435. // 添加窗口关闭监听器,确保定时任务池正确关闭
  436. addWindowListener(new java.awt.event.WindowAdapter() {
  437. @Override
  438. public void windowClosing(java.awt.event.WindowEvent windowEvent) {
  439. // 退出程序
  440. System.exit(0);
  441. }
  442. });
  443. setBounds(0, 0, 1024, 1500);
  444. setLocationRelativeTo( null);
  445. JMenuBar menuBar = new JMenuBar();
  446. menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26));
  447. setJMenuBar(menuBar);
  448. JMenu fileMenu = new JMenu("用户");
  449. fileMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png")));
  450. fileMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  451. menuBar.add(fileMenu);
  452. JMenuItem exitMenuItem = new JMenuItem("退出");
  453. exitMenuItem.setIcon(new ImageIcon(MesClient.class.getResource("/bg/logoff.png")));
  454. exitMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  455. fileMenu.add(exitMenuItem);
  456. exitMenuItem.addMouseListener(new MouseAdapter() {
  457. @Override
  458. public void mousePressed(MouseEvent e) {//只能检测到mousePressed事件
  459. super.mouseClicked(e);
  460. //dispose();
  461. logoff();
  462. }
  463. });
  464. JMenu settingMenu = new JMenu("设置");
  465. //settingMenu.setVisible(false);
  466. settingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
  467. settingMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  468. menuBar.add(settingMenu);
  469. JMenuItem resetTcpMenu = new JMenuItem("\u91CD\u8FDEMES");
  470. resetTcpMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  471. resetTcpMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  472. resetTcpMenu.addMouseListener(new MouseAdapter() {
  473. @Override
  474. public void mousePressed(MouseEvent e) {
  475. super.mouseClicked(e);
  476. //重连mes
  477. nettyClient.future.channel().close();
  478. }
  479. });
  480. settingMenu.add(resetTcpMenu);
  481. contentPane = new JPanel();
  482. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  483. setContentPane(contentPane);
  484. contentPane.setLayout(new BorderLayout(0, 0));
  485. JToolBar toolBar = new JToolBar();
  486. contentPane.add(toolBar, BorderLayout.NORTH);
  487. JLabel equipment_statu_label = new JLabel("状态:");
  488. equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER);
  489. equipment_statu_label.setForeground(Color.BLACK);
  490. equipment_statu_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  491. equipment_statu_label.setBackground(Color.LIGHT_GRAY);
  492. toolBar.add(equipment_statu_label);
  493. status_menu = new JButton("设备未连接MES服务器");
  494. if(tcp_connect_flag) {
  495. status_menu.setText("已连接MES服务器");
  496. status_menu.setForeground(Color.GREEN);
  497. }else {
  498. status_menu.setText("未连接MES服务器");
  499. status_menu.setForeground(Color.DARK_GRAY);
  500. }
  501. status_menu.addActionListener(new ActionListener() {
  502. public void actionPerformed(ActionEvent e) {
  503. }
  504. });
  505. status_menu.setForeground(Color.GREEN);
  506. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  507. status_menu.setBackground(Color.BLACK);
  508. toolBar.add(status_menu);
  509. JLabel space_1 = new JLabel(" ");
  510. toolBar.add(space_1);
  511. JLabel heart_beat_status_label = new JLabel("心跳:");
  512. heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  513. heart_beat_status_label.setForeground(Color.BLACK);
  514. heart_beat_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  515. heart_beat_status_label.setBackground(Color.LIGHT_GRAY);
  516. toolBar.add(heart_beat_status_label);
  517. heart_beat_menu = new JButton("2024-02-20 23:20:10");
  518. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  519. heart_beat_menu.setForeground(Color.GREEN);
  520. heart_beat_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  521. heart_beat_menu.setBackground(Color.BLACK);
  522. toolBar.add(heart_beat_menu);
  523. JLabel space_2 = new JLabel(" ");
  524. toolBar.add(space_2);
  525. JLabel user_status_label = new JLabel("登录用户:");
  526. user_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  527. user_status_label.setForeground(Color.BLACK);
  528. user_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  529. user_status_label.setBackground(Color.LIGHT_GRAY);
  530. toolBar.add(user_status_label);
  531. user_menu = new JButton("JinJuShi");
  532. user_menu.setForeground(Color.GREEN);
  533. user_menu.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  534. user_menu.setBackground(Color.BLACK);
  535. toolBar.add(user_menu);
  536. JLabel space_3 = new JLabel(" ");
  537. toolBar.add(space_3);
  538. JLabel space_4 = new JLabel(" ");
  539. toolBar.add(space_4);
  540. tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  541. tabbedPane.setMinimumSize(new Dimension(400, 50));
  542. tabbedPane.setFont(new Font("宋体", Font.BOLD, 22));
  543. contentPane.add(tabbedPane);
  544. //首页
  545. JPanel indexPanelA = new JPanel();
  546. indexScrollPaneA = new JScrollPane(indexPanelA);
  547. indexPanelA.setLayout(null);
  548. indexPanelA.setPreferredSize(new Dimension(4*MesClientComponent.workWidth, MesClientComponent.workHeight + 40)); // OP180 一行四台 CNC
  549. indexScrollPaneA.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  550. indexScrollPaneA.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  551. product_sn = new JTextField();
  552. product_sn.setHorizontalAlignment(SwingConstants.CENTER);
  553. product_sn.setEditable(false);
  554. product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  555. product_sn.setBounds(81, 70, 602, 70);
  556. product_sn.setVisible(false); // 隐藏工件码输入框
  557. indexPanelA.add(product_sn);
  558. product_sn.setColumns(10);
  559. f_scan_data_bt_1 = new JButton("扫码");
  560. f_scan_data_bt_1.addActionListener(new ActionListener() {
  561. public void actionPerformed(ActionEvent e) {
  562. scan_type = 1;
  563. scanBarcode();
  564. }
  565. });
  566. f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  567. f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  568. f_scan_data_bt_1.setBounds(693, 70, 198, 70);
  569. f_scan_data_bt_1.setVisible(false); // 隐藏扫码按钮
  570. indexPanelA.add(f_scan_data_bt_1);
  571. // String[] hjtitles = new String[]{"焊机1","焊机2","焊机3"};
  572. // String[] hjvals = new String[]{"HJ001","HJ002","HJ003"};
  573. // mesRadioHj = new MesRadio(hjtitles,hjvals);
  574. // mesRadioHj.setSize(500,50);
  575. // mesRadioHj.setBounds(190,170,500,50);
  576. // indexPanelA.add(mesRadioHj);
  577. fxlabel = new JLabel("该工件为返修件,请仔细检查");
  578. fxlabel.setFont(new Font("微软雅黑", Font.PLAIN, 38));
  579. fxlabel.setBounds(81, 170, 810, 70);
  580. fxlabel.setForeground(Color.RED);
  581. fxlabel.setHorizontalAlignment(SwingConstants.CENTER);
  582. fxlabel.setVisible(false);
  583. indexPanelA.add(fxlabel);
  584. // 创建工位面板
  585. createOprnoPanel(indexPanelA);
  586. finish_ok_bt = new JButton("OK");
  587. finish_ok_bt.setEnabled(false);
  588. finish_ok_bt.addActionListener(new ActionListener() {
  589. public void actionPerformed(ActionEvent e) {
  590. if(work_status == 1 && check_quality_result){
  591. String sn = getBarcode(product_sn.getText());
  592. if(sn.isEmpty()){
  593. MesClient.setMenuStatus("工件码为空,请重试",-1);
  594. return;
  595. }
  596. getUser();
  597. String qret = "OK";
  598. Boolean sendret = DataUtil.sendQuality(nettyClient,sn,qret,user20);
  599. if(!sendret){
  600. MesClient.setMenuStatus("消息发送失败,请重试",-1);
  601. return;
  602. }else{
  603. // MesClient.resetScanA();
  604. // MesClient.scan_type = 1;
  605. // MesClient.scanBarcode();
  606. // MesClient.setMenuStatus("结果提交成功,请扫下一件",0);
  607. }
  608. }
  609. }
  610. });
  611. finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
  612. finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  613. finish_ok_bt.setBounds(185, 291, 240, 80);
  614. finish_ok_bt.setEnabled(false);
  615. finish_ok_bt.setVisible(false); // 隐藏OK按钮
  616. indexPanelA.add(finish_ok_bt);
  617. finish_ng_bt = new JButton("NG");
  618. finish_ng_bt.setEnabled(false);
  619. finish_ng_bt.addActionListener(new ActionListener() {
  620. public void actionPerformed(ActionEvent e) {
  621. if(work_status == 1 && check_quality_result){
  622. String sn = getBarcode(product_sn.getText());
  623. if(sn.isEmpty()){
  624. MesClient.setMenuStatus("工件码为空,请重试",-1);
  625. return;
  626. }
  627. getUser();
  628. String qret = "NG";
  629. Boolean sendret = DataUtil.sendQuality(nettyClient,sn,qret,user20);
  630. if(!sendret){
  631. MesClient.setMenuStatus("消息发送失败,请重试",-1);
  632. return;
  633. }else{
  634. // MesClient.resetScanA();
  635. // MesClient.scan_type = 1;
  636. // MesClient.scanBarcode();
  637. // MesClient.setMenuStatus("结果提交成功,请扫下一件",0);
  638. }
  639. }
  640. }
  641. });
  642. finish_ng_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ng_bg.png")));
  643. finish_ng_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  644. finish_ng_bt.setBounds(508, 291, 240, 80);
  645. finish_ng_bt.setEnabled(false);
  646. finish_ng_bt.setVisible(false); // 隐藏NG按钮
  647. indexPanelA.add(finish_ng_bt);
  648. tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null);
  649. tabbedPane.setEnabledAt(0, true);
  650. // searchScrollPane = new JScrollPane((Component) null);
  651. indexPanelC = new JPanel();
  652. searchScrollPaneDj = new JScrollPane(indexPanelC);
  653. indexPanelC.setLayout(null);
  654. tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null);
  655. indexPanelB = new JPanel(new BorderLayout());
  656. indexPanelB.setPreferredSize(new Dimension(990, 580));
  657. searchScrollPane = new JScrollPane(indexPanelB);
  658. tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
  659. tabbedPane.addChangeListener(new ChangeListener() {
  660. @Override
  661. public void stateChanged(ChangeEvent e) {
  662. JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
  663. int selectedIndex = tabbedPane.getSelectedIndex();
  664. System.out.println("selectedIndex:"+selectedIndex);
  665. if(selectedIndex == 1){
  666. }
  667. }
  668. });
  669. }
  670. //页面菜单状态
  671. public static void setMenuStatus(String msg,int error){
  672. if(error == 0){
  673. MesClient.status_menu.setForeground(Color.GREEN);
  674. }else{
  675. MesClient.status_menu.setForeground(Color.RED);
  676. }
  677. MesClient.status_menu.setText(msg);
  678. }
  679. /** 工作记录:按配置机台建分标签,首次点到某机台再加载对应网页 */
  680. public static void initWorkRecordTabs() {
  681. if (workRecordInited) {
  682. return;
  683. }
  684. workRecordInited = true;
  685. indexPanelB.removeAll();
  686. indexPanelB.setLayout(new BorderLayout());
  687. workRecordTabs = new JTabbedPane();
  688. workRecordTabs.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  689. List<String> codes = new ArrayList<>(oprnoMap.keySet());
  690. Collections.sort(codes);
  691. for (String code : codes) {
  692. JPanel holder = new JPanel(new BorderLayout());
  693. holder.putClientProperty("oprno", code);
  694. holder.putClientProperty("loaded", Boolean.FALSE);
  695. workRecordTabs.addTab(code, holder);
  696. }
  697. workRecordTabs.addChangeListener(new ChangeListener() {
  698. @Override
  699. public void stateChanged(ChangeEvent e) {
  700. loadSelectedWorkRecordTab();
  701. }
  702. });
  703. indexPanelB.add(workRecordTabs, BorderLayout.CENTER);
  704. if (workRecordTabs.getTabCount() > 0) {
  705. loadSelectedWorkRecordTab();
  706. }
  707. indexPanelB.revalidate();
  708. indexPanelB.repaint();
  709. }
  710. private static void loadSelectedWorkRecordTab() {
  711. if (workRecordTabs == null) {
  712. return;
  713. }
  714. int i = workRecordTabs.getSelectedIndex();
  715. if (i < 0) {
  716. return;
  717. }
  718. Component c = workRecordTabs.getComponentAt(i);
  719. if (!(c instanceof JPanel)) {
  720. return;
  721. }
  722. JPanel holder = (JPanel) c;
  723. if (Boolean.TRUE.equals(holder.getClientProperty("loaded"))) {
  724. return;
  725. }
  726. String code = String.valueOf(holder.getClientProperty("oprno"));
  727. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/work?oprno=" + code + "&lineSn=" + mes_line_sn;
  728. MesWebView wv = new MesWebView(url);
  729. holder.add(wv, BorderLayout.CENTER);
  730. holder.putClientProperty("loaded", Boolean.TRUE);
  731. holder.revalidate();
  732. holder.repaint();
  733. if (jfxPanel == null) {
  734. jfxPanel = wv;
  735. }
  736. }
  737. public static void getMaterailData(){
  738. try{
  739. JSONObject retObj = DataUtil.getBindMaterail();
  740. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  741. java.util.List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  742. int i = 0;
  743. rowData = new Object[arrs.size()][7];
  744. for (BindMaterialResp bindMaterialResp:arrs){
  745. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  746. rowData[i][1] = bindMaterialResp.getBatchSn();
  747. rowData[i][2] = bindMaterialResp.getLastTimes();
  748. rowData[i][3] = "";
  749. rowData[i][4] = bindMaterialResp.getCraft();
  750. rowData[i][5] = bindMaterialResp.getMaterialId();
  751. rowData[i][6] = bindMaterialResp.getType();
  752. i++;
  753. }
  754. bindBatchPanel();
  755. }
  756. }catch (Exception e){
  757. log.info(e.getMessage());
  758. }
  759. }
  760. public static void updateMaterailData(){
  761. try{
  762. JSONObject retObj = DataUtil.getBindMaterail();
  763. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  764. List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  765. int i = 0;
  766. for (BindMaterialResp bindMaterialResp:arrs){
  767. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  768. rowData[i][1] = bindMaterialResp.getBatchSn();
  769. rowData[i][2] = bindMaterialResp.getLastTimes();
  770. rowData[i][3] = "";
  771. rowData[i][4] = bindMaterialResp.getCraft();
  772. rowData[i][5] = bindMaterialResp.getMaterialId();
  773. rowData[i][6] = bindMaterialResp.getType();
  774. i++;
  775. }
  776. MesClient.table.repaint();
  777. }
  778. }catch (Exception e){
  779. log.info(e.getMessage());
  780. }
  781. }
  782. // 绑定物料批次码
  783. public static void scanBatchSn(BindMaterialResp bindMaterialResp) {
  784. //弹窗扫工件码
  785. String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle();
  786. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  787. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  788. JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType());
  789. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  790. MesClient.setMenuStatus("扫物料:"+bindMaterialResp.getMaterialTitle()+"成功",0);
  791. updateMaterailData();
  792. }else{
  793. if(retObj.get("result")==null){
  794. MesClient.setMenuStatus("请求失败,请重试",-1);
  795. }else{
  796. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  797. MesClient.setMenuStatus(retObj.getString("message"),-1);
  798. }
  799. }
  800. }
  801. }
  802. }
  803. public static void bindBatchPanel(){
  804. JPanel indexPanelBB = new JPanel();
  805. JPanel panel = new JPanel();
  806. panel.setBounds(0, 0, 990, 550);
  807. // panel.setBounds(81, 50, 810, 479);
  808. indexPanelBB.add(panel);
  809. panel.setLayout(new GridLayout(0, 1, 0, 0));
  810. table = new JTable(rowData, columnNames){
  811. public boolean isCellEditable(int row, int column) {
  812. if(column == 3){
  813. return true;
  814. }
  815. return false;
  816. }
  817. };
  818. table.setRowHeight(40);
  819. table.setEnabled(true);
  820. table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  821. table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton());
  822. table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton());
  823. JScrollPane scrollPane = new JScrollPane(table);
  824. panel.add(scrollPane);
  825. JScrollPane indexScrollPaneB = new JScrollPane(indexPanelBB);
  826. indexPanelBB.setLayout(null);
  827. tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null);
  828. }
  829. // 开启发送结果定时任务
  830. public static void startUpdateQualityTimer(){
  831. // 创建异步线程池
  832. ExecutorService executorService = Executors.newSingleThreadExecutor();
  833. // 提交任务
  834. executorService.submit(() -> {
  835. log.info("异步任务执行中...");
  836. // 模拟耗时操作
  837. try {
  838. while (true){
  839. try {
  840. // 确保连接已经打开
  841. if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
  842. JdbcUtils.openConnection();
  843. }
  844. Statement statement = JdbcUtils.conn.createStatement();
  845. String selectSql = "SELECT id, bw FROM submit_record\n" +
  846. "WHERE state = 0 ORDER BY id DESC LIMIT 10";
  847. String updateSql = "UPDATE submit_record \n" +
  848. "SET state = '1'\n" +
  849. "WHERE id = ";
  850. ResultSet rs = statement.executeQuery(selectSql);
  851. while(rs.next()){
  852. int id = rs.getInt("id");
  853. String bw = rs.getString("bw");
  854. // 发送请求
  855. String url = "http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesProductRecord/updateQualityByTiming";
  856. log.info("质量:"+bw);
  857. String s = HttpUtils.sendPostRequestJson(url, bw );
  858. log.info("结果:"+s);
  859. Boolean result = JSONObject.parseObject(s).getBoolean("result");
  860. if(result) {
  861. // 更改状态为 1
  862. statement.executeUpdate(updateSql + id);
  863. }
  864. }
  865. rs.close();
  866. statement.close();
  867. } catch (Exception e) {
  868. log.info(e.getMessage());
  869. }
  870. try {
  871. Thread.sleep(2000);
  872. }catch (Exception e){
  873. log.info(e.getMessage());
  874. }
  875. }
  876. } catch (Exception e) {
  877. log.info(e.getMessage());
  878. }
  879. log.info("异步任务执行完毕");
  880. });
  881. }
  882. /**
  883. * 从 Properties 中提取所有工位信息,填充到 oprnoMap(跳过指定属性)
  884. */
  885. private static void loadOprnoMap(Properties pro) {
  886. Enumeration<?> keys = pro.propertyNames();
  887. while (keys.hasMoreElements()) {
  888. String key = (String) keys.nextElement();
  889. Matcher matcher = OPRNO_PATTERN.matcher(key);
  890. if (matcher.find()) {
  891. String oprnoCode = matcher.group(1); // 工位号(如 OP090A、HSB)
  892. String propertyName = matcher.group(2); // 属性名(如 hgA_out、hgA_qr_code)
  893. // 跳过需要忽略的属性(hgA_qr_code、hgB_qr_code)
  894. if (isSkipProperty(propertyName)) {
  895. log.debug("跳过忽略的属性:{}(工位:{})", propertyName, oprnoCode);
  896. continue;
  897. }
  898. String propertyValue = pro.getProperty(key); // 属性值
  899. // 从 Map 中获取或创建 Oprno 对象
  900. Oprno oprno = oprnoMap.get(oprnoCode);
  901. if (oprno == null) {
  902. oprno = new Oprno();
  903. oprno.setOprno(oprnoCode);
  904. oprnoMap.put(oprnoCode, oprno);
  905. }
  906. // 反射注入属性值
  907. setOprnoProperty(oprno, propertyName, propertyValue);
  908. }
  909. }
  910. // log.info("成功加载 {} 个工位配置:{}", oprnoMap.size(), oprnoMap.keySet());
  911. }
  912. /**
  913. * 判断属性是否需要跳过
  914. */
  915. private static boolean isSkipProperty(String propertyName) {
  916. for (String skipProp : SKIP_PROPERTIES) {
  917. if (skipProp.equals(propertyName)) {
  918. return true;
  919. }
  920. }
  921. return false;
  922. }
  923. /**
  924. * 反射给 Oprno 对象设置属性值
  925. */
  926. private static void setOprnoProperty(Oprno oprno, String propertyName, String propertyValue) {
  927. try {
  928. Field field = Oprno.class.getDeclaredField(propertyName);
  929. field.setAccessible(true);
  930. field.set(oprno, propertyValue);
  931. } catch (NoSuchFieldException e) {
  932. // log.warn("Oprno 类不存在字段:{},该属性值未注入", propertyName);
  933. } catch (IllegalAccessException e) {
  934. // log.error("设置 Oprno 属性失败:{}", propertyName, e);
  935. }
  936. }
  937. }