package com.mes.util; import com.mes.ui.MesClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.awt.*; import java.util.Timer; import java.util.TimerTask; public class WorkTimer { private static final Logger log = LoggerFactory.getLogger(WorkTimer.class); private static Timer stopTimer = null; private static Timer workTimer = null; private WorkTimer() { } public static void start() { if(workTimer != null){ workTimer = null; } workTimer = new Timer(); workTimer.schedule(new WorkTimerTask(), 1000, 2000); } public static void stop() { if (workTimer != null) { workTimer.cancel(); workTimer = null; } } // 发送禁止运行的定时任务 private static class StopTimerTask extends TimerTask { @Override public void run() { try { if (MesClient.work_status == 0 || !MesClient.check_quality_result) { ATEQ.stop(); // 发送重置命令, 禁止设备启动 } } catch (Exception e) { log.error("向设备发送禁止运行的定时任务报错"); } } } // 工作流程定时任务 private static class WorkTimerTask extends TimerTask { @Override public void run() { try { //01031a0000 0800 0100 0080 0700 2e180000e02e00000000000038c7000058b2 // 如果开始工作,则循环查询实时结果 if (MesClient.work_status == 1) { ATEQ.RealtimeResult realtimeResult = ATEQ.readRealtimeResult(); // 循环查询实时结果 log.info(realtimeResult.toString()); Boolean isEnd = false; if(realtimeResult.stepCode == 65535){ // 监测结束 if(MesClient.mesStartFlag == 1){ isEnd = true; } }else{ // 新中间步骤:开启本轮;上一轮已入库则开新一轮 if(MesClient.mesStartFlag == 0 || MesClient.mesTjFlag == 1){ MesClient.mesStartFlag = 1; MesClient.mesTjFlag = 0; } MesClient.setMenuStatus("当前步骤:"+realtimeResult.stepCode+":"+realtimeResult.step, 0); // 当前步骤 } // 实时更新显示的压力值和泄漏值 MesClient.param1.setText(isEnd ? "" : realtimeResult.pressureValue + " " + realtimeResult.pressureUnit); MesClient.param2.setText(isEnd ? "" : realtimeResult.leakValue + " " + realtimeResult.leakUnit); // 如果从实时结果中得到到测试结束,则查询最后结果,并提交结果 if (isEnd && MesClient.mesTjFlag == 0) { MesClient.mesTjFlag = 1; ATEQ.LastResult lastResult = ATEQ.readLastResult(); log.info(lastResult.toString()); MesClient.param1.setText(lastResult.pressureValue+lastResult.pressureUnit); if(lastResult.leakValue == 0 && !lastResult.isPass){ MesClient.param2.setText("9999"); }else{ MesClient.param2.setText(lastResult.leakValue+lastResult.leakUnit); } String lastRet = lastResult.isPass ? "OK" : "NG"; if(lastRet.equals("OK")){ MesClient.result.setText("OK"); MesClient.result.setForeground(Color.GREEN); }else{ MesClient.result.setText("NG"); MesClient.result.setForeground(Color.RED); } String sn = MesClient.product_sn.getText().trim(); TestParam testParam = new TestParam(); testParam.setSn(sn); testParam.setParam1(lastResult.pressureValue + ""); testParam.setParam2(lastResult.pressureUnit); if(lastResult.leakValue == 0 && !lastResult.isPass){ testParam.setParam3("9999"); }else{ testParam.setParam3(lastResult.leakValue + ""); } testParam.setParam4(lastResult.leakUnit); testParam.setResult(lastRet); testParam.setRemark(""); MesClient.fillMesTestMeta(testParam); MesClient.afterTestSaveAndUpload(testParam); MesClient.scan_type = 1; MesClient.scanBarcode(); } } } catch (Exception e) { e.printStackTrace(); log.error("工作定时任务报错"); } } } }