package com.mes.util; import com.mes.ui.DataUtil; import com.mes.ui.MesClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Timer; import java.util.TimerTask; public class WorkTimer { private static final Logger log = LoggerFactory.getLogger(WorkTimer.class); // 工位1 private static Timer stopTimer1 = null; private static Timer workTimer1 = null; public static ATEQ ateq1 = new ATEQ(new SerialUtil(Config.portName1), Config.gw_1); // 工位2 private static Timer stopTimer2 = null; private static Timer workTimer2 = null; public static ATEQ ateq2 = new ATEQ(new SerialUtil(Config.portName2), Config.gw_2); private WorkTimer() { } public static void start() { // 工位1 // if (stopTimer1 == null) { // stopTimer1 = new Timer(); // stopTimer1.schedule(new StopTimerTask1(), 1000, 1000); // } if (workTimer1 == null) { workTimer1 = new Timer(); workTimer1.schedule(new WorkTimerTask1(), 1000, 2000); } // 工位2 // if (stopTimer2 == null) { // stopTimer2 = new Timer(); // stopTimer2.schedule(new StopTimerTask2(), 1000, 1000); // } if (workTimer2 == null) { workTimer2 = new Timer(); workTimer2.schedule(new WorkTimerTask2(), 1000, 2000); } } public static void stop() { // 工位1 if (stopTimer1 != null) { stopTimer1.cancel(); stopTimer1 = null; } if (workTimer1 != null) { workTimer1.cancel(); workTimer1 = null; } // 工位2 if (stopTimer2 != null) { stopTimer2.cancel(); stopTimer2 = null; } if (workTimer2 != null) { workTimer2.cancel(); workTimer2 = null; } } // 工位1发送禁止运行的定时任务 private static class StopTimerTask1 extends TimerTask { @Override public void run() { try { if (MesClient.work_status1 == 0 || !MesClient.check_quality_result1) { ateq1.stop(); // 发送重置命令, 禁止设备启动 } } catch (Exception e) { log.error("{} 向设备发送禁止运行的定时任务报错", Config.gw_1, e); } } } // 工位1工作流程定时任务 private static class WorkTimerTask1 extends TimerTask { @Override public void run() { try { if (MesClient.work_status1 == 1 && MesClient.check_quality_result1) { ATEQ.RealtimeResult realtimeResult = ateq1.readRealtimeResult(); // 循环查询实时结果 log.info("{}: {}", Config.gw_1, realtimeResult.toString()); // 实时更新显示的压力值和泄漏值 Boolean isEnd = realtimeResult.isCycleEnd; MesClient.pressureText_1.setText(isEnd ? "" : realtimeResult.pressureValue + " " + realtimeResult.pressureUnit); MesClient.leakText_1.setText(isEnd ? "" : realtimeResult.leakValue + " " + realtimeResult.leakUnit); MesClient.setMenuState_1(isEnd ? "该工件可以加工" : "正在加工", 0); if (realtimeResult.isCycleEnd && realtimeResult.FIFO_Number > 0) { // 如果查询到测试结束并且待处理结果数大于0 ATEQ.LastResult lastResult = ateq1.readLastResult(); log.info("{}: {}", Config.gw_1, lastResult.toString()); String ret = lastResult.isPass ? "OK" : "NG"; String pressureValue = String.valueOf(lastResult.pressureValue); String pressureString = pressureValue + " " + realtimeResult.pressureUnit; String leakValue; String leakString; if(Math.abs(lastResult.leakValue) < 1e-10 && !lastResult.isPass) { leakValue = "气压过低"; leakString = leakValue; } else { leakValue = String.valueOf(lastResult.leakValue); leakString = leakValue + " " + realtimeResult.leakUnit; } MesClient.pressureText_1.setText(pressureString); MesClient.leakText_1.setText(leakString); String sn = MesClient.getBarcode33(MesClient.product_sn_1.getText()); Boolean result = false; if(lastResult.leakValue < 1e-10 && lastResult.leakValue > -1e-10 && lastResult.isPass == true) { MesClient.setMenuState_1("泄露值为0,请重测", 0); MesClient.resetScanA(); } else { result = DataUtil.sendQuality(sn, ret, MesClient.user20, Config.gw_1); } // 获取当前运行的程序的填充时间、稳定时间、测试时间 ATEQ.Parameters params = ateq1.getParams(lastResult.programNumber); log.info("{}: {}", Config.gw_1, params.toString()); if (result) { MesClient.setMenuState_1("加工完成,结果:" + ret, 0); DataUtil.bindLeakValue(sn, leakString, Config.gw_1); // 将气密参数放入本地SQLite数据库 QMParamsDAO.insert(Config.gw_1, sn.trim(), lastResult.programNumber, ret, pressureValue, leakValue, lastResult.pressureUnit, lastResult.leakUnit, params.fillTime, params.stabilizeTime, params.testTime, MesClient.user20 );//添加作业人员 // 刷新工作面板 MesClient.resetScanA(); } } } } catch (Exception e) { log.error("{} 工作定时任务报错", Config.gw_1, e); } } } // 工位2发送禁止运行的定时任务 private static class StopTimerTask2 extends TimerTask { @Override public void run() { try { if (MesClient.work_status2 == 0 || !MesClient.check_quality_result2) { ateq2.stop(); // 发送重置命令, 禁止设备启动 } } catch (Exception e) { log.error("{} 向设备发送禁止运行的定时任务报错", Config.gw_2); } } } private static class WorkTimerTask2 extends TimerTask { @Override public void run() { try { if (MesClient.work_status2 == 1 && MesClient.check_quality_result2) { ATEQ.RealtimeResult realtimeResult = ateq2.readRealtimeResult(); // 循环查询实时结果 log.info("{}: {}", Config.gw_2, realtimeResult.toString()); // 实时更新显示的压力值和泄漏值 Boolean isEnd = realtimeResult.isCycleEnd; MesClient.pressureText_2.setText(isEnd ? "" : realtimeResult.pressureValue + " " + realtimeResult.pressureUnit); MesClient.leakText_2.setText(isEnd ? "" : realtimeResult.leakValue + " " + realtimeResult.leakUnit); MesClient.setMenuState_2(isEnd ? "该工件可以加工" : "正在加工", 0); if (realtimeResult.isCycleEnd && realtimeResult.FIFO_Number > 0) { // 如果查询到测试结束并且待处理结果数大于0 ATEQ.LastResult lastResult = ateq2.readLastResult(); log.info("{}: {}", Config.gw_2, lastResult.toString()); String ret = lastResult.isPass ? "OK" : "NG"; String pressureValue = String.valueOf(lastResult.pressureValue); String pressureString = pressureValue + " " + realtimeResult.pressureUnit; String leakValue; String leakString; if(Math.abs(lastResult.leakValue) < 1e-10 && !lastResult.isPass) { leakValue = "气压过低"; leakString = leakValue; } else { leakValue = String.valueOf(lastResult.leakValue); leakString = leakValue + " " + realtimeResult.leakUnit; } MesClient.pressureText_2.setText(pressureString); MesClient.leakText_2.setText(leakString); String sn = MesClient.getBarcode33(MesClient.product_sn_2.getText()); Boolean result = DataUtil.sendQuality(sn, ret, MesClient.user20, Config.gw_2); // 获取当前运行的程序的填充时间、稳定时间、测试时间 ATEQ.Parameters params = ateq2.getParams(lastResult.programNumber); log.info("{}: {}", Config.gw_2, params.toString()); if (result) { MesClient.setMenuState_2("加工完成,结果:" + ret, 0); DataUtil.bindLeakValue(sn, leakString, Config.gw_2); // 将气密参数放入本地SQLite数据库 QMParamsDAO.insert(Config.gw_2, sn.trim(), lastResult.programNumber, ret, pressureValue, leakValue, lastResult.pressureUnit, lastResult.leakUnit, params.fillTime, params.stabilizeTime, params.testTime, MesClient.user20); // 刷新工作面板 MesClient.resetScanB(); } else { MesClient.setMenuState_2("结果提交失败,请重试", -1); } } } } catch (Exception e) { log.error("{} 工作定时任务报错", Config.gw_2); } } } }