读取远程共享文件并监听

This commit is contained in:
汤锦科 2024-12-05 14:23:46 +08:00
parent ecfad3d012
commit dab1cc8caa
3 changed files with 189 additions and 24 deletions

View File

@ -124,28 +124,29 @@ public class BaseKnifeServiceImpl implements IBaseKnifeService {
hashMap.put("wmStorageAreaCode", wmsOutPlanDetail.getWmStorageAreaCode()); // 库位编码
hashMap.put("wmStorageAreaName", wmsOutPlanDetail.getWmStorageAreaName()); // 库位名称
hashMap.put("detailStatus", wmsOutPlanDetail.getDetailState()); // 明细状态
hashMap.put("isToolSetting", 0); // 是否对刀
hashMap.put("isToolSetting", nowWmsOutPlanDetailEntity.getIsToolSetting()); // 是否对刀
hashMap.put("toolSettingParam", nowWmsOutPlanDetailEntity.getToolSettingParam()); // 对刀参数
// 读取xml配置文件
if ("PRODUCT".equals(nowWmsOutPlanDetailEntity.getItemOrProduct())) {
try {
// 抓取远程文件
this.getRemoteFile();
// 读取本地文件
File xmlFile = new File(myConfig.getToolSettingXmlPath() + CalendarUtil.getDateTimeStrForFile() + "/TSP1203.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Tools.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Tools paramXml = (Tools) unmarshaller.unmarshal(xmlFile);
hashMap.put("isToolSetting", 1); // 是否对刀
hashMap.put("toolSettingParam", JSON.toJSONString(paramXml)); // 对刀参数
} catch (Exception e) {
System.out.println("系统找不到指定文件!");
// throw new RuntimeException(e);
}
}
// // 读取xml配置文件
// if ("PRODUCT".equals(nowWmsOutPlanDetailEntity.getItemOrProduct())) {
// try {
// // 抓取远程文件
// this.getRemoteFile();
//
// // 读取本地文件
// File xmlFile = new File(myConfig.getToolSettingXmlPath() + CalendarUtil.getDateTimeStrForFile() + "/TSP1203.xml");
// JAXBContext jaxbContext = JAXBContext.newInstance(Tools.class);
// Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
// Tools paramXml = (Tools) unmarshaller.unmarshal(xmlFile);
//
// hashMap.put("isToolSetting", 1); // 是否对刀
// hashMap.put("toolSettingParam", JSON.toJSONString(paramXml)); // 对刀参数
//
// } catch (Exception e) {
// System.out.println("系统找不到指定文件!");
//// throw new RuntimeException(e);
// }
// }
/* 实体 */
String outTime = null;

View File

@ -1,6 +1,8 @@
package com.ktg.mes.md.service.impl;
import com.alibaba.fastjson.JSON;
import com.ktg.common.utils.DateUtils;
import com.ktg.mes.cal.utils.CalendarUtil;
import com.ktg.mes.md.domain.*;
import com.ktg.mes.md.mapper.*;
import com.ktg.mes.md.service.IWmsOutTaskService;
@ -9,6 +11,9 @@ import com.ktg.mes.wm.domain.WmsZdTask;
import com.ktg.mes.wm.mapper.WmStorageAreaMapper;
import com.ktg.mes.wm.mapper.WmsZdTaskMapper;
import com.ktg.mes.wm.service.IWmStorageAreaService;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -16,7 +21,13 @@ import org.springframework.transaction.annotation.Transactional;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.*;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@ -222,7 +233,9 @@ public class WmsOutTaskServiceImpl implements IWmsOutTaskService {
1
);
// this.toolSetting(wmsOutPlanDetailEntity);
// 获取台账实体如果为整刀则开始读取对刀仪数据
BaseKnife baseKnife = baseKnifeMapper.selectBaseKnifeByBaseKnifeId(wmsOutPlanDetailEntity.getBaseKnifeId());
if (Objects.equals("PRODUCT", baseKnife.getItemOrProduct())) this.readToolSettingRemoteFile(wmsOutPlanDetailEntity);
}
// 更新出库任务状态
@ -260,6 +273,157 @@ public class WmsOutTaskServiceImpl implements IWmsOutTaskService {
return true;
}
public void readToolSettingRemoteFile(WmsOutPlanDetailEntity wmsOutPlanDetailEntity) {
// 当前时间
String currentDate = CalendarUtil.getDateTimeStr();
// 配置每十秒执行扫描文件
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
try {
// 创建认证用户
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", myConfig.getRemoteUser(), myConfig.getRemotePass());
// 构建远程文件对象
SmbFile remoteFile = new SmbFile(myConfig.getRemoteUrl(), auth);
// 尝试链接
remoteFile.connect();
if(remoteFile.exists()){
// 获取共享文件夹中文件列表
SmbFile[] smbFiles = remoteFile.listFiles();
for (SmbFile smbFile : smbFiles){
if (smbFile.isFile()){
// Path path = Paths.get(smbFile.getPath()); // 根据文件的绝对路径获取文件
// BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class); // 根据path获取文件的基本属性
// FileTime fileTime = attributes.creationTime(); // 从基本属性中获取文件的创建时间
// long millis = fileTime.toMillis(); // 将创建时间转换成毫秒
// 时间戳比较
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (dateFormat.parse(currentDate).getTime() < smbFile.lastModified()){
// 停止扫描
this.cancel();
// 保存到本地
createFile(smbFile);
// 读取本地文件
File xmlFile = new File(myConfig.getToolSettingXmlPath() + CalendarUtil.getDateTimeStrForFile() + "/" + smbFile.getName());
JAXBContext jaxbContext = JAXBContext.newInstance(Tools.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Tools paramXml = (Tools) unmarshaller.unmarshal(xmlFile);
// 更新对刀参数
wmsOutPlanDetailEntity.setIsToolSetting(1);
wmsOutPlanDetailEntity.setToolSettingParam(JSON.toJSONString(paramXml));
wmsOutPlanDetailEntityMapper.updateWmsOutPlanDetailEntity(wmsOutPlanDetailEntity);
}
}
}
} else {
throw new RuntimeException("远程共享文件夹不存在或无法连接!");
}
} catch (Exception e){
throw new RuntimeException(e);
}
}
};
timer.schedule(task, 0, 10000);
}
/**
* 创建本地文件
*/
public void createFile(SmbFile remoteFile) {
InputStream in = null;
OutputStream out = null;
try {
// 创建本地文件
// File localFile = new File("E:/xml_wjj/" + CalendarUtil.getDateTimeStrForFile() + "/" + remoteFile.getName());
File localFile = new File(myConfig.getToolSettingXmlPath() + CalendarUtil.getDateTimeStrForFile() + "/" + remoteFile.getName());
if (!localFile.getParentFile().exists()) localFile.getParentFile().mkdirs();
if (!localFile.exists()) localFile.createNewFile();
else {
localFile.delete();
localFile.createNewFile();
}
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
// 获取远程输出流
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[4096];
int len = 0;
// 写入
while ((len = in.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
out.flush(); // 刷新缓冲
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (out != null) out.close();
if (in != null) in.close();
} catch (Exception e){
System.out.println("==============START=================");
System.out.println("关闭资源发生错误!");
System.out.println("===============END==================");
}
}
}
public void readToolSettingFile(BaseKnife baseKnife) {
// 当前时间
String currentDate = CalendarUtil.getDateTimeStr();
// 配置每十秒执行扫描文件
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
File folder = new File(myConfig.getRemoteUrl());
File[] files = folder.listFiles();
for (File file : files){
if (file.isFile()) {
try {
// 根据文件的绝对路径获取文件
Path path = Paths.get(file.getAbsolutePath());
// 根据path获取文件的基本属性
BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
// 从基本属性中获取文件的创建时间
FileTime fileTime = attributes.creationTime();
// 将创建时间转换成毫秒
long millis = fileTime.toMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
date.setTime(millis);
// 毫秒转换成时间字符串
String fileCreateTime = dateFormat.format(date);
// 时间戳比较
if (dateFormat.parse(currentDate).getTime() < dateFormat.parse(fileCreateTime).getTime()){
System.out.println("文件创建时间:" + fileCreateTime);
System.out.println("文件名:" + file.getName());
// 停止扫描
this.cancel();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
};
timer.schedule(task, 0, 10000);
}
public void wmsZdTaskFinish(String planCode){
WmsZdTask wmsZdTaskWrapper = new WmsZdTask();
wmsZdTaskWrapper.setCode(planCode);

View File

@ -92,7 +92,7 @@ public class WmsInPlanServiceImpl implements IWmsInPlanService {
public int insertWmsInPlan(WmsInPlan wmsInPlan) {
wmsInPlan.setCreateTime(DateUtils.getNowDate());
// 设置计划编码
wmsInPlan.setPlanCode(MultiModuleCodeGenerator.generateTaskCode("RKP"));
if (wmsInPlan.getPlanCode() == null) wmsInPlan.setPlanCode(MultiModuleCodeGenerator.generateTaskCode("RKP"));
int rows = wmsInPlanMapper.insertWmsInPlan(wmsInPlan);
insertWmsInPlanDetails(wmsInPlan);
return rows;