package org.springblade.mdm.machinefile.filewatch; import lombok.extern.slf4j.Slf4j; import org.springblade.core.tool.utils.SpringUtil; import org.springblade.mdm.basesetting.machine.entity.Machine; import org.springblade.mdm.machinefile.entity.FileMonitorRecord; import org.springblade.mdm.machinefile.entity.MachineFile; import org.springblade.mdm.machinefile.service.FileMonitorRecordService; import org.springblade.mdm.machinefile.service.MachineFileScanService; import org.springblade.mdm.machinefile.service.MachineFileService; import org.springblade.mdm.program.service.ProgramAnnotationService; import java.io.IOException; import java.nio.file.Path; import java.util.Date; @Slf4j public class MachineFileChangeListener implements FileWatcherService.FileChangeListener{ private final MachineFileService machineFileService; ProgramAnnotationService programAnnotationService; private final Machine machine; private final String dirType; public MachineFileChangeListener(MachineFileService aMachineFileService, ProgramAnnotationService annotationService, WatchInfo info) { this.machineFileService = aMachineFileService; this.machine = info.getMachine(); this.dirType = info.getDirType(); this.programAnnotationService = annotationService; } @Override public void onFileCreated(Path filePath) { try { log.info("文件创建{},mcode={},dieType={}",filePath,machine.getCode(),dirType); if(this.waitingForFileComplete(filePath)){ log.info("{}传输完成Create,开始处理",filePath); MachineFileScanService fileScanService = SpringUtil.getBean(MachineFileScanService.class); MachineFile mf = fileScanService.readFileToMachineFile(filePath,this.machine,this.dirType); machineFileService.refreshFileData(mf); }else{ //0928新增:文件传输没完成 log.info("{}传输未完成Created,不处理",filePath); } saveMonitor(filePath,FileMonitorRecord.EVENT_CREATE); } catch (InterruptedException e) { log.error("sleep出错",e); } catch (IOException e) { log.error("机床文件监控created异常",e); }catch (Exception e) { log.error("机床回传文件处理异常create",e); } } /** * 等待文件传输完成(检测20次) * @param filePath 文件Path对象 * @return 最终是否传输完成 * @throws IOException 文件检测问题 * @throws InterruptedException 线程等待可能引发的异常 */ boolean waitingForFileComplete(Path filePath) throws IOException, InterruptedException { final int maxWaitTimes = 20; //文件传输完成检测次数 boolean isCompleted = false; for(int i=0;i