MesClient.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.mes.component.MesRadio;
  5. import com.mes.component.MesWebView;
  6. import com.mes.netty.NettyClient;
  7. import com.mes.util.*;
  8. import javafx.embed.swing.JFXPanel;
  9. import javax.swing.*;
  10. import javax.swing.border.EmptyBorder;
  11. import javax.swing.event.ChangeEvent;
  12. import javax.swing.event.ChangeListener;
  13. import java.awt.*;
  14. import java.awt.event.ActionEvent;
  15. import java.awt.event.ActionListener;
  16. import java.awt.event.MouseAdapter;
  17. import java.awt.event.MouseEvent;
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.math.BigDecimal;
  23. import java.time.LocalDateTime;
  24. import java.util.List;
  25. import java.util.Properties;
  26. import java.util.Timer;
  27. import java.util.TimerTask;
  28. public class MesClient extends JFrame {
  29. private static String Drivde = "org.sqlite.JDBC";
  30. public static int mes_auth = 0; // 权限级别 0=无权限 1=操作工人 2=管理员
  31. public static String mes_gw = ""; // 工位号
  32. public static String mes_gw_des = ""; // 工位名称
  33. public static String mes_server_ip = ""; // 服务器IP地址
  34. public static int mes_tcp_port = 3000; // TCP服务端口
  35. public static int mes_heart_beat_cycle = 10; // 心跳周期
  36. public static int mes_heart_icon_cycle = 1;
  37. public static String mes_line_sn = ""; // 产线编号
  38. //TCP连接
  39. public static NettyClient nettyClient;
  40. //TCP连接状态
  41. public static boolean tcp_connect_flag = false;
  42. //TCP连接请求
  43. public static boolean connect_request_flag = false;
  44. public static boolean com_connect_flag = false;
  45. //session
  46. public static String sessionid = "";
  47. public static JPanel contentPane;
  48. public static MesClient mesClientFrame;
  49. public static JTabbedPane tabbedPane;
  50. public static JScrollPane indexScrollPaneA;
  51. public static JScrollPane searchScrollPane;
  52. public static JScrollPane searchScrollPaneDj;
  53. public static WeightSerialService serialPortService;
  54. public static ParamConfig paramConfig;
  55. public static Boolean check_quality_result = false;
  56. public static Integer work_status = 0;
  57. public static volatile boolean isWeighing = false;
  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 JTextField product_sn;
  64. public static JButton f_scan_data_bt_1;
  65. public static String comboBoxValue = "COM7";
  66. public static String user20 = "";
  67. public static JFrame welcomeWin;
  68. public static JPanel indexPanelB;
  69. public static MesWebView jfxPanel = null;
  70. public static JPanel indexPanelC;
  71. public static MesWebView jfxPanel2 = null;
  72. public static MesRadio mesRadioHj;
  73. public static JTable table;
  74. public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" };
  75. public static Object[][] rowData = null;
  76. public static Timer cjTimer;
  77. public static Timer cjTimer2;
  78. public static Timer autoWeighStopTimer;
  79. public static void main(String[] args) {
  80. EventQueue.invokeLater(new Runnable() {
  81. @Override
  82. public void run() {
  83. try{
  84. //读文件配置
  85. readProperty();
  86. //checkComPort();
  87. // 显示界面
  88. mesClientFrame = new MesClient();
  89. mesClientFrame.setVisible(false);
  90. JdbcUtils.getConn();
  91. welcomeWin = new LoginFarme();
  92. welcomeWin.setVisible(true);
  93. // getMaterailData();
  94. }catch (Exception e){
  95. e.printStackTrace();
  96. }
  97. }
  98. });
  99. }
  100. public static void initComPort() {
  101. // 若已有旧实例,先关闭
  102. if (serialPortService != null) {
  103. serialPortService.close();
  104. serialPortService = null;
  105. }
  106. serialPortService = new WeightSerialService();
  107. serialPortService.setReadingEnabled(isWeighing);
  108. // 默认波特率 9600(可通过 ParamConfig.DEFAULT_BAUD_RATE 修改)
  109. paramConfig = new ParamConfig((String) comboBox.getSelectedItem(), ParamConfig.DEFAULT_BAUD_RATE);
  110. System.out.println("串口号:" + comboBox.getSelectedItem() + " 波特率:" + paramConfig.getBaudRate());
  111. // 注册重量数据监听器:收到数据时同步连接状态并更新 UI
  112. serialPortService.addListener(new WeightSerialService.WeightDataListener() {
  113. @Override
  114. public void onWeightData(WeightData data) {
  115. // 仅在"称重"状态下更新界面显示
  116. if (!isWeighing) {
  117. return;
  118. }
  119. SwingUtilities.invokeLater(new Runnable() {
  120. @Override
  121. public void run() {
  122. if (!isWeighing) {
  123. return;
  124. }
  125. if (weight_text != null) {
  126. weight_text.setText(BigDecimal.valueOf(data.getValue()).multiply(new BigDecimal("10")).stripTrailingZeros().toPlainString());
  127. }
  128. System.out.println("收到重量数据: " + data);
  129. }
  130. });
  131. }
  132. @Override
  133. public void onConnectionChanged(boolean connected) {
  134. com_connect_flag = connected;
  135. System.out.println("串口连接状态: " + (connected ? "已连接" : "已断开"));
  136. }
  137. @Override
  138. public void onError(Throwable t) {
  139. System.err.println("串口通信异常: " + t.getMessage());
  140. t.printStackTrace();
  141. }
  142. });
  143. boolean ok = serialPortService.open(paramConfig);
  144. if (ok) {
  145. com_connect_flag = true;
  146. System.out.println("com串口连接成功...");
  147. } else {
  148. com_connect_flag = false;
  149. System.out.println("com串口连接失败...");
  150. }
  151. }
  152. //定时任务重连吊秤
  153. public static void checkComPort() {
  154. if(MesClient.cjTimer2!=null) {
  155. return;
  156. }
  157. MesClient.cjTimer2 = new Timer(true);
  158. MesClient.cjTimer2.schedule(new TimerTask() {
  159. public void run() {
  160. try {
  161. if (serialPortService == null || !serialPortService.isConnected()) {
  162. System.out.println("吊秤断开连接,正在重连");
  163. initComPort();
  164. }
  165. } catch (Exception e) {
  166. e.printStackTrace();
  167. }
  168. }
  169. }, 2000,2000);
  170. }
  171. public static void setWeighing(boolean weighing) {
  172. if (!weighing) {
  173. cancelAutoWeighStopTimer();
  174. }
  175. isWeighing = weighing;
  176. if (serialPortService != null) {
  177. serialPortService.setReadingEnabled(weighing);
  178. }
  179. if (finish_ok_bt_1 != null) {
  180. finish_ok_bt_1.setText(weighing ? "暂停" : "称重");
  181. }
  182. if (weight_text != null) {
  183. weight_text.setEnabled(weighing);
  184. }
  185. }
  186. private static void cancelAutoWeighStopTimer() {
  187. if (autoWeighStopTimer != null) {
  188. autoWeighStopTimer.cancel();
  189. autoWeighStopTimer = null;
  190. }
  191. }
  192. public static void startAutoWeighing() {
  193. cancelAutoWeighStopTimer();
  194. setWeighing(true);
  195. autoWeighStopTimer = new Timer(true);
  196. autoWeighStopTimer.schedule(new TimerTask() {
  197. @Override
  198. public void run() {
  199. SwingUtilities.invokeLater(new Runnable() {
  200. @Override
  201. public void run() {
  202. setWeighing(false);
  203. }
  204. });
  205. }
  206. }, 2000);
  207. }
  208. //读配置文件
  209. private static void readProperty() throws IOException{
  210. String enconding = "UTF-8";
  211. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  212. Properties pro = new Properties();
  213. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  214. pro.load(br);
  215. mes_gw = pro.getProperty("mes.gw");
  216. // mes_gw_des = pro.getProperty("mes.gw_des");
  217. mes_server_ip = pro.getProperty("mes.server_ip");
  218. mes_tcp_port = Integer.parseInt(pro.getProperty("mes.tcp_port"));
  219. mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle"));
  220. mes_line_sn = pro.getProperty("mes.line_sn");
  221. mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
  222. System.out.println(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle);
  223. }
  224. // 初始化TCP
  225. public static void initTcpConnection() {
  226. try {
  227. if(nettyClient==null) {
  228. //初始化TCP连接
  229. nettyClient = new NettyClient();
  230. //TCP连接状态
  231. tcp_connect_flag = false;
  232. //设置TCP请求状态
  233. connect_request_flag = true;
  234. DataUtil.synrTcp(nettyClient,mes_gw);
  235. }
  236. } catch (Exception e) {
  237. // TODO Auto-generated catch block
  238. e.printStackTrace();
  239. }
  240. }
  241. //启动心跳包程序
  242. public static java.util.Timer heartBeatTimer;
  243. public static java.util.Timer heartBeatIconTimer;
  244. public static boolean iconREDFlag = true;
  245. public static JTextField weight_text;
  246. public static JLabel lblNewLabel;
  247. private static JComboBox comboBox;
  248. public static JButton finish_ok_bt_1;
  249. public static void startHeartBeatTimer() {
  250. if(heartBeatTimer!=null) {
  251. heartBeatTimer.cancel();
  252. }
  253. heartBeatTimer = new java.util.Timer();
  254. heartBeatTimer.schedule(new TimerTask() {
  255. public void run() {
  256. if(nettyClient!=null&&tcp_connect_flag) {
  257. //System.out.println("发送心跳报文");
  258. DataUtil.heartBeat(nettyClient,mes_gw);
  259. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  260. }
  261. }
  262. }, 100,mes_heart_beat_cycle*1000);
  263. //心跳显示图标
  264. if(heartBeatIconTimer!=null) {
  265. heartBeatIconTimer.cancel();
  266. }
  267. heartBeatIconTimer = new Timer();
  268. heartBeatIconTimer.schedule(new TimerTask() {
  269. public void run() {
  270. if(tcp_connect_flag) {
  271. if(iconREDFlag) {
  272. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  273. iconREDFlag = false;
  274. }else {
  275. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  276. iconREDFlag = true;
  277. }
  278. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  279. heart_beat_menu.repaint();
  280. }else {
  281. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  282. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  283. heart_beat_menu.repaint();
  284. //若未连接则尝试连接
  285. if(nettyClient!=null&&!tcp_connect_flag){
  286. System.out.println("TCP已断开");
  287. //TCP重新同步连接
  288. if(!connect_request_flag) {
  289. //System.out.println("TCP重新同步连接");
  290. //设置TCP请求状态,只重新同步连接一次
  291. connect_request_flag = true;
  292. DataUtil.synrTcp(nettyClient,mes_gw);
  293. }
  294. }
  295. }
  296. }
  297. }, 100,mes_heart_icon_cycle*1000);
  298. }
  299. //设置tcp连接状态显示
  300. public static void setTcpStatus() {
  301. if(tcp_connect_flag) {
  302. status_menu.setText("已连接MES服务器");
  303. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  304. heart_beat_menu.repaint();
  305. }else {
  306. status_menu.setText("未连接MES服务器");
  307. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  308. heart_beat_menu.repaint();
  309. }
  310. }
  311. public static void initWarehouseData(){
  312. resetScanA();
  313. }
  314. public static void resetScanA() {
  315. work_status = 0;
  316. check_quality_result = false;
  317. MesClient.finish_ok_bt.setEnabled(false);
  318. product_sn.setText("");
  319. weight_text.setText("");
  320. weight_text.setEnabled(false);
  321. finish_ok_bt.setEnabled(false);
  322. MesClient.setWeighing(false);
  323. finish_ok_bt_1.setEnabled(false);
  324. MesClient.f_scan_data_bt_1.setEnabled(true);
  325. setMenuStatus("请扫工件码",0);
  326. shiftUserCheck();
  327. }
  328. public static int userLoginHours;//用户登录所处小时
  329. //换班用户信息检查
  330. private static void shiftUserCheck() {
  331. LocalDateTime now = LocalDateTime.now();
  332. // 判断时间范围
  333. if (userLoginHours >= 8 && userLoginHours < 20) {
  334. int hour = now.getHour();
  335. if (hour >= 20 || hour < 8) {
  336. logoff();
  337. }
  338. } else {
  339. int hour = now.getHour();
  340. if (hour >= 8 && hour < 20) {
  341. logoff();
  342. }
  343. }
  344. }
  345. public static void resetScanB() {
  346. }
  347. //获取用户20位
  348. public static void getUser() {
  349. user20 = user_menu.getText().toString();
  350. String space_tmp1 = "";
  351. if(user20.length()<20) {
  352. for(int i=0;i<20-user20.length();i++) {
  353. space_tmp1 = space_tmp1 + " ";
  354. }
  355. }
  356. user20 = user20 + space_tmp1;
  357. }
  358. //获取barcode内容36位
  359. public static String getBarcode(String barcodeTmp) {
  360. String barcodeRet = barcodeTmp;
  361. if(barcodeTmp.equalsIgnoreCase("")) {
  362. return "";
  363. }else {
  364. if(barcodeTmp.length()<36) {
  365. String space = "";
  366. for(int i=0;i<36-barcodeTmp.length();i++) {
  367. space = space + " ";
  368. }
  369. barcodeRet = barcodeTmp + space;
  370. }
  371. }
  372. return barcodeRet;
  373. }
  374. public static void scanBarcode() {
  375. if(work_status == 1){
  376. JOptionPane.showMessageDialog(mesClientFrame,"工作中,勿扫码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  377. return;
  378. }
  379. String scanBarcodeTitle = "";
  380. switch(scan_type) {
  381. case 1:
  382. product_sn.setText("");
  383. scanBarcodeTitle = "请扫工件码";
  384. break;
  385. }
  386. //弹窗扫工件码
  387. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  388. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  389. //获取用户
  390. getUser();
  391. //获取扫码内容36位
  392. String barcode36 = getBarcode(scanBarcode);//处理36为码
  393. //工位号
  394. String gw = "";
  395. switch(scan_type) {
  396. case 1:
  397. product_sn.setText(scanBarcode);
  398. weight_text.setText("");
  399. break;
  400. }
  401. //刷新界面
  402. mesClientFrame.repaint();
  403. if(!tcp_connect_flag) {
  404. JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  405. return;
  406. }
  407. // 查询工件质量
  408. Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20);
  409. if(!sendret){
  410. setWeighing(false);
  411. JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  412. return;
  413. }
  414. }else {
  415. JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  416. return;
  417. }
  418. }
  419. public static void logoff() {
  420. // welcomeWin.setVisible(true);
  421. mesClientFrame.setVisible(false);
  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. JMenuBar menuBar = new JMenuBar();
  438. menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26));
  439. setJMenuBar(menuBar);
  440. JMenu fileMenu = new JMenu("用户");
  441. fileMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png")));
  442. fileMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  443. menuBar.add(fileMenu);
  444. JMenuItem exitMenuItem = new JMenuItem("退出");
  445. exitMenuItem.setIcon(new ImageIcon(MesClient.class.getResource("/bg/logoff.png")));
  446. exitMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  447. fileMenu.add(exitMenuItem);
  448. exitMenuItem.addMouseListener(new MouseAdapter() {
  449. @Override
  450. public void mousePressed(MouseEvent e) {//只能检测到mousePressed事件
  451. super.mouseClicked(e);
  452. //dispose();
  453. logoff();
  454. }
  455. });
  456. JMenu settingMenu = new JMenu("设置");
  457. //settingMenu.setVisible(false);
  458. settingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
  459. settingMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  460. menuBar.add(settingMenu);
  461. JMenuItem resetTcpMenu = new JMenuItem("\u91CD\u8FDEMES");
  462. resetTcpMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  463. resetTcpMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  464. resetTcpMenu.addMouseListener(new MouseAdapter() {
  465. @Override
  466. public void mousePressed(MouseEvent e) {
  467. super.mouseClicked(e);
  468. //重连mes
  469. nettyClient.future.channel().close();
  470. }
  471. });
  472. settingMenu.add(resetTcpMenu);
  473. JMenuItem resetTcpMenu_1 = new JMenuItem("刷新工件");
  474. resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  475. resetTcpMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  476. resetTcpMenu_1.addMouseListener(new MouseAdapter() {
  477. @Override
  478. public void mousePressed(MouseEvent e) {
  479. super.mouseClicked(e);
  480. resetScanA();
  481. }
  482. });
  483. settingMenu.add(resetTcpMenu_1);
  484. contentPane = new JPanel();
  485. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  486. setContentPane(contentPane);
  487. contentPane.setLayout(new BorderLayout(0, 0));
  488. JToolBar toolBar = new JToolBar();
  489. contentPane.add(toolBar, BorderLayout.NORTH);
  490. JLabel equipment_statu_label = new JLabel("状态:");
  491. equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER);
  492. equipment_statu_label.setForeground(Color.BLACK);
  493. equipment_statu_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  494. equipment_statu_label.setBackground(Color.LIGHT_GRAY);
  495. toolBar.add(equipment_statu_label);
  496. status_menu = new JButton("设备未连接MES服务器");
  497. if(tcp_connect_flag) {
  498. status_menu.setText("已连接MES服务器");
  499. status_menu.setForeground(Color.GREEN);
  500. }else {
  501. status_menu.setText("未连接MES服务器");
  502. status_menu.setForeground(Color.DARK_GRAY);
  503. }
  504. status_menu.addActionListener(new ActionListener() {
  505. public void actionPerformed(ActionEvent e) {
  506. }
  507. });
  508. status_menu.setForeground(Color.GREEN);
  509. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  510. status_menu.setBackground(Color.BLACK);
  511. toolBar.add(status_menu);
  512. JLabel space_1 = new JLabel(" ");
  513. toolBar.add(space_1);
  514. JLabel heart_beat_status_label = new JLabel("心跳:");
  515. heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  516. heart_beat_status_label.setForeground(Color.BLACK);
  517. heart_beat_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  518. heart_beat_status_label.setBackground(Color.LIGHT_GRAY);
  519. toolBar.add(heart_beat_status_label);
  520. heart_beat_menu = new JButton("2024-02-20 23:20:10");
  521. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  522. heart_beat_menu.setForeground(Color.GREEN);
  523. heart_beat_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  524. heart_beat_menu.setBackground(Color.BLACK);
  525. toolBar.add(heart_beat_menu);
  526. JLabel space_2 = new JLabel(" ");
  527. toolBar.add(space_2);
  528. JLabel user_status_label = new JLabel("登录用户:");
  529. user_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  530. user_status_label.setForeground(Color.BLACK);
  531. user_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  532. user_status_label.setBackground(Color.LIGHT_GRAY);
  533. toolBar.add(user_status_label);
  534. user_menu = new JButton("JinJuShi");
  535. user_menu.setForeground(Color.GREEN);
  536. user_menu.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  537. user_menu.setBackground(Color.BLACK);
  538. toolBar.add(user_menu);
  539. JLabel space_3 = new JLabel(" ");
  540. toolBar.add(space_3);
  541. JLabel space_4 = new JLabel(" ");
  542. toolBar.add(space_4);
  543. comboBox = new JComboBox();
  544. comboBox.setBackground(Color.WHITE);
  545. comboBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  546. comboBox.setBounds(50, 10, 80, 21);
  547. comboBox.setMaximumSize(new Dimension(100, 40));
  548. comboBox.setMinimumSize(new Dimension(100, 30));
  549. comboBox.setPreferredSize(new Dimension(100, 30));
  550. comboBox.setModel(new DefaultComboBoxModel(new String[] {"COM7"}));
  551. toolBar.add(comboBox);
  552. // 串口发生改变
  553. comboBox.addActionListener(new ActionListener() {
  554. @Override
  555. public void actionPerformed(ActionEvent e) {
  556. //修改串口时,先关闭资源,重新初始化
  557. MesClient.setWeighing(false);
  558. if (serialPortService != null) {
  559. serialPortService.close();
  560. }
  561. MesClient.initComPort();
  562. }
  563. });
  564. tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  565. tabbedPane.setMinimumSize(new Dimension(400, 50));
  566. tabbedPane.setFont(new Font("宋体", Font.BOLD, 22));
  567. contentPane.add(tabbedPane);
  568. //首页
  569. JPanel indexPanelA = new JPanel();
  570. indexScrollPaneA = new JScrollPane(indexPanelA);
  571. indexPanelA.setLayout(null);
  572. product_sn = new JTextField();
  573. product_sn.setHorizontalAlignment(SwingConstants.CENTER);
  574. product_sn.setEditable(false);
  575. product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  576. product_sn.setBounds(81, 70, 602, 70);
  577. indexPanelA.add(product_sn);
  578. product_sn.setColumns(10);
  579. f_scan_data_bt_1 = new JButton("扫码");
  580. f_scan_data_bt_1.addActionListener(new ActionListener() {
  581. public void actionPerformed(ActionEvent e) {
  582. scan_type = 1;
  583. scanBarcode();
  584. }
  585. });
  586. f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  587. f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  588. f_scan_data_bt_1.setBounds(693, 70, 198, 70);
  589. indexPanelA.add(f_scan_data_bt_1);
  590. // String[] hjtitles = new String[]{"焊机1","焊机2","焊机3"};
  591. // String[] hjvals = new String[]{"HJ001","HJ002","HJ003"};
  592. // mesRadioHj = new MesRadio(hjtitles,hjvals);
  593. // mesRadioHj.setSize(500,50);
  594. // mesRadioHj.setBounds(190,170,500,50);
  595. // indexPanelA.add(mesRadioHj);
  596. finish_ok_bt = new JButton("OK");
  597. finish_ok_bt.setEnabled(false);
  598. finish_ok_bt.addActionListener(new ActionListener() {
  599. public void actionPerformed(ActionEvent e) {
  600. if (isWeighing) {
  601. JOptionPane.showMessageDialog(mesClientFrame, "请点击暂停键", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
  602. return;
  603. }
  604. if(work_status == 1 && check_quality_result) {
  605. // 调用bindWarehouse绑定重量并上传:wsn=重量, sn=工件码, craft=400006
  606. String sn = getBarcode(product_sn.getText());
  607. getUser();
  608. /* String qret = "OK";
  609. Boolean asendret = DataUtil.sendQuality(nettyClient,sn,qret,user20);
  610. if(!asendret){
  611. JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  612. return;
  613. }*/
  614. String wsn = weight_text.getText();
  615. // 新增重量区间校验
  616. Double weightVal;
  617. try {
  618. weightVal = Double.parseDouble(wsn);
  619. } catch (NumberFormatException a) {
  620. JOptionPane.showMessageDialog(mesClientFrame, "重量格式不正确,请确认!", "提示窗口", JOptionPane.WARNING_MESSAGE);
  621. return;
  622. }
  623. // 判断范围 76.76 ~ 83.22
  624. if (weightVal < 101.92 || weightVal > 110.50) {
  625. JOptionPane.showMessageDialog(mesClientFrame,
  626. String.format("重量不在合格范围!当前重量:%.2f,要求区间:101.92 ~ 110.50,请重新称重", weightVal),
  627. "提示窗口",
  628. JOptionPane.WARNING_MESSAGE);
  629. return;
  630. }
  631. Boolean sendret = DataUtil.bindWarehouse(nettyClient, sn, wsn, user20, "400006");
  632. if (!sendret) {
  633. JOptionPane.showMessageDialog(mesClientFrame, "消息发送失败,请重试", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
  634. return;
  635. }
  636. MesClient.setMenuStatus("正在上传结果...", 0);
  637. }
  638. }
  639. });
  640. finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
  641. finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 40));
  642. finish_ok_bt.setBounds(578, 405, 212, 92);
  643. indexPanelA.add(finish_ok_bt);
  644. weight_text = new JTextField();
  645. weight_text.setHorizontalAlignment(SwingConstants.CENTER);
  646. weight_text.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  647. weight_text.setEditable(false);
  648. weight_text.setColumns(10);
  649. weight_text.setBounds(403, 300, 181, 70);
  650. indexPanelA.add(weight_text);
  651. lblNewLabel = new JLabel("重量");
  652. lblNewLabel.setEnabled(false);
  653. lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
  654. lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 30));
  655. lblNewLabel.setBounds(238, 300, 117, 70);
  656. indexPanelA.add(lblNewLabel);
  657. finish_ok_bt_1 = new JButton("称重");
  658. finish_ok_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
  659. finish_ok_bt_1.addActionListener(new ActionListener() {
  660. public void actionPerformed(ActionEvent e) {
  661. setWeighing(!isWeighing);
  662. }
  663. });
  664. finish_ok_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 40));
  665. finish_ok_bt_1.setEnabled(true);
  666. finish_ok_bt_1.setBounds(207, 405, 212, 92);
  667. indexPanelA.add(finish_ok_bt_1);
  668. tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null);
  669. tabbedPane.setEnabledAt(0, true);
  670. // searchScrollPane = new JScrollPane((Component) null);
  671. indexPanelC = new JPanel();
  672. searchScrollPaneDj = new JScrollPane(indexPanelC);
  673. indexPanelC.setLayout(null);
  674. tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null);
  675. indexPanelB = new JPanel();
  676. searchScrollPane = new JScrollPane(indexPanelB);
  677. indexPanelB.setLayout(null);
  678. tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
  679. tabbedPane.addChangeListener(new ChangeListener() {
  680. @Override
  681. public void stateChanged(ChangeEvent e) {
  682. JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
  683. int selectedIndex = tabbedPane.getSelectedIndex();
  684. System.out.println("selectedIndex:"+selectedIndex);
  685. if(selectedIndex == 1){
  686. }
  687. }
  688. });
  689. MesClient.initComPort();
  690. MesClient.checkComPort();
  691. }
  692. public static void setMenuStatus(String msg,int error){
  693. if(error == 0){
  694. MesClient.status_menu.setForeground(Color.GREEN);
  695. }else{
  696. MesClient.status_menu.setForeground(Color.RED);
  697. }
  698. MesClient.status_menu.setText(msg);
  699. }
  700. public static void getMaterailData(){
  701. JSONObject retObj = DataUtil.getBindMaterail();
  702. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  703. java.util.List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  704. int i = 0;
  705. rowData = new Object[arrs.size()][7];
  706. for (BindMaterialResp bindMaterialResp:arrs){
  707. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  708. rowData[i][1] = bindMaterialResp.getBatchSn();
  709. rowData[i][2] = bindMaterialResp.getLastTimes();
  710. rowData[i][3] = "";
  711. rowData[i][4] = bindMaterialResp.getCraft();
  712. rowData[i][5] = bindMaterialResp.getMaterialId();
  713. rowData[i][6] = bindMaterialResp.getType();
  714. i++;
  715. }
  716. bindBatchPanel();
  717. }
  718. }
  719. public static void updateMaterailData(){
  720. JSONObject retObj = DataUtil.getBindMaterail();
  721. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  722. List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  723. int i = 0;
  724. for (BindMaterialResp bindMaterialResp:arrs){
  725. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  726. rowData[i][1] = bindMaterialResp.getBatchSn();
  727. rowData[i][2] = bindMaterialResp.getLastTimes();
  728. rowData[i][3] = "";
  729. rowData[i][4] = bindMaterialResp.getCraft();
  730. rowData[i][5] = bindMaterialResp.getMaterialId();
  731. rowData[i][6] = bindMaterialResp.getType();
  732. i++;
  733. }
  734. MesClient.table.repaint();
  735. }
  736. }
  737. // 绑定物料批次码
  738. public static void scanBatchSn(BindMaterialResp bindMaterialResp) {
  739. //弹窗扫工件码
  740. String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle();
  741. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  742. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  743. JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType());
  744. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  745. MesClient.setMenuStatus("扫物料:"+bindMaterialResp.getMaterialTitle()+"成功",0);
  746. updateMaterailData();
  747. }else{
  748. if(retObj.get("result")==null){
  749. MesClient.setMenuStatus("请求失败,请重试",-1);
  750. }else{
  751. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  752. MesClient.setMenuStatus(retObj.getString("message"),-1);
  753. }
  754. }
  755. }
  756. }
  757. }
  758. public static void bindBatchPanel(){
  759. JPanel indexPanelBB = new JPanel();
  760. JPanel panel = new JPanel();
  761. panel.setBounds(0, 0, 990, 550);
  762. // panel.setBounds(81, 50, 810, 479);
  763. indexPanelBB.add(panel);
  764. panel.setLayout(new GridLayout(0, 1, 0, 0));
  765. table = new JTable(rowData, columnNames){
  766. public boolean isCellEditable(int row, int column) {
  767. if(column == 3){
  768. return true;
  769. }
  770. return false;
  771. }
  772. };
  773. table.setRowHeight(40);
  774. table.setEnabled(true);
  775. table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  776. table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton());
  777. table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton());
  778. JScrollPane scrollPane = new JScrollPane(table);
  779. panel.add(scrollPane);
  780. JScrollPane indexScrollPaneB = new JScrollPane(indexPanelBB);
  781. indexPanelBB.setLayout(null);
  782. tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null);
  783. }
  784. }