WorkTimer.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package com.mes.util;
  2. import com.mes.ui.DataUtil;
  3. import com.mes.ui.MesClient;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import java.util.Timer;
  7. import java.util.TimerTask;
  8. public class WorkTimer {
  9. private static final Logger log = LoggerFactory.getLogger(WorkTimer.class);
  10. // 工位1
  11. private static Timer stopTimer1 = null;
  12. private static Timer workTimer1 = null;
  13. public static ATEQ ateq1 = new ATEQ(new SerialUtil(Config.portName1), Config.gw_1);
  14. // 工位2
  15. private static Timer stopTimer2 = null;
  16. private static Timer workTimer2 = null;
  17. public static ATEQ ateq2 = new ATEQ(new SerialUtil(Config.portName2), Config.gw_2);
  18. private WorkTimer() {
  19. }
  20. public static void start() {
  21. // 工位1
  22. // if (stopTimer1 == null) {
  23. // stopTimer1 = new Timer();
  24. // stopTimer1.schedule(new StopTimerTask1(), 1000, 1000);
  25. // }
  26. if (workTimer1 == null) {
  27. workTimer1 = new Timer();
  28. workTimer1.schedule(new WorkTimerTask1(), 1000, 2000);
  29. }
  30. // 工位2
  31. // if (stopTimer2 == null) {
  32. // stopTimer2 = new Timer();
  33. // stopTimer2.schedule(new StopTimerTask2(), 1000, 1000);
  34. // }
  35. if (workTimer2 == null) {
  36. workTimer2 = new Timer();
  37. workTimer2.schedule(new WorkTimerTask2(), 1000, 2000);
  38. }
  39. }
  40. public static void stop() {
  41. // 工位1
  42. if (stopTimer1 != null) {
  43. stopTimer1.cancel();
  44. stopTimer1 = null;
  45. }
  46. if (workTimer1 != null) {
  47. workTimer1.cancel();
  48. workTimer1 = null;
  49. }
  50. // 工位2
  51. if (stopTimer2 != null) {
  52. stopTimer2.cancel();
  53. stopTimer2 = null;
  54. }
  55. if (workTimer2 != null) {
  56. workTimer2.cancel();
  57. workTimer2 = null;
  58. }
  59. }
  60. // 工位1发送禁止运行的定时任务
  61. private static class StopTimerTask1 extends TimerTask {
  62. @Override
  63. public void run() {
  64. try {
  65. if (MesClient.work_status1 == 0 || !MesClient.check_quality_result1) {
  66. ateq1.stop(); // 发送重置命令, 禁止设备启动
  67. }
  68. } catch (Exception e) {
  69. log.error("{} 向设备发送禁止运行的定时任务报错", Config.gw_1, e);
  70. }
  71. }
  72. }
  73. // 工位1工作流程定时任务
  74. private static class WorkTimerTask1 extends TimerTask {
  75. @Override
  76. public void run() {
  77. try {
  78. if (MesClient.work_status1 == 1 && MesClient.check_quality_result1) {
  79. ATEQ.RealtimeResult realtimeResult = ateq1.readRealtimeResult(); // 循环查询实时结果
  80. log.info("{}: {}", Config.gw_1, realtimeResult.toString());
  81. // 实时更新显示的压力值和泄漏值
  82. Boolean isEnd = realtimeResult.isCycleEnd;
  83. MesClient.pressureText_1.setText(isEnd ? "" : realtimeResult.pressureValue + " " + realtimeResult.pressureUnit);
  84. MesClient.leakText_1.setText(isEnd ? "" : realtimeResult.leakValue + " " + realtimeResult.leakUnit);
  85. MesClient.setMenuState_1(isEnd ? "该工件可以加工" : "正在加工", 0);
  86. if (realtimeResult.isCycleEnd && realtimeResult.FIFO_Number > 0) { // 如果查询到测试结束并且待处理结果数大于0
  87. ATEQ.LastResult lastResult = ateq1.readLastResult();
  88. log.info("{}: {}", Config.gw_1, lastResult.toString());
  89. String ret = lastResult.isPass ? "OK" : "NG";
  90. String pressureValue = String.valueOf(lastResult.pressureValue);
  91. String pressureString = pressureValue + " " + realtimeResult.pressureUnit;
  92. String leakValue;
  93. String leakString;
  94. if(Math.abs(lastResult.leakValue) < 1e-10 && !lastResult.isPass) {
  95. leakValue = "气压过低";
  96. leakString = leakValue;
  97. } else {
  98. leakValue = String.valueOf(lastResult.leakValue);
  99. leakString = leakValue + " " + realtimeResult.leakUnit;
  100. }
  101. MesClient.pressureText_1.setText(pressureString);
  102. MesClient.leakText_1.setText(leakString);
  103. String sn = MesClient.getBarcode33(MesClient.product_sn_1.getText());
  104. Boolean result = false;
  105. if(lastResult.leakValue < 1e-10 && lastResult.leakValue > -1e-10 && lastResult.isPass == true) {
  106. MesClient.setMenuState_1("泄露值为0,请重测", 0);
  107. MesClient.resetScanA();
  108. } else {
  109. result = DataUtil.sendQuality(sn, ret, MesClient.user20, Config.gw_1);
  110. }
  111. // 获取当前运行的程序的填充时间、稳定时间、测试时间
  112. ATEQ.Parameters params = ateq1.getParams(lastResult.programNumber);
  113. log.info("{}: {}", Config.gw_1, params.toString());
  114. if (result) {
  115. MesClient.setMenuState_1("加工完成,结果:" + ret, 0);
  116. DataUtil.bindLeakValue(sn, leakString, Config.gw_1);
  117. // 将气密参数放入本地SQLite数据库
  118. QMParamsDAO.insert(Config.gw_1, sn.trim(), lastResult.programNumber, ret, pressureValue, leakValue, lastResult.pressureUnit, lastResult.leakUnit, params.fillTime, params.stabilizeTime, params.testTime, MesClient.user20 );//添加作业人员
  119. // 刷新工作面板
  120. MesClient.resetScanA();
  121. }
  122. }
  123. }
  124. } catch (Exception e) {
  125. log.error("{} 工作定时任务报错", Config.gw_1, e);
  126. }
  127. }
  128. }
  129. // 工位2发送禁止运行的定时任务
  130. private static class StopTimerTask2 extends TimerTask {
  131. @Override
  132. public void run() {
  133. try {
  134. if (MesClient.work_status2 == 0 || !MesClient.check_quality_result2) {
  135. ateq2.stop(); // 发送重置命令, 禁止设备启动
  136. }
  137. } catch (Exception e) {
  138. log.error("{} 向设备发送禁止运行的定时任务报错", Config.gw_2);
  139. }
  140. }
  141. }
  142. private static class WorkTimerTask2 extends TimerTask {
  143. @Override
  144. public void run() {
  145. try {
  146. if (MesClient.work_status2 == 1 && MesClient.check_quality_result2) {
  147. ATEQ.RealtimeResult realtimeResult = ateq2.readRealtimeResult(); // 循环查询实时结果
  148. log.info("{}: {}", Config.gw_2, realtimeResult.toString());
  149. // 实时更新显示的压力值和泄漏值
  150. Boolean isEnd = realtimeResult.isCycleEnd;
  151. MesClient.pressureText_2.setText(isEnd ? "" : realtimeResult.pressureValue + " " + realtimeResult.pressureUnit);
  152. MesClient.leakText_2.setText(isEnd ? "" : realtimeResult.leakValue + " " + realtimeResult.leakUnit);
  153. MesClient.setMenuState_2(isEnd ? "该工件可以加工" : "正在加工", 0);
  154. if (realtimeResult.isCycleEnd && realtimeResult.FIFO_Number > 0) { // 如果查询到测试结束并且待处理结果数大于0
  155. ATEQ.LastResult lastResult = ateq2.readLastResult();
  156. log.info("{}: {}", Config.gw_2, lastResult.toString());
  157. String ret = lastResult.isPass ? "OK" : "NG";
  158. String pressureValue = String.valueOf(lastResult.pressureValue);
  159. String pressureString = pressureValue + " " + realtimeResult.pressureUnit;
  160. String leakValue;
  161. String leakString;
  162. if(Math.abs(lastResult.leakValue) < 1e-10 && !lastResult.isPass) {
  163. leakValue = "气压过低";
  164. leakString = leakValue;
  165. } else {
  166. leakValue = String.valueOf(lastResult.leakValue);
  167. leakString = leakValue + " " + realtimeResult.leakUnit;
  168. }
  169. MesClient.pressureText_2.setText(pressureString);
  170. MesClient.leakText_2.setText(leakString);
  171. String sn = MesClient.getBarcode33(MesClient.product_sn_2.getText());
  172. Boolean result = DataUtil.sendQuality(sn, ret, MesClient.user20, Config.gw_2);
  173. // 获取当前运行的程序的填充时间、稳定时间、测试时间
  174. ATEQ.Parameters params = ateq2.getParams(lastResult.programNumber);
  175. log.info("{}: {}", Config.gw_2, params.toString());
  176. if (result) {
  177. MesClient.setMenuState_2("加工完成,结果:" + ret, 0);
  178. DataUtil.bindLeakValue(sn, leakString, Config.gw_2);
  179. // 将气密参数放入本地SQLite数据库
  180. QMParamsDAO.insert(Config.gw_2, sn.trim(), lastResult.programNumber, ret, pressureValue, leakValue, lastResult.pressureUnit, lastResult.leakUnit, params.fillTime, params.stabilizeTime, params.testTime, MesClient.user20);
  181. // 刷新工作面板
  182. MesClient.resetScanB();
  183. } else {
  184. MesClient.setMenuState_2("结果提交失败,请重试", -1);
  185. }
  186. }
  187. }
  188. } catch (Exception e) {
  189. log.error("{} 工作定时任务报错", Config.gw_2);
  190. }
  191. }
  192. }
  193. }