MesClient.java 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.github.s7connector.api.S7Connector;
  5. import com.github.s7connector.api.S7Serializer;
  6. import com.github.s7connector.api.factory.S7ConnectorFactory;
  7. import com.github.s7connector.api.factory.S7SerializerFactory;
  8. import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
  9. import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
  10. import com.mes.component.MesRadio;
  11. import com.mes.component.MesWebView;
  12. import com.mes.netty.NettyClient;
  13. import com.mes.util.DateLocalUtils;
  14. import com.mes.util.HttpUtils;
  15. import com.mes.util.JdbcUtils;
  16. import javafx.embed.swing.JFXPanel;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import javax.swing.*;
  20. import javax.swing.border.EmptyBorder;
  21. import javax.swing.event.ChangeEvent;
  22. import javax.swing.event.ChangeListener;
  23. import java.awt.*;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.ActionListener;
  26. import java.awt.event.MouseAdapter;
  27. import java.awt.event.MouseEvent;
  28. import java.io.BufferedReader;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import java.io.InputStreamReader;
  32. import java.sql.ResultSet;
  33. import java.sql.SQLException;
  34. import java.sql.Statement;
  35. import java.time.LocalDateTime;
  36. import java.util.List;
  37. import java.util.Map;
  38. import java.util.ArrayList;
  39. import java.util.Properties;
  40. import java.util.Timer;
  41. import java.util.TimerTask;
  42. import java.util.concurrent.ExecutorService;
  43. import java.util.concurrent.Executors;
  44. public class MesClient extends JFrame {
  45. public static final Logger log = LoggerFactory.getLogger(MesClient.class);
  46. public static int mes_auth = 0; // 权限级别 0=无权限 1=操作工人 2=管理员
  47. public static String mes_gw = ""; // 工位号
  48. public static String mes_gw_des = "液冷板FSW"; // 工位名称
  49. public static String mes_server_ip = ""; // 服务器IP地址
  50. public static int mes_tcp_port = 3000; // TCP服务端口
  51. public static int mes_heart_beat_cycle = 10; // 心跳周期
  52. public static int mes_heart_icon_cycle = 1;
  53. public static String mes_line_sn = ""; // 产线编号
  54. //TCP连接
  55. public static NettyClient nettyClient;
  56. //TCP连接状态
  57. public static boolean tcp_connect_flag = false;
  58. //TCP连接请求
  59. public static boolean connect_request_flag = false;
  60. //session
  61. public static String sessionid = "";
  62. public static JPanel contentPane;
  63. public static MesClient mesClientFrame;
  64. public static JTabbedPane tabbedPane;
  65. public static JScrollPane indexScrollPaneA;
  66. public static JScrollPane searchScrollPane;
  67. public static JScrollPane searchScrollPaneDj;
  68. public static Boolean check_quality_result = false;
  69. public static Integer work_status = 0;
  70. public static JButton heart_beat_menu;
  71. public static JButton status_menu;
  72. public static JButton user_menu;
  73. public static int scan_type = 0;
  74. public static JButton finish_ok_bt;
  75. public static JButton finish_ng_bt;
  76. public static JTextField product_sn;
  77. public static JButton f_scan_data_bt_1;
  78. public static String user20 = "";
  79. public static JFrame welcomeWin;
  80. public static JPanel indexPanelB;
  81. public static MesWebView jfxPanel = null;
  82. public static JPanel indexPanelC;
  83. public static MesWebView jfxPanel2 = null;
  84. public static MesRadio mesRadioHj;
  85. public static JTable table;
  86. public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" };
  87. public static Object[][] rowData = null;
  88. public static JLabel fxlabel;
  89. public static JLabel fxlabel2;
  90. public static Integer fxlabel2Flag = 0;
  91. // public static String plcUrl = "192.168.88.89"; // 180AF
  92. public static String plcUrl = "192.168.88.99"; // 180BE 280AB
  93. public static S7PLC s7PLC = new S7PLC(EPlcType.SINUMERIK_828D, plcUrl);
  94. public static S7Connector s7Connector = null;
  95. public static Integer tjFlag = 0; // 0=未扫码 1=已扫码,设备未运行 2=设备运行中 3=设备运行结束,结果未提交到MES
  96. public static Integer tjStatus = 0; // 0=重置 1=提交失败
  97. public static Integer tjTime = 0; // 0=未超过限制时间 1=超过限制时间
  98. public static Integer yxTime = 0;
  99. public static String tjResult = "OK"; // 加工结果,只要不急停就算加工完成
  100. public static String tjFlagText2 = "设备运行中";
  101. public static String tjFlagText3 = "设备运行结束,提交结果中";
  102. public static String tjFlagText4 = "结果已上传MES,请扫下一件";
  103. public static String tjFlagText5 = "已扫码,等待设备运行";
  104. public static String tjFlagTextErr = "结果上传MES失败,请重试";
  105. //MES屏蔽开关
  106. public static boolean mes_flag = false; //是否屏蔽MES true=屏蔽MES false=不屏蔽MES
  107. public static JButton mesbtn;
  108. public static String programNo = "";
  109. // 设备实时参数
  110. public static double actSpindleSpeed = 0; // 主轴转速
  111. public static double feedRate = 0; // 进给速度
  112. // 数据缓存
  113. public static List<String> deviceParams = new ArrayList<>();
  114. // 显示组件
  115. public static JLabel spindleSpeedLabel;
  116. public static JLabel feedRateLabel;
  117. public static JLabel torqueLabel;
  118. public static Integer curPoint = 0;
  119. public static List<String> curPointX = new ArrayList<>();
  120. public static List<String> curPointY = new ArrayList<>();
  121. public static void main(String[] args) {
  122. if (LockUtil.getInstance().isAppActive() == true){
  123. // JOptionPane.showMessageDialog(null, "已有一个程序在运行,程序退出");
  124. return;
  125. }
  126. EventQueue.invokeLater(new Runnable() {
  127. @Override
  128. public void run() {
  129. try{
  130. //读文件配置
  131. readProperty();
  132. // S7Util.sendStartSignal(true);
  133. // S7Util.sendMesStatus(true);
  134. //
  135. // System.out.println(S7Util.getMesStatus());
  136. // System.out.println(S7Util.getDeviceStatus());
  137. // System.out.println(S7Util.getDeviceStart());
  138. //// System.out.println(S7Util.getMesStatus());
  139. // 显示界面
  140. mesClientFrame = new MesClient();
  141. mesClientFrame.setVisible(false);
  142. JdbcUtils.getConn();
  143. welcomeWin = new LoginFarme();
  144. welcomeWin.setVisible(true);
  145. getMaterailData();
  146. // startUpdateQualityTimer();
  147. // S7Util.sendStartSignal(true);
  148. //
  149. // boolean ret = S7Util.getDeviceStart();
  150. // System.out.println(ret);
  151. monitorDevice();
  152. fswCheckPoint();
  153. }catch (Exception e){
  154. e.printStackTrace();
  155. }
  156. }
  157. });
  158. }
  159. //读配置文件
  160. private static void readProperty() throws IOException{
  161. String enconding = "UTF-8";
  162. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  163. Properties pro = new Properties();
  164. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  165. pro.load(br);
  166. mes_gw = pro.getProperty("mes.gw");
  167. mes_server_ip = pro.getProperty("mes.server_ip");
  168. mes_tcp_port = Integer.parseInt(pro.getProperty("mes.tcp_port"));
  169. mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle"));
  170. mes_line_sn = pro.getProperty("mes.line_sn");
  171. mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
  172. log.info(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle);
  173. }
  174. // 初始化TCP
  175. public static void initTcpConnection() {
  176. try {
  177. if(nettyClient==null) {
  178. //初始化TCP连接
  179. nettyClient = new NettyClient();
  180. //TCP连接状态
  181. tcp_connect_flag = false;
  182. //设置TCP请求状态
  183. connect_request_flag = true;
  184. DataUtil.synrTcp(nettyClient,mes_gw);
  185. }
  186. } catch (Exception e) {
  187. // TODO Auto-generated catch block
  188. e.printStackTrace();
  189. }
  190. }
  191. //启动心跳包程序
  192. public static java.util.Timer heartBeatTimer;
  193. public static java.util.Timer heartBeatIconTimer;
  194. public static boolean iconREDFlag = true;
  195. public static void startHeartBeatTimer() {
  196. if(heartBeatTimer!=null) {
  197. heartBeatTimer.cancel();
  198. }
  199. heartBeatTimer = new java.util.Timer();
  200. heartBeatTimer.schedule(new TimerTask() {
  201. public void run() {
  202. if(nettyClient!=null&&tcp_connect_flag) {
  203. //System.out.println("发送心跳报文");
  204. DataUtil.heartBeat(nettyClient,mes_gw);
  205. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  206. }
  207. }
  208. }, 100,mes_heart_beat_cycle*1000);
  209. //心跳显示图标
  210. if(heartBeatIconTimer!=null) {
  211. heartBeatIconTimer.cancel();
  212. }
  213. heartBeatIconTimer = new Timer();
  214. heartBeatIconTimer.schedule(new TimerTask() {
  215. public void run() {
  216. if(tcp_connect_flag) {
  217. if(iconREDFlag) {
  218. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  219. iconREDFlag = false;
  220. }else {
  221. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  222. iconREDFlag = true;
  223. }
  224. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  225. heart_beat_menu.repaint();
  226. }else {
  227. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  228. heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
  229. heart_beat_menu.repaint();
  230. //若未连接则尝试连接
  231. if(nettyClient!=null&&!tcp_connect_flag){
  232. System.out.println("TCP已断开");
  233. //TCP重新同步连接
  234. if(!connect_request_flag) {
  235. //System.out.println("TCP重新同步连接");
  236. //设置TCP请求状态,只重新同步连接一次
  237. connect_request_flag = true;
  238. DataUtil.synrTcp(nettyClient,mes_gw);
  239. }
  240. }
  241. }
  242. }
  243. }, 100,mes_heart_icon_cycle*1000);
  244. }
  245. // 设备参数读取定时器
  246. public static java.util.Timer deviceParamTimer;
  247. public static void startDeviceParamTimer() {
  248. if(deviceParamTimer != null) {
  249. deviceParamTimer.cancel();
  250. }
  251. deviceParamTimer = new java.util.Timer();
  252. deviceParamTimer.schedule(new TimerTask() {
  253. public void run() {
  254. try {
  255. if (work_status == 1){
  256. // 读取转速和进给
  257. actSpindleSpeed = S7Util.readActSpindleSpeed(s7PLC);
  258. feedRate = S7Util.readFeedRate(s7PLC);
  259. final double torque = S7Util.readTorque(s7PLC);
  260. // 更新UI
  261. final double spindle = actSpindleSpeed;
  262. final double feed = feedRate;
  263. SwingUtilities.invokeLater(() -> {
  264. spindleSpeedLabel.setText(String.format("转速: %.1f rpm", spindle));
  265. feedRateLabel.setText(String.format("进给: %.1f mm/min", feed));
  266. torqueLabel.setText(String.format("扭矩: %.1f ", torque));
  267. });
  268. // 存储数据
  269. final String currentSn = product_sn.getText();
  270. if(currentSn != null && !currentSn.isEmpty()) {
  271. String record_time = DateLocalUtils.getCurrentTime();
  272. deviceParams.add(actSpindleSpeed + "|" + feedRate + "|" + record_time);
  273. if(deviceParams.size() == 60) {
  274. try {
  275. JdbcUtils.insertDeviceParamData(mes_gw, mes_line_sn, currentSn, JSON.toJSONString(deviceParams));
  276. log.info("设备参数数据已存储: " + currentSn);
  277. deviceParams = new ArrayList<>();
  278. }catch (Exception e) {
  279. log.info("存储设备参数异常: " + e.getMessage());
  280. }
  281. }
  282. }
  283. }
  284. } catch (Exception e) {
  285. log.info("读取设备参数异常: " + e.getMessage());
  286. }
  287. }
  288. }, 100, 500); // 首次延迟100ms,之后每500ms执行
  289. }
  290. // 设备参数上传定时器
  291. public static java.util.Timer upDeviceParamsTimer;
  292. public static void startUpDeviceParamsTimer() {
  293. if(upDeviceParamsTimer != null) {
  294. upDeviceParamsTimer.cancel();
  295. }
  296. upDeviceParamsTimer = new java.util.Timer();
  297. upDeviceParamsTimer.schedule(new TimerTask() {
  298. public void run() {
  299. try{
  300. List<Map<String, Object>> prods = JdbcUtils.getDeviceParams();
  301. System.out.println("prods:"+ JSON.toJSONString(prods));
  302. if(prods.size() > 0){
  303. JSONObject retObj = DataUtil.upDeviceParams(JSON.toJSONString(prods));
  304. if(retObj != null && retObj.get("result") != null && retObj.get("result").toString().equalsIgnoreCase("true")) {
  305. for(Map<String, Object> prodReq : prods) {
  306. JdbcUtils.updateDeviceParamSync(Integer.valueOf(prodReq.get("id").toString()), 1);
  307. }
  308. log.info("设备参数数据上传成功,数量: " + prods.size());
  309. }
  310. }
  311. }catch (Exception e){
  312. log.info("上传设备参数异常: " + e.getMessage());
  313. }
  314. }
  315. }, 1000, 30*1000); // 首次延迟1秒,之后每30秒执行
  316. }
  317. public static java.util.Timer fswCheckPointTimer;
  318. public static void fswCheckPoint() {
  319. if(fswCheckPointTimer != null) {
  320. fswCheckPointTimer.cancel();
  321. }
  322. fswCheckPointTimer = new java.util.Timer();
  323. fswCheckPointTimer.schedule(new TimerTask() {
  324. public void run() {
  325. try{
  326. if(work_status == 1 && fxlabel2Flag == 1){ // 获取点位
  327. boolean ret = S7Util.getParamFlag();
  328. if(ret){
  329. curPoint += 1;
  330. curPointX.add(String.valueOf(S7Util.readXPosition()));
  331. curPointY.add(String.valueOf(S7Util.readYPosition()));
  332. }
  333. if(curPoint == 4){
  334. String pointx1 = "";
  335. String pointx2 = "";
  336. String pointx3 = "";
  337. String pointx4 = "";
  338. String pointy1 = "";
  339. String pointy2 = "";
  340. String pointy3 = "";
  341. String pointy4 = "";
  342. int i = 0;
  343. for(String str : curPointX){
  344. if(i == 0){
  345. pointx1 = str;
  346. }else if(i == 1){
  347. pointx2 = str;
  348. }else if(i == 2){
  349. pointx3 = str;
  350. }else if(i == 3){
  351. pointx4 = str;
  352. }
  353. i++;
  354. }
  355. int j = 0;
  356. for(String str : curPointX){
  357. if(i == 0){
  358. pointy1 = str;
  359. }else if(i == 1){
  360. pointy2 = str;
  361. }else if(i == 2){
  362. pointy3 = str;
  363. }else if(i == 3){
  364. pointy4 = str;
  365. }
  366. j++;
  367. }
  368. JSONObject retObj = DataUtil.saveFswPoint(product_sn.getText(),pointx1+"_"+pointy1,pointx2+"_"+pointy2,pointx3+"_"+pointy3,pointx4+"_"+pointy4);
  369. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  370. MesClient.curPointX.clear();
  371. MesClient.curPointY.clear();
  372. MesClient.curPoint = 0;
  373. }
  374. }
  375. }
  376. }catch (Exception e){
  377. log.info("异常: " + e.getMessage());
  378. }
  379. }
  380. }, 1000, 500); // 首次延迟1秒,之后每30秒执行
  381. }
  382. //设置tcp连接状态显示
  383. public static void setTcpStatus() {
  384. if(tcp_connect_flag) {
  385. status_menu.setText("已连接MES服务器");
  386. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  387. heart_beat_menu.repaint();
  388. }else {
  389. status_menu.setText("未连接MES服务器");
  390. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
  391. heart_beat_menu.repaint();
  392. }
  393. }
  394. public static void initWarehouseData(){
  395. resetScanA();
  396. }
  397. public static void resetScanA() {
  398. work_status = 0;
  399. check_quality_result = false;
  400. MesClient.finish_ok_bt.setEnabled(false);
  401. MesClient.finish_ng_bt.setEnabled(false);
  402. product_sn.setText("");
  403. MesClient.fxlabel.setText("");
  404. MesClient.fxlabel.setVisible(false);
  405. MesClient.programNo = "";
  406. MesClient.tjFlag = 0;
  407. MesClient.tjTime = 0;
  408. MesClient.yxTime = 0;
  409. MesClient.f_scan_data_bt_1.setEnabled(true);
  410. MesClient.status_menu.setText("请扫工件码");
  411. S7Util.sendStartSignal(true);
  412. MesClient.curPointX.clear();
  413. MesClient.curPointY.clear();
  414. MesClient.curPoint = 0;
  415. updateMaterailData();
  416. // shiftUserCheck();
  417. }
  418. public static int userLoginHours;//用户登录所处小时
  419. //换班用户信息检查
  420. private static void shiftUserCheck() {
  421. LocalDateTime now = LocalDateTime.now();
  422. // 判断时间范围
  423. if (userLoginHours >= 8 && userLoginHours < 20) {
  424. int hour = now.getHour();
  425. if (hour >= 20 || hour < 8) {
  426. logoff();
  427. }
  428. } else {
  429. int hour = now.getHour();
  430. if (hour >= 8 && hour < 20) {
  431. logoff();
  432. }
  433. }
  434. }
  435. public static void resetScanB() {
  436. }
  437. //获取用户20位
  438. public static void getUser() {
  439. user20 = user_menu.getText().toString();
  440. String space_tmp1 = "";
  441. if(user20.length()<20) {
  442. for(int i=0;i<20-user20.length();i++) {
  443. space_tmp1 = space_tmp1 + " ";
  444. }
  445. }
  446. user20 = user20 + space_tmp1;
  447. }
  448. //获取barcode内容36位
  449. public static String getBarcode(String barcodeTmp) {
  450. String barcodeRet = barcodeTmp;
  451. if(barcodeTmp.equalsIgnoreCase("")) {
  452. return "";
  453. }else {
  454. if(barcodeTmp.length()<36) {
  455. String space = "";
  456. for(int i=0;i<36-barcodeTmp.length();i++) {
  457. space = space + " ";
  458. }
  459. barcodeRet = barcodeTmp + space;
  460. }
  461. }
  462. return barcodeRet;
  463. }
  464. public static void scanBarcode() {
  465. if(work_status == 1){
  466. JOptionPane.showMessageDialog(mesClientFrame,"工作中,勿扫码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  467. return;
  468. }
  469. String scanBarcodeTitle = "";
  470. switch(scan_type) {
  471. case 1:
  472. product_sn.setText("");
  473. scanBarcodeTitle = "请扫工件码";
  474. break;
  475. }
  476. //弹窗扫工件码
  477. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  478. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  479. //获取用户
  480. getUser();
  481. //获取扫码内容36位
  482. String barcode36 = getBarcode(scanBarcode);//处理36为码
  483. //工位号
  484. String gw = "";
  485. switch(scan_type) {
  486. case 1:
  487. product_sn.setText(scanBarcode);
  488. break;
  489. }
  490. //刷新界面
  491. mesClientFrame.repaint();
  492. if(!tcp_connect_flag) {
  493. MesClient.setMenuStatus("设备未连接Mes服务器",-1);
  494. // JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  495. return;
  496. }
  497. //弹框扫码,如果和规定的工装编号一致才可以继续
  498. String scanBarcode1 = JOptionPane.showInputDialog(null, "请扫工装二维码");
  499. if (!scanBarcode1.equalsIgnoreCase("OP280A")){
  500. //菜单栏给出提示信息
  501. MesClient.setMenuStatus("请扫正确的工装二维码",-1);
  502. return;
  503. }
  504. // 查询工件质量
  505. Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20);
  506. if(!sendret){
  507. MesClient.setMenuStatus("消息发送失败,请重试",-1);
  508. // JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  509. return;
  510. }
  511. }else {
  512. MesClient.setMenuStatus("请扫工件码,请重试",-1);
  513. // JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  514. return;
  515. }
  516. }
  517. public static void logoff() {
  518. // welcomeWin.setVisible(true);
  519. mesClientFrame.setVisible(false);
  520. System.exit(0);
  521. nettyClient = null;
  522. // if(heartBeatTimer!=null) {
  523. // heartBeatTimer.cancel();
  524. // }
  525. // if(heartBeatIconTimer!=null) {
  526. // heartBeatIconTimer.cancel();
  527. // }
  528. tcp_connect_flag = false;
  529. connect_request_flag = false;
  530. }
  531. public MesClient() {
  532. setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png")));
  533. setTitle("MES系统客户端:"+mes_gw + "- " + mes_gw_des);
  534. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  535. setBounds(0, 0, 1024, 768);
  536. JMenuBar menuBar = new JMenuBar();
  537. menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26));
  538. setJMenuBar(menuBar);
  539. JMenu fileMenu = new JMenu("用户");
  540. fileMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png")));
  541. fileMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  542. menuBar.add(fileMenu);
  543. JMenuItem exitMenuItem = new JMenuItem("退出");
  544. exitMenuItem.setIcon(new ImageIcon(MesClient.class.getResource("/bg/logoff.png")));
  545. exitMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  546. fileMenu.add(exitMenuItem);
  547. exitMenuItem.addMouseListener(new MouseAdapter() {
  548. @Override
  549. public void mousePressed(MouseEvent e) {//只能检测到mousePressed事件
  550. super.mouseClicked(e);
  551. //dispose();
  552. logoff();
  553. }
  554. });
  555. JMenu settingMenu = new JMenu("设置");
  556. //settingMenu.setVisible(false);
  557. settingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
  558. settingMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  559. menuBar.add(settingMenu);
  560. JMenuItem resetTcpMenu = new JMenuItem("\u91CD\u8FDEMES");
  561. resetTcpMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  562. resetTcpMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  563. resetTcpMenu.addMouseListener(new MouseAdapter() {
  564. @Override
  565. public void mousePressed(MouseEvent e) {
  566. super.mouseClicked(e);
  567. //重连mes
  568. nettyClient.future.channel().close();
  569. }
  570. });
  571. settingMenu.add(resetTcpMenu);
  572. JMenuItem resetTcpMenu_1 = new JMenuItem("刷新工件");
  573. resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  574. resetTcpMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  575. resetTcpMenu_1.addMouseListener(new MouseAdapter() {
  576. @Override
  577. public void mousePressed(MouseEvent e) {
  578. super.mouseClicked(e);
  579. resetScanA();
  580. }
  581. });
  582. settingMenu.add(resetTcpMenu_1);
  583. JMenuItem mes_block_menu = new JMenuItem("MES屏蔽");
  584. mes_block_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  585. mes_block_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  586. mes_block_menu.addMouseListener(new MouseAdapter() {
  587. @Override
  588. public void mousePressed(MouseEvent e) {
  589. super.mouseClicked(e);
  590. Color currentColor = mes_block_menu.getBackground();
  591. // 判断是否为绿色
  592. if (Color.GREEN.equals(currentColor)) {
  593. // 还原成默认颜色(一般是 null 或菜单条的背景色)
  594. mes_block_menu.setBackground(UIManager.getColor("MenuItem.background"));
  595. mes_flag = false;
  596. } else {
  597. // 设置为绿色
  598. mes_block_menu.setBackground(Color.GREEN);
  599. mes_flag = true;
  600. }
  601. }
  602. });
  603. settingMenu.add(mes_block_menu);
  604. JMenuItem resetTcpMenu_2 = new JMenuItem("程序点检");
  605. resetTcpMenu_2.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  606. resetTcpMenu_2.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  607. resetTcpMenu_2.addMouseListener(new MouseAdapter() {
  608. @Override
  609. public void mousePressed(MouseEvent e) {
  610. super.mouseClicked(e);
  611. // resetScanA();
  612. boolean ret = fxlabel2Flag == 1?false:true;
  613. if(fxlabel2Flag == 1){
  614. fxlabel2Flag = 0;
  615. }else{
  616. fxlabel2Flag = 1;
  617. }
  618. fxlabel2.setVisible(ret);
  619. }
  620. });
  621. settingMenu.add(resetTcpMenu_2);
  622. contentPane = new JPanel();
  623. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  624. setContentPane(contentPane);
  625. contentPane.setLayout(new BorderLayout(0, 0));
  626. JToolBar toolBar = new JToolBar();
  627. contentPane.add(toolBar, BorderLayout.NORTH);
  628. JLabel equipment_statu_label = new JLabel("状态:");
  629. equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER);
  630. equipment_statu_label.setForeground(Color.BLACK);
  631. equipment_statu_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  632. equipment_statu_label.setBackground(Color.LIGHT_GRAY);
  633. toolBar.add(equipment_statu_label);
  634. status_menu = new JButton("设备未连接MES服务器");
  635. if(tcp_connect_flag) {
  636. status_menu.setText("已连接MES服务器");
  637. status_menu.setForeground(Color.GREEN);
  638. }else {
  639. status_menu.setText("未连接MES服务器");
  640. status_menu.setForeground(Color.DARK_GRAY);
  641. }
  642. status_menu.addActionListener(new ActionListener() {
  643. public void actionPerformed(ActionEvent e) {
  644. }
  645. });
  646. status_menu.setForeground(Color.GREEN);
  647. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  648. status_menu.setBackground(Color.BLACK);
  649. toolBar.add(status_menu);
  650. JLabel space_1 = new JLabel(" ");
  651. toolBar.add(space_1);
  652. JLabel heart_beat_status_label = new JLabel("心跳:");
  653. heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  654. heart_beat_status_label.setForeground(Color.BLACK);
  655. heart_beat_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  656. heart_beat_status_label.setBackground(Color.LIGHT_GRAY);
  657. toolBar.add(heart_beat_status_label);
  658. heart_beat_menu = new JButton("2024-02-20 23:20:10");
  659. heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  660. heart_beat_menu.setForeground(Color.GREEN);
  661. heart_beat_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  662. heart_beat_menu.setBackground(Color.BLACK);
  663. toolBar.add(heart_beat_menu);
  664. JLabel space_2 = new JLabel(" ");
  665. toolBar.add(space_2);
  666. JLabel user_status_label = new JLabel("登录用户:");
  667. user_status_label.setHorizontalAlignment(SwingConstants.CENTER);
  668. user_status_label.setForeground(Color.BLACK);
  669. user_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  670. user_status_label.setBackground(Color.LIGHT_GRAY);
  671. toolBar.add(user_status_label);
  672. user_menu = new JButton("JinJuShi");
  673. user_menu.setForeground(Color.GREEN);
  674. user_menu.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  675. user_menu.setBackground(Color.BLACK);
  676. toolBar.add(user_menu);
  677. JLabel space_3 = new JLabel(" ");
  678. toolBar.add(space_3);
  679. JLabel space_4 = new JLabel(" ");
  680. toolBar.add(space_4);
  681. mesbtn = new JButton("MES已开启");
  682. mesbtn.setBackground(Color.GREEN);
  683. mesbtn.setForeground(Color.WHITE);
  684. mesbtn.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  685. toolBar.add(mesbtn);
  686. mesbtn.addActionListener(new ActionListener() {
  687. public void actionPerformed(ActionEvent e) {
  688. String pass = "503833";
  689. // Boolean ret = S7Util.getMesStatus();
  690. //弹窗扫工件码
  691. String scanBarcode = JOptionPane.showInputDialog(null, "请输入密码");
  692. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")&&scanBarcode.equals(pass)) {
  693. MesClient.mes_flag = !MesClient.mes_flag;
  694. // S7Util.sendMesStatus(!ret); // 屏蔽
  695. formatMesStatus();
  696. }else{
  697. MesClient.setMenuStatus("密码错误",-1);
  698. }
  699. }
  700. });
  701. tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  702. tabbedPane.setMinimumSize(new Dimension(400, 50));
  703. tabbedPane.setFont(new Font("宋体", Font.BOLD, 22));
  704. contentPane.add(tabbedPane);
  705. //首页
  706. JPanel indexPanelA = new JPanel();
  707. indexScrollPaneA = new JScrollPane(indexPanelA);
  708. indexPanelA.setLayout(null);
  709. fxlabel2 = new JLabel("程序点检中");
  710. fxlabel2.setFont(new Font("微软雅黑", Font.PLAIN, 24));
  711. fxlabel2.setBounds(81, 0, 810, 70);
  712. fxlabel2.setForeground(Color.RED);
  713. fxlabel2.setHorizontalAlignment(SwingConstants.CENTER);
  714. fxlabel2.setVisible(false);
  715. indexPanelA.add(fxlabel2);
  716. product_sn = new JTextField();
  717. product_sn.setHorizontalAlignment(SwingConstants.CENTER);
  718. product_sn.setEditable(false);
  719. product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  720. product_sn.setBounds(81, 70, 602, 70);
  721. indexPanelA.add(product_sn);
  722. product_sn.setColumns(10);
  723. f_scan_data_bt_1 = new JButton("扫码");
  724. f_scan_data_bt_1.addActionListener(new ActionListener() {
  725. public void actionPerformed(ActionEvent e) {
  726. scan_type = 1;
  727. scanBarcode();
  728. }
  729. });
  730. f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  731. f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  732. f_scan_data_bt_1.setBounds(693, 70, 198, 70);
  733. indexPanelA.add(f_scan_data_bt_1);
  734. // String[] hjtitles = new String[]{"焊机1","焊机2","焊机3"};
  735. // String[] hjvals = new String[]{"HJ001","HJ002","HJ003"};
  736. // mesRadioHj = new MesRadio(hjtitles,hjvals);
  737. // mesRasdioHj.setSize(500,50);
  738. // mesRadioHj.setBounds(190,170,500,50);
  739. // indexPanelA.add(mesRadioHj);
  740. fxlabel = new JLabel("该工件为返修件,请仔细检查");
  741. fxlabel.setFont(new Font("微软雅黑", Font.PLAIN, 38));
  742. fxlabel.setBounds(81, 170, 810, 70);
  743. fxlabel.setForeground(Color.RED);
  744. fxlabel.setHorizontalAlignment(SwingConstants.CENTER);
  745. fxlabel.setVisible(false);
  746. indexPanelA.add(fxlabel);
  747. finish_ok_bt = new JButton("OK");
  748. finish_ok_bt.setEnabled(false);
  749. finish_ok_bt.addActionListener(new ActionListener() {
  750. public void actionPerformed(ActionEvent e) {
  751. if(work_status == 1 && check_quality_result){
  752. String sn = getBarcode(product_sn.getText());
  753. getUser();
  754. String qret = "OK";
  755. Boolean sendret = DataUtil.sendQuality(nettyClient,sn,qret,user20);
  756. if(!sendret){
  757. MesClient.setMenuStatus("消息发送失败,请重试",-1);
  758. return;
  759. }else{
  760. MesClient.resetScanA();
  761. MesClient.scan_type = 1;
  762. MesClient.scanBarcode();
  763. MesClient.setMenuStatus("结果提交成功,请扫下一件",0);
  764. }
  765. }
  766. }
  767. });
  768. finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
  769. finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  770. finish_ok_bt.setBounds(185, 291, 240, 80);
  771. finish_ok_bt.setEnabled(false);
  772. indexPanelA.add(finish_ok_bt);
  773. finish_ng_bt = new JButton("NG");
  774. finish_ng_bt.setEnabled(false);
  775. finish_ng_bt.addActionListener(new ActionListener() {
  776. public void actionPerformed(ActionEvent e) {
  777. if(work_status == 1 && check_quality_result){
  778. String sn = getBarcode(product_sn.getText());
  779. getUser();
  780. String qret = "NG";
  781. Boolean sendret = DataUtil.sendQuality(nettyClient,sn,qret,user20);
  782. if(!sendret){
  783. MesClient.setMenuStatus("消息发送失败,请重试",-1);
  784. return;
  785. }else{
  786. MesClient.resetScanA();
  787. MesClient.scan_type = 1;
  788. MesClient.scanBarcode();
  789. MesClient.setMenuStatus("结果提交成功,请扫下一件",0);
  790. }
  791. }
  792. }
  793. });
  794. finish_ng_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ng_bg.png")));
  795. finish_ng_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  796. finish_ng_bt.setBounds(508, 291, 240, 80);
  797. finish_ng_bt.setEnabled(false);
  798. indexPanelA.add(finish_ng_bt);
  799. tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null);
  800. tabbedPane.setEnabledAt(0, true);
  801. // searchScrollPane = new JScrollPane((Component) null);
  802. indexPanelC = new JPanel();
  803. searchScrollPaneDj = new JScrollPane(indexPanelC);
  804. indexPanelC.setLayout(null);
  805. // 转速显示
  806. spindleSpeedLabel = new JLabel("转速: 0 rpm");
  807. spindleSpeedLabel.setBounds(81, 200, 400, 40);
  808. spindleSpeedLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  809. indexPanelA.add(spindleSpeedLabel);
  810. // 进给显示
  811. feedRateLabel = new JLabel("进给: 0 mm/min");
  812. feedRateLabel.setBounds(320, 200, 400, 40);
  813. feedRateLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  814. indexPanelA.add(feedRateLabel);
  815. // 扭矩显示
  816. torqueLabel = new JLabel("扭矩: 0");
  817. torqueLabel.setBounds(600, 200, 400, 40);
  818. torqueLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  819. indexPanelA.add(torqueLabel);
  820. tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null);
  821. indexPanelB = new JPanel();
  822. searchScrollPane = new JScrollPane(indexPanelB);
  823. indexPanelB.setLayout(null);
  824. tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
  825. tabbedPane.addChangeListener(new ChangeListener() {
  826. @Override
  827. public void stateChanged(ChangeEvent e) {
  828. JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
  829. int selectedIndex = tabbedPane.getSelectedIndex();
  830. System.out.println("selectedIndex:"+selectedIndex);
  831. if(selectedIndex == 1){
  832. }
  833. }
  834. });
  835. }
  836. public static void setMenuStatus(String msg,int error){
  837. if(error == 0){
  838. MesClient.status_menu.setForeground(Color.GREEN);
  839. }else{
  840. MesClient.status_menu.setForeground(Color.RED);
  841. }
  842. MesClient.status_menu.setText(msg);
  843. }
  844. public static void getMaterailData(){
  845. try{
  846. JSONObject retObj = DataUtil.getBindMaterail();
  847. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  848. java.util.List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  849. int i = 0;
  850. rowData = new Object[arrs.size()][7];
  851. for (BindMaterialResp bindMaterialResp:arrs){
  852. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  853. rowData[i][1] = bindMaterialResp.getBatchSn();
  854. rowData[i][2] = bindMaterialResp.getLastTimes();
  855. rowData[i][3] = "";
  856. rowData[i][4] = bindMaterialResp.getCraft();
  857. rowData[i][5] = bindMaterialResp.getMaterialId();
  858. rowData[i][6] = bindMaterialResp.getType();
  859. i++;
  860. }
  861. bindBatchPanel();
  862. }
  863. }catch (Exception e){
  864. log.info(e.getMessage());
  865. }
  866. }
  867. public static void updateMaterailData(){
  868. try{
  869. JSONObject retObj = DataUtil.getBindMaterail();
  870. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  871. List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
  872. int i = 0;
  873. for (BindMaterialResp bindMaterialResp:arrs){
  874. rowData[i][0] = bindMaterialResp.getMaterialTitle();
  875. rowData[i][1] = bindMaterialResp.getBatchSn();
  876. rowData[i][2] = bindMaterialResp.getLastTimes();
  877. rowData[i][3] = "";
  878. rowData[i][4] = bindMaterialResp.getCraft();
  879. rowData[i][5] = bindMaterialResp.getMaterialId();
  880. rowData[i][6] = bindMaterialResp.getType();
  881. i++;
  882. }
  883. MesClient.table.repaint();
  884. }
  885. }catch (Exception e){
  886. log.info(e.getMessage());
  887. }
  888. }
  889. // 绑定物料批次码
  890. public static void scanBatchSn(BindMaterialResp bindMaterialResp) {
  891. //弹窗扫工件码
  892. String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle();
  893. String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
  894. if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
  895. JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType());
  896. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  897. MesClient.setMenuStatus("扫物料:"+bindMaterialResp.getMaterialTitle()+"成功",0);
  898. updateMaterailData();
  899. }else{
  900. if(retObj.get("result")==null){
  901. MesClient.setMenuStatus("请求失败,请重试",-1);
  902. }else{
  903. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  904. MesClient.setMenuStatus(retObj.getString("message"),-1);
  905. }
  906. }
  907. }
  908. }
  909. }
  910. public static void bindBatchPanel(){
  911. JPanel indexPanelBB = new JPanel();
  912. JPanel panel = new JPanel();
  913. panel.setBounds(0, 0, 990, 550);
  914. // panel.setBounds(81, 50, 810, 479);
  915. indexPanelBB.add(panel);
  916. panel.setLayout(new GridLayout(0, 1, 0, 0));
  917. table = new JTable(rowData, columnNames){
  918. public boolean isCellEditable(int row, int column) {
  919. if(column == 3){
  920. return true;
  921. }
  922. return false;
  923. }
  924. };
  925. table.setRowHeight(40);
  926. table.setEnabled(true);
  927. table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  928. table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton());
  929. table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton());
  930. JScrollPane scrollPane = new JScrollPane(table);
  931. panel.add(scrollPane);
  932. JScrollPane indexScrollPaneB = new JScrollPane(indexPanelBB);
  933. indexPanelBB.setLayout(null);
  934. tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null);
  935. }
  936. // 开启发送结果定时任务
  937. public static void startUpdateQualityTimer(){
  938. // 创建异步线程池
  939. ExecutorService executorService = Executors.newSingleThreadExecutor();
  940. // 提交任务
  941. executorService.submit(() -> {
  942. log.info("异步任务执行中...");
  943. // 模拟耗时操作
  944. try {
  945. while (true){
  946. try {
  947. // 确保连接已经打开
  948. if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
  949. JdbcUtils.openConnection();
  950. }
  951. Statement statement = JdbcUtils.conn.createStatement();
  952. String selectSql = "SELECT id, bw FROM submit_record\n" +
  953. "WHERE state = 0 ORDER BY id DESC LIMIT 10";
  954. String updateSql = "UPDATE submit_record \n" +
  955. "SET state = '1'\n" +
  956. "WHERE id = ";
  957. ResultSet rs = statement.executeQuery(selectSql);
  958. while(rs.next()){
  959. int id = rs.getInt("id");
  960. String bw = rs.getString("bw");
  961. // 发送请求
  962. String url = "http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesProductRecord/updateQualityByTiming";
  963. log.info("质量:"+bw);
  964. String s = HttpUtils.sendPostRequestJson(url, bw );
  965. log.info("结果:"+s);
  966. Boolean result = JSONObject.parseObject(s).getBoolean("result");
  967. if(result) {
  968. // 更改状态为 1
  969. statement.executeUpdate(updateSql + id);
  970. }
  971. }
  972. rs.close();
  973. statement.close();
  974. } catch (Exception e) {
  975. log.info(e.getMessage());
  976. }
  977. try {
  978. Thread.sleep(2000);
  979. }catch (Exception e){
  980. log.info(e.getMessage());
  981. }
  982. }
  983. } catch (Exception e) {
  984. log.info(e.getMessage());
  985. }
  986. log.info("异步任务执行完毕");
  987. });
  988. }
  989. // 开启发送结果定时任务
  990. public static void monitorDevice(){
  991. // 创建异步线程池
  992. ExecutorService executorService = Executors.newSingleThreadExecutor();
  993. // 提交任务
  994. executorService.submit(() -> {
  995. log.info("监控设备异步任务执行中...");
  996. // 模拟耗时操作
  997. try {
  998. while (true){
  999. try{
  1000. formatMesStatus();
  1001. }catch (Exception e){
  1002. e.printStackTrace();
  1003. }
  1004. try {
  1005. initS7();
  1006. if (mes_flag){
  1007. S7Util.getDeviceShield();
  1008. }else{
  1009. if (work_status == 0){
  1010. boolean ss = S7Util.getDeviceStart();
  1011. if(!ss){
  1012. S7Util.sendStartSignal(true);
  1013. }
  1014. }
  1015. S7Util.getDeviceState();
  1016. }
  1017. } catch (Exception e) {
  1018. log.info(e.getMessage());
  1019. }
  1020. try {
  1021. Thread.sleep(1000);
  1022. }catch (Exception e){
  1023. log.info(e.getMessage());
  1024. }
  1025. }
  1026. } catch (Exception e) {
  1027. log.info(e.getMessage());
  1028. }
  1029. log.info("异步任务执行完毕");
  1030. });
  1031. }
  1032. public static void formatMesStatus(){
  1033. // Boolean ret = S7Util.getMesStatus();
  1034. Boolean ret = MesClient.mes_flag;
  1035. if(ret){ // MES屏蔽
  1036. mesbtn.setBackground(Color.RED);
  1037. mesbtn.setText("MES已屏蔽");
  1038. }else{
  1039. mesbtn.setBackground(Color.GREEN);
  1040. mesbtn.setText("MES已开启");
  1041. }
  1042. }
  1043. public static void initS7(){
  1044. try {
  1045. if(s7Connector == null) {
  1046. //PLC地址
  1047. String ipAddress = plcUrl;
  1048. //默认端口
  1049. // int port = 102;
  1050. int rack=0;
  1051. int slot=3;
  1052. int timeout=1000;
  1053. s7Connector=
  1054. S7ConnectorFactory
  1055. .buildTCPConnector()
  1056. .withHost(ipAddress)
  1057. // .withPort(port) //optional
  1058. // .withRack(rack) //optional
  1059. // .withSlot(slot) //optional
  1060. .withTimeout(timeout) //连接超时时间
  1061. .build();
  1062. S7Serializer s7Serializer2L = S7SerializerFactory.buildSerializer(s7Connector);
  1063. }
  1064. } catch (Exception e) { // 如果连接失败保存失败再次尝试连接一次
  1065. s7Connector = null;
  1066. e.printStackTrace();
  1067. }
  1068. }
  1069. }