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<maxWaitTimes;i++){
|
isCompleted = FileLockChecker.isFileComplete(filePath);
|
if(isCompleted){
|
break;
|
}
|
}
|
return isCompleted;
|
}
|
|
@Override
|
public void onFileModified(Path filePath) {
|
//文件修改
|
log.info("文件修改:{}",filePath);
|
try {
|
if(waitingForFileComplete(filePath)){
|
log.info("文件传输完成Modify{}",filePath);
|
MachineFileScanService fileScanService = SpringUtil.getBean(MachineFileScanService.class);
|
MachineFile mf = fileScanService.readFileToMachineFile(filePath,this.machine,this.dirType);
|
machineFileService.refreshFileData(mf);
|
}else{
|
log.warn("文件传输中,后续再操作{}",filePath);
|
}
|
} catch (IOException e) {
|
log.error("回传文件操作IO错误",e);
|
} catch (InterruptedException e) {
|
log.error("sleep出错",e);
|
} catch (Exception e) {
|
log.error("机床回传文件处理异常",e);
|
}
|
}
|
|
@Override
|
public void onFileDeleted(Path filePath) {
|
String path = filePath.toString();
|
log.info("文件删除{}",filePath);
|
|
try {
|
|
String name = filePath.getFileName().toString();
|
|
saveMonitor(filePath,FileMonitorRecord.EVENT_DELETE);
|
|
MachineFile fileInDb = machineFileService.getExistsFile(name,this.dirType,this.machine.getCode());
|
if(fileInDb != null){
|
fileInDb.markFileDeleted();
|
machineFileService.updateById(fileInDb);
|
}else{
|
log.info("数据库内不存在文件{}",filePath);
|
}
|
} catch (Exception e) {
|
log.error("机床回传文件onDelete处理异常",e);
|
}
|
}
|
|
void saveMonitor(Path filePath,int eventType){
|
FileMonitorRecordService fileMonitorRecordService = SpringUtil.getBean(FileMonitorRecordService.class);
|
FileMonitorRecord monitorRecord = new FileMonitorRecord();
|
monitorRecord.setName(filePath.getFileName().toString());
|
monitorRecord.setDirPath(filePath.getParent().toString());
|
|
monitorRecord.setEventType(eventType);
|
monitorRecord.setCreateTime(new Date());
|
fileMonitorRecordService.save(monitorRecord);
|
}
|
}
|