| | |
| | | import org.springblade.mdm.commons.service.ParamService; |
| | | import org.springblade.mdm.machinefile.entity.MachineFile; |
| | | import org.springblade.mdm.program.service.ProgramAnnotationService; |
| | | import org.springblade.mdm.program.service.programannotation.AnnotationUtil; |
| | | import org.springblade.mdm.utils.FileContentUtil; |
| | | import org.springblade.system.pojo.entity.DictBiz; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | |
| | | byte[] buffer = new byte[2048]; |
| | | |
| | | //MachineFile existFileInDb; |
| | | |
| | | List<DictBiz> annotatiionList = programAnnotationService.getAnnotionDictList(); |
| | | //boolean exists;//文件是否存在于数据库中 |
| | | for (Path filePath : files) { |
| | | //existFileInDb = null; |
| | | //exists = false; |
| | | |
| | | /*MachineFile mf = new MachineFile(); |
| | | mf.setTenantId("000000"); |
| | | mf.setName(filePath.toFile().getName()); |
| | | mf.setDirType(dirType); |
| | | mf.setMachineCode(machine.getCode());*/ |
| | | |
| | | try { |
| | | //读取内容,确定程序状态 |
| | | /*int pstatus = readProgramStatus(filePath,machine.getMachineGroupCode(),annotatiionList); |
| | | mf.setProgramStatus(pstatus); |
| | | BasicFileAttributes attrs = Files.readAttributes( |
| | | filePath, |
| | | BasicFileAttributes.class |
| | | ); |
| | | |
| | | FileTime creationTime = attrs.creationTime(); |
| | | Date creationDate = new Date(creationTime.toMillis()); |
| | | mf.setFileCreateTime(creationDate); |
| | | |
| | | FileTime modifyTime = attrs.lastModifiedTime(); |
| | | mf.setFileModifyTime(new Date(modifyTime.toMillis())); |
| | | mf.setFileSize(Files.size(filePath)); |
| | | try (InputStream inputStream = Files.newInputStream(filePath)) { |
| | | // 使用输入流读取文件内容 |
| | | int bytesRead = inputStream.read(buffer); |
| | | mf.setMd5(DigestUtils.md5Hex(buffer)); |
| | | } catch (IOException e) { |
| | | log.error("读取文件md5失败",e); |
| | | continue;//有错误,掠过 |
| | | }*/ |
| | | |
| | | MachineFile mf = readFileToMachineFile(filePath,machine,dirType,annotatiionList); |
| | | |
| | | machineFileService.refreshFileData(mf); |
| | | /* |
| | | existFileInDb = machineFileService.getExistsFile(mf.getName(),dirType,machine.getCode()); |
| | | System.out.println("文件创建时间: " + mf.getFileCreateTime()); |
| | | |
| | | if(existFileInDb == null) { |
| | | machineFileService.save(mf); |
| | | }else{ |
| | | 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); |
| | | |
| | | }*/ |
| | | } catch (IOException e) { |
| | | log.error("读取文件信息失败",e); |
| | | } |
| | |
| | | /** |
| | | * 读取文件中的程序状态 |
| | | * @param filePath 文件路径 |
| | | * @param machineGroupCode |
| | | * @param controlSystem |
| | | * @param annotatiionList 注释配置字典 |
| | | * @return 状态的整数之和 |
| | | * @throws IOException |
| | | */ |
| | | int readProgramStatus(Path filePath,String machineGroupCode,List<DictBiz> annotatiionList) throws IOException { |
| | | int readProgramStatus(Path filePath,String controlSystem,List<DictBiz> annotatiionList) throws IOException { |
| | | int programStatus = MachineFile.PROGRAM_STATUS_UNKNOWN; |
| | | try(InputStream ins = Files.newInputStream(filePath); |
| | | BufferedInputStream bis = new BufferedInputStream(ins)){ |
| | |
| | | } |
| | | bis.reset(); |
| | | String statusLine = FileContentUtil.readLineAt(ins,ProgramAnnotationService.STATUS_LINE_INDEX); |
| | | if(programAnnotationService.isAnnotation(statusLine,machineGroupCode,annotatiionList)){ |
| | | String progStatusTxt = programAnnotationService.removeAnnotation(machineGroupCode,statusLine,annotatiionList); |
| | | if(AnnotationUtil.isAnnotation(statusLine,controlSystem,annotatiionList)){ |
| | | String progStatusTxt = programAnnotationService.removeAnnotation(controlSystem,statusLine,annotatiionList); |
| | | if(ProgramAnnotationService.SQ.equals(progStatusTxt)){ |
| | | programStatus = MachineFile.PROGRAM_STATUS_TRY; |
| | | }else if(ProgramAnnotationService.GH.equals(progStatusTxt)){ |