GunPanel.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package com.mes.component;
  2. import com.mes.ui.MesClient;
  3. import com.mes.ui.YgslUtil;
  4. import com.mes.util.JdbcUtils;
  5. import com.mes.ygsl.YgslClient;
  6. import com.mes.ygsl.YgslConfig;
  7. import com.mes.ygsl.YgslParam;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.util.List;
  11. import java.util.Timer;
  12. import java.util.TimerTask;
  13. public class GunPanel extends JPanel {
  14. private YgslConfig config;
  15. private int gunIndex;
  16. private int currentCount = 0;
  17. private int taskCount = 0;
  18. private boolean isOnline = false;
  19. private boolean isConnected = false;
  20. private YgslClient gunClient;
  21. private Timer heartBeatTimer;
  22. private List<String> tighteningIds;
  23. private JLabel nameLabel;
  24. private JLabel statusLabel;
  25. private JLabel scheduleLabel;
  26. private JLabel torqueLabel;
  27. private JLabel angleLabel;
  28. private OnTighteningDataListener dataListener;
  29. private OnTaskCompletedListener taskCompletedListener;
  30. private OnStatusChangeListener statusChangeListener;
  31. public interface OnTighteningDataListener {
  32. void onTighteningData(GunPanel panel, String tighteningStatus, String torque, String angle,
  33. String tighteningID, String jobID, String pos);
  34. }
  35. public interface OnTaskCompletedListener {
  36. void onTaskCompleted(GunPanel panel);
  37. }
  38. public interface OnStatusChangeListener {
  39. void onStatusChanged(GunPanel panel, boolean isOnline);
  40. }
  41. public GunPanel(YgslConfig config) {
  42. this.config = config;
  43. this.gunIndex = config.getGunIndex();
  44. this.taskCount = config.getTaskCount();
  45. this.tighteningIds = new java.util.ArrayList<>();
  46. initUI();
  47. }
  48. private void initUI() {
  49. setLayout(null);
  50. setPreferredSize(new Dimension(230, 140));
  51. setOpaque(false);
  52. JLabel titleLabel = new JLabel(config.getGunName());
  53. titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
  54. titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
  55. titleLabel.setForeground(Color.BLACK);
  56. titleLabel.setBounds(0, 5, 160, 28);
  57. add(titleLabel);
  58. statusLabel = new JLabel("离线");
  59. statusLabel.setHorizontalAlignment(SwingConstants.CENTER);
  60. statusLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
  61. statusLabel.setBounds(165, 8, 60, 22);
  62. statusLabel.setForeground(Color.RED);
  63. add(statusLabel);
  64. JLabel scheduleTitleLabel = new JLabel("进度:");
  65. scheduleTitleLabel.setHorizontalAlignment(SwingConstants.RIGHT);
  66. scheduleTitleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  67. scheduleTitleLabel.setForeground(Color.BLACK);
  68. scheduleTitleLabel.setBounds(10, 40, 60, 25);
  69. add(scheduleTitleLabel);
  70. scheduleLabel = new JLabel("0/" + taskCount);
  71. scheduleLabel.setHorizontalAlignment(SwingConstants.LEFT);
  72. scheduleLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
  73. scheduleLabel.setForeground(Color.BLACK);
  74. scheduleLabel.setBounds(75, 40, 90, 25);
  75. add(scheduleLabel);
  76. JLabel torqueTitleLabel = new JLabel("扭矩:");
  77. torqueTitleLabel.setHorizontalAlignment(SwingConstants.RIGHT);
  78. torqueTitleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  79. torqueTitleLabel.setForeground(Color.BLACK);
  80. torqueTitleLabel.setBounds(10, 75, 60, 25);
  81. add(torqueTitleLabel);
  82. torqueLabel = new JLabel("");
  83. torqueLabel.setHorizontalAlignment(SwingConstants.LEFT);
  84. torqueLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
  85. torqueLabel.setForeground(Color.BLACK);
  86. torqueLabel.setBounds(75, 75, 100, 25);
  87. add(torqueLabel);
  88. JLabel angleTitleLabel = new JLabel("角度:");
  89. angleTitleLabel.setHorizontalAlignment(SwingConstants.RIGHT);
  90. angleTitleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  91. angleTitleLabel.setForeground(Color.BLACK);
  92. angleTitleLabel.setBounds(10, 110, 60, 25);
  93. add(angleTitleLabel);
  94. angleLabel = new JLabel("");
  95. angleLabel.setHorizontalAlignment(SwingConstants.LEFT);
  96. angleLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
  97. angleLabel.setForeground(Color.BLACK);
  98. angleLabel.setBounds(75, 110, 100, 25);
  99. add(angleLabel);
  100. }
  101. public void connect() {
  102. if (gunClient != null) {
  103. return;
  104. }
  105. gunClient = new YgslClient(config.getIpAddress(), config.getPort(), gunIndex);
  106. gunClient.setGunPanel(this);
  107. try {
  108. gunClient.run();
  109. isConnected = true;
  110. startHeartBeatTimer();
  111. } catch (Exception e) {
  112. e.printStackTrace();
  113. isConnected = false;
  114. scheduleReconnect();
  115. }
  116. }
  117. public void disconnect() {
  118. stopHeartBeatTimer();
  119. if (gunClient != null) {
  120. gunClient.close();
  121. gunClient = null;
  122. }
  123. isConnected = false;
  124. isOnline = false;
  125. updateStatusUI();
  126. }
  127. private void scheduleReconnect() {
  128. Timer reconnectTimer = new Timer();
  129. reconnectTimer.schedule(new TimerTask() {
  130. @Override
  131. public void run() {
  132. if (!isConnected) {
  133. connect();
  134. }
  135. }
  136. }, 3000);
  137. }
  138. private void startHeartBeatTimer() {
  139. stopHeartBeatTimer();
  140. heartBeatTimer = new Timer();
  141. heartBeatTimer.schedule(new TimerTask() {
  142. @Override
  143. public void run() {
  144. if (gunClient != null && isConnected) {
  145. if (isOnline) {
  146. YgslUtil.comHeart(gunClient);
  147. } else {
  148. scheduleReconnect();
  149. }
  150. }
  151. updateStatusUI();
  152. }
  153. }, 1000, 3000);
  154. }
  155. private void stopHeartBeatTimer() {
  156. if (heartBeatTimer != null) {
  157. heartBeatTimer.cancel();
  158. heartBeatTimer = null;
  159. }
  160. }
  161. public void setOnline(boolean online) {
  162. this.isOnline = online;
  163. updateStatusUI();
  164. if (statusChangeListener != null) {
  165. statusChangeListener.onStatusChanged(this, online);
  166. }
  167. }
  168. private void updateStatusUI() {
  169. SwingUtilities.invokeLater(() -> {
  170. if (isOnline) {
  171. statusLabel.setText("在线");
  172. statusLabel.setForeground(Color.GREEN);
  173. } else {
  174. statusLabel.setText("离线");
  175. statusLabel.setForeground(Color.RED);
  176. }
  177. });
  178. }
  179. public void processTighteningData(String content) {
  180. if (MesClient.work_status != 1) {
  181. YgslUtil.lastTighteningResultDataAcknowledge(gunClient);
  182. return;
  183. }
  184. String tighteningStatus = com.mes.ygsl.YgslParam.getTighteningStatus(content);
  185. String torqueStatus = com.mes.ygsl.YgslParam.getTorqueStatus(content);
  186. String angleStatus = com.mes.ygsl.YgslParam.getAngleStatus(content);
  187. String torque = com.mes.ygsl.YgslParam.getTorque(content);
  188. String angle = com.mes.ygsl.YgslParam.getAngle(content);
  189. String tighteningID = com.mes.ygsl.YgslParam.getTighteningID(content);
  190. String jobID = com.mes.ygsl.YgslParam.getJobID(content);
  191. String torqueMin = YgslParam.getTorqueMinLimit(content);
  192. String torqueMax = YgslParam.getTorqueMaxLimit(content);
  193. String torqueFinal = YgslParam.getTorqueFinalTarget(content);
  194. String angleMin = YgslParam.getAngleMin(content);
  195. String angleMax = YgslParam.getAngleMax(content);
  196. String angleFinal = YgslParam.getFinalAngleTarget(content);
  197. if (tighteningID.isEmpty() || tighteningIds.contains(tighteningID)) {
  198. YgslUtil.lastTighteningResultDataAcknowledge(gunClient);
  199. return;
  200. }
  201. tighteningIds.add(tighteningID);
  202. String pos = String.valueOf(gunIndex);
  203. Boolean checkRet = JdbcUtils.checkTighteningById(tighteningID, pos, jobID);
  204. if (!checkRet) {
  205. if (tighteningStatus.equals("0")) {
  206. if (torqueStatus.equals("1")) {
  207. torqueLabel.setForeground(Color.BLACK);
  208. } else {
  209. torqueLabel.setForeground(Color.RED);
  210. }
  211. if (angleStatus.equals("1")) {
  212. angleLabel.setForeground(Color.BLACK);
  213. } else {
  214. angleLabel.setForeground(Color.RED);
  215. }
  216. } else {
  217. torqueLabel.setForeground(Color.BLACK);
  218. angleLabel.setForeground(Color.BLACK);
  219. if (tighteningStatus.equals("1")) {
  220. currentCount++;
  221. }
  222. }
  223. final int displayCount = tighteningStatus.equals("1") ? currentCount : currentCount + 1;
  224. SwingUtilities.invokeLater(() -> {
  225. torqueLabel.setText(torque);
  226. angleLabel.setText(angle);
  227. scheduleLabel.setText(displayCount + "/" + taskCount);
  228. });
  229. JdbcUtils.insertTighteningData(
  230. MesClient.mes_gw,MesClient.mes_line_sn,MesClient.product_sn.getText(),
  231. tighteningStatus,torqueStatus,angleStatus,torqueMin,torqueMax,torqueFinal,torque,
  232. angleMin,angleMax,angleFinal,angle,tighteningID,jobID,"",
  233. pos,MesClient.user_menu.getText()
  234. );
  235. if (currentCount >= taskCount) {
  236. disableTool();
  237. if (taskCompletedListener != null) {
  238. taskCompletedListener.onTaskCompleted(this);
  239. }
  240. }
  241. }
  242. YgslUtil.lastTighteningResultDataAcknowledge(gunClient);
  243. if (dataListener != null) {
  244. dataListener.onTighteningData(this, tighteningStatus, torque, angle, tighteningID, jobID, pos);
  245. }
  246. }
  247. public void enableTool() {
  248. if (gunClient != null && isOnline) {
  249. YgslUtil.enableTool(gunClient);
  250. }
  251. }
  252. public void disableTool() {
  253. if (gunClient != null && isOnline) {
  254. YgslUtil.disableTool(gunClient);
  255. }
  256. }
  257. public void pSet() {
  258. System.out.println("GunPanel[" + gunIndex + "] pSet called, isOnline=" + isOnline + ", gunClient=" + (gunClient != null));
  259. if (gunClient != null && isOnline) {
  260. YgslUtil.pSet(gunClient);
  261. } else {
  262. System.err.println("GunPanel[" + gunIndex + "] pSet skipped - not online!");
  263. }
  264. }
  265. public void startJob() {
  266. System.out.println("GunPanel[" + gunIndex + "] startJob called, isOnline=" + isOnline + ", gunClient=" + (gunClient != null));
  267. if (gunClient != null && isOnline) {
  268. YgslUtil.selectJob(gunClient);
  269. } else {
  270. System.err.println("GunPanel[" + gunIndex + "] startJob skipped - not online!");
  271. }
  272. }
  273. public void reset() {
  274. currentCount = 0;
  275. tighteningIds.clear();
  276. SwingUtilities.invokeLater(() -> {
  277. torqueLabel.setText("");
  278. angleLabel.setText("");
  279. scheduleLabel.setText("0/" + taskCount);
  280. torqueLabel.setForeground(Color.BLACK);
  281. angleLabel.setForeground(Color.BLACK);
  282. });
  283. }
  284. public boolean isTaskCompleted() {
  285. return currentCount >= taskCount;
  286. }
  287. public int getCurrentCount() {
  288. return currentCount;
  289. }
  290. public int getTaskCount() {
  291. return taskCount;
  292. }
  293. public boolean isOnline() {
  294. return isOnline;
  295. }
  296. public YgslConfig getConfig() {
  297. return config;
  298. }
  299. public int getGunIndex() {
  300. return gunIndex;
  301. }
  302. public YgslClient getGunClient() {
  303. return gunClient;
  304. }
  305. public int getAutoSubmit() {
  306. return config.getAutoSubmit();
  307. }
  308. public void setDataListener(OnTighteningDataListener listener) {
  309. this.dataListener = listener;
  310. }
  311. public void setTaskCompletedListener(OnTaskCompletedListener listener) {
  312. this.taskCompletedListener = listener;
  313. }
  314. public void setStatusChangeListener(OnStatusChangeListener listener) {
  315. this.statusChangeListener = listener;
  316. }
  317. public void destroy() {
  318. disconnect();
  319. }
  320. public void updateConfig(YgslConfig newConfig) {
  321. this.config = newConfig;
  322. this.taskCount = newConfig.getTaskCount();
  323. SwingUtilities.invokeLater(() -> {
  324. scheduleLabel.setText(currentCount + "/" + taskCount);
  325. });
  326. }
  327. }