MesClient.java 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONArray;
  4. import com.alibaba.fastjson2.JSONObject;
  5. import com.github.s7connector.impl.utils.S7Utils;
  6. import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
  7. import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
  8. import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
  9. import com.mes.component.MesRadio;
  10. import com.mes.component.ProductTypePanel;
  11. import com.mes.component.MesWebView;
  12. import com.mes.component.MyDialog;
  13. import com.mes.netty.NettyClient;
  14. import com.mes.util.ClientVersionUtils;
  15. import com.mes.util.ClientUpgrade;
  16. import com.mes.util.ConfigUtil;
  17. import com.mes.util.DateLocalUtils;
  18. import com.mes.util.HttpUtils;
  19. import com.mes.util.IweldCloudUtil;
  20. import com.mes.util.JdbcUtils;
  21. import javafx.embed.swing.JFXPanel;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import javax.swing.*;
  25. import javax.swing.border.EmptyBorder;
  26. import javax.swing.event.ChangeEvent;
  27. import javax.swing.event.ChangeListener;
  28. import java.awt.*;
  29. import java.awt.event.ActionEvent;
  30. import java.awt.event.ActionListener;
  31. import java.awt.event.MouseAdapter;
  32. import java.awt.event.MouseEvent;
  33. import java.io.BufferedReader;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.io.InputStreamReader;
  37. import java.sql.ResultSet;
  38. import java.sql.SQLException;
  39. import java.sql.Statement;
  40. import java.time.LocalDateTime;
  41. import java.util.ArrayList;
  42. import java.util.LinkedHashMap;
  43. import java.util.List;
  44. import java.util.Map;
  45. import java.util.Properties;
  46. import java.util.Timer;
  47. import java.util.TimerTask;
  48. public class MesClient extends JFrame {
  49. public static final Logger log = LoggerFactory.getLogger(MesClient.class);
  50. private static String Drivde = "org.sqlite.JDBC";
  51. public static int mes_auth = 0;
  52. public static String mes_gw = "";
  53. public static String mes_gw_des = "";
  54. public static String mes_server_ip = "";
  55. public static int mes_tcp_port = 3000;
  56. public static int mes_heart_beat_cycle = 10;
  57. public static int mes_heart_icon_cycle = 1;
  58. public static String mes_line_sn = "";
  59. public static String client_version = "0.0.0"; // 打包发布版本,需和后台 JAR 管理版本号对齐
  60. public static String mes_gwflag = ""; // 工位标识
  61. public static NettyClient nettyClient;
  62. public static boolean tcp_connect_flag = false;
  63. public static boolean connect_request_flag = false;
  64. private static volatile long lastTcpReconnectMs = 0;
  65. private static final Object tcpReconnectLock = new Object();
  66. /** 断线后延迟重连,避免报文尚未回包就被 SYNR 新连接打断 */
  67. public static void scheduleTcpReconnect(){
  68. synchronized (tcpReconnectLock){
  69. long now = System.currentTimeMillis();
  70. if(now - lastTcpReconnectMs < 3000){
  71. return;
  72. }
  73. if(connect_request_flag || nettyClient == null){
  74. return;
  75. }
  76. connect_request_flag = true;
  77. lastTcpReconnectMs = now;
  78. }
  79. new Timer(true).schedule(new TimerTask() {
  80. @Override
  81. public void run() {
  82. try{
  83. if(nettyClient != null && !tcp_connect_flag){
  84. System.out.println("TCP断线,延迟重连...");
  85. DataUtil.synrTcp(nettyClient, mes_gw);
  86. }
  87. }finally {
  88. connect_request_flag = false;
  89. }
  90. }
  91. }, 2000);
  92. }
  93. //session
  94. public static String sessionid = "";
  95. public static JPanel contentPane;
  96. public static MesClient mesClientFrame;
  97. public static JTabbedPane tabbedPane;
  98. public static JScrollPane indexScrollPaneA;
  99. public static JScrollPane indexScrollPaneB;
  100. public static JScrollPane searchScrollPane;
  101. public static JScrollPane searchScrollPaneDj;
  102. public static JScrollPane searchScrollPaneDjB;
  103. public static JScrollPane searchScrollPaneGz;
  104. public static Boolean check_quality_result = false;
  105. public static Integer work_status = 0;
  106. public static Integer work_status2 = 0;
  107. public static Boolean check_quality_result2 = false;
  108. public static Integer batch_status = 0; //A面 1=已绑定 0=未绑定
  109. public static Integer batch_status2 = 0; //B面 1=已绑定 0=未绑定
  110. public static JButton heart_beat_menu;
  111. public static JButton status_menu;
  112. public static JButton user_menu;
  113. public static int scan_type = 0;
  114. public static JButton batch_scan_1;
  115. public static JButton batch_scan_2;
  116. public static JTextField product_sn;
  117. public static JLabel pxstatus1;
  118. public static JLabel pxstatus2;
  119. public static Integer tjFlag = 0; // 0=未开始 1=已扫码,设备未启动 2=设备运行中 3=设备运行结束
  120. public static Integer tjFlaga = 0; // 0=未开始 1=已扫码,设备未启动 2=设备运行中 3=设备运行结束
  121. public static Integer tjFlagb = 0; // 0=未开始 1=已扫码,设备未启动 2=设备运行中 3=设备运行结束
  122. public static Integer tjStatusa = 0; // 1=提交失败
  123. public static Integer tjStatusb = 0; // 1=提交失败
  124. public static String curFlag = ""; // 当前工作的面板
  125. public static String curSna = "";
  126. public static String curSnb = "";
  127. public static String curPage = "";
  128. public static Integer checkA = 0;
  129. public static Integer checkB = 0;
  130. public static Integer lastpage = 0;
  131. public static Boolean mesQualityFlagA = false; // true=可工作
  132. public static Boolean mesQualityFlagB = false; // true=可工作
  133. public static Boolean mesErrorFlagA = false; // true=提交MES失败,手动提交
  134. public static Boolean mesErrorFlagB = false; // true=提交MES失败,手动提交
  135. public static String user20 = "";
  136. public static JFrame welcomeWin;
  137. public static JPanel indexPanelB;
  138. public static MesWebView jfxPanel = null;
  139. public static MesRadio mesRadioHj;
  140. public static ProductTypePanel productTypePanel;
  141. public static JLabel productTypeCurrentLabel;
  142. public static final int PRODUCT_SN_LENGTH = 32;
  143. public static final String PREFIX_ZJ = "000020021815-01000016"; //智界
  144. public static final String PREFIX_SJ = "000020022029-01000016"; //尚界
  145. public static final boolean PRODUCT_TYPE_SWITCH_ENABLED = false;
  146. public static final String PRODUCT_TYPE_SWITCH_PASSWORD = "503833";
  147. public static final String PRODUCT_TYPE_ZJ = ProductTypePanel.TYPE_ZJ;
  148. public static final String PRODUCT_TYPE_SJ = ProductTypePanel.TYPE_SJ;
  149. public static JPanel indexPanelC;
  150. public static MesWebView jfxPanel2 = null;
  151. public static JPanel indexPanelDjB;
  152. public static MesWebView jfxPanelDjB = null;
  153. // 开班点检(本地UI,不再加载网页)
  154. public static JLabel[] dj_a_lamps = new JLabel[6];
  155. public static JLabel[] dj_b_lamps = new JLabel[6];
  156. public static JButton dj_start_btn;
  157. public static S7PLC s7PLC = new S7PLC(EPlcType.S1200, "192.168.0.1"); //100 B工位
  158. // public static S7PLC s7PLC = new S7PLC(EPlcType.S1200, "192.168.7.2"); //2 A工位
  159. public static List<String> hjparams = new ArrayList<>();
  160. public static JTable table;
  161. public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" };
  162. public static Object[][] rowData = null;
  163. // public static Object[][] rowData = {
  164. // };
  165. public static void main(String[] args) {
  166. if (ClientUpgrade.handleInstallArgs(args)) {
  167. return;
  168. }
  169. EventQueue.invokeLater(new Runnable() {
  170. @Override
  171. public void run() {
  172. try{
  173. //鐠囩粯鏋冩禒鍫曞帳缂冿拷
  174. readProperty();
  175. if (ClientUpgrade.recoverIncompleteInstall()) {
  176. return;
  177. }
  178. ClientUpgrade.cleanupInstallLeftovers();
  179. ClientUpgrade.checkIfConfigured(null);
  180. // 閺勫墽銇氶悾宀勬桨
  181. mesClientFrame = new MesClient();
  182. mesClientFrame.setVisible(false);
  183. JdbcUtils.getConn();
  184. welcomeWin = new LoginFarme();
  185. welcomeWin.setVisible(true);
  186. getMaterailData();
  187. //获取当前面
  188. textFocus();
  189. getPlcParam();
  190. //
  191. upParams();
  192. //
  193. getPlcParams();
  194. }catch (Exception e){
  195. e.printStackTrace();
  196. }
  197. }
  198. });
  199. }
  200. public static Timer cjTimerText;
  201. public static void textFocus() {
  202. if(cjTimerText!=null) {
  203. cjTimerText.cancel();
  204. }
  205. cjTimerText = new Timer();
  206. cjTimerText.schedule(new TimerTask() {
  207. public void run() {
  208. txtfa.requestFocusInWindow();
  209. MesClient.curPage = PlcUtil.getCurAside(s7PLC);
  210. if(MesClient.curPage .equals("A")){
  211. page_text.setText("当前:A面");
  212. }else{
  213. page_text.setText("当前:B面");
  214. }
  215. }
  216. }, 1000,1000);
  217. }
  218. public static Timer cjTimer3;
  219. public static void upParams() {
  220. if(cjTimer3!=null) {
  221. cjTimer3.cancel();
  222. }
  223. cjTimer3 = new Timer();
  224. cjTimer3.schedule(new TimerTask() {
  225. public void run() {
  226. try{
  227. List<CmtReq> prods = JdbcUtils.getCmtParams();
  228. System.out.println("prods:"+ JSON.toJSONString(prods));
  229. if(prods.size() > 0){
  230. // 定时上传参数
  231. JSONObject retObj = DataUtil.upParams(JSON.toJSONString(prods));
  232. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  233. // 更新sqlite已同步
  234. for(CmtReq prodReq:prods){
  235. JdbcUtils.updateProdSync(prodReq.getId(),1);
  236. }
  237. }
  238. }
  239. }catch (Exception e){
  240. e.printStackTrace();
  241. }
  242. }
  243. }, 1000,30*1000);
  244. }
  245. public static Timer cjTimer;
  246. public static void getPlcParam() {
  247. if(cjTimer!=null) {
  248. cjTimer.cancel();
  249. }
  250. cjTimer = new Timer();
  251. cjTimer.schedule(new TimerTask() {
  252. public void run() {
  253. try{
  254. if(!MesClient.sessionid.isEmpty()){
  255. PlcUtil.getStatusA(s7PLC);
  256. }
  257. }catch (Exception e){
  258. e.printStackTrace();
  259. }
  260. try{
  261. if(!MesClient.sessionid.isEmpty()){
  262. PlcUtil.getStatusB(s7PLC);
  263. }
  264. }catch (Exception e){
  265. e.printStackTrace();
  266. }
  267. }
  268. }, 1000,2000);
  269. }
  270. public static Timer cjTimer2;
  271. public static void getPlcParams() {
  272. if(cjTimer2!=null) {
  273. cjTimer2.cancel();
  274. }
  275. cjTimer2 = new Timer();
  276. cjTimer2.schedule(new TimerTask() {
  277. public void run() {
  278. try{
  279. IweldCloudUtil.collectWeldingParams();
  280. }catch (Exception e){
  281. e.printStackTrace();
  282. }
  283. }
  284. }, 1000, IweldCloudUtil.getCollectIntervalMs());
  285. }
  286. //鐠囧鍘ょ純顔芥瀮娴狅拷
  287. private static String parseClientVersion(String value) {
  288. if (value == null || value.trim().isEmpty()) {
  289. return "0.0.0";
  290. }
  291. return ClientVersionUtils.normalize(value.trim());
  292. }
  293. private static void readProperty() throws IOException{
  294. Properties pro = ConfigUtil.loadProperties();
  295. ConfigUtil.printStartupInfo();
  296. mes_gw = pro.getProperty("mes.gw");
  297. client_version = parseClientVersion(pro.getProperty("mes.client.version"));
  298. // mes_gw_des = pro.getProperty("mes.gw_des");
  299. mes_server_ip = pro.getProperty("mes.server_ip");
  300. mes_tcp_port = Integer.parseInt(pro.getProperty("mes.tcp_port"));
  301. mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle"));
  302. mes_line_sn = pro.getProperty("mes.line_sn");
  303. mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
  304. mes_gwflag = pro.getProperty("mes.gwflag");
  305. System.out.println(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle);
  306. }
  307. // 閸掓繂顫愰崠鏈P
  308. public static void initTcpConnection() {
  309. try {
  310. if(nettyClient==null) {
  311. //閸掓繂顫愰崠鏈P鏉╃偞甯�
  312. nettyClient = new NettyClient();
  313. //TCP鏉╃偞甯撮悩鑸碉拷锟�
  314. tcp_connect_flag = false;
  315. //鐠佸墽鐤員CP鐠囬攱鐪伴悩鑸碉拷锟�
  316. connect_request_flag = true;
  317. DataUtil.synrTcp(nettyClient,mes_gw);
  318. }
  319. } catch (Exception e) {
  320. // TODO Auto-generated catch block
  321. e.printStackTrace();
  322. }
  323. }
  324. //閸氼垰濮╄箛鍐儲閸栧懐鈻兼惔锟�
  325. public static java.util.Timer heartBeatTimer;
  326. public static java.util.Timer heartBeatIconTimer;
  327. public static java.util.Timer iweldLoginTimer;
  328. public static boolean iconREDFlag = true;
  329. public static JTextField param1;
  330. public static JTextField param2;
  331. public static JTextField param3;
  332. public static JTextField param4;
  333. public static JTextField product_sn2;
  334. public static JTextField param21;
  335. public static JTextField param22;
  336. public static JTextField param23;
  337. public static JTextField param24;
  338. public static void startHeartBeatTimer() {
  339. if(heartBeatTimer!=null) {
  340. heartBeatTimer.cancel();
  341. }
  342. heartBeatTimer = new java.util.Timer();
  343. heartBeatTimer.schedule(new TimerTask() {
  344. public void run() {
  345. if(nettyClient!=null&&tcp_connect_flag) {
  346. //System.out.println("閸欐垿锟戒礁绺剧捄铏Г閺傦拷");
  347. DataUtil.heartBeat(nettyClient,mes_gw);
  348. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  349. }
  350. }
  351. }, 100,mes_heart_beat_cycle*1000);
  352. //韫囧啳鐑﹂弰鍓с仛閸ョ偓鐖�
  353. if(heartBeatIconTimer!=null) {
  354. heartBeatIconTimer.cancel();
  355. }
  356. heartBeatIconTimer = new Timer();
  357. heartBeatIconTimer.schedule(new TimerTask() {
  358. public void run() {
  359. if(tcp_connect_flag) {
  360. if(iconREDFlag) {
  361. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  362. iconREDFlag = false;
  363. }else {
  364. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  365. iconREDFlag = true;
  366. }
  367. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  368. heart_beat_menu.repaint();
  369. }else {
  370. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  371. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  372. heart_beat_menu.repaint();
  373. // TCP 断线时延迟重连
  374. if(nettyClient!=null && !tcp_connect_flag){
  375. scheduleTcpReconnect();
  376. }
  377. }
  378. }
  379. }, 100,mes_heart_icon_cycle*1000);
  380. }
  381. public static void startIweldLoginTimer() {
  382. if(iweldLoginTimer != null) {
  383. iweldLoginTimer.cancel();
  384. }
  385. iweldLoginTimer = new Timer();
  386. long twoHoursMs = 2 * 60 * 60 * 1000L;
  387. iweldLoginTimer.schedule(new TimerTask() {
  388. public void run() {
  389. IweldCloudUtil.login();
  390. }
  391. }, 0, twoHoursMs);
  392. }
  393. //鐠佸墽鐤唗cp鏉╃偞甯撮悩鑸碉拷浣规▔缁�锟�
  394. public static void setTcpStatus() {
  395. if(tcp_connect_flag) {
  396. status_menu.setText("已连接MES服务器");
  397. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  398. heart_beat_menu.repaint();
  399. }else {
  400. status_menu.setText("未连接MES服务器");
  401. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  402. heart_beat_menu.repaint();
  403. }
  404. }
  405. public static void initWarehouseData(){
  406. loadProductTypeFromDb();
  407. resetScanA();
  408. resetScanB();
  409. PlcUtil.changeEnable(s7PLC,false);
  410. }
  411. public static void resetScanA() {
  412. work_status = 0;
  413. check_quality_result = false;
  414. MesClient.mesQualityFlagA = false;
  415. product_sn.setText("");
  416. MesClient.pxstatus1.setText("A");
  417. MesClient.param1.setText("");
  418. MesClient.param2.setText("");
  419. MesClient.param3.setText("");
  420. MesClient.param4.setText("");
  421. MesClient.param21.setText("");
  422. MesClient.param22.setText("");
  423. MesClient.param23.setText("");
  424. MesClient.param24.setText("");
  425. MesClient.curSna = "";
  426. MesClient.tjFlaga = 0;
  427. MesClient.checkA = 0;
  428. // MesClient.batch_status = 0;
  429. // MesClient.textField.setText("");
  430. // MesClient.batch_scan_1.setEnabled(false);
  431. MesClient.formatScanType(1);
  432. // PlcUtil.changeEnable(MesClient.s7PLC,false);
  433. updateMaterailData(); // 更新物料
  434. shiftUserCheck();
  435. refreshProductTypeLock();
  436. }
  437. public static int userLoginHours;//用户登录所处小时
  438. public static JTextField textField;
  439. public static JTextField textField_1;
  440. public static JTextField txtfa;
  441. public static JLabel scan_type_text;
  442. public static JLabel page_text;
  443. //换班用户信息检查
  444. private static void shiftUserCheck() {
  445. LocalDateTime now = LocalDateTime.now();
  446. // 判断时间范围
  447. if (userLoginHours >= 8 && userLoginHours < 20) {
  448. int hour = now.getHour();
  449. if (hour >= 20 || hour < 8) {
  450. logoff();
  451. }
  452. } else {
  453. int hour = now.getHour();
  454. if (hour >= 8 && hour < 20) {
  455. logoff();
  456. }
  457. }
  458. }
  459. public static void resetScanB() {
  460. work_status2 = 0;
  461. check_quality_result2 = false;
  462. MesClient.mesQualityFlagB = false;
  463. product_sn2.setText("");
  464. MesClient.pxstatus2.setText("B");
  465. MesClient.param1.setText("");
  466. MesClient.param2.setText("");
  467. MesClient.param3.setText("");
  468. MesClient.param4.setText("");
  469. MesClient.param21.setText("");
  470. MesClient.param22.setText("");
  471. MesClient.param23.setText("");
  472. MesClient.param24.setText("");
  473. MesClient.curSnb = "";
  474. MesClient.tjFlagb = 0;
  475. MesClient.checkB = 0;
  476. // MesClient.batch_status2 = 0;
  477. // MesClient.textField_1.setText("");
  478. // MesClient.batch_scan_2.setEnabled(false);
  479. MesClient.formatScanType(1);
  480. // PlcUtil.changeEnable(MesClient.s7PLC,false);
  481. updateMaterailData(); // 更新物料
  482. // shiftUserCheck();
  483. refreshProductTypeLock();
  484. }
  485. //閼惧嘲褰囬悽銊﹀煕20娴o拷
  486. public static void getUser() {
  487. user20 = user_menu.getText().toString();
  488. String space_tmp1 = "";
  489. if(user20.length()<20) {
  490. for(int i=0;i<20-user20.length();i++) {
  491. space_tmp1 = space_tmp1 + " ";
  492. }
  493. }
  494. user20 = user20 + space_tmp1;
  495. }
  496. /** 提交质量结果,并将焊接实时参数落本地库 */
  497. public static boolean submitQualityAndFlush(String face, String result) {
  498. getUser();
  499. String rawSn = "A".equals(face) ? product_sn.getText() : product_sn2.getText();
  500. if(rawSn == null || rawSn.trim().isEmpty()){
  501. return false;
  502. }
  503. String sn = getBarcode(rawSn.trim());
  504. Boolean sendret = DataUtil.sendQuality(nettyClient, sn, result, user20, face);
  505. IweldCloudUtil.flushWeldingParamsOnFinish(face);
  506. return sendret != null && sendret;
  507. }
  508. /** manualImmediateReset=true 时发送成功后立即复位;自动流程由 MesRevice 等待 MES 回包后复位 */
  509. public static void handleSubmitResult(String face, String result, boolean manualImmediateReset) {
  510. if(!tcp_connect_flag){
  511. setMenuStatus("未连接MES服务器", -1);
  512. return;
  513. }
  514. String label = "A".equals(face) ? "A面" : "B面";
  515. String rawSn = "A".equals(face) ? product_sn.getText() : product_sn2.getText();
  516. if(rawSn == null || rawSn.trim().isEmpty()){
  517. setMenuStatus(label + "无工件码,请先扫码", -1);
  518. return;
  519. }
  520. boolean sendret = submitQualityAndFlush(face, result);
  521. if(!sendret){
  522. if("A".equals(face)){
  523. pxstatus1.setText("A:结果上传MES失败");
  524. tjStatusa = 1;
  525. }else{
  526. pxstatus2.setText("B:结果上传MES失败");
  527. tjStatusb = 1;
  528. }
  529. setMenuStatus(label + "提交失败,请重试", -1);
  530. return;
  531. }
  532. if(manualImmediateReset){
  533. if("A".equals(face)){
  534. pxstatus1.setText("OK".equals(result) ? "A:提交成功" : "A:已提交NG");
  535. status_menu.setText("OK".equals(result) ? "A件提交成功" : "A件已提交NG");
  536. resetScanA();
  537. }else{
  538. pxstatus2.setText("OK".equals(result) ? "B:提交成功" : "B:已提交NG");
  539. status_menu.setText("OK".equals(result) ? "B件提交成功" : "B件已提交NG");
  540. resetScanB();
  541. }
  542. setMenuStatus(label + ("OK".equals(result) ? "提交成功" : "已提交NG"), 0);
  543. }
  544. }
  545. public static void promptManualSubmit(String face) {
  546. String label = "A".equals(face) ? "A面" : "B面";
  547. String rawSn = "A".equals(face) ? product_sn.getText() : product_sn2.getText();
  548. if(rawSn == null || rawSn.trim().isEmpty()){
  549. setMenuStatus(label + "无工件码,请先扫码", -1);
  550. return;
  551. }
  552. int choice = JOptionPane.showOptionDialog(
  553. mesClientFrame,
  554. label + " 工件: " + rawSn.trim() + "\n请选择提交结果",
  555. "手动提交-" + label,
  556. JOptionPane.DEFAULT_OPTION,
  557. JOptionPane.QUESTION_MESSAGE,
  558. null,
  559. new Object[]{"OK", "NG", "取消"},
  560. "OK"
  561. );
  562. if(choice == 0){
  563. handleSubmitResult(face, "OK", true);
  564. }else if(choice == 1){
  565. handleSubmitResult(face, "NG", true);
  566. }
  567. }
  568. public static String getBarcode(String barcodeTmp) {
  569. String barcodeRet = barcodeTmp;
  570. if(barcodeTmp.equalsIgnoreCase("")) {
  571. return "";
  572. }else {
  573. if(barcodeTmp.length()<36) {
  574. String space = "";
  575. for(int i=0;i<36-barcodeTmp.length();i++) {
  576. space = space + " ";
  577. }
  578. barcodeRet = barcodeTmp + space;
  579. }
  580. }
  581. return barcodeRet;
  582. }
  583. public static void logoff() {
  584. welcomeWin.setVisible(true);
  585. mesClientFrame.setVisible(false);
  586. nettyClient = null;
  587. if(heartBeatTimer!=null) {
  588. heartBeatTimer.cancel();
  589. }
  590. if(heartBeatIconTimer!=null) {
  591. heartBeatIconTimer.cancel();
  592. }
  593. if(iweldLoginTimer!=null) {
  594. iweldLoginTimer.cancel();
  595. }
  596. tcp_connect_flag = false;
  597. connect_request_flag = false;
  598. }
  599. public MesClient() {
  600. setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png")));
  601. setTitle("MES系统客户端:"+mes_gw + "- " + mes_gw_des);
  602. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  603. setBounds(0, 0, 1024, 768);
  604. JMenuBar menuBar = new JMenuBar();
  605. menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26));
  606. setJMenuBar(menuBar);
  607. JMenu fileMenu = new JMenu("\u7528\u6237");
  608. fileMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png")));
  609. fileMenu.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  610. menuBar.add(fileMenu);
  611. JMenuItem exitMenuItem = new JMenuItem("退出");
  612. exitMenuItem.setIcon(new ImageIcon(MesClient.class.getResource("/bg/logoff.png")));
  613. exitMenuItem.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
  614. fileMenu.add(exitMenuItem);
  615. exitMenuItem.addMouseListener(new MouseAdapter() {
  616. @Override
  617. public void mousePressed(MouseEvent e) {//閸欘亣鍏樺Λ锟藉ù瀣煂mousePressed娴滃娆�
  618. super.mouseClicked(e);
  619. //dispose();
  620. logoff();
  621. }
  622. });
  623. JMenu settingMenu = new JMenu("设置");
  624. //settingMenu.setVisible(false);
  625. settingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
  626. settingMenu.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  627. menuBar.add(settingMenu);
  628. JMenuItem resetTcpMenu = new JMenuItem("\u91CD\u8FDEMES");
  629. resetTcpMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  630. resetTcpMenu.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  631. resetTcpMenu.addMouseListener(new MouseAdapter() {
  632. @Override
  633. public void mousePressed(MouseEvent e) {
  634. super.mouseClicked(e);
  635. //闁插秷绻沵es
  636. if(nettyClient != null && nettyClient.isChannelActive()){
  637. nettyClient.future.channel().close();
  638. }
  639. }
  640. });
  641. settingMenu.add(resetTcpMenu);
  642. JMenuItem resetTcpMenu_1 = new JMenuItem("\u91CD\u65B0\u626B\u7801-A");
  643. resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  644. resetTcpMenu_1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  645. resetTcpMenu_1.addMouseListener(new MouseAdapter() {
  646. @Override
  647. public void mousePressed(MouseEvent e) {
  648. super.mouseClicked(e);
  649. resetScanA();
  650. }
  651. });
  652. settingMenu.add(resetTcpMenu_1);
  653. JMenuItem resetTcpMenu_1_1 = new JMenuItem("\u91CD\u65B0\u626B\u7801-B");
  654. resetTcpMenu_1_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  655. resetTcpMenu_1_1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  656. resetTcpMenu_1_1.addMouseListener(new MouseAdapter() {
  657. @Override
  658. public void mousePressed(MouseEvent e) {
  659. super.mouseClicked(e);
  660. resetScanB();
  661. }
  662. });
  663. settingMenu.add(resetTcpMenu_1_1);
  664. JMenuItem resetTcpMenu_1sd = new JMenuItem("手动提交-A");
  665. resetTcpMenu_1sd.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  666. resetTcpMenu_1sd.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  667. resetTcpMenu_1sd.addMouseListener(new MouseAdapter() {
  668. @Override
  669. public void mousePressed(MouseEvent e) {
  670. super.mouseClicked(e);
  671. promptManualSubmit("A");
  672. }
  673. });
  674. settingMenu.add(resetTcpMenu_1sd);
  675. JMenuItem resetTcpMenu_1_1sd = new JMenuItem("手动提交-B");
  676. resetTcpMenu_1_1sd.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  677. resetTcpMenu_1_1sd.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  678. resetTcpMenu_1_1sd.addMouseListener(new MouseAdapter() {
  679. @Override
  680. public void mousePressed(MouseEvent e) {
  681. super.mouseClicked(e);
  682. promptManualSubmit("B");
  683. }
  684. });
  685. settingMenu.add(resetTcpMenu_1_1sd);
  686. contentPane = new JPanel();
  687. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  688. setContentPane(contentPane);
  689. contentPane.setLayout(new BorderLayout(0, 0));
  690. JToolBar toolBar = new JToolBar();
  691. contentPane.add(toolBar, BorderLayout.NORTH);
  692. JLabel equipment_statu_label = new JLabel("\u72B6\u6001\uFF1A");
  693. equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER);
  694. equipment_statu_label.setForeground(Color.BLACK);
  695. equipment_statu_label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  696. equipment_statu_label.setBackground(Color.LIGHT_GRAY);
  697. toolBar.add(equipment_statu_label);
  698. status_menu = new JButton("设备未连接MES服务器");
  699. if(tcp_connect_flag) {
  700. status_menu.setText("已连接MES服务器");
  701. status_menu.setForeground(Color.GREEN);
  702. }else {
  703. status_menu.setText("未连接MES服务器");
  704. status_menu.setForeground(Color.DARK_GRAY);
  705. }
  706. status_menu.addActionListener(new ActionListener() {
  707. public void actionPerformed(ActionEvent e) {
  708. }
  709. });
  710. status_menu.setForeground(Color.GREEN);
  711. status_menu.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  712. status_menu.setBackground(Color.BLACK);
  713. toolBar.add(status_menu);
  714. JLabel space_1 = new JLabel(" ");
  715. toolBar.add(space_1);
  716. JLabel heart_beat_status_label = new JLabel("\u5FC3\u8DF3\uFF1A");
  717. heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  718. heart_beat_status_label.setForeground(Color.BLACK);
  719. heart_beat_status_label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  720. heart_beat_status_label.setBackground(Color.LIGHT_GRAY);
  721. toolBar.add(heart_beat_status_label);
  722. heart_beat_menu = new JButton("2024-02-20 23:20:10");
  723. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  724. heart_beat_menu.setForeground(Color.GREEN);
  725. heart_beat_menu.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  726. heart_beat_menu.setBackground(Color.BLACK);
  727. toolBar.add(heart_beat_menu);
  728. JLabel space_2 = new JLabel(" ");
  729. toolBar.add(space_2);
  730. JLabel user_status_label = new JLabel("\u767B\u5F55\u7528\u6237\uFF1A");
  731. user_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  732. user_status_label.setForeground(Color.BLACK);
  733. user_status_label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  734. user_status_label.setBackground(Color.LIGHT_GRAY);
  735. toolBar.add(user_status_label);
  736. user_menu = new JButton("JinJuShi");
  737. user_menu.setForeground(Color.GREEN);
  738. user_menu.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
  739. user_menu.setBackground(Color.BLACK);
  740. toolBar.add(user_menu);
  741. JLabel space_3 = new JLabel(" ");
  742. toolBar.add(space_3);
  743. JLabel space_4 = new JLabel(" ");
  744. toolBar.add(space_4);
  745. tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  746. tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
  747. tabbedPane.setMinimumSize(new Dimension(400, 50));
  748. tabbedPane.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 22));
  749. contentPane.add(tabbedPane);
  750. //妫f牠銆�
  751. JPanel indexPanelA = new JPanel();
  752. indexPanelA.setLayout(null);
  753. indexScrollPaneA = new JScrollPane(indexPanelA);
  754. indexScrollPaneA.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  755. indexScrollPaneA.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  756. product_sn = new JTextField();
  757. product_sn.setText("");
  758. product_sn.setHorizontalAlignment(SwingConstants.CENTER);
  759. product_sn.setEditable(false);
  760. product_sn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
  761. product_sn.setBounds(26, 152, 446, 70);
  762. indexPanelA.add(product_sn);
  763. product_sn.setColumns(10);
  764. // String[] hjtitles = new String[]{"閻掑﹥婧�1","閻掑﹥婧�2","閻掑﹥婧�3"};
  765. // String[] hjvals = new String[]{"HJ001","HJ002","HJ003"};
  766. // mesRadioHj = new MesRadio(hjtitles,hjvals);
  767. // mesRadioHj.setSize(500,50);
  768. // mesRadioHj.setBounds(190,170,500,50);
  769. // indexPanelA.add(mesRadioHj);
  770. product_sn2 = new JTextField();
  771. product_sn2.setText("");
  772. product_sn2.setHorizontalAlignment(SwingConstants.CENTER);
  773. product_sn2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
  774. product_sn2.setEditable(false);
  775. product_sn2.setColumns(10);
  776. product_sn2.setBounds(517, 152, 446, 70);
  777. indexPanelA.add(product_sn2);
  778. JSeparator separator = new JSeparator();
  779. separator.setBounds(450, 247, 0, 120);
  780. indexPanelA.add(separator);
  781. JSeparator separator_1 = new JSeparator();
  782. separator_1.setOrientation(SwingConstants.VERTICAL);
  783. separator_1.setBounds(495, 91, 23, 250);
  784. indexPanelA.add(separator_1);
  785. pxstatus1 = new JLabel("A");
  786. pxstatus1.setForeground(new Color(255, 128, 64));
  787. pxstatus1.setHorizontalAlignment(SwingConstants.CENTER);
  788. pxstatus1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  789. pxstatus1.setBounds(26, 98, 446, 44);
  790. indexPanelA.add(pxstatus1);
  791. pxstatus2 = new JLabel("B");
  792. pxstatus2.setForeground(new Color(255, 128, 64));
  793. pxstatus2.setHorizontalAlignment(SwingConstants.CENTER);
  794. pxstatus2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  795. pxstatus2.setBounds(517, 98, 446, 44);
  796. indexPanelA.add(pxstatus2);
  797. JSeparator separator_2 = new JSeparator();
  798. separator_2.setBounds(10, 264, 973, 13);
  799. indexPanelA.add(separator_2);
  800. JLabel lblNewLabel_4 = new JLabel("焊机1");
  801. lblNewLabel_4.setHorizontalAlignment(SwingConstants.CENTER);
  802. lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 24));
  803. lblNewLabel_4.setBounds(170, 290, 156, 40);
  804. lblNewLabel_4.setVisible(true);
  805. indexPanelA.add(lblNewLabel_4);
  806. JLabel lblNewLabel_4_1 = new JLabel("焊机2");
  807. lblNewLabel_4_1.setHorizontalAlignment(SwingConstants.CENTER);
  808. lblNewLabel_4_1.setFont(new Font("微软雅黑", Font.PLAIN, 24));
  809. lblNewLabel_4_1.setBounds(670, 287, 156, 40);
  810. lblNewLabel_4_1.setVisible(true);
  811. indexPanelA.add(lblNewLabel_4_1);
  812. JSeparator separator_1_1 = new JSeparator();
  813. separator_1_1.setOrientation(SwingConstants.VERTICAL);
  814. separator_1_1.setBounds(495, 278, 23, 232);
  815. indexPanelA.add(separator_1_1);
  816. textField = new JTextField();
  817. textField.setText("");
  818. textField.setHorizontalAlignment(SwingConstants.CENTER);
  819. textField.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
  820. textField.setEditable(false);
  821. textField.setColumns(10);
  822. textField.setBounds(26, 333, 446, 70);
  823. // indexPanelA.add(textField);
  824. batch_scan_1 = new JButton("解绑");
  825. batch_scan_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/delete.png")));
  826. batch_scan_1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 32));
  827. batch_scan_1.setEnabled(false);
  828. batch_scan_1.setVisible(false);
  829. batch_scan_1.setBounds(136, 430, 230, 80);
  830. batch_scan_1.addActionListener(new ActionListener() {
  831. public void actionPerformed(ActionEvent e) {
  832. if(MesClient.curPage.equals("B")){
  833. MesClient.setMenuStatus("该状态后梁无法解绑",-1);
  834. }else{
  835. String sn = MesClient.product_sn.getText().trim();
  836. String wsn = MesClient.textField.getText().trim();
  837. JSONObject retObj = DataUtil.unBindWarehouseData(sn,wsn,user_menu.getText().trim(),"400007");
  838. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  839. MesClient.setMenuStatus("后梁码解绑成功",0);
  840. MesClient.formatScanType(2);
  841. textField.setText("");
  842. batch_scan_1.setEnabled(false);
  843. PlcUtil.changeEnableA(s7PLC,false);
  844. }else{
  845. MesClient.formatScanType(2);
  846. batch_scan_1.setEnabled(true);
  847. if(retObj.get("result")==null){
  848. MesClient.setMenuStatus("请求失败,后梁码解绑失败,请重试",-1);
  849. }else{
  850. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  851. MesClient.setMenuStatus(retObj.getString("message"),-1);
  852. }
  853. }
  854. }
  855. }
  856. }
  857. });
  858. indexPanelA.add(batch_scan_1);
  859. textField_1 = new JTextField();
  860. textField_1.setText("");
  861. textField_1.setHorizontalAlignment(SwingConstants.CENTER);
  862. textField_1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
  863. textField_1.setEditable(false);
  864. textField_1.setColumns(10);
  865. textField_1.setBounds(517, 333, 446, 70);
  866. // indexPanelA.add(textField_1);
  867. batch_scan_2 = new JButton("解绑");
  868. batch_scan_2.setIcon(new ImageIcon(MesClient.class.getResource("/bg/delete.png")));
  869. batch_scan_2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 32));
  870. batch_scan_2.setEnabled(false);
  871. batch_scan_2.setVisible(false);
  872. batch_scan_2.setBounds(633, 430, 230, 80);
  873. batch_scan_2.addActionListener(new ActionListener() {
  874. public void actionPerformed(ActionEvent e) {
  875. if(MesClient.curPage.equals("A")){
  876. MesClient.setMenuStatus("该状态后梁无法解绑",-1);
  877. }else{
  878. String sn = MesClient.product_sn2.getText().trim();
  879. String wsn = MesClient.textField_1.getText().trim();
  880. JSONObject retObj = DataUtil.unBindWarehouseData(sn,wsn,user_menu.getText().trim(),"400007");
  881. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  882. MesClient.setMenuStatus("后梁码解绑成功",0);
  883. MesClient.formatScanType(2);
  884. textField_1.setText("");
  885. batch_scan_2.setEnabled(false);
  886. PlcUtil.changeEnableB(s7PLC,false);
  887. }else{
  888. if(retObj.get("result")==null){
  889. MesClient.setMenuStatus("请求失败,前梁码解绑失败,请重试",-1);
  890. }else{
  891. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  892. MesClient.setMenuStatus(retObj.getString("message"),-1);
  893. }
  894. }
  895. }
  896. }
  897. }
  898. });
  899. indexPanelA.add(batch_scan_2);
  900. JLabel pxstatus1_1 = new JLabel("A后梁");
  901. pxstatus1_1.setHorizontalAlignment(SwingConstants.CENTER);
  902. pxstatus1_1.setForeground(new Color(0, 0, 0));
  903. pxstatus1_1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  904. pxstatus1_1.setBounds(26, 283, 446, 44);
  905. // indexPanelA.add(pxstatus1_1);
  906. JLabel pxstatus1_1_1 = new JLabel("B后梁");
  907. pxstatus1_1_1.setHorizontalAlignment(SwingConstants.CENTER);
  908. pxstatus1_1_1.setForeground(Color.BLACK);
  909. pxstatus1_1_1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
  910. pxstatus1_1_1.setBounds(517, 283, 446, 44);
  911. // indexPanelA.add(pxstatus1_1_1);
  912. txtfa = new JTextField();
  913. txtfa.setHorizontalAlignment(SwingConstants.LEFT);
  914. txtfa.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
  915. txtfa.setColumns(10);
  916. txtfa.setBounds(239, 10, 503, 60);
  917. indexPanelA.add(txtfa);
  918. txtfa.addActionListener(new ActionListener() {
  919. public void actionPerformed(ActionEvent e) {
  920. scanBarcode();
  921. }
  922. });
  923. page_text = new JLabel("当前:A面");
  924. page_text.setForeground(new Color(0, 255, 64));
  925. page_text.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  926. page_text.setHorizontalAlignment(SwingConstants.RIGHT);
  927. page_text.setBounds(97, 10, 132, 60);
  928. indexPanelA.add(page_text);
  929. scan_type_text = new JLabel("镭雕码");
  930. scan_type_text.setForeground(new Color(255, 128, 64));
  931. scan_type_text.setHorizontalAlignment(SwingConstants.LEFT);
  932. scan_type_text.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  933. scan_type_text.setBounds(752, 10, 132, 60);
  934. indexPanelA.add(scan_type_text);
  935. // 焊机参数:第一行 电压/电流/送丝速度,第二行 程序名(整行)
  936. int welder1X = 26;
  937. int welder1W = 446;
  938. int welder2X = 517;
  939. int welder2W = 446;
  940. int metricColW = 140;
  941. int metricLabelY = 328;
  942. int metricFieldY = 348;
  943. int metricFieldH = 28;
  944. int progLabelY = 386;
  945. int progFieldY = 406;
  946. int progFieldH = 28;
  947. JLabel lblDyA = new JLabel("电压");
  948. lblDyA.setHorizontalAlignment(SwingConstants.CENTER);
  949. lblDyA.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  950. lblDyA.setBounds(welder1X, metricLabelY, metricColW, 20);
  951. indexPanelA.add(lblDyA);
  952. param1 = new JTextField();
  953. param1.setEnabled(false);
  954. param1.setEditable(false);
  955. param1.setHorizontalAlignment(SwingConstants.CENTER);
  956. param1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  957. param1.setBounds(welder1X, metricFieldY, metricColW, metricFieldH);
  958. indexPanelA.add(param1);
  959. JLabel lblDlA = new JLabel("电流");
  960. lblDlA.setHorizontalAlignment(SwingConstants.CENTER);
  961. lblDlA.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  962. lblDlA.setBounds(welder1X + metricColW + 8, metricLabelY, metricColW, 20);
  963. indexPanelA.add(lblDlA);
  964. param2 = new JTextField();
  965. param2.setEnabled(false);
  966. param2.setEditable(false);
  967. param2.setHorizontalAlignment(SwingConstants.CENTER);
  968. param2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  969. param2.setBounds(welder1X + metricColW + 8, metricFieldY, metricColW, metricFieldH);
  970. indexPanelA.add(param2);
  971. JLabel lblSsA = new JLabel("送丝速度");
  972. lblSsA.setHorizontalAlignment(SwingConstants.CENTER);
  973. lblSsA.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  974. lblSsA.setBounds(welder1X + (metricColW + 8) * 2, metricLabelY, metricColW, 20);
  975. indexPanelA.add(lblSsA);
  976. param3 = new JTextField();
  977. param3.setEnabled(false);
  978. param3.setEditable(false);
  979. param3.setHorizontalAlignment(SwingConstants.CENTER);
  980. param3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  981. param3.setBounds(welder1X + (metricColW + 8) * 2, metricFieldY, metricColW, metricFieldH);
  982. indexPanelA.add(param3);
  983. JLabel lblProgA = new JLabel("程序名");
  984. lblProgA.setHorizontalAlignment(SwingConstants.CENTER);
  985. lblProgA.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  986. lblProgA.setBounds(welder1X, progLabelY, welder1W, 20);
  987. indexPanelA.add(lblProgA);
  988. param4 = new JTextField();
  989. param4.setEnabled(false);
  990. param4.setEditable(false);
  991. param4.setHorizontalAlignment(SwingConstants.CENTER);
  992. param4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  993. param4.setBounds(welder1X, progFieldY, welder1W, progFieldH);
  994. indexPanelA.add(param4);
  995. JLabel lblDyB = new JLabel("电压");
  996. lblDyB.setHorizontalAlignment(SwingConstants.CENTER);
  997. lblDyB.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  998. lblDyB.setBounds(welder2X, metricLabelY, metricColW, 20);
  999. indexPanelA.add(lblDyB);
  1000. param21 = new JTextField();
  1001. param21.setEnabled(false);
  1002. param21.setEditable(false);
  1003. param21.setHorizontalAlignment(SwingConstants.CENTER);
  1004. param21.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  1005. param21.setBounds(welder2X, metricFieldY, metricColW, metricFieldH);
  1006. indexPanelA.add(param21);
  1007. JLabel lblDlB = new JLabel("电流");
  1008. lblDlB.setHorizontalAlignment(SwingConstants.CENTER);
  1009. lblDlB.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  1010. lblDlB.setBounds(welder2X + metricColW + 8, metricLabelY, metricColW, 20);
  1011. indexPanelA.add(lblDlB);
  1012. param22 = new JTextField();
  1013. param22.setEnabled(false);
  1014. param22.setEditable(false);
  1015. param22.setHorizontalAlignment(SwingConstants.CENTER);
  1016. param22.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  1017. param22.setBounds(welder2X + metricColW + 8, metricFieldY, metricColW, metricFieldH);
  1018. indexPanelA.add(param22);
  1019. JLabel lblSsB = new JLabel("送丝速度");
  1020. lblSsB.setHorizontalAlignment(SwingConstants.CENTER);
  1021. lblSsB.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  1022. lblSsB.setBounds(welder2X + (metricColW + 8) * 2, metricLabelY, metricColW, 20);
  1023. indexPanelA.add(lblSsB);
  1024. param23 = new JTextField();
  1025. param23.setEnabled(false);
  1026. param23.setEditable(false);
  1027. param23.setHorizontalAlignment(SwingConstants.CENTER);
  1028. param23.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  1029. param23.setBounds(welder2X + (metricColW + 8) * 2, metricFieldY, metricColW, metricFieldH);
  1030. indexPanelA.add(param23);
  1031. JLabel lblProgB = new JLabel("程序名");
  1032. lblProgB.setHorizontalAlignment(SwingConstants.CENTER);
  1033. lblProgB.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  1034. lblProgB.setBounds(welder2X, progLabelY, welder2W, 20);
  1035. indexPanelA.add(lblProgB);
  1036. param24 = new JTextField();
  1037. param24.setEnabled(false);
  1038. param24.setEditable(false);
  1039. param24.setHorizontalAlignment(SwingConstants.CENTER);
  1040. param24.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
  1041. param24.setBounds(welder2X, progFieldY, welder2W, progFieldH);
  1042. indexPanelA.add(param24);
  1043. indexPanelA.setPreferredSize(new Dimension(993, 450));
  1044. indexPanelA.revalidate();
  1045. tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null);
  1046. tabbedPane.setEnabledAt(0, true);
  1047. if (PRODUCT_TYPE_SWITCH_ENABLED) {
  1048. JPanel indexPanelProductType = new JPanel();
  1049. indexPanelProductType.setLayout(null);
  1050. JScrollPane productTypeScrollPane = new JScrollPane(indexPanelProductType);
  1051. JLabel productTypeTitle = new JLabel("生产类型设置");
  1052. productTypeTitle.setFont(new Font("微软雅黑", Font.BOLD, 28));
  1053. productTypeTitle.setForeground(new Color(64, 64, 64));
  1054. productTypeTitle.setHorizontalAlignment(SwingConstants.CENTER);
  1055. productTypeTitle.setBounds(81, 80, 810, 50);
  1056. indexPanelProductType.add(productTypeTitle);
  1057. JLabel productTypeHint = new JLabel("请选择当前生产的工件类型,切换后将保存到本地并用于扫码校验");
  1058. productTypeHint.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  1059. productTypeHint.setForeground(new Color(140, 140, 140));
  1060. productTypeHint.setHorizontalAlignment(SwingConstants.CENTER);
  1061. productTypeHint.setBounds(81, 140, 810, 40);
  1062. indexPanelProductType.add(productTypeHint);
  1063. productTypePanel = new ProductTypePanel(PRODUCT_TYPE_SWITCH_PASSWORD);
  1064. productTypePanel.setBounds(276, 210, 420, 72);
  1065. productTypePanel.setOnChangeListener(() -> {
  1066. if (work_status == 0 && work_status2 == 0) {
  1067. saveProductTypeToDb();
  1068. if (PRODUCT_TYPE_SWITCH_ENABLED) {
  1069. setMenuStatus("当前生产类型:" + getProductTypeName() + ",请扫工件码",0);
  1070. } else {
  1071. setMenuStatus("请扫工件码",0);
  1072. }
  1073. }
  1074. refreshProductTypeCurrentLabel();
  1075. });
  1076. indexPanelProductType.add(productTypePanel);
  1077. productTypeCurrentLabel = new JLabel();
  1078. productTypeCurrentLabel.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  1079. productTypeCurrentLabel.setForeground(new Color(64, 64, 64));
  1080. productTypeCurrentLabel.setHorizontalAlignment(SwingConstants.CENTER);
  1081. productTypeCurrentLabel.setBounds(81, 310, 810, 40);
  1082. refreshProductTypeCurrentLabel();
  1083. indexPanelProductType.add(productTypeCurrentLabel);
  1084. tabbedPane.addTab("生产类型", new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")), productTypeScrollPane, null);
  1085. }
  1086. JPanel indexPanelGz = new JPanel();
  1087. searchScrollPaneGz = new JScrollPane(indexPanelGz);
  1088. indexPanelGz.setLayout(null);
  1089. indexPanelC = new JPanel(new BorderLayout());
  1090. // initStartShiftCheckPanel(indexPanelC);
  1091. tabbedPane.addTab("A面点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexPanelC, null);
  1092. indexPanelDjB = new JPanel(new BorderLayout());
  1093. tabbedPane.addTab("B面点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexPanelDjB, null);
  1094. indexPanelB = new JPanel();
  1095. searchScrollPane = new JScrollPane(indexPanelB);
  1096. indexPanelB.setLayout(null);
  1097. tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
  1098. // bindBatchPanel();
  1099. tabbedPane.addChangeListener(new ChangeListener() {
  1100. @Override
  1101. public void stateChanged(ChangeEvent e) {
  1102. JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
  1103. int selectedIndex = tabbedPane.getSelectedIndex();
  1104. System.out.println("selectedIndex:"+selectedIndex);
  1105. if(selectedIndex == 1){
  1106. }
  1107. }
  1108. });
  1109. }
  1110. // 开班点检页面初始化(仿“工作面板”写法:null布局+绝对坐标)
  1111. private static void initStartShiftCheckPanel(JPanel panel) {
  1112. // A面 6 灯
  1113. JLabel aLabel = new JLabel("A面状态");
  1114. aLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 22));
  1115. aLabel.setBounds(20, 50, 150, 40);
  1116. panel.add(aLabel);
  1117. int lampY1 = 110;
  1118. int lampXStart = 80;
  1119. int lampGap = 140;
  1120. for (int i = 0; i < 6; i++) {
  1121. JLabel lamp = new JLabel();
  1122. lamp.setBounds(lampXStart + i * lampGap, lampY1, 40, 40);
  1123. lamp.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  1124. panel.add(lamp);
  1125. dj_a_lamps[i] = lamp;
  1126. JLabel idx = new JLabel(String.valueOf(i + 1));
  1127. idx.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
  1128. idx.setBounds(lampXStart + i * lampGap + 12, lampY1 + 45, 40, 30);
  1129. panel.add(idx);
  1130. }
  1131. // B面 6 灯
  1132. JLabel bLabel = new JLabel("B面状态");
  1133. bLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 22));
  1134. bLabel.setBounds(20, 230, 150, 40);
  1135. panel.add(bLabel);
  1136. int lampY2 = 290;
  1137. for (int i = 0; i < 6; i++) {
  1138. JLabel lamp = new JLabel();
  1139. lamp.setBounds(lampXStart + i * lampGap, lampY2, 40, 40);
  1140. lamp.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  1141. panel.add(lamp);
  1142. dj_b_lamps[i] = lamp;
  1143. JLabel idx = new JLabel(String.valueOf(i + 1));
  1144. idx.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
  1145. idx.setBounds(lampXStart + i * lampGap + 12, lampY2 + 45, 40, 30);
  1146. panel.add(idx);
  1147. }
  1148. // 按钮
  1149. dj_start_btn = new JButton("开班点检");
  1150. dj_start_btn.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 26));
  1151. dj_start_btn.setBounds(380, 450, 260, 70);
  1152. panel.add(dj_start_btn);
  1153. dj_start_btn.addActionListener(new ActionListener() {
  1154. @Override
  1155. public void actionPerformed(ActionEvent e) {
  1156. byte[] djStatus = PlcUtil.getDjStatus(s7PLC);
  1157. if(djStatus == null || djStatus.length < 12){
  1158. JOptionPane.showMessageDialog(mesClientFrame, "点检信号读取失败", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
  1159. return;
  1160. }
  1161. updateDjLamps(djStatus);
  1162. boolean allOn = isTwelveLampsAllOn(djStatus);
  1163. if(!allOn){
  1164. JOptionPane.showMessageDialog(mesClientFrame, "点检项缺失,请检查设备", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
  1165. return;
  1166. }
  1167. JSONObject retObj = DataUtil.saveDj();
  1168. if (retObj.get("result") != null && retObj.get("result").toString().equalsIgnoreCase("true")){
  1169. JOptionPane.showMessageDialog(mesClientFrame, "点检成功", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
  1170. }else {
  1171. JOptionPane.showMessageDialog(mesClientFrame, "点检记录上传失败", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
  1172. }
  1173. }
  1174. });
  1175. }
  1176. // 点检灯状态更新:非0亮绿,0置灰
  1177. public static void updateDjLamps(byte[] djStatus){
  1178. Icon greyIcon = new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png"));
  1179. Icon greenIcon = new ImageIcon(MesClient.class.getResource("/bg/green_dot.png"));
  1180. for (int i = 0; i < 6; i++) {
  1181. if(dj_a_lamps[i] != null){
  1182. dj_a_lamps[i].setIcon(djStatus[i] != 0 ? greenIcon : greyIcon);
  1183. }
  1184. if(dj_b_lamps[i] != null){
  1185. dj_b_lamps[i].setIcon(djStatus[i + 6] != 0 ? greenIcon : greyIcon);
  1186. }
  1187. }
  1188. if(indexPanelC != null){
  1189. indexPanelC.repaint();
  1190. }
  1191. }
  1192. private static boolean isTwelveLampsAllOn(byte[] djStatus){
  1193. for (int i = 0; i < 12; i++) {
  1194. if(djStatus[i] == 0){
  1195. return false;
  1196. }
  1197. }
  1198. return true;
  1199. }
  1200. public static void formatScanType(int st){
  1201. scan_type = st;
  1202. if(scan_type == 1){
  1203. scan_type_text.setText("镭雕码");
  1204. }else if(scan_type == 2){
  1205. scan_type_text.setText("后梁码");
  1206. }else{
  1207. scan_type_text.setText("");
  1208. }
  1209. }
  1210. public static void loadProductTypeFromDb() {
  1211. if (productTypePanel == null) {
  1212. return;
  1213. }
  1214. String savedType = JdbcUtils.getClientConfig(
  1215. JdbcUtils.getProductTypeConfigKey(mes_gw),
  1216. PRODUCT_TYPE_ZJ
  1217. );
  1218. if (PRODUCT_TYPE_SJ.equals(savedType)) {
  1219. productTypePanel.setResult(PRODUCT_TYPE_SJ);
  1220. } else {
  1221. productTypePanel.setResult(PRODUCT_TYPE_ZJ);
  1222. }
  1223. refreshProductTypeCurrentLabel();
  1224. }
  1225. public static void refreshProductTypeCurrentLabel() {
  1226. if (productTypeCurrentLabel != null) {
  1227. productTypeCurrentLabel.setText("当前选择:" + getProductTypeName() + "(前缀 " + getProductSnPrefix() + ")");
  1228. }
  1229. }
  1230. public static void saveProductTypeToDb() {
  1231. if (productTypePanel == null) {
  1232. return;
  1233. }
  1234. JdbcUtils.saveClientConfig(
  1235. JdbcUtils.getProductTypeConfigKey(mes_gw),
  1236. productTypePanel.getResult()
  1237. );
  1238. }
  1239. public static String getProductSnPrefix() {
  1240. if (productTypePanel != null && PRODUCT_TYPE_SJ.equals(productTypePanel.getResult())) {
  1241. return PREFIX_SJ;
  1242. }
  1243. return PREFIX_ZJ;
  1244. }
  1245. public static String getProductTypeName() {
  1246. if (productTypePanel != null) {
  1247. return productTypePanel.getTypeName();
  1248. }
  1249. return "智界";
  1250. }
  1251. private static String getProductTypeNameBySn(String sn) {
  1252. if (sn.startsWith(PREFIX_SJ)) {
  1253. return "尚界";
  1254. }
  1255. if (sn.startsWith(PREFIX_ZJ)) {
  1256. return "智界";
  1257. }
  1258. return "未知";
  1259. }
  1260. private static String validateProductSnMessage(String sn) {
  1261. if (sn == null || sn.isEmpty()) {
  1262. return "工件码为空,请重试";
  1263. }
  1264. if (sn.length() != PRODUCT_SN_LENGTH) {
  1265. return "工件码长度须为32位,当前" + sn.length() + "位";
  1266. }
  1267. if (!PRODUCT_TYPE_SWITCH_ENABLED) {
  1268. return null;
  1269. }
  1270. String prefix = getProductSnPrefix();
  1271. if (!sn.startsWith(prefix)) {
  1272. return "工件码前缀与当前类型(" + getProductTypeName() + ")不匹配,当前工件码为" + getProductTypeNameBySn(sn);
  1273. }
  1274. return null;
  1275. }
  1276. private static void updateProductTypePanelEnabled(boolean enabled) {
  1277. if (productTypePanel != null) {
  1278. productTypePanel.setEnabled(enabled);
  1279. }
  1280. }
  1281. private static void refreshProductTypeLock() {
  1282. boolean busy = (product_sn != null && !product_sn.getText().trim().isEmpty())
  1283. || (product_sn2 != null && !product_sn2.getText().trim().isEmpty())
  1284. || Boolean.TRUE.equals(mesQualityFlagA)
  1285. || Boolean.TRUE.equals(mesQualityFlagB);
  1286. updateProductTypePanelEnabled(!busy);
  1287. }
  1288. public static void scanBarcode() {
  1289. String scanBarcodeSn = txtfa.getText().trim();
  1290. txtfa.setText("");
  1291. System.out.println("scan_type:"+scan_type);
  1292. //刷新界面
  1293. mesClientFrame.repaint();
  1294. System.out.println("456");
  1295. System.out.println("156"+scan_type);
  1296. if(MesClient.scan_type == 1){ // 镭雕码
  1297. System.out.println("type1");
  1298. if(scanBarcodeSn.isEmpty()){
  1299. System.out.println("type2");
  1300. MesClient.setMenuStatus("镭雕码不能为空",1);
  1301. return;
  1302. }
  1303. String validateMsg = validateProductSnMessage(scanBarcodeSn);
  1304. if (validateMsg != null) {
  1305. MesClient.setMenuStatus(validateMsg, 1);
  1306. return;
  1307. }
  1308. if(MesClient.curPage.equals("A")){
  1309. System.out.println("typeA");
  1310. if(!product_sn.getText().isEmpty() && mesQualityFlagA){
  1311. MesClient.setMenuStatus("A面已存在工件码,勿重复扫码",1);
  1312. return;
  1313. }
  1314. product_sn.setText(scanBarcodeSn);
  1315. }else{
  1316. System.out.println("typeB");
  1317. if(!product_sn2.getText().isEmpty() && mesQualityFlagB){
  1318. MesClient.setMenuStatus("B面已存在工件码,勿重复扫码",1);
  1319. return;
  1320. }
  1321. product_sn2.setText(scanBarcodeSn);
  1322. }
  1323. MesClient.setMenuStatus("扫码成功",0);
  1324. refreshProductTypeLock();
  1325. //刷新界面
  1326. mesClientFrame.repaint();
  1327. System.out.println("123");
  1328. getUser();
  1329. // 查询工件质量
  1330. Boolean sendret = DataUtil.checkQuality(nettyClient,scanBarcodeSn,user20,MesClient.curPage);
  1331. if(!sendret){
  1332. MesClient.setMenuStatus("消息发送失败,请重试",1);
  1333. }
  1334. }
  1335. }
  1336. public static void setMenuStatus(String msg,int error){
  1337. if(error == 0){
  1338. MesClient.status_menu.setForeground(Color.GREEN);
  1339. }else{
  1340. MesClient.status_menu.setForeground(Color.RED);
  1341. }
  1342. MesClient.status_menu.setText(msg);
  1343. }
  1344. /** A/B 面绑定工位:gwflag=A 绑 OP060A/B,gwflag=B 绑 OP060C/D */
  1345. public static String[] getMaterialOprnos(){
  1346. if("A".equals(mes_gwflag)){
  1347. return new String[]{mes_gw + "A", mes_gw + "B"};
  1348. }else if("B".equals(mes_gwflag)){
  1349. return new String[]{mes_gw + "C", mes_gw + "D"};
  1350. }
  1351. return new String[]{mes_gw};
  1352. }
  1353. /** A/B 共用库存:合并各面查询结果,剩余次数取较小值 */
  1354. private static String materialRowKey(BindMaterialResp item){
  1355. return String.valueOf(item.getMaterialId()) + "|" + String.valueOf(item.getCraft()) + "|" + String.valueOf(item.getType());
  1356. }
  1357. private static Integer parseLastTimes(String lastTimes){
  1358. if(lastTimes == null || lastTimes.trim().isEmpty()){
  1359. return null;
  1360. }
  1361. try{
  1362. return Integer.parseInt(lastTimes.trim());
  1363. }catch (NumberFormatException e){
  1364. return null;
  1365. }
  1366. }
  1367. private static String mergeSharedLastTimes(String left, String right){
  1368. Integer leftVal = parseLastTimes(left);
  1369. Integer rightVal = parseLastTimes(right);
  1370. if(leftVal == null){
  1371. return right != null ? right : "";
  1372. }
  1373. if(rightVal == null){
  1374. return left;
  1375. }
  1376. return String.valueOf(Math.min(leftVal, rightVal));
  1377. }
  1378. private static String pickSharedBatchSn(String left, String right){
  1379. if(left != null && !left.trim().isEmpty()){
  1380. return left;
  1381. }
  1382. return right != null ? right : "";
  1383. }
  1384. private static void mergeSharedMaterial(BindMaterialResp target, BindMaterialResp source){
  1385. target.setLastTimes(mergeSharedLastTimes(target.getLastTimes(), source.getLastTimes()));
  1386. target.setBatchSn(pickSharedBatchSn(target.getBatchSn(), source.getBatchSn()));
  1387. }
  1388. private static List<BindMaterialResp> loadSharedMaterialList(){
  1389. Map<String, BindMaterialResp> merged = new LinkedHashMap<>();
  1390. for(String oprno : getMaterialOprnos()){
  1391. JSONObject retObj = DataUtil.getBindMaterail(oprno);
  1392. if(retObj == null || retObj.get("result") == null || !retObj.get("result").toString().equalsIgnoreCase("true")){
  1393. continue;
  1394. }
  1395. List<BindMaterialResp> arrs = retObj.getList("data", BindMaterialResp.class);
  1396. if(arrs == null || arrs.isEmpty()){
  1397. continue;
  1398. }
  1399. for(BindMaterialResp item : arrs){
  1400. String key = materialRowKey(item);
  1401. BindMaterialResp existing = merged.get(key);
  1402. if(existing == null){
  1403. merged.put(key, item);
  1404. }else{
  1405. mergeSharedMaterial(existing, item);
  1406. }
  1407. }
  1408. }
  1409. return new ArrayList<>(merged.values());
  1410. }
  1411. private static Object[][] buildMaterialRowData(){
  1412. List<BindMaterialResp> arrs = loadSharedMaterialList();
  1413. if(arrs.isEmpty()){
  1414. return new Object[0][7];
  1415. }
  1416. Object[][] rows = new Object[arrs.size()][7];
  1417. int i = 0;
  1418. for (BindMaterialResp bindMaterialResp : arrs){
  1419. rows[i][0] = bindMaterialResp.getMaterialTitle();
  1420. rows[i][1] = bindMaterialResp.getBatchSn() != null ? bindMaterialResp.getBatchSn() : "";
  1421. rows[i][2] = bindMaterialResp.getLastTimes() != null ? bindMaterialResp.getLastTimes() : "";
  1422. rows[i][3] = "";
  1423. rows[i][4] = bindMaterialResp.getCraft();
  1424. rows[i][5] = bindMaterialResp.getMaterialId();
  1425. rows[i][6] = bindMaterialResp.getType();
  1426. i++;
  1427. }
  1428. return rows;
  1429. }
  1430. public static void getMaterailData(){
  1431. rowData = buildMaterialRowData();
  1432. bindBatchPanel();
  1433. }
  1434. public static void updateMaterailData(){
  1435. rowData = buildMaterialRowData();
  1436. if(MesClient.table != null){
  1437. MesClient.table.setModel(new javax.swing.table.DefaultTableModel(rowData, columnNames){
  1438. public boolean isCellEditable(int row, int column) {
  1439. return column == 3;
  1440. }
  1441. });
  1442. MesClient.table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton());
  1443. MesClient.table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton());
  1444. MesClient.table.revalidate();
  1445. MesClient.table.repaint();
  1446. }
  1447. }
  1448. // 绑定物料批次码(A/B 共用,扫码一次同步绑定各面工位)
  1449. public static void scanBatchSn(BindMaterialResp bindMaterialResp) {
  1450. String scanBarcodeTitle = "请扫物料:" + bindMaterialResp.getMaterialTitle();
  1451. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  1452. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  1453. String[] oprnos = getMaterialOprnos();
  1454. if(oprnos.length == 0){
  1455. MesClient.setMenuStatus("工位配置异常,请检查 config.properties", -1);
  1456. return;
  1457. }
  1458. String failOprno = null;
  1459. String failMsg = null;
  1460. for(String oprno : oprnos){
  1461. JSONObject retObj = DataUtil.saveBindMaterail(
  1462. scanBarcode,
  1463. bindMaterialResp.getCraft(),
  1464. bindMaterialResp.getMaterialId(),
  1465. bindMaterialResp.getType(),
  1466. oprno.trim());
  1467. if(retObj == null || retObj.get("result") == null){
  1468. failOprno = oprno;
  1469. failMsg = "请求失败,请重试";
  1470. break;
  1471. }
  1472. if(!retObj.get("result").toString().equalsIgnoreCase("true")){
  1473. failOprno = oprno;
  1474. failMsg = retObj.getString("message");
  1475. if(failMsg == null || failMsg.isEmpty()){
  1476. failMsg = "绑定失败";
  1477. }
  1478. break;
  1479. }
  1480. }
  1481. if(failOprno == null){
  1482. MesClient.setMenuStatus("扫物料:" + bindMaterialResp.getMaterialTitle() + "成功(A/B共用)", 0);
  1483. updateMaterailData();
  1484. }else if(oprnos.length > 1){
  1485. MesClient.setMenuStatus(failOprno + "绑定失败:" + failMsg, -1);
  1486. updateMaterailData();
  1487. }else{
  1488. MesClient.setMenuStatus(failMsg, -1);
  1489. }
  1490. }
  1491. }
  1492. public static void bindBatchPanel(){
  1493. JPanel indexPanelBB = new JPanel();
  1494. JPanel panel = new JPanel();
  1495. panel.setBounds(0, 0, 990, 550);
  1496. // panel.setBounds(81, 50, 810, 479);
  1497. indexPanelBB.add(panel);
  1498. panel.setLayout(new GridLayout(0, 1, 0, 0));
  1499. table = new JTable(rowData, columnNames){
  1500. public boolean isCellEditable(int row, int column) {
  1501. if(column == 3){
  1502. return true;
  1503. }
  1504. return false;
  1505. }
  1506. };
  1507. table.setRowHeight(40);
  1508. table.setEnabled(true);
  1509. table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  1510. table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton());
  1511. table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton());
  1512. JScrollPane scrollPane = new JScrollPane(table);
  1513. panel.add(scrollPane);
  1514. indexScrollPaneB = new JScrollPane(indexPanelBB);
  1515. indexPanelBB.setLayout(null);
  1516. tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null);
  1517. }
  1518. // 开启发送结果定时任务
  1519. public static void startUpdateQualityTimer(){
  1520. new Timer().schedule(new TimerTask() {
  1521. @Override
  1522. public void run() {
  1523. try {
  1524. JdbcUtils.getConn();
  1525. Statement statement = JdbcUtils.conn.createStatement();
  1526. String selectSql = "SELECT id, bw FROM submit_record\n" +
  1527. "WHERE state = 0 ORDER BY id DESC LIMIT 10";
  1528. String updateSql = "UPDATE submit_record \n" +
  1529. "SET state = '1'\n" +
  1530. "WHERE id = ";
  1531. ResultSet rs = statement.executeQuery(selectSql);
  1532. while(rs.next()){
  1533. int id = rs.getInt("id");
  1534. String bw = rs.getString("bw");
  1535. // 发送请求
  1536. String url = "http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesProductRecord/updateQualityByTiming";
  1537. String s = HttpUtils.sendPostRequestJson(url, bw );
  1538. Boolean result = JSONObject.parseObject(s).getBoolean("result");
  1539. if(result) {
  1540. // 更改状态为 1
  1541. statement.executeUpdate(updateSql + id);
  1542. }
  1543. }
  1544. rs.close();
  1545. statement.close();
  1546. } catch (SQLException | IOException e) {
  1547. // throw new RuntimeException(e);
  1548. e.printStackTrace();
  1549. }
  1550. }
  1551. },1000, 5000);
  1552. }
  1553. public static void formatBatchSn(){
  1554. if(MesClient.batch_status == 1 && MesClient.tjFlaga == 1){
  1555. PlcUtil.changeEnableA(MesClient.s7PLC,true);
  1556. }else{
  1557. PlcUtil.changeEnableA(MesClient.s7PLC,false);
  1558. }
  1559. if(MesClient.batch_status2 == 1 && MesClient.tjFlagb == 1){
  1560. PlcUtil.changeEnableB(MesClient.s7PLC,true);
  1561. }else{
  1562. PlcUtil.changeEnableB(MesClient.s7PLC,false);
  1563. }
  1564. }
  1565. private static String checkSnType(String sn){
  1566. String str = sn.substring(9,12);
  1567. if(str.equals("113")){
  1568. return "MX11-2";
  1569. }else if(str.equals("119")){
  1570. return "MX11-2S";
  1571. }else{
  1572. return "";
  1573. }
  1574. }
  1575. // 检查工件是否匹配
  1576. private static Boolean checkSnAndBatch(String sn,String batchSn){
  1577. String gdStr1 = "151245P000012131001000"; // MX11-2
  1578. String gdStr2 = "151245P000017812001000"; // MX11-2S
  1579. String cate = sn.substring(0,22);
  1580. String ptype = checkSnType(batchSn);
  1581. if(ptype.equals("MX11-2") && cate.equals(gdStr1)){
  1582. return true;
  1583. }
  1584. if(ptype.equals("MX11-2S") && cate.equals(gdStr2)){
  1585. return true;
  1586. }
  1587. return false;
  1588. }
  1589. }