WorkTimer.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. MesClient.mesStartFlag = 1;
  56. MesClient.setMenuStatus("当前步骤:["+realtimeResult.stepCode+"]"+realtimeResult.step, 0); // 当前步骤
  57. }
  58. // 实时更新显示的压力值和泄漏值
  59. MesClient.param1.setText(isEnd ? "" : realtimeResult.pressureValue + " " + realtimeResult.pressureUnit);
  60. MesClient.param2.setText(isEnd ? "" : realtimeResult.leakValue + " " + realtimeResult.leakUnit);
  61. // 如果从实时结果中得到到测试结束,则查询最后结果,并提交结果
  62. if (isEnd) {
  63. ATEQ.LastResult lastResult = ATEQ.readLastResult();
  64. log.info(lastResult.toString());
  65. MesClient.param1.setText(lastResult.pressureValue+lastResult.pressureUnit);
  66. if(lastResult.leakValue == 0 && !lastResult.isPass){
  67. MesClient.param2.setText("9999");
  68. }else{
  69. MesClient.param2.setText(lastResult.leakValue+lastResult.leakUnit);
  70. }
  71. String lastRet = lastResult.isPass ? "OK" : "NG";
  72. if(lastRet.equals("OK")){
  73. MesClient.result.setText("OK");
  74. MesClient.result.setForeground(Color.GREEN);
  75. }else{
  76. MesClient.result.setText("NG");
  77. MesClient.result.setForeground(Color.RED);
  78. }
  79. String testDate = DateLocalUtils.getCurrentTime();
  80. TestParam testParam = new TestParam();
  81. testParam.setSn(MesClient.product_sn.getText().trim());
  82. testParam.setUcode(MesClient.user_menu.getText().trim());
  83. testParam.setLineSn(MesClient.getEffectiveLineSn());
  84. testParam.setOprno(MesClient.getEffectiveOprno());
  85. testParam.setOprnoTitle(MesClient.configParam.getOprnoTitle());
  86. testParam.setParam1(lastResult.pressureValue + "");
  87. testParam.setParam2(lastResult.pressureUnit);
  88. testParam.setParam3(lastResult.leakValue+"");
  89. testParam.setParam4(lastResult.leakUnit);
  90. testParam.setParam5(MesClient.programNo.getText().trim());
  91. testParam.setResult(lastRet);
  92. testParam.setDeviceType("ateq");
  93. testParam.setRemark("");
  94. testParam.setCreateTime(testDate);
  95. MesClient.afterTestSaveAndUpload(testParam);
  96. }
  97. }
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. log.error("工作定时任务报错:"+e.getMessage());
  101. }
  102. }
  103. }
  104. }