MesClientComponent.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package com.mes.component;
  2. import com.mes.ui.DataUtil;
  3. import com.mes.ui.MesClient;
  4. import com.mes.ui.Oprno;
  5. import com.mes.util.S7PLCUtils;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.FocusAdapter;
  13. import java.awt.event.FocusEvent;
  14. import java.util.Timer;
  15. import java.util.TimerTask;
  16. public class MesClientComponent extends JPanel {
  17. public static final Logger log = LoggerFactory.getLogger(MesClientComponent.class);
  18. // 工位类
  19. public Oprno oprnoClass = new Oprno();
  20. // 工位号
  21. // 线体
  22. public static String lineSn = "XT";
  23. // 输入框
  24. public JTextField productSn;
  25. // 状态栏
  26. public JButton status_menu;
  27. // 宽度
  28. public static Integer workWidth = 350;
  29. // 高度
  30. public static Integer workHeight = 400;
  31. // 扫码按钮
  32. public JButton scanBtn;
  33. // 刷新按钮
  34. public JButton resetBtn;
  35. // 工作状态
  36. // 0:未扫码,等待允许扫码信号 1:扫码成功,质量检查 2:质量合格,开始做件 3:加工完成,提交结果
  37. public Integer work_status = 0;
  38. public JLabel sllabel1;
  39. public JLabel sllabel2;
  40. // UI(布局随 workWidth、workHeight 缩放,避免写死 350×400 下的像素坐标)
  41. public MesClientComponent(Oprno opno){
  42. oprnoClass = opno;
  43. int w = workWidth;
  44. int h = workHeight;
  45. this.setLayout(null);
  46. this.setBackground(Color.WHITE);
  47. this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
  48. int margin = Math.max(8, w / 40);
  49. int hTitle = Math.max(36, (int) Math.round(h * 0.15));
  50. int hStatus = Math.max(32, (int) Math.round(h * 0.15));
  51. int hSn = Math.max(32, (int) Math.round(h * 0.15));
  52. int hBtn = Math.max(40, (int) Math.round(h * 0.15));
  53. int hLab = Math.max(28, (int) Math.round(h * 0.125));
  54. int gap = Math.max(4, margin / 2);
  55. int y = 0;
  56. JLabel oprnoLabel = new JLabel(oprnoClass.getOprno());
  57. oprnoLabel.setHorizontalAlignment(SwingConstants.CENTER);
  58. oprnoLabel.setForeground(Color.BLACK);
  59. int titleFont = Math.max(16, Math.min(36, w / 11));
  60. oprnoLabel.setFont(new Font("微软雅黑", Font.PLAIN, titleFont));
  61. oprnoLabel.setBounds(0, y, w, hTitle);
  62. this.add(oprnoLabel);
  63. y += hTitle;
  64. status_menu = new JButton("等待上料信号");
  65. status_menu.setBounds(margin, y, w - 2 * margin, hStatus);
  66. status_menu.setForeground(Color.GREEN);
  67. status_menu.addActionListener(new ActionListener() {
  68. public void actionPerformed(ActionEvent e) {
  69. }
  70. });
  71. int statusFont = Math.max(14, Math.min(22, w / 16));
  72. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, statusFont));
  73. status_menu.setBackground(Color.BLACK);
  74. this.add(status_menu);
  75. y += hStatus + gap;
  76. productSn = new JTextField("");
  77. productSn.setHorizontalAlignment(SwingConstants.CENTER);
  78. productSn.setForeground(Color.BLACK);
  79. int snFont = Math.max(14, Math.min(22, w / 16));
  80. productSn.setFont(new Font("微软雅黑", Font.PLAIN, snFont));
  81. productSn.setEditable(false);
  82. productSn.setBounds(margin, y, w - 2 * margin, hSn);
  83. productSn.addFocusListener(new FocusAdapter() {
  84. @Override
  85. public void focusGained(FocusEvent e) {
  86. }
  87. });
  88. this.add(productSn);
  89. y += hSn + gap;
  90. scanBtn = new JButton("扫码");
  91. final MesClientComponent thisComponent = this;
  92. scanBtn.addActionListener(new ActionListener() {
  93. public void actionPerformed(ActionEvent e) {
  94. System.out.println("扫码");
  95. thisComponent.scanBtnClick();
  96. }
  97. });
  98. scanBtn.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  99. int btnFont = Math.max(16, Math.min(32, w / 10));
  100. scanBtn.setFont(new Font("微软雅黑", Font.PLAIN, btnFont));
  101. int btnGap = gap;
  102. int btnW = (w - 2 * margin - btnGap) / 2;
  103. scanBtn.setBounds(margin, y, btnW, hBtn);
  104. this.add(scanBtn);
  105. resetBtn = new JButton("刷新");
  106. resetBtn.addActionListener(new ActionListener() {
  107. public void actionPerformed(ActionEvent e) {
  108. resetBtnClick();
  109. }
  110. });
  111. resetBtn.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
  112. resetBtn.setFont(new Font("微软雅黑", Font.PLAIN, btnFont));
  113. resetBtn.setBounds(margin + btnW + btnGap, y, btnW, hBtn);
  114. this.add(resetBtn);
  115. y += hBtn + gap;
  116. sllabel1 = new JLabel("上料允许");
  117. sllabel1.setHorizontalAlignment(SwingConstants.CENTER);
  118. sllabel1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  119. sllabel1.setForeground(Color.BLACK);
  120. sllabel1.setBounds(0, y, w / 2, hLab);
  121. this.add(sllabel1);
  122. sllabel2 = new JLabel("下料允许");
  123. sllabel2.setHorizontalAlignment(SwingConstants.CENTER);
  124. sllabel2.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
  125. sllabel2.setForeground(Color.BLACK);
  126. sllabel2.setBounds(w / 2, y, w - w / 2, hLab);
  127. this.add(sllabel2);
  128. this.startOprnoTimer();
  129. }
  130. // 扫码按钮
  131. public void scanBtnClick() {
  132. if (work_status != 0) {
  133. status_menu.setText("工件加工中,请勿重复扫码");
  134. return;
  135. }
  136. try {
  137. String sn = "";
  138. boolean hgA = MesClient.s7PLC.readBoolean(oprnoClass.getHgA_out());
  139. boolean hgB = MesClient.s7PLC.readBoolean(oprnoClass.getHgB_out());
  140. if (!hgA && !hgB) {
  141. sn = "";
  142. }
  143. if (hgA) {
  144. sn = MesClient.s7PLC.readString(oprnoClass.getHgA_sn());
  145. }else if (hgB) {
  146. sn = MesClient.s7PLC.readString(oprnoClass.getHgB_sn());
  147. }else {
  148. sn = MesClient.s7PLC.readString(oprnoClass.getProduct_sn());
  149. }
  150. if (sn == null || sn.trim().isEmpty() || "".equals(sn)) {
  151. setMenuStatus("扫码结果为空", -1);
  152. return;
  153. }
  154. sn = sn.trim();
  155. productSn.setText(sn);
  156. work_status = 1;
  157. setMenuStatus("已读取工件码,等待校验", 0);
  158. } catch (Exception e) {
  159. log.error("读取 PLC 工件码失败", e);
  160. setMenuStatus("读取 PLC 失败", -1);
  161. }
  162. }
  163. // 定时任务
  164. public Timer oprnoTimer;
  165. public void startOprnoTimer() {
  166. if(oprnoTimer!=null) {
  167. oprnoTimer.cancel();
  168. }
  169. oprnoTimer = new Timer();
  170. oprnoTimer.schedule(new TimerTask() {
  171. public void run() {
  172. if (work_status == 0){
  173. // 未扫码,等待允许扫码信号
  174. // 当遍历出有上料点位为true的工位时
  175. if (MesClient.s7PLC.readBoolean(oprnoClass.getHgA_out()) || MesClient.s7PLC.readBoolean(oprnoClass.getHgB_out())) {
  176. MesClient.s7PLC.writeBoolean(oprnoClass.getMes_out(),false);
  177. String sn = "";
  178. boolean hgA = MesClient.s7PLC.readBoolean(oprnoClass.getHgA_out());
  179. boolean hgB = MesClient.s7PLC.readBoolean(oprnoClass.getHgB_out());
  180. if (hgA) {
  181. // 获取A号料
  182. sn = MesClient.s7PLC.readString(oprnoClass.getHgA_sn());
  183. }
  184. if (hgB) {
  185. sn = MesClient.s7PLC.readString(oprnoClass.getHgB_sn());
  186. }
  187. if (sn == null || sn.trim().isEmpty() || "".equals(sn)){
  188. setMenuStatus("扫码结果为空", -1);
  189. return;
  190. }
  191. productSn.setText(sn);
  192. work_status = 1;
  193. }
  194. }else if (work_status == 1){
  195. // 已扫码,等待允许加工信号
  196. String sn = productSn.getText();
  197. if (sn == null || sn.trim().isEmpty() || "".equals(sn)) {
  198. setMenuStatus("读码结果为空", -1);
  199. productSn.setText("");
  200. work_status = 0;
  201. return;
  202. }
  203. Boolean checkQualityResult = DataUtil.checkQualityByGw(MesClient.nettyClient, sn,MesClient.user20,oprnoClass.getOprno());
  204. if (!checkQualityResult){
  205. setMenuStatus("消息发送失败,请重试",-1);
  206. }
  207. }else if (work_status == 2){
  208. if (MesClient.s7PLC.readBoolean(oprnoClass.getOut_ok())){
  209. MesClient.s7PLC.writeBoolean(oprnoClass.getMes_in(),false);
  210. }
  211. if (MesClient.s7PLC.readBoolean(oprnoClass.getProcess_ok())){
  212. MesClient.s7PLC.writeBoolean(oprnoClass.getMes_out(),true);
  213. String sn = productSn.getText();
  214. String ret = "OK";
  215. if (MesClient.s7PLC.readBoolean(oprnoClass.getProcess_result_ng())){
  216. ret = "NG";
  217. }
  218. Boolean result = DataUtil.sendQualityByGW(MesClient.nettyClient, sn, ret, MesClient.user20, oprnoClass.getOprno());
  219. if (!result){
  220. setMenuStatus("消息发送失败,请重试",-1);
  221. }
  222. }
  223. }else if (work_status == 3){
  224. productSn.setText("");
  225. work_status = 0;
  226. }
  227. }
  228. },100,1000);
  229. }
  230. // 刷新
  231. public void resetBtnClick() {
  232. productSn.setText("");
  233. work_status = 0;
  234. setMenuStatus("请扫码",0);
  235. }
  236. public void setMenuStatus(String msg,int error){
  237. if(error == 0){
  238. this.status_menu.setForeground(Color.GREEN);
  239. }else{
  240. this.status_menu.setForeground(Color.RED);
  241. }
  242. this.status_menu.setText(msg);
  243. }
  244. }