WorkTimer.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.mes.util;
  2. import com.mes.ui.MesClient;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import java.awt.*;
  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. private static Timer stopTimer = null;
  11. private static Timer workTimer = null;
  12. private WorkTimer() {
  13. }
  14. public static void start() {
  15. if(workTimer != null){
  16. workTimer = null;
  17. }
  18. workTimer = new Timer();
  19. workTimer.schedule(new WorkTimerTask(), 1000, 2000);
  20. }
  21. public static void stop() {
  22. if (workTimer != null) {
  23. workTimer.cancel();
  24. workTimer = null;
  25. }
  26. }
  27. // 发送禁止运行的定时任务
  28. private static class StopTimerTask extends TimerTask {
  29. @Override
  30. public void run() {
  31. try {
  32. if (MesClient.work_status == 0 || !MesClient.check_quality_result) {
  33. ATEQ.stop(); // 发送重置命令, 禁止设备启动
  34. }
  35. } catch (Exception e) {
  36. log.error("向设备发送禁止运行的定时任务报错");
  37. }
  38. }
  39. }
  40. // 工作流程定时任务
  41. private static class WorkTimerTask extends TimerTask {
  42. @Override
  43. public void run() {
  44. try { //01031a0000 0800 0100 0080 0700 2e180000e02e00000000000038c7000058b2
  45. // 如果开始工作,则循环查询实时结果
  46. if (MesClient.work_status == 1) {
  47. ATEQ.RealtimeResult realtimeResult = ATEQ.readRealtimeResult(); // 循环查询实时结果
  48. log.info(realtimeResult.toString());
  49. Boolean isEnd = false;
  50. if(realtimeResult.stepCode == 65535){ // 监测结束
  51. if(MesClient.mesStartFlag == 1){
  52. isEnd = true;
  53. }
  54. }else{
  55. // 新中间步骤:开启本轮;上一轮已入库则开新一轮
  56. if(MesClient.mesStartFlag == 0 || MesClient.mesTjFlag == 1){
  57. MesClient.mesStartFlag = 1;
  58. MesClient.mesTjFlag = 0;
  59. }
  60. MesClient.setMenuStatus("当前步骤:"+realtimeResult.stepCode+":"+realtimeResult.step, 0); // 当前步骤
  61. }
  62. // 实时更新显示的压力值和泄漏值
  63. MesClient.param1.setText(isEnd ? "" : realtimeResult.pressureValue + " " + realtimeResult.pressureUnit);
  64. MesClient.param2.setText(isEnd ? "" : realtimeResult.leakValue + " " + realtimeResult.leakUnit);
  65. // 如果从实时结果中得到到测试结束,则查询最后结果,并提交结果
  66. if (isEnd && MesClient.mesTjFlag == 0) {
  67. MesClient.mesTjFlag = 1;
  68. ATEQ.LastResult lastResult = ATEQ.readLastResult();
  69. log.info(lastResult.toString());
  70. MesClient.param1.setText(lastResult.pressureValue+lastResult.pressureUnit);
  71. if(lastResult.leakValue == 0 && !lastResult.isPass){
  72. MesClient.param2.setText("9999");
  73. }else{
  74. MesClient.param2.setText(lastResult.leakValue+lastResult.leakUnit);
  75. }
  76. String lastRet = lastResult.isPass ? "OK" : "NG";
  77. if(lastRet.equals("OK")){
  78. MesClient.result.setText("OK");
  79. MesClient.result.setForeground(Color.GREEN);
  80. }else{
  81. MesClient.result.setText("NG");
  82. MesClient.result.setForeground(Color.RED);
  83. }
  84. String sn = MesClient.product_sn.getText().trim();
  85. TestParam testParam = new TestParam();
  86. testParam.setSn(sn);
  87. testParam.setParam1(lastResult.pressureValue + "");
  88. testParam.setParam2(lastResult.pressureUnit);
  89. if(lastResult.leakValue == 0 && !lastResult.isPass){
  90. testParam.setParam3("9999");
  91. }else{
  92. testParam.setParam3(lastResult.leakValue + "");
  93. }
  94. testParam.setParam4(lastResult.leakUnit);
  95. testParam.setResult(lastRet);
  96. testParam.setRemark("");
  97. MesClient.fillMesTestMeta(testParam);
  98. MesClient.afterTestSaveAndUpload(testParam);
  99. MesClient.scan_type = 1;
  100. MesClient.scanBarcode();
  101. }
  102. }
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. log.error("工作定时任务报错");
  106. }
  107. }
  108. }
  109. }