yangys
2025-09-06 61763cc1a182effa7e5535ac72d54a6a56f029eb
blade-service/blade-mdm/src/main/java/org/springblade/mdm/task/MachineFileScanTask.java
@@ -1,28 +1,16 @@
package org.springblade.mdm.task;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.mdm.basesetting.machine.MachineService;
import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.gkw.programnode.entity.MachineFile;
import org.springblade.mdm.gkw.programnode.service.MachineFileService;
import org.springblade.mdm.basesetting.machine.service.MachineService;
import org.springblade.mdm.commons.service.ParamService;
import org.springblade.mdm.machinefile.service.MachineFileScanService;
import org.springblade.mdm.machinefile.service.MachineFileService;
import org.springblade.mdm.program.service.ProgramAnnotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.Date;
import java.util.List;
@Slf4j
@Component
@@ -32,56 +20,73 @@
   private MachineFileService machineFileService;
   @Autowired
   private MachineService machineService;
   @Autowired
   private ParamService paramService;
   @Autowired
   private ProgramAnnotationService programAnnotationService;
   @Autowired
   private MachineFileScanService machineFileScanService;
   // 每5秒执行一次
   //@Scheduled(fixedRate = 1000000)
   //@Scheduled(cron = "0 1 0 * * ?") // 每天上午0点1分执行
   @Scheduled(cron = "0 15 19 * * ?") //test
   public void executeEvery5Seconds() {
      scanMachineFile();
   //@Scheduled(cron = "0 */5 * * * ?")
   //@Scheduled(cron = "0 15 19 * * ?") //test
   @Scheduled(cron = "${task.cron.machine_file_scan:0 1 * * * ?}")
   public void execute() {
      String networkType = paramService.getParamValue(ParamService.NETWORK_TYPE,ParamService.NETWORK_TYPE_SHEMI);
      if(!ParamService.NETWORK_TYPE_SHEMI.equals(networkType)){
         //非涉密网,才扫描目录文件
         machineFileScanService.scanMachineFile();
      }
   }
   /**
    * 扫描所有机床的配置之目录
    */
   /*
   public void scanMachineFile() {
      List<Machine> machines = machineService.lambdaQuery().eq(Machine::getStatus,Machine.STATUS_ENABLE).list();
      for (Machine machine : machines) {
         //扫描新闻界
         //扫描新文件
         try {
            scanDir(machine,MachineFile.DIR_TYPE_REC);
         }catch(Exception e) {
            log.error("REC扫描文件异常,机床"+machine.getCode(),e);
            log.error("REC扫描文件异常,机床{}", machine.getCode(), e);
         }
         try {
            scanDir(machine,MachineFile.DIR_TYPE_SEND);
         }catch(Exception e) {
            log.error("SEND扫描文件异常,机床"+machine.getCode(),e);
            log.error("SEND扫描文件异常,机床{}", machine.getCode(), e);
         }
         try {
            scanDir(machine,MachineFile.DIR_TYPE_TEMP);
         }catch(Exception e) {
            log.error("TEMP扫描文件异常,机床"+machine.getCode(),e);
            log.error("TEMP扫描文件异常,机床{}", machine.getCode(), e);
         }
         //清理数据库,删除文件不存在的数据
         try {
            clearDeletedReccords(machine,MachineFile.DIR_TYPE_REC);
         }catch(Exception e) {
            log.error("REC扫描文件异常,机床"+machine.getCode(),e);
            log.error("REC扫描文件异常,机床{}", machine.getCode(), e);
         }
         try {
            clearDeletedReccords(machine,MachineFile.DIR_TYPE_SEND);
         }catch(Exception e) {
            log.error("SEND扫描文件异常,机床"+machine.getCode(),e);
            log.error("SEND扫描文件异常,机床{}", machine.getCode(), e);
         }
         try {
            clearDeletedReccords(machine,MachineFile.DIR_TYPE_TEMP);
         }catch(Exception e) {
            log.error("TEMP扫描文件异常,机床"+machine.getCode(),e);
            log.error("TEMP扫描文件异常,机床{}", machine.getCode(), e);
         }
      }
   }
   */
   /**
    * 扫描目录
@@ -89,6 +94,7 @@
    * @param dirType 目录类型
    * @throws IOException
    */
   /*
   public void scanDir(Machine machine,String dirType) throws IOException {
      String basePath = MachineFileService.getBasePath(machine,dirType);
      if(basePath == null) {
@@ -106,10 +112,13 @@
      byte[] buffer = new byte[2048];
      boolean exists;//文件是否存在于数据库中
      MachineFile existFileInDb;
      List<DictBiz> annotatiionList = programAnnotationService.getAnnotionDictList();
      //boolean exists;//文件是否存在于数据库中
      for (Path filePath : files) {
         exists = false;
         existFileInDb = null;
         //exists = false;
         MachineFile mf = new MachineFile();
@@ -119,6 +128,9 @@
         mf.setMachineCode(machine.getCode());
         try {
            //读取内容,确定程序状态
            int pstatus = readProgramStatus(filePath,machine.getMachineGroupCode(),annotatiionList);
            mf.setProgramStatus(pstatus);
            BasicFileAttributes attrs = Files.readAttributes(
               filePath,
               BasicFileAttributes.class
@@ -129,7 +141,7 @@
            mf.setFileCreateTime(creationDate);
            FileTime modifyTime = attrs.lastModifiedTime();
            mf.setFileModifyDate(new Date(modifyTime.toMillis()));
            mf.setFileModifyTime(new Date(modifyTime.toMillis()));
            mf.setFileSize(Files.size(filePath));
            try (InputStream inputStream = Files.newInputStream(filePath)) {
               // 使用输入流读取文件内容
@@ -140,28 +152,73 @@
               continue;//有错误,掠过
            }
            exists = machineFileService.fileExists(mf.getName(),mf.getMd5(),machine.getCode());
            //exists = machineFileService.fileExists(mf.getName(),dirType,machine.getCode());
            existFileInDb = machineFileService.getExistsFile(mf.getName(),dirType,machine.getCode());
            System.out.println("文件创建时间: " + creationDate);
         } catch (IOException e) {
            log.error("读取文件信息失败",e);
         }
         if(!exists) {
         if(existFileInDb == null) {
            machineFileService.save(mf);
         }else{
            log.info("文件已如果掠过:{}",filePath.getFileName());
            log.info("文件已存在掠过:{}",filePath.getFileName());
            //已存在咋整。
            existFileInDb.setFileSize(mf.getFileSize());
            existFileInDb.setProgramStatus(mf.getProgramStatus());
            existFileInDb.setFileCreateTime(mf.getFileCreateTime());
            existFileInDb.setFileModifyTime(mf.getFileModifyTime());
            if(!StringUtils.equals(existFileInDb.getMd5(),mf.getMd5())){
               //文件内容发生变化了,设置状态为初始状态
               existFileInDb.setStatus(MachineFile.STATUS_NORMAL);
            }
            existFileInDb.setMd5(mf.getMd5());
            machineFileService.updateById(existFileInDb);
         }
      }
   }
   */
   /**
    * 读取文件中的程序状态
    * @param filePath 文件路径
    * @param machineGroupCode
    * @param annotatiionList 注释配置字典
    * @return 状态的整数之和
    * @throws IOException
    */
   /*
   int readProgramStatus(Path filePath,String machineGroupCode,List<DictBiz> annotatiionList) throws IOException {
      int programStatus = MachineFile.PROGRAM_STATUS_UNKNOWN;
      try(InputStream ins = Files.newInputStream(filePath)){
         String statusLine = FileContentUtil.readLineAt(ins,ProgramAnnotationService.STATUS_LINE_INDEX);
         if(programAnnotationService.isAnnotation(statusLine,machineGroupCode,annotatiionList)){
            String progStatusTxt = programAnnotationService.removeAnnotation(machineGroupCode,statusLine,annotatiionList);
            if(ProgramAnnotationService.SQ.equals(progStatusTxt)){
               programStatus = MachineFile.PROGRAM_STATUS_TRY;
            }else if(ProgramAnnotationService.GH.equals(progStatusTxt)){
               programStatus = MachineFile.PROGRAM_STATUS_CURED;
            }else if(ProgramAnnotationService.PL.equals(progStatusTxt)){
               programStatus = MachineFile.PROGRAM_STATUS_DIVIASTION;
            }
         }
      }
      return programStatus;
   }
   */
   /**
    * 清理已经删除了文件的记录
    * @param machine
    * @param dirType
    * @throws IOException
    */
   /*
   void clearDeletedReccords(Machine machine,String dirType) throws IOException {
      List<MachineFile> machineFiles =  this.machineFileService.lambdaQuery().eq(MachineFile::getDirType,dirType)
         .eq(MachineFile::getMachineCode,machine.getCode()).list();
@@ -172,6 +229,10 @@
         return;
      }
      Path dirPath = Paths.get(basePath);
      if(!dirPath.toFile().exists()){
         log.warn("扫描目录:{} 不存在",dirPath);
         return;
      }
      List<Path> files = Files.list(dirPath)
         .filter(Files::isRegularFile).toList();
@@ -180,10 +241,9 @@
         long findCount = files.stream().filter(filePath -> filePath.toFile().getName().equals(mf.getName())).count();
         if(findCount == 0){
            //文件夹内没找到,需要删除记录
            //this.machineFileService.save(mf);
            mf.markFileDeleted();
            this.machineFileService.updateById(mf);
         }
      }
   }
   }*/
}