WorkJPanelOP65.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. package com.mes.component;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
  4. import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
  5. import com.mes.ui.DataUtil;
  6. import com.mes.ui.MesClient;
  7. import com.mes.util.TCPClient;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import javax.swing.*;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import java.util.Timer;
  14. import java.util.TimerTask;
  15. public class WorkJPanelOP65 extends JPanel {
  16. public static final Logger log = LoggerFactory.getLogger(WorkJPanelOP65.class);
  17. public String oprno = "OP075"; // 原OP065打磨,产线改造后工位号OP075
  18. public String pos = "A";
  19. public String lastSn = ""; // 最近的码
  20. public String lineSn = "XT";
  21. public JLabel sllabel1;
  22. public JLabel sllabel2;
  23. public JButton scanBtn;
  24. public JTextField productSn;
  25. public JButton status_menu;
  26. public JButton resetBtn;
  27. public JButton finish_ok_bt;
  28. public JButton finish_ng_bt;
  29. public Integer workWidth = 940;
  30. public Integer workHeight = 400;
  31. public Boolean check_quality_result = false;
  32. public Integer work_status = 0;
  33. public String plcDb2 = "DB7010";
  34. public JLabel turnmeslabel;
  35. public JLabel aFaceLabel;
  36. public JLabel bFaceLabel;
  37. public ImageIcon imageGreen;
  38. public ImageIcon imageGray;
  39. public boolean boolFlag;
  40. public Integer isLocal = 0; // 0=正式 1=本地
  41. public int lastFaceState = -1;
  42. // PLC 地址配置
  43. public S7PLC s7PLC = null;
  44. public String plcIp1 = "192.168.90.85"; // plc5 西侧
  45. public String plcIp2 = "192.168.90.86"; // plc6 东侧
  46. public String addrFaceA = plcDb2 + ".6.0"; // B 面到位信号
  47. public String addrFaceB = plcDb2 + ".6.1"; // A 面到位信号
  48. public String addrFeedback = plcDb2 + ".6.2"; // 转动反馈信号
  49. //55扫码完成反馈
  50. public String addrScanFinish = plcDb2 + ".7.0";
  51. //55定位台状态
  52. public String addrPush = plcDb2 + ".7.1";
  53. private JLabel scanFinish55Label; // 55扫码信号显示与操作
  54. private JLabel push55Label; // 55到位信号显示
  55. private String orientation = "1";
  56. // 逻辑记录变量
  57. private int lastPush55State = -1; // 记录上一次55到位状态
  58. public JButton signalBtn; // 信号监控按钮
  59. public PlcSignalMonitor03 signalMonitor; // 信号监控组件
  60. public WorkJPanelOP65(String position){
  61. this.setLayout(null);
  62. this.setBackground(Color.WHITE);
  63. this.pos = position;
  64. if(MesClient.mes_line_pos.equals("east")){ // 东侧
  65. s7PLC = new S7PLC(EPlcType.S1500, plcIp2);
  66. orientation = "1"; //东
  67. }else{
  68. s7PLC = new S7PLC(EPlcType.S1500, plcIp1);
  69. orientation = "2"; //西
  70. }
  71. JLabel oprnoLabel = new JLabel(oprno+pos);
  72. oprnoLabel.setHorizontalAlignment(SwingConstants.CENTER);
  73. oprnoLabel.setForeground(Color.BLACK);
  74. oprnoLabel.setFont(new Font("微软雅黑", Font.PLAIN, 30));
  75. oprnoLabel.setBounds(0, 0, workWidth, 60);
  76. this.add(oprnoLabel);
  77. imageGreen = new ImageIcon(MesClient.class.getResource("/bg/green_dot.png"));
  78. imageGreen.setImage(imageGreen.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH));
  79. imageGray = new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png"));
  80. imageGray.setImage(imageGray.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH));
  81. turnmeslabel = new JLabel("MES转动反馈");
  82. turnmeslabel.setHorizontalAlignment(SwingConstants.LEFT);
  83. turnmeslabel.setIcon(imageGreen);
  84. turnmeslabel.setForeground(Color.BLACK);
  85. turnmeslabel.setBounds(270, 200, workWidth/3, 30);
  86. turnmeslabel.addMouseListener(new MouseAdapter() {
  87. public void mouseClicked(MouseEvent e) {
  88. boolean ret = readBoolean(addrFeedback);
  89. int choice = JOptionPane.showConfirmDialog(null, "确定修改MES完成反馈结果为"+(ret?"false":"true")+"吗?", "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  90. if (choice == JOptionPane.YES_OPTION) {
  91. writeBoolean(addrFeedback,!ret);
  92. log.info("修改MES扫码反馈结果为"+(ret?"false":"true"));
  93. }
  94. }
  95. });
  96. this.add(turnmeslabel);
  97. aFaceLabel = new JLabel("当前A面");
  98. aFaceLabel.setHorizontalAlignment(SwingConstants.LEFT);
  99. aFaceLabel.setIcon(imageGreen);
  100. aFaceLabel.setForeground(Color.BLACK);
  101. aFaceLabel.setBounds(400, 200, workWidth/3, 30);
  102. this.add(aFaceLabel);
  103. bFaceLabel = new JLabel("当前B面");
  104. bFaceLabel.setHorizontalAlignment(SwingConstants.LEFT);
  105. bFaceLabel.setIcon(imageGreen);
  106. bFaceLabel.setForeground(Color.BLACK);
  107. bFaceLabel.setBounds(500, 200, workWidth/3, 30);
  108. this.add(bFaceLabel);
  109. push55Label = new JLabel("55定位台状态");
  110. push55Label.setHorizontalAlignment(SwingConstants.LEFT);
  111. push55Label.setIcon(imageGray); // 初始为灰色
  112. push55Label.setForeground(Color.BLACK);
  113. push55Label.setBounds(270, 240, workWidth/3, 30); // 放在转动反馈下方一行
  114. this.add(push55Label);
  115. scanFinish55Label = new JLabel("55扫码反馈");
  116. scanFinish55Label.setHorizontalAlignment(SwingConstants.LEFT);
  117. scanFinish55Label.setIcon(imageGray);
  118. scanFinish55Label.setForeground(Color.BLACK);
  119. scanFinish55Label.setBounds(400, 240, workWidth/3, 30);
  120. scanFinish55Label.addMouseListener(new MouseAdapter() {
  121. @Override
  122. public void mouseClicked(MouseEvent e) {
  123. // 读取当前55扫码状态
  124. boolean ret = readBoolean(addrScanFinish);
  125. int choice = JOptionPane.showConfirmDialog(null,
  126. "确定修改55扫码完成反馈结果为 " + (ret ? "false" : "true") + " 吗?",
  127. "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  128. if (choice == JOptionPane.YES_OPTION) {
  129. writeBoolean(addrScanFinish, !ret);
  130. log.info("手动修改55扫码反馈结果为: " + !ret);
  131. }
  132. }
  133. });
  134. this.add(scanFinish55Label);
  135. status_menu = new JButton("请扫工件码");
  136. status_menu.setBounds(0, 60, workWidth, 60);
  137. status_menu.setForeground(Color.GREEN);
  138. status_menu.addActionListener(new ActionListener() {
  139. public void actionPerformed(ActionEvent e) {
  140. }
  141. });
  142. status_menu.setForeground(Color.GREEN);
  143. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  144. status_menu.setBackground(Color.BLACK);
  145. this.add(status_menu);
  146. productSn = new JTextField("");
  147. productSn.setHorizontalAlignment(SwingConstants.CENTER);
  148. productSn.setForeground(Color.BLACK);
  149. productSn.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  150. productSn.setEditable(true);
  151. productSn.setBounds(0, 130, this.workWidth-200, 60);
  152. productSn.addKeyListener(new KeyAdapter() {
  153. @Override
  154. public void keyPressed(KeyEvent e) {
  155. if (e.getKeyCode() == KeyEvent.VK_ENTER) {
  156. // 处理Enter键被按下的逻辑
  157. String sn = productSn.getText().trim();
  158. if(!sn.isEmpty()){
  159. // MesClient.createQRCode(productSn.getText(),oprno);
  160. productSn.setEnabled(false);
  161. // 查询工件质量
  162. JSONObject retObj = DataUtil.checkQuality(sn,oprno+pos,MesClient.user_menu.getText());
  163. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  164. check_quality_result = true;//质量合格,可以绑定加工
  165. work_status = 1;
  166. setMenuStatus("工件可以加工", 0);
  167. finish_ok_bt.setEnabled(true);
  168. finish_ng_bt.setEnabled(true);
  169. }else{
  170. check_quality_result = false;
  171. work_status = 0;
  172. if(retObj.get("result")==null){
  173. setMenuStatus("请求失败,请重试",-1);
  174. }else{
  175. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  176. setMenuStatus(retObj.getString("message"),-1);
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. });
  184. // productSn.addMouseListener(new MouseAdapter() {
  185. // public void mouseClicked(MouseEvent e) {
  186. //
  187. // }
  188. // });
  189. this.add(productSn);
  190. finish_ok_bt = new JButton("OK");
  191. finish_ok_bt.setEnabled(false);
  192. finish_ok_bt.addActionListener(new ActionListener() {
  193. public void actionPerformed(ActionEvent e) {
  194. String sn = productSn.getText().trim();
  195. if(sn.isEmpty()){
  196. return;
  197. }
  198. JSONObject retObj = DataUtil.sendQuality(sn,oprno+pos,"OK",MesClient.user_menu.getText());
  199. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  200. resetScanA();
  201. setMenuStatus("结果提交成功,请扫下一件",0);
  202. writeBoolean(addrFeedback,true); //完成反馈
  203. }else{
  204. if(retObj.get("result")==null){
  205. setMenuStatus("请求失败,请重试",-1);
  206. }else{
  207. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  208. setMenuStatus(retObj.getString("message"),-1);
  209. }
  210. }
  211. }
  212. }
  213. });
  214. finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
  215. finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  216. finish_ok_bt.setBounds(185, 291, 240, 80);
  217. finish_ok_bt.setEnabled(false);
  218. this.add(finish_ok_bt);
  219. finish_ng_bt = new JButton("NG");
  220. finish_ng_bt.setEnabled(false);
  221. finish_ng_bt.addActionListener(new ActionListener() {
  222. public void actionPerformed(ActionEvent e) {
  223. String sn = productSn.getText().trim();
  224. if(sn.isEmpty()){
  225. return;
  226. }
  227. JSONObject retObj = DataUtil.sendQuality(sn,oprno+pos,"NG",MesClient.user_menu.getText());
  228. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  229. resetScanA();
  230. setMenuStatus("结果提交成功,请扫下一件",0);
  231. writeBoolean(addrFeedback,true); //完成反馈
  232. }else{
  233. if(retObj.get("result")==null){
  234. setMenuStatus("请求失败,请重试",-1);
  235. }else{
  236. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  237. setMenuStatus(retObj.getString("message"),-1);
  238. }
  239. }
  240. }
  241. }
  242. });
  243. finish_ng_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ng_bg.png")));
  244. finish_ng_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  245. finish_ng_bt.setBounds(508, 291, 240, 80);
  246. finish_ng_bt.setEnabled(false);
  247. this.add(finish_ng_bt);
  248. resetBtn = new JButton("刷新");
  249. resetBtn.addActionListener(new ActionListener() {
  250. public void actionPerformed(ActionEvent e) {
  251. resetScanA();
  252. }
  253. });
  254. resetBtn.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  255. resetBtn.setFont(new Font("微软雅黑", Font.PLAIN, 32));
  256. resetBtn.setBounds(this.workWidth-190, 130, 190, 60);
  257. this.add(resetBtn);
  258. textFocus();
  259. if (isLocal == 0) {
  260. writeBoolean(addrFeedback, false);
  261. writeBoolean(addrScanFinish, false);
  262. log.info("程序启动:55扫码信号已初始化为 false");
  263. log.info("程序启动:65扫码完成信号已初始化为 false");
  264. }
  265. startTurntableMonitor();
  266. start55MonitorTimer();
  267. // 信号监控按钮
  268. signalMonitor = new PlcSignalMonitor03(s7PLC, plcDb2, oprno + pos, MesClient.mesClientFrame);
  269. signalBtn = signalMonitor.createSignalButton();
  270. signalBtn.setBounds(workWidth*2/3 + 63, 230, 90, 26);
  271. this.add(signalBtn);
  272. }
  273. public void setMenuStatus(String msg,int error){
  274. if(error == 0){
  275. this.status_menu.setForeground(Color.GREEN);
  276. }else{
  277. this.status_menu.setForeground(Color.RED);
  278. }
  279. this.status_menu.setText(msg);
  280. }
  281. public Timer cjTimerText;
  282. public void textFocus() {
  283. if(cjTimerText!=null) {
  284. cjTimerText.cancel();
  285. }
  286. cjTimerText = new Timer();
  287. cjTimerText.schedule(new TimerTask() {
  288. public void run() {
  289. productSn.requestFocusInWindow();
  290. }
  291. }, 1000,1000);
  292. }
  293. public void resetScanA() {
  294. check_quality_result = false;
  295. work_status = 0;
  296. productSn.setText("");
  297. productSn.setEnabled(true);
  298. setMenuStatus("请扫工件码",0);
  299. finish_ok_bt.setEnabled(true);
  300. finish_ng_bt.setEnabled(true);
  301. }
  302. private Timer turntableTimer;
  303. public void startTurntableMonitor() {
  304. if (turntableTimer != null) {
  305. turntableTimer.cancel();
  306. }
  307. turntableTimer = new Timer();
  308. turntableTimer.schedule(new TimerTask() {
  309. @Override
  310. public void run() {
  311. try {
  312. if (MesClient.isLogin == 1) {
  313. // 1. 读取 A、B 面信号
  314. boolean isA = readBoolean(addrFaceA);
  315. boolean isB = readBoolean(addrFaceB);
  316. boolean isBack = readBoolean(addrFeedback);
  317. //改变状态灯
  318. aFaceLabel.setIcon(!isA ? imageGreen : imageGray);
  319. bFaceLabel.setIcon(!isB ? imageGreen : imageGray);
  320. turnmeslabel.setIcon(isBack ? imageGreen : imageGray);
  321. int currentFace = -1;
  322. if (isA && !isB) {
  323. currentFace = 0; // 当前在 A 面
  324. } else if (isB && !isA) {
  325. currentFace = 1; // 当前在 B 面
  326. }else { //既不在A,也不在B面
  327. currentFace = 2;
  328. }
  329. // 2. 捕捉切换瞬间
  330. // 如果当前面有效,且与上次记录不同,且不是刚启动后的第一次识别
  331. if (currentFace != -1 && lastFaceState != -1 && currentFace != lastFaceState) {
  332. log.info("检测到面切换,自动重置反馈信号");
  333. writeBoolean(addrFeedback, false); // 切换时重置
  334. }
  335. // 3. 更新记录状态
  336. if (currentFace != -1) {
  337. lastFaceState = currentFace;
  338. }
  339. }
  340. } catch (Exception e) {
  341. log.error("监控异常: " + e.getMessage());
  342. }
  343. }
  344. }, 1000, 1500); // 1秒后启动,每1.5秒扫描一次
  345. }
  346. private Timer monitor55Timer;
  347. public void start55MonitorTimer() {
  348. if (monitor55Timer != null) {
  349. monitor55Timer.cancel();
  350. }
  351. monitor55Timer = new Timer();
  352. monitor55Timer.schedule(new TimerTask() {
  353. @Override
  354. public void run() {
  355. try {
  356. if (MesClient.isLogin == 1) {
  357. JSONObject signalMessage = DataUtil.getSignal(orientation);
  358. //如果已扫码,就将扫码信号写为true
  359. String data = signalMessage.getString("data");
  360. //为1则已扫码
  361. if(data.equals("1")){
  362. writeBoolean(addrScanFinish, true);
  363. }
  364. // 1. 读取 55 工位相关信号
  365. boolean isPush = readBoolean(addrPush);
  366. boolean isScanFinish = readBoolean(addrScanFinish);
  367. // 2. 更新 UI 状态灯图标
  368. push55Label.setIcon(isPush ? imageGreen : imageGray);
  369. scanFinish55Label.setIcon(isScanFinish ? imageGreen : imageGray);
  370. // 3. 核心联动逻辑:55定位台信号发生改变时重置扫码信号
  371. int currentPush = isPush ? 1 : 0;
  372. if (lastPush55State != -1 && currentPush != lastPush55State) {
  373. log.info("检测到55到位信号切换 ({} -> {}),执行55扫码信号自动重置",
  374. lastPush55State, currentPush);
  375. // 执行重置
  376. writeBoolean(addrScanFinish, false);
  377. DataUtil.sendChangeByorientation(orientation);
  378. }
  379. // 4. 更新上次状态记录
  380. lastPush55State = currentPush;
  381. }
  382. } catch (Exception e) {
  383. log.error("55工位信号监控异常: " + e.getMessage());
  384. }
  385. }
  386. }, 1000, 1500); // 2秒后启动,每1.5秒执行一次
  387. }
  388. //旋转反馈
  389. public boolean writeBoolean(String db, boolean permit){
  390. try {
  391. s7PLC.writeBoolean(db,permit);
  392. // log.info("<"+this.oprno+this.pos+">写入"+db+"-->成功,写入内容:{}", permit ? "true" : "false");
  393. } catch (Exception e) {
  394. log.error("<"+this.oprno+this.pos+">写入"+db+"-->失败,写入内容:{}", permit ? "true" : "false");
  395. log.error(e.getMessage(), e);
  396. return false;
  397. }
  398. return true;
  399. }
  400. public boolean readBoolean(String db){
  401. boolean permit;
  402. try {
  403. permit = s7PLC.readBoolean(db);
  404. // log.info("<"+this.oprno+this.pos+">读取"+db+"-->成功,结果:{}", permit ? "true" : "false");
  405. } catch (Exception e) {
  406. log.error("<"+this.oprno+this.pos+">读取"+db+"-->失败", e);
  407. return false;
  408. }
  409. return permit;
  410. }
  411. }