MesClient.java 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.fazecast.jSerialComm.SerialPort;
  4. import com.fazecast.jSerialComm.SerialPortEvent;
  5. import com.mes.component.MesRadio;
  6. import com.mes.component.MesWebView;
  7. import com.mes.laser.LaserFlowManager;
  8. import com.mes.netty.NettyClient;
  9. import com.mes.util.DateLocalUtils;
  10. import com.mes.util.JdbcUtils;
  11. import javafx.embed.swing.JFXPanel;
  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.ActionEvent;
  18. import java.awt.event.ActionListener;
  19. import java.awt.event.MouseAdapter;
  20. import java.awt.event.MouseEvent;
  21. import java.io.BufferedReader;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.InputStreamReader;
  25. import java.time.LocalDateTime;
  26. import java.util.List;
  27. import java.util.Properties;
  28. import java.util.Timer;
  29. import java.util.TimerTask;
  30. import java.util.concurrent.ExecutorService;
  31. import java.util.concurrent.Executors;
  32. public class MesClient extends JFrame {
  33. private static String Drivde = "org.sqlite.JDBC";
  34. public static int mes_auth = 0; // 权限级别 0=无权限 1=操作工人 2=管理员
  35. public static String mes_gw = ""; // 工位号
  36. public static String mes_gw_des = ""; // 工位名称
  37. public static String mes_server_ip = ""; // 服务器IP地址
  38. public static int mes_tcp_port = 3000; // TCP服务端口
  39. public static int mes_heart_beat_cycle = 10; // 心跳周期
  40. public static int mes_heart_icon_cycle = 1;
  41. public static String mes_line_sn = ""; // 产线编号
  42. //TCP连接
  43. public static NettyClient nettyClient;
  44. //TCP连接状态
  45. public static boolean tcp_connect_flag = false;
  46. //TCP连接请求
  47. public static boolean connect_request_flag = false;
  48. //session
  49. public static String sessionid = "";
  50. public static JPanel contentPane;
  51. public static MesClient mesClientFrame;
  52. public static JTabbedPane tabbedPane;
  53. public static JScrollPane indexScrollPaneA;
  54. public static JScrollPane searchScrollPane;
  55. public static JScrollPane searchScrollPaneDj;
  56. public static Boolean check_quality_result = false;
  57. public static Integer work_status = 0;
  58. public static JButton heart_beat_menu;
  59. public static JButton status_menu;
  60. public static JButton user_menu;
  61. public static int scan_type = 0;
  62. public static JButton finish_ok_bt;
  63. public static JButton finish_ng_bt;
  64. public static JTextField product_sn;
  65. public static JButton f_scan_data_bt_1;
  66. public static JTextField batch_sn; // 物料批次码
  67. public static JButton batch_sn_btn;
  68. public static String user20 = "";
  69. public static JFrame welcomeWin;
  70. public static JPanel indexPanelB;
  71. public static MesWebView jfxPanel = null;
  72. public static JPanel indexPanelC;
  73. public static MesWebView jfxPanel2 = null;
  74. public static MesRadio mesRadioHj;
  75. public static JTable table;
  76. public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" };
  77. public static Object[][] rowData = null;
  78. public static Boolean checkState = false; // 点检状态 false=未点检 true=已点检
  79. public static SerialPort serialPort; // 串口对象
  80. // 镭雕相关
  81. public static JButton laser_get_code_bt; // 获取镭雕码按钮
  82. public static JButton laser_reset_bt; // 人工复位按钮
  83. public static LaserFlowManager laserFlowManager;
  84. public static void main(String[] args) {
  85. if (LockUtil.getInstance().isAppActive() == true){
  86. // JOptionPane.showMessageDialog(null, "已有一个程序在运行,程序退出");
  87. return;
  88. }else{
  89. // 设置支持高DPI
  90. System.setProperty("sun.java2d.uiScale", "1.0");
  91. System.setProperty("sun.java2d.dpiaware", "true");
  92. EventQueue.invokeLater(new Runnable() {
  93. @Override
  94. public void run() {
  95. try{
  96. //读文件配置
  97. readProperty();
  98. // 显示界面
  99. mesClientFrame = new MesClient();
  100. mesClientFrame.setVisible(false);
  101. JdbcUtils.getConn();
  102. welcomeWin = new LoginFarme();
  103. welcomeWin.setVisible(true);
  104. getMaterailData();
  105. serialPort = ModbusRtu.connect();
  106. // checkTimer()已移除:原用于每3秒驱动继电器开关(formatDeviceState),
  107. // 继电器功能已不再使用,保留会导致无意义的轮询打印
  108. // 启动镭雕流程管理器(含本地HTTP服务,接收镭雕软件完成上报)
  109. initLaserFlow();
  110. }catch (Exception e){
  111. e.printStackTrace();
  112. }
  113. }
  114. });
  115. }
  116. }
  117. public static java.util.Timer checkTimer;
  118. public static void checkTimer() {
  119. if(checkTimer!=null) {
  120. checkTimer.cancel();
  121. }
  122. checkTimer = new java.util.Timer();
  123. checkTimer.schedule(new TimerTask() {
  124. public void run() {
  125. if(!MesClient.sessionid.isEmpty()){
  126. getCheckResult();
  127. }
  128. }
  129. }, 100,3*1000);
  130. }
  131. //读配置文件
  132. private static void readProperty() throws IOException{
  133. String enconding = "UTF-8";
  134. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  135. Properties pro = new Properties();
  136. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  137. pro.load(br);
  138. mes_gw = pro.getProperty("mes.gw");
  139. // mes_gw_des = pro.getProperty("mes.gw_des");
  140. mes_server_ip = pro.getProperty("mes.server_ip");
  141. mes_tcp_port = Integer.parseInt(pro.getProperty("mes.tcp_port"));
  142. mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle"));
  143. mes_line_sn = pro.getProperty("mes.line_sn");
  144. mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
  145. System.out.println(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle);
  146. }
  147. // 初始化TCP
  148. public static void initTcpConnection() {
  149. try {
  150. if(nettyClient==null) {
  151. //初始化TCP连接
  152. nettyClient = new NettyClient();
  153. //TCP连接状态
  154. tcp_connect_flag = false;
  155. //设置TCP请求状态
  156. connect_request_flag = true;
  157. DataUtil.synrTcp(nettyClient,mes_gw);
  158. }
  159. } catch (Exception e) {
  160. // TODO Auto-generated catch block
  161. e.printStackTrace();
  162. }
  163. }
  164. //启动心跳包程序
  165. public static java.util.Timer heartBeatTimer;
  166. public static java.util.Timer heartBeatIconTimer;
  167. public static boolean iconREDFlag = true;
  168. public static void startHeartBeatTimer() {
  169. if(heartBeatTimer!=null) {
  170. heartBeatTimer.cancel();
  171. }
  172. heartBeatTimer = new java.util.Timer();
  173. heartBeatTimer.schedule(new TimerTask() {
  174. public void run() {
  175. if(nettyClient!=null&&tcp_connect_flag) {
  176. //System.out.println("发送心跳报文");
  177. DataUtil.heartBeat(nettyClient,mes_gw);
  178. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  179. }
  180. }
  181. }, 100,mes_heart_beat_cycle*1000);
  182. //心跳显示图标
  183. if(heartBeatIconTimer!=null) {
  184. heartBeatIconTimer.cancel();
  185. }
  186. heartBeatIconTimer = new Timer();
  187. heartBeatIconTimer.schedule(new TimerTask() {
  188. public void run() {
  189. if(tcp_connect_flag) {
  190. if(iconREDFlag) {
  191. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  192. iconREDFlag = false;
  193. }else {
  194. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  195. iconREDFlag = true;
  196. }
  197. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  198. heart_beat_menu.repaint();
  199. }else {
  200. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  201. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  202. heart_beat_menu.repaint();
  203. //若未连接则尝试连接
  204. if(nettyClient!=null&&!tcp_connect_flag){
  205. System.out.println("TCP已断开");
  206. //TCP重新同步连接
  207. if(!connect_request_flag) {
  208. //System.out.println("TCP重新同步连接");
  209. //设置TCP请求状态,只重新同步连接一次
  210. connect_request_flag = true;
  211. DataUtil.synrTcp(nettyClient,mes_gw);
  212. }
  213. }
  214. }
  215. }
  216. }, 100,mes_heart_icon_cycle*1000);
  217. }
  218. //设置tcp连接状态显示
  219. public static void setTcpStatus() {
  220. if(tcp_connect_flag) {
  221. status_menu.setText("已连接MES服务器");
  222. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  223. heart_beat_menu.repaint();
  224. }else {
  225. status_menu.setText("未连接MES服务器");
  226. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  227. heart_beat_menu.repaint();
  228. }
  229. }
  230. public static void initWarehouseData(){
  231. resetScanA();
  232. }
  233. public static void resetScanA() {
  234. work_status = 0;
  235. check_quality_result = false;
  236. MesClient.finish_ok_bt.setEnabled(false);
  237. MesClient.finish_ng_bt.setEnabled(false);
  238. product_sn.setText("");
  239. batch_sn.setText("");
  240. // MesClient.f_scan_data_bt_1.setEnabled(true);
  241. MesClient.setMenuStatus("请扫工件码",0);
  242. updateMaterailData();
  243. // getCheckResult()调用已移除:原用于驱动继电器(formatDeviceState),继电器功能已不再使用
  244. // 注释掉换班检查,避免自动退出
  245. // shiftUserCheck();
  246. }
  247. public static int userLoginHours;//用户登录所处小时
  248. //换班用户信息检查(已注释,避免早上8点自动关闭)
  249. /*
  250. private static void shiftUserCheck() {
  251. LocalDateTime now = LocalDateTime.now();
  252. // 判断时间范围
  253. if (userLoginHours >= 8 && userLoginHours < 20) {
  254. int hour = now.getHour();
  255. if (hour >= 20 || hour < 8) {
  256. logoff();
  257. }
  258. } else {
  259. int hour = now.getHour();
  260. if (hour >= 8 && hour < 20) {
  261. logoff();
  262. }
  263. }
  264. }
  265. */
  266. public static void resetScanB() {
  267. }
  268. //获取用户20位
  269. public static void getUser() {
  270. user20 = user_menu.getText().toString();
  271. String space_tmp1 = "";
  272. if(user20.length()<20) {
  273. for(int i=0;i<20-user20.length();i++) {
  274. space_tmp1 = space_tmp1 + " ";
  275. }
  276. }
  277. user20 = user20 + space_tmp1;
  278. }
  279. //获取barcode内容36位
  280. public static String getBarcode(String barcodeTmp) {
  281. String barcodeRet = barcodeTmp;
  282. if(barcodeTmp.equalsIgnoreCase("")) {
  283. return "";
  284. }else {
  285. if(barcodeTmp.length()<36) {
  286. String space = "";
  287. for(int i=0;i<36-barcodeTmp.length();i++) {
  288. space = space + " ";
  289. }
  290. barcodeRet = barcodeTmp + space;
  291. }
  292. }
  293. return barcodeRet;
  294. }
  295. public static void setMenuStatus(String msg,int error){
  296. if(error == 0){
  297. MesClient.status_menu.setForeground(Color.GREEN);
  298. }else{
  299. MesClient.status_menu.setForeground(Color.RED);
  300. }
  301. MesClient.status_menu.setText(msg);
  302. }
  303. public static void scanBarcode() {
  304. String scanBarcodeTitle = "";
  305. switch(scan_type) {
  306. case 1:
  307. product_sn.setText("");
  308. scanBarcodeTitle = "请扫工件码";
  309. break;
  310. case 2:
  311. batch_sn.setText("");
  312. scanBarcodeTitle = "请扫物料批次码";
  313. break;
  314. }
  315. //弹窗扫工件码
  316. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  317. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  318. //获取用户
  319. getUser();
  320. //获取扫码内容36位
  321. String barcode36 = getBarcode(scanBarcode);//处理36为码
  322. //工位号
  323. String gw = "";
  324. switch(scan_type) {
  325. case 1:
  326. product_sn.setText(scanBarcode);
  327. break;
  328. case 2:
  329. batch_sn.setText(scanBarcode);
  330. break;
  331. }
  332. //刷新界面
  333. mesClientFrame.repaint();
  334. if(!tcp_connect_flag) {
  335. MesClient.setMenuStatus("设备未连接Mes服务器",1);
  336. // JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  337. return;
  338. }
  339. // if(!product_sn.getText().isEmpty() && !batch_sn.getText().isEmpty()){
  340. // finish_ok_bt.setEnabled(true);
  341. // finish_ng_bt.setEnabled(true);
  342. // }
  343. if(!product_sn.getText().isEmpty()){
  344. // 扫码后自动提交OK
  345. String sn = getBarcode(product_sn.getText());
  346. if(!sn.isEmpty()){
  347. String qret = "OK";
  348. Boolean sendret = DataUtil.sendCreate(nettyClient,sn,qret,user20);
  349. if(!sendret){
  350. MesClient.setMenuStatus("消息发送失败,请重试",1);
  351. return;
  352. }
  353. // 提交成功后,弹出新的扫码框
  354. SwingUtilities.invokeLater(new Runnable() {
  355. @Override
  356. public void run() {
  357. scan_type = 1;
  358. scanBarcode();
  359. }
  360. });
  361. }
  362. }
  363. // 查询工件质量
  364. // Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20);
  365. // if(!sendret){
  366. // JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  367. // return;
  368. // }
  369. }else {
  370. JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  371. return;
  372. }
  373. }
  374. // 复用op040既有建档提交逻辑(原finish_ok_bt/finish_ng_bt里的sendCreate调用抽出来)
  375. // sn: 用于提交的编码(客户编码),qret: OK/NG
  376. public static boolean submitLaserResult(String sn, String qret){
  377. if(sn == null || sn.isEmpty()){
  378. setMenuStatus("客户编码为空,无法提交",1);
  379. return false;
  380. }
  381. getUser();
  382. String snPad = getBarcode(sn);
  383. Boolean sendret = DataUtil.sendCreate(nettyClient, snPad, qret, user20);
  384. if(!sendret){
  385. setMenuStatus("结果提交MES失败,请重试",1);
  386. return false;
  387. }
  388. return true;
  389. }
  390. // 初始化镭雕流程管理器
  391. public static void initLaserFlow(){
  392. laserFlowManager = new LaserFlowManager(new LaserFlowManager.UiCallback() {
  393. @Override
  394. public void onStatus(String msg, boolean error) {
  395. SwingUtilities.invokeLater(() -> setMenuStatus(msg, error ? 1 : 0));
  396. }
  397. @Override
  398. public void onLockButton(boolean lock) {
  399. SwingUtilities.invokeLater(() -> {
  400. if(laser_get_code_bt != null){
  401. laser_get_code_bt.setEnabled(!lock);
  402. }
  403. });
  404. }
  405. @Override
  406. public void onCodeReceived(String code) {
  407. SwingUtilities.invokeLater(() -> product_sn.setText(code));
  408. }
  409. @Override
  410. public boolean onSubmitResult(String customerSn, String qret) {
  411. return submitLaserResult(customerSn, qret);
  412. }
  413. });
  414. laserFlowManager.startCompleteServer();
  415. }
  416. public static void logoff() {
  417. // welcomeWin.setVisible(true);
  418. mesClientFrame.setVisible(false);
  419. if(laserFlowManager != null){
  420. laserFlowManager.stopCompleteServer();
  421. }
  422. nettyClient = null;
  423. // if(heartBeatTimer!=null) {
  424. // heartBeatTimer.cancel();
  425. // }
  426. // if(heartBeatIconTimer!=null) {
  427. // heartBeatIconTimer.cancel();
  428. // }
  429. tcp_connect_flag = false;
  430. connect_request_flag = false;
  431. }
  432. public MesClient() {
  433. setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png")));
  434. setTitle("MES系统客户端:"+mes_gw + "- " + mes_gw_des);
  435. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  436. setBounds(0, 0, 1024, 768);
  437. // 确保标题栏正确显示
  438. validate();
  439. JMenuBar menuBar = new JMenuBar();
  440. menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26));
  441. setJMenuBar(menuBar);
  442. JMenu fileMenu = new JMenu("用户");
  443. fileMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png")));
  444. fileMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  445. menuBar.add(fileMenu);
  446. JMenuItem exitMenuItem = new JMenuItem("退出");
  447. exitMenuItem.setIcon(new ImageIcon(MesClient.class.getResource("/bg/logoff.png")));
  448. exitMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  449. fileMenu.add(exitMenuItem);
  450. exitMenuItem.addMouseListener(new MouseAdapter() {
  451. @Override
  452. public void mousePressed(MouseEvent e) {//只能检测到mousePressed事件
  453. super.mouseClicked(e);
  454. //dispose();
  455. logoff();
  456. }
  457. });
  458. JMenu settingMenu = new JMenu("设置");
  459. //settingMenu.setVisible(false);
  460. settingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
  461. settingMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  462. menuBar.add(settingMenu);
  463. JMenuItem resetTcpMenu = new JMenuItem("\u91CD\u8FDEMES");
  464. resetTcpMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  465. resetTcpMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  466. resetTcpMenu.addMouseListener(new MouseAdapter() {
  467. @Override
  468. public void mousePressed(MouseEvent e) {
  469. super.mouseClicked(e);
  470. //重连mes
  471. nettyClient.future.channel().close();
  472. }
  473. });
  474. settingMenu.add(resetTcpMenu);
  475. JMenuItem resetTcpMenu_1 = new JMenuItem("刷新工件");
  476. resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  477. resetTcpMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  478. resetTcpMenu_1.addMouseListener(new MouseAdapter() {
  479. @Override
  480. public void mousePressed(MouseEvent e) {
  481. super.mouseClicked(e);
  482. resetScanA();
  483. }
  484. });
  485. settingMenu.add(resetTcpMenu_1);
  486. contentPane = new JPanel();
  487. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  488. setContentPane(contentPane);
  489. contentPane.setLayout(new BorderLayout(0, 0));
  490. JToolBar toolBar = new JToolBar();
  491. contentPane.add(toolBar, BorderLayout.NORTH);
  492. JLabel equipment_statu_label = new JLabel("状态:");
  493. equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER);
  494. equipment_statu_label.setForeground(Color.BLACK);
  495. equipment_statu_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  496. equipment_statu_label.setBackground(Color.LIGHT_GRAY);
  497. toolBar.add(equipment_statu_label);
  498. status_menu = new JButton("设备未连接MES服务器");
  499. if(tcp_connect_flag) {
  500. status_menu.setText("已连接MES服务器");
  501. status_menu.setForeground(Color.GREEN);
  502. }else {
  503. status_menu.setText("未连接MES服务器");
  504. status_menu.setForeground(Color.DARK_GRAY);
  505. }
  506. status_menu.addActionListener(new ActionListener() {
  507. public void actionPerformed(ActionEvent e) {
  508. }
  509. });
  510. status_menu.setForeground(Color.GREEN);
  511. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  512. status_menu.setBackground(Color.BLACK);
  513. toolBar.add(status_menu);
  514. JLabel space_1 = new JLabel(" ");
  515. toolBar.add(space_1);
  516. JLabel heart_beat_status_label = new JLabel("心跳:");
  517. heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  518. heart_beat_status_label.setForeground(Color.BLACK);
  519. heart_beat_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  520. heart_beat_status_label.setBackground(Color.LIGHT_GRAY);
  521. toolBar.add(heart_beat_status_label);
  522. heart_beat_menu = new JButton("2024-02-20 23:20:10");
  523. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  524. heart_beat_menu.setForeground(Color.GREEN);
  525. heart_beat_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  526. heart_beat_menu.setBackground(Color.BLACK);
  527. toolBar.add(heart_beat_menu);
  528. JLabel space_2 = new JLabel(" ");
  529. toolBar.add(space_2);
  530. JLabel user_status_label = new JLabel("登录用户:");
  531. user_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  532. user_status_label.setForeground(Color.BLACK);
  533. user_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  534. user_status_label.setBackground(Color.LIGHT_GRAY);
  535. toolBar.add(user_status_label);
  536. user_menu = new JButton("JinJuShi");
  537. user_menu.setForeground(Color.GREEN);
  538. user_menu.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  539. user_menu.setBackground(Color.BLACK);
  540. toolBar.add(user_menu);
  541. JLabel space_3 = new JLabel(" ");
  542. toolBar.add(space_3);
  543. JLabel space_4 = new JLabel(" ");
  544. toolBar.add(space_4);
  545. tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  546. tabbedPane.setMinimumSize(new Dimension(400, 50));
  547. tabbedPane.setFont(new Font("宋体", Font.BOLD, 22));
  548. contentPane.add(tabbedPane);
  549. //首页
  550. JPanel indexPanelA = new JPanel();
  551. indexScrollPaneA = new JScrollPane(indexPanelA);
  552. indexPanelA.setLayout(null);
  553. // 镭雕:获取镭雕码 / 复位按钮,放在扫码框(回显框)上方
  554. laser_get_code_bt = new JButton("获取镭雕码");
  555. laser_get_code_bt.addActionListener(new ActionListener() {
  556. public void actionPerformed(ActionEvent e) {
  557. laserFlowManager.startLaserFlow(mes_gw, mes_line_sn);
  558. }
  559. });
  560. laser_get_code_bt.setFont(new Font("微软雅黑", Font.PLAIN, 24));
  561. laser_get_code_bt.setBounds(81, 40, 300, 50);
  562. indexPanelA.add(laser_get_code_bt);
  563. // 镭雕:人工复位按钮(完成信号超时或异常时使用,强制解锁并作废当前请求)
  564. laser_reset_bt = new JButton("复位");
  565. laser_reset_bt.addActionListener(new ActionListener() {
  566. public void actionPerformed(ActionEvent e) {
  567. laserFlowManager.manualReset();
  568. }
  569. });
  570. laser_reset_bt.setFont(new Font("微软雅黑", Font.PLAIN, 24));
  571. laser_reset_bt.setBounds(400, 40, 150, 50);
  572. indexPanelA.add(laser_reset_bt);
  573. product_sn = new JTextField();
  574. product_sn.setHorizontalAlignment(SwingConstants.CENTER);
  575. product_sn.setEditable(false);
  576. product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  577. product_sn.setBounds(81, 100, 602, 70);
  578. indexPanelA.add(product_sn);
  579. product_sn.setColumns(10);
  580. // 原扫码按钮不再需要,隐藏但保留逻辑(scanBarcode等方法仍被其他流程复用)
  581. f_scan_data_bt_1 = new JButton("扫码");
  582. f_scan_data_bt_1.addActionListener(new ActionListener() {
  583. public void actionPerformed(ActionEvent e) {
  584. scan_type = 1;
  585. scanBarcode();
  586. }
  587. });
  588. f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  589. f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  590. f_scan_data_bt_1.setBounds(693, 100, 198, 70);
  591. f_scan_data_bt_1.setVisible(false);
  592. indexPanelA.add(f_scan_data_bt_1);
  593. JLabel batch_label = new JLabel("绑定物料批次码");
  594. batch_label.setHorizontalAlignment(SwingConstants.CENTER);
  595. batch_label.setForeground(Color.BLACK);
  596. batch_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  597. batch_label.setBackground(Color.LIGHT_GRAY);
  598. batch_label.setBounds(81, 140, 602, 70);
  599. // indexPanelA.add(batch_label);
  600. batch_sn = new JTextField();
  601. batch_sn.setHorizontalAlignment(SwingConstants.CENTER);
  602. batch_sn.setEditable(false);
  603. batch_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  604. batch_sn.setBounds(81, 210, 602, 70);
  605. // indexPanelA.add(batch_sn);
  606. batch_sn.setColumns(10);
  607. batch_sn_btn = new JButton("扫码");
  608. batch_sn_btn.addActionListener(new ActionListener() {
  609. public void actionPerformed(ActionEvent e) {
  610. scan_type = 2;
  611. scanBarcode();
  612. }
  613. });
  614. batch_sn_btn.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  615. batch_sn_btn.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  616. batch_sn_btn.setBounds(693, 210, 198, 70);
  617. // indexPanelA.add(batch_sn_btn);
  618. // String[] hjtitles = new String[]{"焊机1","焊机2","焊机3"};
  619. // String[] hjvals = new String[]{"HJ001","HJ002","HJ003"};
  620. // mesRadioHj = new MesRadio(hjtitles,hjvals);
  621. // mesRadioHj.setSize(500,50);
  622. // mesRadioHj.setBounds(190,170,500,50);
  623. // indexPanelA.add(mesRadioHj);
  624. finish_ok_bt = new JButton("OK");
  625. finish_ok_bt.setEnabled(false);
  626. finish_ok_bt.addActionListener(new ActionListener() {
  627. public void actionPerformed(ActionEvent e) {
  628. String sn = getBarcode(product_sn.getText());
  629. // String batchsn = getBarcode(batch_sn.getText());
  630. if(sn.isEmpty()){
  631. MesClient.setMenuStatus("请扫工件码",1);
  632. return;
  633. }
  634. // if(batchsn.isEmpty()){
  635. // MesClient.setMenuStatus("请扫物料批次码",1);
  636. // return;
  637. // }
  638. getUser();
  639. String qret = "OK";
  640. Boolean sendret = DataUtil.sendCreate(nettyClient,sn,qret,user20);
  641. if(!sendret){
  642. MesClient.setMenuStatus("消息发送失败,请重试",1);
  643. // JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  644. return;
  645. }
  646. }
  647. });
  648. finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
  649. finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  650. finish_ok_bt.setBounds(185, 341, 240, 80);
  651. finish_ok_bt.setEnabled(false);
  652. // 镭雕工位不需要手动点OK,完成信号收到后自动提交,隐藏但保留逻辑复用
  653. finish_ok_bt.setVisible(false);
  654. indexPanelA.add(finish_ok_bt);
  655. finish_ng_bt = new JButton("NG");
  656. finish_ng_bt.setEnabled(false);
  657. finish_ng_bt.addActionListener(new ActionListener() {
  658. public void actionPerformed(ActionEvent e) {
  659. String sn = getBarcode(product_sn.getText());
  660. // String batchsn = getBarcode(batch_sn.getText());
  661. if(sn.isEmpty()){
  662. MesClient.setMenuStatus("请扫工件码",1);
  663. return;
  664. }
  665. // if(batchsn.isEmpty()){
  666. // MesClient.setMenuStatus("请扫物料批次码",1);
  667. // return;
  668. // }
  669. getUser();
  670. String qret = "NG";
  671. Boolean sendret = DataUtil.sendCreate(nettyClient,sn,qret,user20);
  672. if(!sendret){
  673. MesClient.setMenuStatus("消息发送失败,请重试",1);
  674. // JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  675. return;
  676. }
  677. }
  678. });
  679. finish_ng_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ng_bg.png")));
  680. finish_ng_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  681. finish_ng_bt.setBounds(508, 341, 240, 80);
  682. finish_ng_bt.setEnabled(false);
  683. // 镭雕工位不需要手动点NG,隐藏但保留逻辑复用
  684. finish_ng_bt.setVisible(false);
  685. indexPanelA.add(finish_ng_bt);
  686. // ===== 调试面板:供应商真实接口未接通前,用于模拟镭雕软件回传的各阶段信号 =====
  687. JLabel debugTitle = new JLabel("调试面板(模拟镭雕软件信号)");
  688. debugTitle.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  689. debugTitle.setForeground(Color.GRAY);
  690. debugTitle.setBounds(760, 40, 260, 30);
  691. indexPanelA.add(debugTitle);
  692. JTextField debugSteelSnInput = new JTextField();
  693. debugSteelSnInput.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  694. debugSteelSnInput.setBounds(760, 80, 220, 40);
  695. debugSteelSnInput.setToolTipText("输入测试用钢印码,留空则自动生成");
  696. indexPanelA.add(debugSteelSnInput);
  697. JButton debugSimGetCodeBtn = new JButton("模拟获取镭雕码");
  698. debugSimGetCodeBtn.addActionListener(new ActionListener() {
  699. public void actionPerformed(ActionEvent e) {
  700. laserFlowManager.simulateGetCode(debugSteelSnInput.getText());
  701. }
  702. });
  703. debugSimGetCodeBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  704. debugSimGetCodeBtn.setBounds(760, 130, 220, 50);
  705. indexPanelA.add(debugSimGetCodeBtn);
  706. JButton debugSimOkBtn = new JButton("模拟完成信号(OK)");
  707. debugSimOkBtn.addActionListener(new ActionListener() {
  708. public void actionPerformed(ActionEvent e) {
  709. laserFlowManager.simulateComplete("OK", "");
  710. }
  711. });
  712. debugSimOkBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  713. debugSimOkBtn.setBounds(760, 190, 220, 50);
  714. indexPanelA.add(debugSimOkBtn);
  715. JButton debugSimNgBtn = new JButton("模拟完成信号(NG)");
  716. debugSimNgBtn.addActionListener(new ActionListener() {
  717. public void actionPerformed(ActionEvent e) {
  718. laserFlowManager.simulateComplete("NG", "模拟打码失败");
  719. }
  720. });
  721. debugSimNgBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  722. debugSimNgBtn.setBounds(760, 250, 220, 50);
  723. indexPanelA.add(debugSimNgBtn);
  724. JButton debugGenSnBtn = new JButton("测试生成客户编码");
  725. debugGenSnBtn.addActionListener(new ActionListener() {
  726. public void actionPerformed(ActionEvent e) {
  727. laserFlowManager.manualGenCustomerSn(debugSteelSnInput.getText());
  728. }
  729. });
  730. debugGenSnBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  731. debugGenSnBtn.setBounds(760, 310, 220, 50);
  732. indexPanelA.add(debugGenSnBtn);
  733. // ===== 调试面板结束 =====
  734. JButton modbusBtn = new JButton("关");
  735. modbusBtn.addActionListener(new ActionListener() {
  736. public void actionPerformed(ActionEvent e) {
  737. ModbusRtu.closeDevice(serialPort);
  738. }
  739. });
  740. modbusBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  741. modbusBtn.setBounds(208, 241, 160, 60);
  742. // indexPanelA.add(modbusBtn);
  743. JButton modbusBtn2 = new JButton("开");
  744. modbusBtn2.addActionListener(new ActionListener() {
  745. public void actionPerformed(ActionEvent e) {
  746. ModbusRtu.openDevice(serialPort);
  747. }
  748. });
  749. modbusBtn2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  750. modbusBtn2.setBounds(388, 241, 160, 60);
  751. // indexPanelA.add(modbusBtn2);
  752. JButton modbusBtn3 = new JButton("查询");
  753. modbusBtn3.addActionListener(new ActionListener() {
  754. public void actionPerformed(ActionEvent e) {
  755. ModbusRtu.readCoil(serialPort,0);
  756. }
  757. });
  758. modbusBtn3.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  759. modbusBtn3.setBounds(568, 241, 160, 60);
  760. // indexPanelA.add(modbusBtn3);
  761. VisualUiEditorPanel.applySavedDesign(indexPanelA);
  762. tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null);
  763. tabbedPane.setEnabledAt(0, true);
  764. // searchScrollPane = new JScrollPane((Component) null);
  765. indexPanelC = new JPanel();
  766. searchScrollPaneDj = new JScrollPane(indexPanelC);
  767. indexPanelC.setLayout(null);
  768. tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null);
  769. indexPanelB = new JPanel();
  770. searchScrollPane = new JScrollPane(indexPanelB);
  771. indexPanelB.setLayout(null);
  772. tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
  773. tabbedPane.addTab("UI设计", null, new VisualUiEditorPanel(indexPanelA), null);
  774. tabbedPane.addChangeListener(new ChangeListener() {
  775. @Override
  776. public void stateChanged(ChangeEvent e) {
  777. JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
  778. int selectedIndex = tabbedPane.getSelectedIndex();
  779. System.out.println("selectedIndex:"+selectedIndex);
  780. // getCheckResult()调用已移除:原用于驱动继电器(formatDeviceState),
  781. // 继电器功能已不再使用
  782. }
  783. });
  784. // 确保窗口布局正确
  785. pack();
  786. setSize(1024, 768);
  787. validate();
  788. repaint();
  789. }
  790. // 获取开班点检状态
  791. public static void getCheckResult(){
  792. try{
  793. JSONObject retObj = DataUtil.getCheckResult();
  794. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { // 已点检
  795. MesClient.checkState = true;
  796. }else{ // 未点检
  797. MesClient.checkState = false;
  798. }
  799. }catch (Exception e){
  800. e.printStackTrace();
  801. MesClient.checkState = false;
  802. }
  803. MesClient.formatDeviceState();
  804. }
  805. // 格式化设备启动状态
  806. public static void formatDeviceState(){
  807. try{
  808. if(MesClient.checkState){ // 检查物料是否已绑定
  809. if(rowData == null){
  810. ModbusRtu.openDevice(serialPort);
  811. }else{
  812. // 判断物料是否可启动
  813. int flag = 0;
  814. int i = 0;
  815. for (Object object:rowData){
  816. Integer times = Integer.valueOf(String.valueOf(rowData[i][2]));
  817. if(times < 1){
  818. flag = 1;
  819. }
  820. i++;
  821. }
  822. if(flag == 1){ // 不能启动
  823. ModbusRtu.closeDevice(serialPort);
  824. }else{
  825. // 可启动
  826. ModbusRtu.openDevice(serialPort);
  827. }
  828. }
  829. }else{
  830. //不能启动
  831. ModbusRtu.closeDevice(serialPort);
  832. }
  833. }catch (Exception e){
  834. e.printStackTrace();
  835. }
  836. }
  837. public static void getMaterailData(){
  838. JSONObject retObj = DataUtil.getBindMaterail();
  839. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  840. java.util.List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  841. int i = 0;
  842. rowData = new Object[arrs.size()][7];
  843. for (BindMaterialResp bindMaterialResp:arrs){
  844. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  845. rowData[i][1] = bindMaterialResp.getBatchSn();
  846. rowData[i][2] = bindMaterialResp.getLastTimes();
  847. rowData[i][3] = "";
  848. rowData[i][4] = bindMaterialResp.getCraft();
  849. rowData[i][5] = bindMaterialResp.getMaterialId();
  850. rowData[i][6] = bindMaterialResp.getType();
  851. i++;
  852. }
  853. bindBatchPanel();
  854. }
  855. }
  856. public static void updateMaterailData(){
  857. JSONObject retObj = DataUtil.getBindMaterail();
  858. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  859. List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  860. int i = 0;
  861. for (BindMaterialResp bindMaterialResp:arrs){
  862. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  863. rowData[i][1] = bindMaterialResp.getBatchSn();
  864. rowData[i][2] = bindMaterialResp.getLastTimes();
  865. rowData[i][3] = "";
  866. rowData[i][4] = bindMaterialResp.getCraft();
  867. rowData[i][5] = bindMaterialResp.getMaterialId();
  868. rowData[i][6] = bindMaterialResp.getType();
  869. i++;
  870. }
  871. MesClient.table.repaint();
  872. }
  873. }
  874. // 绑定物料批次码
  875. public static void scanBatchSn(BindMaterialResp bindMaterialResp) {
  876. //弹窗扫工件码
  877. String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle();
  878. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  879. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  880. JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType());
  881. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  882. MesClient.setMenuStatus("扫物料:"+bindMaterialResp.getMaterialTitle()+"成功",0);
  883. updateMaterailData();
  884. }else{
  885. if(retObj.get("result")==null){
  886. MesClient.setMenuStatus("请求失败,请重试",-1);
  887. }else{
  888. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  889. MesClient.setMenuStatus(retObj.getString("message"),-1);
  890. }
  891. }
  892. }
  893. }
  894. }
  895. public static void bindBatchPanel(){
  896. JPanel indexPanelBB = new JPanel();
  897. JPanel panel = new JPanel();
  898. panel.setBounds(0, 0, 990, 550);
  899. // panel.setBounds(81, 50, 810, 479);
  900. indexPanelBB.add(panel);
  901. panel.setLayout(new GridLayout(0, 1, 0, 0));
  902. table = new JTable(rowData, columnNames){
  903. public boolean isCellEditable(int row, int column) {
  904. if(column == 3){
  905. return true;
  906. }
  907. return false;
  908. }
  909. };
  910. table.setRowHeight(40);
  911. table.setEnabled(true);
  912. table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  913. table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton());
  914. table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton());
  915. JScrollPane scrollPane = new JScrollPane(table);
  916. panel.add(scrollPane);
  917. JScrollPane indexScrollPaneB = new JScrollPane(indexPanelBB);
  918. indexPanelBB.setLayout(null);
  919. tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null);
  920. }
  921. }