yangys
2025-09-29 4c7296d45efe849dc70a3b2e2240c905481a91c9
blade-service/blade-mdm/src/main/java/org/springblade/mdm/machinefile/filewatch/MachineFileChangeListener.java
@@ -1,6 +1,5 @@
package org.springblade.mdm.machinefile.filewatch;
import com.alibaba.excel.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.mdm.basesetting.machine.entity.Machine;
@@ -11,23 +10,16 @@
import org.springblade.mdm.machinefile.service.MachineFileService;
import org.springblade.mdm.program.service.ProgramAnnotationService;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@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) {
@@ -41,10 +33,15 @@
      try {
         log.info("文件创建{},mcode={},dieType={}",filePath,machine.getCode(),dirType);
         if(FileLockChecker.isFileComplete(filePath)){
         if(this.waitingForFileComplete(filePath)){
            log.info("{}传输完成Create,开始处理",filePath);
            MachineFileScanService fileScanService = SpringUtil.getBean(MachineFileScanService.class);
            MachineFile mf = fileScanService.readFileToMachineFile(filePath,this.machine,this.dirType,programAnnotationService.getAnnotionDictList());
            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) {
@@ -56,16 +53,34 @@
      }
   }
   /**
    * 等待文件传输完成(检测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<maxWaitTimes;i++){
         isCompleted = FileLockChecker.isFileComplete(filePath);
         if(isCompleted){
            break;
         }
      }
      return isCompleted;
   }
   @Override
   public void onFileModified(Path filePath) {
      //文件修改
      log.info("文件修改:{}",filePath);
      try {
         if(FileLockChecker.isFileComplete(filePath)){
            log.info("文件传输完成{}",filePath);
         if(waitingForFileComplete(filePath)){
            log.info("文件传输完成Modify{}",filePath);
            MachineFileScanService fileScanService = SpringUtil.getBean(MachineFileScanService.class);
            MachineFile mf = fileScanService.readFileToMachineFile(filePath,this.machine,this.dirType,programAnnotationService.getAnnotionDictList());
            MachineFile mf = fileScanService.readFileToMachineFile(filePath,this.machine,this.dirType);
            machineFileService.refreshFileData(mf);
         }else{
            log.warn("文件传输中,后续再操作{}",filePath);