MesClient.java 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.mes.component.GunPanel;
  5. import com.mes.component.MesRadio;
  6. import com.mes.component.MesWebView;
  7. import com.mes.netty.NettyClient;
  8. import com.mes.util.DateLocalUtils;
  9. import com.mes.util.JdbcUtils;
  10. import com.mes.ygsl.YgslConfig;
  11. import io.netty.buffer.ByteBuf;
  12. import io.netty.buffer.Unpooled;
  13. import javafx.embed.swing.JFXPanel;
  14. import javax.swing.*;
  15. import javax.swing.border.EmptyBorder;
  16. import javax.swing.event.ChangeEvent;
  17. import javax.swing.event.ChangeListener;
  18. import java.awt.*;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ActionListener;
  21. import java.awt.event.MouseAdapter;
  22. import java.awt.event.MouseEvent;
  23. import java.io.BufferedReader;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.InputStreamReader;
  27. import java.time.LocalDateTime;
  28. import java.util.*;
  29. import java.util.List;
  30. import java.util.Timer;
  31. public class MesClient extends JFrame {
  32. private static String Drivde = "org.sqlite.JDBC";
  33. public static int mes_auth = 0; // 权限级别 0=无权限 1=操作工人 2=管理员
  34. public static String mes_gw = ""; // 工位号
  35. public static String mes_gw_des = ""; // 工位名称
  36. public static String mes_server_ip = ""; // 服务器IP地址
  37. public static int mes_tcp_port = 3000; // TCP服务端口
  38. public static int mes_heart_beat_cycle = 10; // 心跳周期
  39. public static int mes_heart_icon_cycle = 1;
  40. public static String mes_line_sn = ""; // 产线编号
  41. //TCP连接
  42. public static NettyClient nettyClient;
  43. //TCP连接状态
  44. public static boolean tcp_connect_flag = false;
  45. //TCP连接请求
  46. public static boolean connect_request_flag = false;
  47. //session
  48. public static String sessionid = "";
  49. public static JPanel contentPane;
  50. public static MesClient mesClientFrame;
  51. public static JTabbedPane tabbedPane;
  52. public static JScrollPane indexScrollPaneA;
  53. public static JScrollPane searchScrollPane;
  54. public static JScrollPane searchScrollPaneDj;
  55. public static Boolean check_quality_result = false;
  56. public static Integer work_status = 0;
  57. public static Integer pz_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 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 List<GunPanel> gunPanels = new ArrayList<>();
  77. public static JPanel gunPanelContainer;
  78. public static JLabel pzlabel;
  79. public static java.util.Timer submitCheckTimer;
  80. public static boolean autoSubmitEnabled = true;
  81. public static void main(String[] args) {
  82. // 设置全局异常处理器,防止程序突然崩溃
  83. Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
  84. @Override
  85. public void uncaughtException(Thread t, Throwable e) {
  86. System.err.println("=== 捕获到未处理的异常 ===");
  87. System.err.println("线程: " + t.getName());
  88. System.err.println("异常信息: " + e.getMessage());
  89. e.printStackTrace();
  90. // 显示错误对话框
  91. SwingUtilities.invokeLater(new Runnable() {
  92. @Override
  93. public void run() {
  94. JOptionPane.showMessageDialog(
  95. mesClientFrame,
  96. "程序发生错误:\n" + e.getMessage(),
  97. "错误",
  98. JOptionPane.ERROR_MESSAGE
  99. );
  100. }
  101. });
  102. }
  103. });
  104. EventQueue.invokeLater(new Runnable() {
  105. @Override
  106. public void run() {
  107. try{
  108. //读文件配置
  109. readProperty();
  110. // 显示界面
  111. mesClientFrame = new MesClient();
  112. mesClientFrame.setVisible(false);
  113. JdbcUtils.getConn();
  114. welcomeWin = new LoginFarme();
  115. welcomeWin.setVisible(true);
  116. if(mes_gw.equals("OP290")){
  117. pz_status = 0; // 未拍照
  118. }else{
  119. pz_status = 1; // 已拍照
  120. }
  121. getMaterailData();
  122. upParams();
  123. deviceOnline();
  124. checkpz();
  125. startSubmitCheckTimer();
  126. }catch (Exception e){
  127. System.err.println("=== 初始化过程中发生异常 ===");
  128. e.printStackTrace();
  129. JOptionPane.showMessageDialog(
  130. null,
  131. "程序初始化失败:\n" + e.getMessage(),
  132. "错误",
  133. JOptionPane.ERROR_MESSAGE
  134. );
  135. }
  136. }
  137. });
  138. }
  139. public static void initGunPanels() {
  140. gunPanels.clear();
  141. // 清空容器中的所有组件,防止重复添加
  142. if (gunPanelContainer != null) {
  143. gunPanelContainer.removeAll();
  144. }
  145. System.out.println("initGunPanels - gunPanelContainer: " + gunPanelContainer);
  146. List<YgslConfig> configs = JdbcUtils.getEnabledYgslConfigs();
  147. System.out.println("initGunPanels - 获取到配置: " + configs.size() + " 把枪");
  148. for (YgslConfig config : configs) {
  149. GunPanel gunPanel = new GunPanel(config);
  150. gunPanel.setTaskCompletedListener(new GunPanel.OnTaskCompletedListener() {
  151. @Override
  152. public void onTaskCompleted(GunPanel panel) {
  153. checkAllGunsCompleted();
  154. }
  155. });
  156. gunPanels.add(gunPanel);
  157. if (gunPanelContainer != null) {
  158. gunPanelContainer.add(gunPanel);
  159. gunPanel.connect();
  160. System.out.println("已添加枪到容器: " + config.getGunName());
  161. } else {
  162. System.out.println("gunPanelContainer 为空,无法添加!");
  163. }
  164. }
  165. // 刷新容器显示
  166. if (gunPanelContainer != null) {
  167. gunPanelContainer.revalidate();
  168. gunPanelContainer.repaint();
  169. }
  170. System.out.println("已加载 " + gunPanels.size() + " 把扭力枪");
  171. }
  172. public static void startSubmitCheckTimer() {
  173. if (submitCheckTimer != null) {
  174. submitCheckTimer.cancel();
  175. }
  176. submitCheckTimer = new Timer();
  177. submitCheckTimer.schedule(new TimerTask() {
  178. @Override
  179. public void run() {
  180. try {
  181. checkAllGunsCompleted();
  182. } catch (Exception e) {
  183. System.err.println("=== submitCheckTimer定时任务异常 ===");
  184. e.printStackTrace();
  185. }
  186. }
  187. }, 1000, 1000);
  188. }
  189. private static void checkAllGunsCompleted() {
  190. if (work_status != 1 || !check_quality_result) {
  191. return;
  192. }
  193. if (!autoSubmitEnabled) {
  194. return;
  195. }
  196. boolean allCompleted = true;
  197. for (GunPanel panel : gunPanels) {
  198. if (!panel.isTaskCompleted()) {
  199. allCompleted = false;
  200. break;
  201. }
  202. }
  203. if (allCompleted) {
  204. final boolean submitSuccess = submitQualityResult();
  205. SwingUtilities.invokeLater(() -> {
  206. if (submitSuccess) {
  207. setMenuStatus("所有任务完成,结果已提交", 0);
  208. resetScanA();
  209. // scanBarcode();
  210. } else {
  211. setMenuStatus("结果提交失败,请重试", -1);
  212. }
  213. });
  214. }
  215. }
  216. private static boolean submitQualityResult() {
  217. String sn = getBarcode(product_sn.getText());
  218. getUser();
  219. String qret = "OK";
  220. return DataUtil.sendQuality(nettyClient, sn, qret, user20);
  221. }
  222. //读配置文件
  223. private static void readProperty() throws IOException{
  224. String enconding = "UTF-8";
  225. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  226. Properties pro = new Properties();
  227. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  228. pro.load(br);
  229. mes_gw = pro.getProperty("mes.gw");
  230. // mes_gw_des = pro.getProperty("mes.gw_des");
  231. mes_server_ip = pro.getProperty("mes.server_ip");
  232. mes_tcp_port = Integer.parseInt(pro.getProperty("mes.tcp_port"));
  233. mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle"));
  234. mes_line_sn = pro.getProperty("mes.line_sn");
  235. mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
  236. System.out.println(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle);
  237. }
  238. // 初始化TCP
  239. public static void initTcpConnection() {
  240. try {
  241. if(nettyClient==null) {
  242. //初始化TCP连接
  243. nettyClient = new NettyClient();
  244. //TCP连接状态
  245. tcp_connect_flag = false;
  246. //设置TCP请求状态
  247. connect_request_flag = true;
  248. DataUtil.synrTcp(nettyClient,mes_gw);
  249. }
  250. } catch (Exception e) {
  251. // TODO Auto-generated catch block
  252. e.printStackTrace();
  253. }
  254. }
  255. //启动心跳包程序
  256. public static java.util.Timer heartBeatTimer;
  257. public static java.util.Timer heartBeatIconTimer;
  258. public static boolean iconREDFlag = true;
  259. public static void startHeartBeatTimer() {
  260. if(heartBeatTimer!=null) {
  261. heartBeatTimer.cancel();
  262. }
  263. heartBeatTimer = new java.util.Timer();
  264. heartBeatTimer.schedule(new TimerTask() {
  265. public void run() {
  266. try {
  267. if(nettyClient!=null&&tcp_connect_flag) {
  268. //System.out.println("发送心跳报文");
  269. DataUtil.heartBeat(nettyClient,mes_gw);
  270. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  271. }
  272. } catch (Exception e) {
  273. System.err.println("=== 心跳发送异常 ===");
  274. e.printStackTrace();
  275. }
  276. }
  277. }, 100,mes_heart_beat_cycle*1000);
  278. //心跳显示图标
  279. if(heartBeatIconTimer!=null) {
  280. heartBeatIconTimer.cancel();
  281. }
  282. heartBeatIconTimer = new Timer();
  283. heartBeatIconTimer.schedule(new TimerTask() {
  284. public void run() {
  285. try {
  286. if(tcp_connect_flag) {
  287. if(iconREDFlag) {
  288. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  289. iconREDFlag = false;
  290. }else {
  291. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  292. iconREDFlag = true;
  293. }
  294. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  295. heart_beat_menu.repaint();
  296. }else {
  297. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  298. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  299. heart_beat_menu.repaint();
  300. //若未连接则尝试连接
  301. if(nettyClient!=null&&!tcp_connect_flag){
  302. System.out.println("TCP已断开");
  303. //TCP重新同步连接
  304. if(!connect_request_flag) {
  305. //System.out.println("TCP重新同步连接");
  306. //设置TCP请求状态,只重新同步连接一次
  307. connect_request_flag = true;
  308. DataUtil.synrTcp(nettyClient,mes_gw);
  309. }
  310. }
  311. }
  312. } catch (Exception e) {
  313. System.err.println("=== 心跳图标更新异常 ===");
  314. e.printStackTrace();
  315. }
  316. }
  317. }, 100,mes_heart_icon_cycle*1000);
  318. }
  319. public static void startYgslHeartBeatTimer() {
  320. }
  321. //设置tcp连接状态显示
  322. public static void setTcpStatus() {
  323. if(tcp_connect_flag) {
  324. // status_menu.setText("已连接MES服务器");
  325. MesClient.setMenuStatus("已连接MES服务器",0);
  326. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  327. heart_beat_menu.repaint();
  328. }else {
  329. // status_menu.setText("未连接MES服务器");
  330. MesClient.setMenuStatus("未连接MES服务器",-1);
  331. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  332. heart_beat_menu.repaint();
  333. }
  334. }
  335. public static java.util.Timer cjTimer5;
  336. public static void deviceOnline() {
  337. if(cjTimer5 != null) {
  338. cjTimer5.cancel();
  339. }
  340. cjTimer5 = new java.util.Timer();
  341. cjTimer5.schedule(new java.util.TimerTask() {
  342. public void run() {
  343. try{
  344. for (GunPanel panel : gunPanels) {
  345. if (panel.isOnline()) {
  346. if (work_status == 1 && pz_status == 1) {
  347. if (panel.isTaskCompleted()) {
  348. panel.disableTool();
  349. } else {
  350. panel.enableTool();
  351. }
  352. } else {
  353. panel.disableTool();
  354. }
  355. }
  356. }
  357. }catch (Exception e){
  358. System.err.println("=== deviceOnline定时任务异常 ===");
  359. e.printStackTrace();
  360. }
  361. }
  362. }, 1000, 1000);
  363. }
  364. public static java.util.Timer cjTimer2;
  365. public static void upParams() {
  366. if(cjTimer2!=null) {
  367. cjTimer2.cancel();
  368. }
  369. cjTimer2 = new Timer();
  370. cjTimer2.schedule(new TimerTask() {
  371. public void run() {
  372. try{
  373. List<YgslResp> prods = JdbcUtils.getYgsl();
  374. System.out.println("datas:"+ JSON.toJSONString(prods));
  375. if(prods.size() > 0){
  376. // 定时上传参数
  377. JSONObject retObj = DataUtil.upParams(JSON.toJSONString(prods));
  378. if(retObj!=null && retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  379. // 更新sqlite已同步
  380. for(YgslResp prodReq:prods){
  381. JdbcUtils.updateSync(prodReq.getId(),1);
  382. }
  383. }
  384. }
  385. }catch (Exception e){
  386. System.err.println("=== upParams定时任务异常 ===");
  387. e.printStackTrace();
  388. }
  389. }
  390. }, 1000,30*1000);
  391. }
  392. public static java.util.Timer cjTimer3;
  393. public static void checkpz() {
  394. if(cjTimer3!=null) {
  395. cjTimer3.cancel();
  396. }
  397. cjTimer3 = new Timer();
  398. cjTimer3.schedule(new TimerTask() {
  399. public void run() {
  400. try{
  401. if(work_status == 1 && mes_gw.equals("OP290") && pz_status == 0){
  402. JSONObject retObj = DataUtil.checkpz(product_sn.getText());
  403. if(retObj!=null && retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  404. pz_status = 1; // 已拍照
  405. pzlabel.setVisible(true);
  406. }
  407. }
  408. }catch (Exception e){
  409. System.err.println("=== checkpz定时任务异常 ===");
  410. e.printStackTrace();
  411. }
  412. }
  413. }, 1000,2*1000);
  414. }
  415. public static void initWarehouseData(){
  416. resetScanA();
  417. }
  418. public static void resetScanA() {
  419. work_status = 0;
  420. check_quality_result = false;
  421. MesClient.finish_ok_bt.setEnabled(false);
  422. MesClient.finish_ng_bt.setEnabled(false);
  423. product_sn.setText("");
  424. MesClient.f_scan_data_bt_1.setEnabled(true);
  425. MesClient.setMenuStatus("请扫工件码",0);
  426. for (GunPanel panel : gunPanels) {
  427. panel.reset();
  428. panel.disableTool();
  429. }
  430. pzlabel.setVisible(false);
  431. if(mes_gw.equals("OP290")){
  432. pz_status = 0;
  433. }else{
  434. pz_status = 1;
  435. }
  436. updateMaterailData();
  437. shiftUserCheck();
  438. }
  439. public static int userLoginHours;//用户登录所处小时
  440. //换班用户信息检查
  441. private static void shiftUserCheck() {
  442. LocalDateTime now = LocalDateTime.now();
  443. // 判断时间范围
  444. if (userLoginHours >= 8 && userLoginHours < 20) {
  445. int hour = now.getHour();
  446. if (hour >= 20 || hour < 8) {
  447. logoff();
  448. }
  449. } else {
  450. int hour = now.getHour();
  451. if (hour >= 8 && hour < 20) {
  452. logoff();
  453. }
  454. }
  455. }
  456. public static void resetScanB() {
  457. }
  458. //获取用户20位
  459. public static void getUser() {
  460. user20 = user_menu.getText().toString();
  461. String space_tmp1 = "";
  462. if(user20.length()<20) {
  463. for(int i=0;i<20-user20.length();i++) {
  464. space_tmp1 = space_tmp1 + " ";
  465. }
  466. }
  467. user20 = user20 + space_tmp1;
  468. }
  469. //获取barcode内容36位
  470. public static String getBarcode(String barcodeTmp) {
  471. String barcodeRet = barcodeTmp;
  472. if(barcodeTmp.equalsIgnoreCase("")) {
  473. return "";
  474. }else {
  475. if(barcodeTmp.length()<36) {
  476. String space = "";
  477. for(int i=0;i<36-barcodeTmp.length();i++) {
  478. space = space + " ";
  479. }
  480. barcodeRet = barcodeTmp + space;
  481. }
  482. }
  483. return barcodeRet;
  484. }
  485. public static void scanBarcode() {
  486. if(work_status == 1){
  487. JOptionPane.showMessageDialog(mesClientFrame,"工作中,勿扫码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  488. return;
  489. }
  490. String scanBarcodeTitle = "";
  491. switch(scan_type) {
  492. case 1:
  493. product_sn.setText("");
  494. scanBarcodeTitle = "请扫工件码";
  495. break;
  496. }
  497. //弹窗扫工件码
  498. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  499. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  500. //获取用户
  501. getUser();
  502. //获取扫码内容36位
  503. String barcode36 = getBarcode(scanBarcode);//处理36为码
  504. //工位号
  505. String gw = "";
  506. switch(scan_type) {
  507. case 1:
  508. product_sn.setText(scanBarcode);
  509. break;
  510. }
  511. //刷新界面
  512. mesClientFrame.repaint();
  513. if(!tcp_connect_flag) {
  514. JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  515. return;
  516. }
  517. // 查询工件质量
  518. Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20);
  519. if(!sendret){
  520. JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  521. return;
  522. }
  523. }else {
  524. JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  525. return;
  526. }
  527. }
  528. public static void logoff() {
  529. // welcomeWin.setVisible(true);
  530. mesClientFrame.setVisible(false);
  531. nettyClient = null;
  532. // if(heartBeatTimer!=null) {
  533. // heartBeatTimer.cancel();
  534. // }
  535. // if(heartBeatIconTimer!=null) {
  536. // heartBeatIconTimer.cancel();
  537. // }
  538. tcp_connect_flag = false;
  539. connect_request_flag = false;
  540. }
  541. public MesClient() {
  542. setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png")));
  543. setTitle("MES系统客户端:"+mes_gw + "- " + mes_gw_des);
  544. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  545. setBounds(0, 0, 1024, 768);
  546. JMenuBar menuBar = new JMenuBar();
  547. menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26));
  548. setJMenuBar(menuBar);
  549. JMenu fileMenu = new JMenu("用户");
  550. fileMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png")));
  551. fileMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  552. menuBar.add(fileMenu);
  553. JMenuItem exitMenuItem = new JMenuItem("退出");
  554. exitMenuItem.setIcon(new ImageIcon(MesClient.class.getResource("/bg/logoff.png")));
  555. exitMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  556. fileMenu.add(exitMenuItem);
  557. exitMenuItem.addMouseListener(new MouseAdapter() {
  558. @Override
  559. public void mousePressed(MouseEvent e) {//只能检测到mousePressed事件
  560. super.mouseClicked(e);
  561. //dispose();
  562. logoff();
  563. }
  564. });
  565. JMenu settingMenu = new JMenu("设置");
  566. //settingMenu.setVisible(false);
  567. settingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
  568. settingMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  569. menuBar.add(settingMenu);
  570. JMenuItem resetTcpMenu = new JMenuItem("\u91CD\u8FDEMES");
  571. resetTcpMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  572. resetTcpMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  573. resetTcpMenu.addMouseListener(new MouseAdapter() {
  574. @Override
  575. public void mousePressed(MouseEvent e) {
  576. super.mouseClicked(e);
  577. //重连mes
  578. nettyClient.future.channel().close();
  579. }
  580. });
  581. settingMenu.add(resetTcpMenu);
  582. JMenuItem resetTcpMenu_1 = new JMenuItem("刷新工件");
  583. resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  584. resetTcpMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  585. resetTcpMenu_1.addMouseListener(new MouseAdapter() {
  586. @Override
  587. public void mousePressed(MouseEvent e) {
  588. super.mouseClicked(e);
  589. resetScanA();
  590. }
  591. });
  592. settingMenu.add(resetTcpMenu_1);
  593. JMenuItem resetTcpMenu_11 = new JMenuItem("手动提交");
  594. resetTcpMenu_11.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  595. resetTcpMenu_11.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  596. resetTcpMenu_11.addMouseListener(new MouseAdapter() {
  597. @Override
  598. public void mousePressed(MouseEvent e) {
  599. super.mouseClicked(e);
  600. MesClient.finish_ok_bt.setEnabled(true);
  601. MesClient.finish_ng_bt.setEnabled(true);
  602. }
  603. });
  604. settingMenu.add(resetTcpMenu_11);
  605. contentPane = new JPanel();
  606. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  607. setContentPane(contentPane);
  608. contentPane.setLayout(new BorderLayout(0, 0));
  609. JToolBar toolBar = new JToolBar();
  610. contentPane.add(toolBar, BorderLayout.NORTH);
  611. JLabel equipment_statu_label = new JLabel("状态:");
  612. equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER);
  613. equipment_statu_label.setForeground(Color.BLACK);
  614. equipment_statu_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  615. equipment_statu_label.setBackground(Color.LIGHT_GRAY);
  616. toolBar.add(equipment_statu_label);
  617. status_menu = new JButton("设备未连接MES服务器");
  618. if(tcp_connect_flag) {
  619. status_menu.setText("已连接MES服务器");
  620. status_menu.setForeground(Color.GREEN);
  621. }else {
  622. status_menu.setText("未连接MES服务器");
  623. status_menu.setForeground(Color.DARK_GRAY);
  624. }
  625. status_menu.addActionListener(new ActionListener() {
  626. public void actionPerformed(ActionEvent e) {
  627. }
  628. });
  629. status_menu.setForeground(Color.GREEN);
  630. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  631. status_menu.setBackground(Color.BLACK);
  632. toolBar.add(status_menu);
  633. JLabel space_1 = new JLabel(" ");
  634. toolBar.add(space_1);
  635. JLabel heart_beat_status_label = new JLabel("心跳:");
  636. heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  637. heart_beat_status_label.setForeground(Color.BLACK);
  638. heart_beat_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  639. heart_beat_status_label.setBackground(Color.LIGHT_GRAY);
  640. toolBar.add(heart_beat_status_label);
  641. heart_beat_menu = new JButton("2024-02-20 23:20:10");
  642. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  643. heart_beat_menu.setForeground(Color.GREEN);
  644. heart_beat_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  645. heart_beat_menu.setBackground(Color.BLACK);
  646. toolBar.add(heart_beat_menu);
  647. JLabel space_2 = new JLabel(" ");
  648. toolBar.add(space_2);
  649. JLabel user_status_label = new JLabel("登录用户:");
  650. user_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  651. user_status_label.setForeground(Color.BLACK);
  652. user_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  653. user_status_label.setBackground(Color.LIGHT_GRAY);
  654. toolBar.add(user_status_label);
  655. user_menu = new JButton("JinJuShi");
  656. user_menu.setForeground(Color.GREEN);
  657. user_menu.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  658. user_menu.setBackground(Color.BLACK);
  659. toolBar.add(user_menu);
  660. JLabel space_3 = new JLabel(" ");
  661. toolBar.add(space_3);
  662. JLabel space_4 = new JLabel(" ");
  663. toolBar.add(space_4);
  664. tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  665. tabbedPane.setMinimumSize(new Dimension(400, 50));
  666. tabbedPane.setFont(new Font("宋体", Font.BOLD, 22));
  667. contentPane.add(tabbedPane);
  668. //首页
  669. JPanel indexPanelA = new JPanel();
  670. indexScrollPaneA = new JScrollPane(indexPanelA);
  671. indexPanelA.setLayout(null);
  672. pzlabel = new JLabel("水嘴密封圈已拍照");
  673. pzlabel.setHorizontalAlignment(SwingConstants.CENTER);
  674. pzlabel.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  675. pzlabel.setForeground(Color.green);
  676. pzlabel.setBounds(81, 20, 602, 40);
  677. pzlabel.setVisible(false);
  678. indexPanelA.add(pzlabel);
  679. product_sn = new JTextField();
  680. product_sn.setHorizontalAlignment(SwingConstants.CENTER);
  681. product_sn.setEditable(false);
  682. product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  683. product_sn.setBounds(81, 70, 602, 70);
  684. indexPanelA.add(product_sn);
  685. product_sn.setColumns(10);
  686. f_scan_data_bt_1 = new JButton("扫码");
  687. f_scan_data_bt_1.addActionListener(new ActionListener() {
  688. public void actionPerformed(ActionEvent e) {
  689. scan_type = 1;
  690. scanBarcode();
  691. }
  692. });
  693. f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  694. f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  695. f_scan_data_bt_1.setBounds(693, 70, 198, 70);
  696. indexPanelA.add(f_scan_data_bt_1);
  697. // String[] hjtitles = new String[]{"焊机1","焊机2","焊机3"};
  698. // String[] hjvals = new String[]{"HJ001","HJ002","HJ003"};
  699. // mesRadioHj = new MesRadio(hjtitles,hjvals);
  700. // mesRadioHj.setSize(500,50);
  701. // mesRadioHj.setBounds(190,170,500,50);
  702. // indexPanelA.add(mesRadioHj);
  703. finish_ok_bt = new JButton("OK");
  704. finish_ok_bt.setEnabled(false);
  705. finish_ok_bt.addActionListener(new ActionListener() {
  706. public void actionPerformed(ActionEvent e) {
  707. if(work_status == 1 && check_quality_result){
  708. String sn = getBarcode(product_sn.getText());
  709. getUser();
  710. String qret = "OK";
  711. Boolean sendret = DataUtil.sendQuality(nettyClient,sn,qret,user20);
  712. if(!sendret){
  713. JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  714. return;
  715. }
  716. }
  717. }
  718. });
  719. finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
  720. finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  721. finish_ok_bt.setBounds(185, 405, 240, 80);
  722. finish_ok_bt.setEnabled(false);
  723. indexPanelA.add(finish_ok_bt);
  724. finish_ng_bt = new JButton("NG");
  725. finish_ng_bt.setEnabled(false);
  726. finish_ng_bt.addActionListener(new ActionListener() {
  727. public void actionPerformed(ActionEvent e) {
  728. if(work_status == 1 && check_quality_result){
  729. String sn = getBarcode(product_sn.getText());
  730. getUser();
  731. String qret = "NG";
  732. Boolean sendret = DataUtil.sendQuality(nettyClient,sn,qret,user20);
  733. if(!sendret){
  734. JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  735. return;
  736. }
  737. }
  738. }
  739. });
  740. finish_ng_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ng_bg.png")));
  741. finish_ng_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  742. finish_ng_bt.setBounds(495, 405, 240, 80);
  743. finish_ng_bt.setEnabled(false);
  744. indexPanelA.add(finish_ng_bt);
  745. gunPanelContainer = new JPanel();
  746. gunPanelContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 15));
  747. gunPanelContainer.setBounds(20, 160, 750, 300);
  748. gunPanelContainer.setOpaque(false);
  749. indexPanelA.add(gunPanelContainer);
  750. tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null);
  751. tabbedPane.setEnabledAt(0, true);
  752. // searchScrollPane = new JScrollPane((Component) null);
  753. indexPanelC = new JPanel();
  754. searchScrollPaneDj = new JScrollPane(indexPanelC);
  755. indexPanelC.setLayout(null);
  756. tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null);
  757. indexPanelB = new JPanel();
  758. searchScrollPane = new JScrollPane(indexPanelB);
  759. indexPanelB.setLayout(null);
  760. tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
  761. // 增加工具配置页面
  762. JPanel indexConfigPanel = new JPanel();
  763. indexConfigPanel.setLayout(null);
  764. tabbedPane.addTab("工具配置", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexConfigPanel, null);
  765. initGunConfigPanel(indexConfigPanel);
  766. tabbedPane.addChangeListener(new ChangeListener() {
  767. @Override
  768. public void stateChanged(ChangeEvent e) {
  769. JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
  770. int selectedIndex = tabbedPane.getSelectedIndex();
  771. System.out.println("selectedIndex:"+selectedIndex);
  772. if(selectedIndex == 1){
  773. }
  774. }
  775. });
  776. }
  777. public static void setMenuStatus(String msg,int error){
  778. if(error == 0){
  779. MesClient.status_menu.setForeground(Color.GREEN);
  780. }else{
  781. MesClient.status_menu.setForeground(Color.RED);
  782. }
  783. MesClient.status_menu.setText(msg);
  784. }
  785. public static void getMaterailData(){
  786. JSONObject retObj = DataUtil.getBindMaterail();
  787. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  788. java.util.List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  789. int i = 0;
  790. rowData = new Object[arrs.size()][7];
  791. for (BindMaterialResp bindMaterialResp:arrs){
  792. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  793. rowData[i][1] = bindMaterialResp.getBatchSn();
  794. rowData[i][2] = bindMaterialResp.getLastTimes();
  795. rowData[i][3] = "";
  796. rowData[i][4] = bindMaterialResp.getCraft();
  797. rowData[i][5] = bindMaterialResp.getMaterialId();
  798. rowData[i][6] = bindMaterialResp.getType();
  799. i++;
  800. }
  801. bindBatchPanel();
  802. }
  803. }
  804. public static void updateMaterailData(){
  805. JSONObject retObj = DataUtil.getBindMaterail();
  806. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  807. List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  808. int i = 0;
  809. for (BindMaterialResp bindMaterialResp:arrs){
  810. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  811. rowData[i][1] = bindMaterialResp.getBatchSn();
  812. rowData[i][2] = bindMaterialResp.getLastTimes();
  813. rowData[i][3] = "";
  814. rowData[i][4] = bindMaterialResp.getCraft();
  815. rowData[i][5] = bindMaterialResp.getMaterialId();
  816. rowData[i][6] = bindMaterialResp.getType();
  817. i++;
  818. }
  819. MesClient.table.repaint();
  820. }
  821. }
  822. // 绑定物料批次码
  823. public static void scanBatchSn(BindMaterialResp bindMaterialResp) {
  824. //弹窗扫工件码
  825. String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle();
  826. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  827. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  828. JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType());
  829. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  830. MesClient.setMenuStatus("扫物料:"+bindMaterialResp.getMaterialTitle()+"成功",0);
  831. updateMaterailData();
  832. }else{
  833. if(retObj.get("result")==null){
  834. MesClient.setMenuStatus("请求失败,请重试",-1);
  835. }else{
  836. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  837. MesClient.setMenuStatus(retObj.getString("message"),-1);
  838. }
  839. }
  840. }
  841. }
  842. }
  843. public static void bindBatchPanel(){
  844. JPanel indexPanelBB = new JPanel();
  845. JPanel panel = new JPanel();
  846. panel.setBounds(0, 0, 990, 550);
  847. // panel.setBounds(81, 50, 810, 479);
  848. indexPanelBB.add(panel);
  849. panel.setLayout(new GridLayout(0, 1, 0, 0));
  850. table = new JTable(rowData, columnNames){
  851. public boolean isCellEditable(int row, int column) {
  852. if(column == 3){
  853. return true;
  854. }
  855. return false;
  856. }
  857. };
  858. table.setRowHeight(40);
  859. table.setEnabled(true);
  860. table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  861. table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton());
  862. table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton());
  863. JScrollPane scrollPane = new JScrollPane(table);
  864. panel.add(scrollPane);
  865. JScrollPane indexScrollPaneB = new JScrollPane(indexPanelBB);
  866. indexPanelBB.setLayout(null);
  867. tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null);
  868. }
  869. private static JTable configTable;
  870. private static JButton addGunButton;
  871. private static JButton saveConfigButton;
  872. public static void initGunConfigPanel(JPanel panel) {
  873. JLabel titleLabel = new JLabel("扭力枪配置管理");
  874. titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
  875. titleLabel.setBounds(350, 20, 300, 40);
  876. panel.add(titleLabel);
  877. String[] columnNames = {"枪号", "名称", "IP地址", "端口", "任务数", "启用", "自动提交"};
  878. List<YgslConfig> configs = JdbcUtils.getYgslConfigs();
  879. Object[][] data = new Object[configs.size()][7];
  880. for (int i = 0; i < configs.size(); i++) {
  881. YgslConfig config = configs.get(i);
  882. data[i][0] = config.getGunIndex();
  883. data[i][1] = config.getGunName();
  884. data[i][2] = config.getIpAddress();
  885. data[i][3] = config.getPort();
  886. data[i][4] = config.getTaskCount();
  887. data[i][5] = config.getEnabled() == 1 ? "是" : "否";
  888. data[i][6] = config.getAutoSubmit() == 1 ? "是" : "否";
  889. }
  890. configTable = new JTable(data, columnNames);
  891. configTable.setRowHeight(35);
  892. configTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  893. configTable.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
  894. JScrollPane scrollPane = new JScrollPane(configTable);
  895. scrollPane.setBounds(50, 70, 900, 400);
  896. panel.add(scrollPane);
  897. addGunButton = new JButton("添加枪");
  898. addGunButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  899. addGunButton.setBounds(150, 490, 120, 40);
  900. addGunButton.addActionListener(new ActionListener() {
  901. @Override
  902. public void actionPerformed(ActionEvent e) {
  903. addNewGun(panel);
  904. }
  905. });
  906. panel.add(addGunButton);
  907. saveConfigButton = new JButton("保存配置");
  908. saveConfigButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  909. saveConfigButton.setBounds(300, 490, 120, 40);
  910. saveConfigButton.addActionListener(new ActionListener() {
  911. @Override
  912. public void actionPerformed(ActionEvent e) {
  913. saveGunConfigs();
  914. }
  915. });
  916. panel.add(saveConfigButton);
  917. JButton deleteButton = new JButton("删除选中");
  918. deleteButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  919. deleteButton.setBounds(450, 490, 120, 40);
  920. deleteButton.addActionListener(new ActionListener() {
  921. @Override
  922. public void actionPerformed(ActionEvent e) {
  923. int selectedRow = configTable.getSelectedRow();
  924. if (selectedRow >= 0) {
  925. int confirm = JOptionPane.showConfirmDialog(mesClientFrame, "确定要删除选中的配置吗?", "确认删除", JOptionPane.YES_NO_OPTION);
  926. if (confirm == JOptionPane.YES_OPTION) {
  927. List<YgslConfig> allConfigs = JdbcUtils.getYgslConfigs();
  928. if (selectedRow < allConfigs.size()) {
  929. JdbcUtils.deleteYgslConfig(allConfigs.get(selectedRow).getId());
  930. reloadGunConfigs(panel);
  931. }
  932. }
  933. } else {
  934. JOptionPane.showMessageDialog(mesClientFrame, "请先选中一行", "提示", JOptionPane.INFORMATION_MESSAGE);
  935. }
  936. }
  937. });
  938. panel.add(deleteButton);
  939. configTable.addMouseListener(new MouseAdapter() {
  940. public void mouseDoubleClick(MouseEvent e) {
  941. int row = configTable.getSelectedRow();
  942. if (row >= 0) {
  943. editGunConfig(row, panel);
  944. }
  945. }
  946. });
  947. }
  948. private static void editGunConfig(int row, JPanel panel) {
  949. List<YgslConfig> configs = JdbcUtils.getYgslConfigs();
  950. if (row >= configs.size()) return;
  951. YgslConfig config = configs.get(row);
  952. JDialog dialog = new JDialog(mesClientFrame, "编辑扭力枪", true);
  953. dialog.setSize(400, 400);
  954. dialog.setLocationRelativeTo(mesClientFrame);
  955. dialog.setLayout(null);
  956. JLabel idLabel = new JLabel("ID: " + config.getId());
  957. idLabel.setBounds(50, 20, 300, 30);
  958. dialog.add(idLabel);
  959. JLabel nameLabel = new JLabel("枪号:");
  960. nameLabel.setBounds(50, 60, 80, 30);
  961. dialog.add(nameLabel);
  962. JTextField nameField = new JTextField(String.valueOf(config.getGunIndex()));
  963. nameField.setBounds(140, 60, 180, 30);
  964. dialog.add(nameField);
  965. JLabel gunNameLabel = new JLabel("名称:");
  966. gunNameLabel.setBounds(50, 100, 80, 30);
  967. dialog.add(gunNameLabel);
  968. JTextField gunNameField = new JTextField(config.getGunName());
  969. gunNameField.setBounds(140, 100, 180, 30);
  970. dialog.add(gunNameField);
  971. JLabel ipLabel = new JLabel("IP地址:");
  972. ipLabel.setBounds(50, 140, 80, 30);
  973. dialog.add(ipLabel);
  974. JTextField ipField = new JTextField(config.getIpAddress());
  975. ipField.setBounds(140, 140, 180, 30);
  976. dialog.add(ipField);
  977. JLabel portLabel = new JLabel("端口:");
  978. portLabel.setBounds(50, 180, 80, 30);
  979. dialog.add(portLabel);
  980. JTextField portField = new JTextField(String.valueOf(config.getPort()));
  981. portField.setBounds(140, 180, 180, 30);
  982. dialog.add(portField);
  983. JLabel taskLabel = new JLabel("任务数:");
  984. taskLabel.setBounds(50, 220, 80, 30);
  985. dialog.add(taskLabel);
  986. JTextField taskField = new JTextField(String.valueOf(config.getTaskCount()));
  987. taskField.setBounds(140, 220, 180, 30);
  988. dialog.add(taskField);
  989. JLabel enabledLabel = new JLabel("启用:");
  990. enabledLabel.setBounds(50, 260, 80, 30);
  991. dialog.add(enabledLabel);
  992. JComboBox<String> enabledBox = new JComboBox<>(new String[]{"否", "是"});
  993. enabledBox.setBounds(140, 260, 180, 30);
  994. enabledBox.setSelectedIndex(config.getEnabled() == 1 ? 1 : 0);
  995. dialog.add(enabledBox);
  996. JButton okButton = new JButton("保存");
  997. okButton.setBounds(100, 310, 80, 30);
  998. okButton.addActionListener(new ActionListener() {
  999. @Override
  1000. public void actionPerformed(ActionEvent e) {
  1001. try {
  1002. config.setGunIndex(Integer.parseInt(nameField.getText()));
  1003. config.setGunName(gunNameField.getText());
  1004. config.setIpAddress(ipField.getText());
  1005. config.setPort(Integer.parseInt(portField.getText()));
  1006. config.setTaskCount(Integer.parseInt(taskField.getText()));
  1007. config.setEnabled(enabledBox.getSelectedIndex() == 1 ? 1 : 0);
  1008. JdbcUtils.updateYgslConfig(config);
  1009. dialog.dispose();
  1010. reloadGunConfigs(panel);
  1011. } catch (Exception ex) {
  1012. JOptionPane.showMessageDialog(dialog, "输入格式错误,请检查", "错误", JOptionPane.ERROR_MESSAGE);
  1013. }
  1014. }
  1015. });
  1016. dialog.add(okButton);
  1017. JButton cancelButton = new JButton("取消");
  1018. cancelButton.setBounds(200, 310, 80, 30);
  1019. cancelButton.addActionListener(new ActionListener() {
  1020. @Override
  1021. public void actionPerformed(ActionEvent e) {
  1022. dialog.dispose();
  1023. }
  1024. });
  1025. dialog.add(cancelButton);
  1026. dialog.setVisible(true);
  1027. }
  1028. private static void addNewGun(JPanel panel) {
  1029. JDialog dialog = new JDialog(mesClientFrame, "添加扭力枪", true);
  1030. dialog.setSize(400, 350);
  1031. dialog.setLocationRelativeTo(mesClientFrame);
  1032. dialog.setLayout(null);
  1033. JLabel nameLabel = new JLabel("枪号:");
  1034. nameLabel.setBounds(50, 30, 80, 30);
  1035. dialog.add(nameLabel);
  1036. JTextField nameField = new JTextField();
  1037. nameField.setBounds(140, 30, 180, 30);
  1038. dialog.add(nameField);
  1039. JLabel gunNameLabel = new JLabel("名称:");
  1040. gunNameLabel.setBounds(50, 80, 80, 30);
  1041. dialog.add(gunNameLabel);
  1042. JTextField gunNameField = new JTextField("M5");
  1043. gunNameField.setBounds(140, 80, 180, 30);
  1044. dialog.add(gunNameField);
  1045. JLabel ipLabel = new JLabel("IP地址:");
  1046. ipLabel.setBounds(50, 130, 80, 30);
  1047. dialog.add(ipLabel);
  1048. JTextField ipField = new JTextField("192.168.5.100");
  1049. ipField.setBounds(140, 130, 180, 30);
  1050. dialog.add(ipField);
  1051. JLabel portLabel = new JLabel("端口:");
  1052. portLabel.setBounds(50, 180, 80, 30);
  1053. dialog.add(portLabel);
  1054. JTextField portField = new JTextField("4545");
  1055. portField.setBounds(140, 180, 180, 30);
  1056. dialog.add(portField);
  1057. JLabel taskLabel = new JLabel("任务数:");
  1058. taskLabel.setBounds(50, 230, 80, 30);
  1059. dialog.add(taskLabel);
  1060. JTextField taskField = new JTextField("12");
  1061. taskField.setBounds(140, 230, 180, 30);
  1062. dialog.add(taskField);
  1063. JButton okButton = new JButton("确定");
  1064. okButton.setBounds(100, 280, 80, 30);
  1065. okButton.addActionListener(new ActionListener() {
  1066. @Override
  1067. public void actionPerformed(ActionEvent e) {
  1068. try {
  1069. int gunIndex = Integer.parseInt(nameField.getText());
  1070. String gunName = gunNameField.getText();
  1071. String ip = ipField.getText();
  1072. int port = Integer.parseInt(portField.getText());
  1073. int taskCount = Integer.parseInt(taskField.getText());
  1074. YgslConfig config = new YgslConfig(gunIndex, gunName, ip, port, taskCount);
  1075. JdbcUtils.insertYgslConfig(config);
  1076. dialog.dispose();
  1077. reloadGunConfigs(panel);
  1078. } catch (Exception ex) {
  1079. JOptionPane.showMessageDialog(dialog, "输入格式错误,请检查", "错误", JOptionPane.ERROR_MESSAGE);
  1080. }
  1081. }
  1082. });
  1083. dialog.add(okButton);
  1084. JButton cancelButton = new JButton("取消");
  1085. cancelButton.setBounds(200, 280, 80, 30);
  1086. cancelButton.addActionListener(new ActionListener() {
  1087. @Override
  1088. public void actionPerformed(ActionEvent e) {
  1089. dialog.dispose();
  1090. }
  1091. });
  1092. dialog.add(cancelButton);
  1093. dialog.setVisible(true);
  1094. }
  1095. private static void saveGunConfigs() {
  1096. try {
  1097. List<YgslConfig> configs = JdbcUtils.getYgslConfigs();
  1098. int rowCount = configTable.getRowCount();
  1099. for (int i = 0; i < rowCount && i < configs.size(); i++) {
  1100. YgslConfig config = configs.get(i);
  1101. config.setGunIndex((Integer) configTable.getValueAt(i, 0));
  1102. config.setGunName((String) configTable.getValueAt(i, 1));
  1103. config.setIpAddress((String) configTable.getValueAt(i, 2));
  1104. config.setPort((Integer) configTable.getValueAt(i, 3));
  1105. config.setTaskCount((Integer) configTable.getValueAt(i, 4));
  1106. config.setEnabled(((String) configTable.getValueAt(i, 5)).equals("是") ? 1 : 0);
  1107. config.setAutoSubmit(((String) configTable.getValueAt(i, 6)).equals("是") ? 1 : 0);
  1108. JdbcUtils.updateYgslConfig(config);
  1109. }
  1110. JOptionPane.showMessageDialog(mesClientFrame, "配置保存成功!重启后生效", "提示", JOptionPane.INFORMATION_MESSAGE);
  1111. } catch (Exception e) {
  1112. e.printStackTrace();
  1113. JOptionPane.showMessageDialog(mesClientFrame, "保存失败:" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
  1114. }
  1115. }
  1116. private static void reloadGunConfigs(JPanel panel) {
  1117. panel.removeAll();
  1118. initGunConfigPanel(panel);
  1119. panel.revalidate();
  1120. panel.repaint();
  1121. }
  1122. }