|
@@ -1,467 +0,0 @@
|
|
|
-package com.jiayue.ipfcst.fileupload.IEC102.shandong;
|
|
|
-
|
|
|
-import cn.hutool.core.map.MapUtil;
|
|
|
-import cn.hutool.core.util.ArrayUtil;
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
-import com.jiayue.ipfcst.common.data.constant.enums.FileStatusEnum;
|
|
|
-import com.jiayue.ipfcst.common.data.constant.enums.FileTypeEnum;
|
|
|
-import com.jiayue.ipfcst.common.data.entity.UploadFileChannel;
|
|
|
-import com.jiayue.ipfcst.common.data.entity.UploadFileLog;
|
|
|
-import com.jiayue.ipfcst.common.data.entity.UploadObject;
|
|
|
-import com.jiayue.ipfcst.common.data.repository.UploadFileLogRepository;
|
|
|
-import com.jiayue.ipfcst.fileupload.util.*;
|
|
|
-import io.netty.channel.ChannelHandlerContext;
|
|
|
-import org.apache.commons.lang.StringUtils;
|
|
|
-import org.apache.commons.lang.time.DateFormatUtils;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Propagation;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-import java.io.*;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.LinkedHashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-/**
|
|
|
- * 山东业务处理类
|
|
|
- *
|
|
|
- * @author xsl
|
|
|
- * @version 3.0
|
|
|
- */
|
|
|
-@Service
|
|
|
-public class ShanDongHandlerService extends SdBase102Service {
|
|
|
- @Autowired
|
|
|
- UploadFileLogRepository uploadFileLogRepository;
|
|
|
-
|
|
|
- @Transactional(propagation = Propagation.REQUIRED)
|
|
|
- public String handlerMessage(String receiveMessage, UploadObject uploadObject, UploadFileChannel uploadFileChannel, Logger log, ChannelHandlerContext ctx) {
|
|
|
- String[] rmArray = IEC102Uitl.stingToArray(receiveMessage);
|
|
|
- String sendMessage = "";
|
|
|
- String sendCtrlChinese = "";
|
|
|
- String receiveCtrlChinese = "";
|
|
|
-
|
|
|
- // 判断ASDU类型
|
|
|
- if ("FF".equals(rmArray[5])) {
|
|
|
- receiveCtrlChinese = "心跳";
|
|
|
- // 心跳
|
|
|
- sendMessage = "6800000000FF0000";
|
|
|
- sendCtrlChinese = "心跳";
|
|
|
- } else if ("FE".equals(rmArray[5])) {
|
|
|
- if ("0101".equals(rmArray[6] + rmArray[7])) {
|
|
|
- // 主站召唤用户名密码
|
|
|
- receiveCtrlChinese = "主站召唤用户名密码";
|
|
|
- String[] returnMessage = {"68", "3C", "00", "00", "00", "FE", "00", "00", "18", "userNameHex", "passWordHex"};
|
|
|
- try {
|
|
|
- // 用户名转16进制并且30字节用0补全
|
|
|
- String userNameHex = ByteUtil.bytesToHexStringFromHighToLow(uploadFileChannel.getUploadUserName().getBytes("UTF-8"));
|
|
|
- returnMessage[9] = StrUtil.padAfter(userNameHex, 30 * 2, '0');
|
|
|
- // 密码转16进制并且30字节用0补全
|
|
|
- String passWordHex = ByteUtil.bytesToHexStringFromHighToLow(uploadFileChannel.getUploadPassword().getBytes("UTF-8"));
|
|
|
- returnMessage[10] = StrUtil.padAfter(passWordHex, 30 * 2, '0');
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
- log.error(ctx.channel().remoteAddress()+" - "+"用户名密码转码异常:", e);
|
|
|
- }
|
|
|
- sendMessage = ArrayUtil.join(returnMessage, "");
|
|
|
- sendCtrlChinese = "子站应答用户名密码";
|
|
|
- } else if ("0001".equals(rmArray[6] + rmArray[7]) && "20".equals(rmArray[8])) {
|
|
|
- // 子站不用响应主站的报文
|
|
|
- receiveCtrlChinese = "主站回复用户名密码认证成功";
|
|
|
- } else if ("0001".equals(rmArray[6] + rmArray[7]) && !"20".equals(rmArray[8])) {
|
|
|
- // 子站不用响应主站的报文
|
|
|
- receiveCtrlChinese = "主站回复用户名密码认证失败";
|
|
|
- } else {
|
|
|
- receiveCtrlChinese = "主站报文程序无法解析";
|
|
|
- }
|
|
|
- } else {
|
|
|
- // 判断主站索要的ASDU文件类型是否存在
|
|
|
- int fileTypeCode = ByteUtil.hexToDec(rmArray[5]);
|
|
|
- String fileType = "";
|
|
|
-
|
|
|
- if (fileTypeCode==180){
|
|
|
- fileType = "B4";
|
|
|
- }
|
|
|
- else{
|
|
|
- Map<String,Integer> codeMap = super.getFileCode();
|
|
|
- for (Map.Entry<String, Integer> entry : codeMap.entrySet()) {
|
|
|
- if (entry.getKey().equals("E1")||entry.getKey().equals("E2")||entry.getKey().equals("E4")||entry.getKey().equals("E5")||entry.getKey().equals("E11")||entry.getKey().equals("E12")||entry.getKey().equals("E15")||entry.getKey().equals("E16")||entry.getKey().equals("E17")||entry.getKey().equals("E18")||entry.getKey().equals("E19")){
|
|
|
- if (fileTypeCode == entry.getValue().intValue()){
|
|
|
- fileType = entry.getKey();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (StringUtils.isEmpty(fileType)) {
|
|
|
- receiveCtrlChinese = "主站报文里文件类型码没找到,子站无法解析";
|
|
|
- String[] rmArrayCopy = ArrayUtil.clone(rmArray);
|
|
|
- rmArrayCopy[6] = "00";
|
|
|
- rmArrayCopy[7] = "00";
|
|
|
- rmArrayCopy[8] = "13";
|
|
|
- sendMessage = ArrayUtil.join(rmArrayCopy, "");
|
|
|
- sendCtrlChinese = "本地没有" + fileType + "文件上报";
|
|
|
- }
|
|
|
- else if ("B4".equals(fileType)) {// 下载的发电计划
|
|
|
- if ("13".equals(rmArray[8])) {
|
|
|
- receiveCtrlChinese = "主站没有发电计划文件";
|
|
|
- }
|
|
|
- else if ("16".equals(rmArray[8])) {
|
|
|
- receiveCtrlChinese = "主站有发电计划文件";
|
|
|
- String[] rmArrayCopy = ArrayUtil.clone(rmArray);
|
|
|
- rmArrayCopy[6] = "01";
|
|
|
- rmArrayCopy[7] = "00";
|
|
|
- rmArrayCopy[8] = "17";
|
|
|
- sendMessage = ArrayUtil.join(rmArrayCopy, "");
|
|
|
- sendCtrlChinese = "子站召唤发电计划文件";
|
|
|
- }
|
|
|
- else if ("07".equals(rmArray[8])) {
|
|
|
- // 最后一帧
|
|
|
- receiveCtrlChinese = "主站传送最后一帧的发电计划文件";
|
|
|
- String fileNameHex = "";
|
|
|
- //取文件名报文
|
|
|
- for (int i = 9; i < 128; i++) {
|
|
|
- if ("00".equals(rmArray[i])) {
|
|
|
- break;
|
|
|
- }
|
|
|
- fileNameHex = fileNameHex + rmArray[i];
|
|
|
- }
|
|
|
- String charsetName = uploadObject.getUploadFileCharSetEnum().getMessage();
|
|
|
- String planFileName = "";
|
|
|
- try {
|
|
|
- planFileName = new String(ByteUtil.toByteArray(fileNameHex), charsetName);
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
- log.error(ctx.channel().remoteAddress()+" - "+"在接收发电计划时转换文件名失败:",e);
|
|
|
- }
|
|
|
-
|
|
|
- // 获取本次文件内容
|
|
|
- String tempStr = "";
|
|
|
- if (FileConstant.downLoadFileContentMap.get(uploadObject.getObjectNo() + "-B4") != null) {
|
|
|
- // 上次剩余的
|
|
|
- tempStr = FileConstant.downLoadFileContentMap.get(uploadObject.getObjectNo() + "-B4");
|
|
|
- }
|
|
|
- String fileContentHex = tempStr + receiveMessage.substring(306);
|
|
|
-
|
|
|
- OutputStreamWriter osw = null;
|
|
|
- try {
|
|
|
- int len = fileContentHex.length();
|
|
|
- byte[] data = new byte[len / 2];
|
|
|
- for (int i = 0; i < len; i += 2) {
|
|
|
- data[i / 2] = (byte) ((Character.digit(fileContentHex.charAt(i), 16) << 4) + Character.digit(fileContentHex.charAt(i + 1), 16));
|
|
|
- }
|
|
|
- String downLoadPath = FileUtil.getDownloadFilePath() + File.separator + "sdpowerplan" + File.separator + "new";
|
|
|
- File destDir = new File(downLoadPath);
|
|
|
- if (!destDir.exists()) {// 如果目录不存在则创建目录
|
|
|
- boolean b = destDir.mkdirs();
|
|
|
- if (!b) // 如果创建失败则抛出异常
|
|
|
- throw new RuntimeException(downLoadPath + " 目录创建失败");
|
|
|
- }
|
|
|
- osw = new OutputStreamWriter(new FileOutputStream(downLoadPath+File.separator+planFileName),"UTF-8");
|
|
|
- String res = new String(data,charsetName);
|
|
|
- osw.write(res);
|
|
|
- osw.close();
|
|
|
- sendCtrlChinese = "子站接收发电计划成功";
|
|
|
- sendMessage="6891000000B400000A";
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(ctx.channel().remoteAddress()+" - "+"保存发电计划失败:",e);
|
|
|
- sendCtrlChinese = "子站接收发电计划失败";
|
|
|
- sendMessage="6891000000B4000014";
|
|
|
- } finally {
|
|
|
- if (osw != null) {
|
|
|
- try {
|
|
|
- osw.close();
|
|
|
- } catch (IOException e) {
|
|
|
- log.error(ctx.channel().remoteAddress()+" - "+"保存发电计划关闭流失败",e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- sendMessage = sendMessage+receiveMessage.substring(18,298)+"00000000";
|
|
|
- FileConstant.downLoadFileContentMap.remove(uploadObject.getObjectNo() + "-B4");
|
|
|
- }
|
|
|
- else if ("08".equals(rmArray[8])) {
|
|
|
- // 不是最后一帧
|
|
|
- receiveCtrlChinese = "主站传送不是最后一帧的发电计划文件";
|
|
|
- StringBuffer tempSB = new StringBuffer("6891000000B4010017");
|
|
|
- for (int i = 9; i <= 136; i++) {
|
|
|
- tempSB.append(rmArray[i]);
|
|
|
- }
|
|
|
- tempSB.append("0000000000000000");
|
|
|
- sendMessage = tempSB.toString();
|
|
|
- // 将文件内容放入临时变量中,等待最后一帧
|
|
|
- String tempStr = "";
|
|
|
- if (FileConstant.downLoadFileContentMap.get(uploadObject.getObjectNo() + "-B4") != null) {
|
|
|
- // 上次剩余的
|
|
|
- tempStr = FileConstant.downLoadFileContentMap.get(uploadObject.getObjectNo() + "-B4");
|
|
|
- }
|
|
|
- // 将本次文件内容也存入临时变量中
|
|
|
- FileConstant.downLoadFileContentMap.put(uploadObject.getObjectNo() + "-B4", tempStr + receiveMessage.substring(306));
|
|
|
- sendCtrlChinese = "子站召唤下一帧发电计划文件";
|
|
|
- }
|
|
|
- else{
|
|
|
- receiveCtrlChinese = "主站报文里文件类型码没找到,子站无法解析";
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
- if ("15".equals(rmArray[8])) {
|
|
|
- String finalFileType = fileType;
|
|
|
- Map<String, FileMutableInteger> filterMap = FileConstant.sdUploadCountMap.entrySet().stream().filter(r -> uploadObject.getObjectNo().equals(r.getKey().substring(0, r.getKey().indexOf("@"))) && finalFileType.equals(r.getKey().substring(r.getKey().indexOf("@") + 1, r.getKey().lastIndexOf("@"))))
|
|
|
- .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
|
|
|
- (oldValue, newValue) -> oldValue, LinkedHashMap::new));
|
|
|
-
|
|
|
- // 上报文件名
|
|
|
- String fileNameKey = "";
|
|
|
- String filePath = "";
|
|
|
- if (MapUtil.isNotEmpty(filterMap)) {
|
|
|
- // 不是第一次要文件了
|
|
|
- for (Map.Entry<String, FileMutableInteger> entry : filterMap.entrySet()) {
|
|
|
- fileNameKey = entry.getKey();
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if ("".equals(fileNameKey)) {
|
|
|
- // 从待上报缓存中取文件类型
|
|
|
- Map<String, UploadFileLog> readyUploadFileMap = sdFilterFileByChannelNo(uploadObject.getObjectNo(), fileType, FileConstant.readyUploadFileMap,log);
|
|
|
- for (Map.Entry<String, UploadFileLog> entry : readyUploadFileMap.entrySet()) {
|
|
|
- // 从待上报缓存中取一个加入到累计次数变量中
|
|
|
- FileMutableInteger fileMutableInteger = new FileMutableInteger(0);
|
|
|
- FileConstant.sdUploadCountMap.put(entry.getKey(), fileMutableInteger);
|
|
|
- fileNameKey = entry.getKey();
|
|
|
- String[] keys = fileNameKey.split("@");
|
|
|
- filePath = FileUtil.getFileUploadPath() + File.separator + "process" + File.separator + keys[0] + File.separator + keys[1] + File.separator + keys[2];
|
|
|
- break;
|
|
|
- }
|
|
|
- } else {
|
|
|
- String[] keys = fileNameKey.split("@");
|
|
|
- filePath = FileUtil.getFileUploadPath() + File.separator + "process" + File.separator + keys[0] + File.separator + keys[1] + File.separator + keys[2];
|
|
|
- }
|
|
|
-
|
|
|
- String fileTypeName = FileTypeEnum.valueOf(fileType).getMessage();
|
|
|
- receiveCtrlChinese = "召唤" + fileTypeName + "文件信息";
|
|
|
-
|
|
|
- if ("".equals(filePath)) {
|
|
|
- // 说明没有文件上报
|
|
|
- String[] rmArrayCopy = ArrayUtil.clone(rmArray);
|
|
|
- rmArrayCopy[6] = "00";
|
|
|
- rmArrayCopy[7] = "00";
|
|
|
- rmArrayCopy[8] = "13";
|
|
|
- sendMessage = ArrayUtil.join(rmArrayCopy, "");
|
|
|
- sendCtrlChinese = "没有" + fileTypeName + "文件上报";
|
|
|
- } else {
|
|
|
- String[] returnMessage = {"68", "91", "00", "00", "00", rmArray[5], "00", "00", "16", "fileNameHex", "fileTimeHex", "fileContentLengthHex", "00000000", "00000000"};
|
|
|
- String charsetName = uploadObject.getUploadFileCharSetEnum().getMessage();
|
|
|
- // 产生上报信息
|
|
|
- String[] tempKey = fileNameKey.split("@");
|
|
|
- // 生成文件名
|
|
|
- String fileName = tempKey[2];
|
|
|
- byte[] tempFileNameBytes = null;
|
|
|
- try {
|
|
|
- tempFileNameBytes = fileName.getBytes(charsetName);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("转换文件名编码失败", e);
|
|
|
- }
|
|
|
- // 获取文件名称长度
|
|
|
- String fileNameLength = uploadFileChannel.getUploadFileNameLengthEnum().getMessage();
|
|
|
- String fileNameByte = ByteUtil.Byte2String(tempFileNameBytes);
|
|
|
- returnMessage[9] = StrUtil.padAfter(fileNameByte.replace(" ", ""), Integer.parseInt(fileNameLength) * 2, '0');
|
|
|
- String fileContentByte = super.createFileMessageHex(filePath, charsetName, log);
|
|
|
- String[] fileContentByteArray = IEC102Uitl.stingToArray(fileContentByte);
|
|
|
- returnMessage[11] = ByteUtil.bytesToHexStringFromHighToLow(ByteUtil.intToBytesFromLowToHigh(fileContentByteArray.length));
|
|
|
- Long fileCreateTime = FileUtil.getFileCreateTime(filePath);
|
|
|
- returnMessage[10] = ByteUtil.bytesToHexStringFromHighToLow(ByteUtil.intToBytesFromLowToHigh(ByteUtil.hexToDec(Long.toHexString(fileCreateTime))));
|
|
|
- sendCtrlChinese = "找到" + fileTypeName + "文件上报:" + fileName;
|
|
|
- sendMessage = ArrayUtil.join(returnMessage, "");
|
|
|
- }
|
|
|
- } else if ("17".equals(rmArray[8])) {
|
|
|
- // 召唤需要传输的文件数据
|
|
|
- String finalFileType1 = fileType;
|
|
|
- Map<String, FileMutableInteger> filterMap = FileConstant.sdUploadCountMap.entrySet().stream().filter(r -> uploadObject.getObjectNo().equals(r.getKey().substring(0, r.getKey().indexOf("@"))) && finalFileType1.equals(r.getKey().substring(r.getKey().indexOf("@") + 1, r.getKey().lastIndexOf("@"))))
|
|
|
- .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
|
|
|
- (oldValue, newValue) -> oldValue, LinkedHashMap::new));
|
|
|
- String fileTypeName = FileTypeEnum.valueOf(fileType).getMessage();
|
|
|
-
|
|
|
- receiveCtrlChinese = "召唤" + fileTypeName + "文件数据";
|
|
|
-
|
|
|
- String charsetName = uploadObject.getUploadFileCharSetEnum().getMessage();
|
|
|
-
|
|
|
- if (MapUtil.isNotEmpty(filterMap)) {
|
|
|
- String fileNameKey = "";
|
|
|
- String filePath = "";
|
|
|
- // 取上报的文件名
|
|
|
- for (Map.Entry<String, FileMutableInteger> entry : filterMap.entrySet()) {
|
|
|
- fileNameKey = entry.getKey();
|
|
|
- String[] keys = fileNameKey.split("@");
|
|
|
- filePath = FileUtil.getFileUploadPath() + File.separator + "process" + File.separator + keys[0] + File.separator + keys[1] + File.separator + keys[2];
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- String[] returnMessage = {"68", "dataLengthHex", rmArray[5], "00", "00", "messageEnd", "fileNameHex", "fileTimeHex", "fileCountLengthHex", "contentIdx", "fileContentLength", "fileData"};
|
|
|
- String fileContentStartIndex = "";
|
|
|
- // 获取缓存中文件内容
|
|
|
- Map<String, String> fileContentMap = new HashMap<>();
|
|
|
-
|
|
|
- String fileContentByte = "";
|
|
|
- if (FileConstant.fileContentByIdxMap.get(fileNameKey) == null) {
|
|
|
- // 缓存中没有文件内容则新生成文件内容报文
|
|
|
- fileContentByte = super.createFileMessageHex(filePath, charsetName, log);
|
|
|
- fileContentMap.put("contentByteIdx", "0");
|
|
|
- } else {
|
|
|
- fileContentMap = FileConstant.fileContentByIdxMap.get(fileNameKey);
|
|
|
- fileContentByte = fileContentMap.get("fileContent");
|
|
|
- }
|
|
|
- String[] fileContentByteArray = IEC102Uitl.stingToArray(fileContentByte);
|
|
|
- // 获取通道单次文件传输字节
|
|
|
- String singleByte = uploadFileChannel.getUploadFileSingleByteEnum().getMessage();
|
|
|
- // 减去145,因应用数据长度包含文件名、文件生成时间、文件总长度等等的字节数
|
|
|
- int maxFileContentByte = (Integer.parseInt(singleByte)*20 - 145);
|
|
|
-
|
|
|
- String[] tempKey = fileNameKey.split("@");
|
|
|
- // 生成文件名
|
|
|
- String fileName = tempKey[2];
|
|
|
- byte[] tempFileNameBytes = null;
|
|
|
- try {
|
|
|
- tempFileNameBytes = fileName.getBytes(charsetName);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("转换文件名编码失败", e);
|
|
|
- }
|
|
|
- // 本次传输的文件内容字节
|
|
|
- String[] contentByteArray = null;
|
|
|
- if (fileContentByteArray.length <= maxFileContentByte) {
|
|
|
- // 一次可以传输完,生成控制域码
|
|
|
- returnMessage[5] = "07";
|
|
|
- Integer idx = Integer.parseInt(fileContentMap.get("contentByteIdx"));
|
|
|
- if (idx != 0) {
|
|
|
- // 最后一次的传输
|
|
|
- idx = idx + 1;
|
|
|
- }
|
|
|
- fileContentStartIndex = ByteUtil.bytesToHexStringFromHighToLow(ByteUtil.intToBytesFromLowToHigh(idx));
|
|
|
- FileConstant.fileContentByIdxMap.remove(fileNameKey);
|
|
|
- contentByteArray = fileContentByteArray;
|
|
|
- sendCtrlChinese = "最后一帧传输" + fileTypeName + "文件完成:"+fileName;
|
|
|
- } else {
|
|
|
- // 生成不是最后一帧控制域码
|
|
|
- returnMessage[5] = "08";
|
|
|
- Map<String, String> tempContentMap = new HashMap<>();
|
|
|
- String[] remainFileContent = new String[fileContentByteArray.length-maxFileContentByte];
|
|
|
- System.arraycopy(fileContentByteArray,maxFileContentByte,remainFileContent,0,fileContentByteArray.length-maxFileContentByte);
|
|
|
- // 将剩余的文件内容报文存入缓存中
|
|
|
- tempContentMap.put("fileContent", ArrayUtil.join(remainFileContent,""));
|
|
|
- // 上次字节结束的位置
|
|
|
- Integer lastIdx = Integer.parseInt(fileContentMap.get("contentByteIdx"));
|
|
|
- if (lastIdx == 0) {
|
|
|
- fileContentStartIndex = ByteUtil.bytesToHexStringFromHighToLow(ByteUtil.intToBytesFromLowToHigh(0));
|
|
|
- } else {
|
|
|
- fileContentStartIndex = ByteUtil.bytesToHexStringFromHighToLow(ByteUtil.intToBytesFromLowToHigh(lastIdx + 1));
|
|
|
- }
|
|
|
- tempContentMap.put("contentByteIdx", String.valueOf(lastIdx+maxFileContentByte-1));
|
|
|
- FileConstant.fileContentByIdxMap.put(fileNameKey, tempContentMap);
|
|
|
- // 本次传输的文件内容字节
|
|
|
- contentByteArray = java.util.Arrays.copyOf(fileContentByteArray,maxFileContentByte);
|
|
|
- sendCtrlChinese = "分包传输" + fileTypeName + "文件:"+fileName;
|
|
|
- }
|
|
|
-
|
|
|
- String fileNameLength = uploadFileChannel.getUploadFileNameLengthEnum().getMessage();
|
|
|
- String fileNameByte = ByteUtil.Byte2String(tempFileNameBytes);
|
|
|
-
|
|
|
- // 文件名长度
|
|
|
- returnMessage[6] = StrUtil.padAfter(fileNameByte.replace(" ", ""), Integer.parseInt(fileNameLength) * 2, '0');
|
|
|
- String fileCountLengthHex = super.createFileMessageHex(filePath, charsetName, log);
|
|
|
- // 文件总长度
|
|
|
- returnMessage[8] = ByteUtil.bytesToHexStringFromHighToLow(ByteUtil.intToBytesFromLowToHigh(fileCountLengthHex.length() / 2));
|
|
|
- Long fileCreateTime = FileUtil.getFileCreateTime(filePath);
|
|
|
- // 文件时间
|
|
|
- returnMessage[7] = ByteUtil.bytesToHexStringFromHighToLow(ByteUtil.intToBytesFromLowToHigh(ByteUtil.hexToDec(Long.toHexString(fileCreateTime))));
|
|
|
- // 文件偏移量
|
|
|
- returnMessage[9] = fileContentStartIndex;
|
|
|
- // 本包传输的文件内容数据长度
|
|
|
- returnMessage[10] = ByteUtil.bytesToHexStringFromHighToLow(ByteUtil.intToBytesFromLowToHigh(contentByteArray.length));
|
|
|
- // 本包传输的文件内容数据
|
|
|
- returnMessage[11] = ArrayUtil.join(contentByteArray,"");
|
|
|
-
|
|
|
- // 计算校验和,获取从[控制域]到[校验和]之前的报文
|
|
|
- StringBuffer tempSB = new StringBuffer("");
|
|
|
- for (int i = 5; i <= 11; i++) {
|
|
|
- tempSB.append(returnMessage[i]);
|
|
|
- }
|
|
|
- // 计算应用数据长度
|
|
|
- returnMessage[1] = ByteUtil.bytesToHexStringFromHighToLow(ByteUtil.intToBytesFromLowToHigh(tempSB.length() / 2));
|
|
|
- sendMessage = ArrayUtil.join(returnMessage, "");
|
|
|
- } else {
|
|
|
- // 上报次数缓存中没有此通道的文件,无法上报
|
|
|
- String[] rmArrayCopy = ArrayUtil.clone(rmArray);
|
|
|
- rmArrayCopy[6] = "00";
|
|
|
- rmArrayCopy[7] = "00";
|
|
|
- rmArrayCopy[8] = "13";
|
|
|
- sendMessage = ArrayUtil.join(rmArrayCopy, "");
|
|
|
- sendCtrlChinese = "由于主站不正常交互,回复无文件";
|
|
|
- }
|
|
|
- } else if ("0A".equals(rmArray[8])) {
|
|
|
- String fileTypeName = FileTypeEnum.valueOf(fileType).getMessage();
|
|
|
- receiveCtrlChinese = "主站接收" + fileTypeName + "文件成功";
|
|
|
- super.totalFileUploadNums(uploadObject,uploadFileChannel, "", log);
|
|
|
- } else if ("14".equals(rmArray[8])) {
|
|
|
- String fileTypeName = FileTypeEnum.valueOf(fileType).getMessage();
|
|
|
- receiveCtrlChinese = "主站接收" + fileTypeName + "文件失败";
|
|
|
- super.totalFileUploadNums(uploadObject,uploadFileChannel, receiveCtrlChinese, log);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- log.info(ctx.channel().remoteAddress()+" - "+"接收报文:[" + receiveCtrlChinese + "]" + IEC102Uitl.delimiterStringBySpace(receiveMessage));
|
|
|
- if (!"".equals(sendMessage)) {
|
|
|
- log.info(ctx.channel().remoteAddress()+" - "+"发送报文:[" + sendCtrlChinese + "]" + IEC102Uitl.delimiterStringBySpace(sendMessage.toUpperCase()));
|
|
|
- }
|
|
|
- return sendMessage.toUpperCase();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据通道过滤缓存Map的key是否存在
|
|
|
- *
|
|
|
- * @param channelNo
|
|
|
- * @param map
|
|
|
- * @return
|
|
|
- */
|
|
|
- private Map<String, UploadFileLog> sdFilterFileByChannelNo(String channelNo, String fileType, Map<String, UploadFileLog> map,Logger log) {
|
|
|
- Map<String, UploadFileLog> filterMap = map.entrySet().stream().filter(r -> channelNo.equals(r.getKey().substring(0, r.getKey().indexOf("@"))) && fileType.equals(r.getKey().substring(r.getKey().indexOf("@") + 1, r.getKey().lastIndexOf("@"))))
|
|
|
- .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
|
|
|
- (oldValue, newValue) -> oldValue, LinkedHashMap::new));
|
|
|
-
|
|
|
- // 再将失效时间的文件移除
|
|
|
- Map<String, UploadFileLog> fileterFileMap = new HashMap<>();
|
|
|
- Date d = new Date();
|
|
|
- for (Map.Entry<String, UploadFileLog> entry : filterMap.entrySet()) {
|
|
|
- UploadFileLog u = entry.getValue();
|
|
|
-
|
|
|
- if (u.getUploadFileEndTime()!=null){
|
|
|
- if (d.getTime()>u.getUploadFileEndTime()){
|
|
|
- // 过期文件
|
|
|
- String[] keys = entry.getKey().split("@");
|
|
|
- String srcPath = FileUtil.getFileUploadPath() + File.separator + "process" + File.separator + keys[0] + File.separator + keys[1] + File.separator + keys[2];
|
|
|
- String targetPath = FileUtil.getFileUploadPath() + File.separator + "expire" + File.separator + keys[0] + File.separator + DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMdd") + File.separator + keys[1];
|
|
|
- try {
|
|
|
- FileUtil.move(srcPath, targetPath);
|
|
|
- log.info("上报对象编号:"+keys[0]+",将过期的文件移到expire过期文件夹下:"+keys[2]);
|
|
|
- u.setFileStatusEnum(FileStatusEnum.E7);
|
|
|
- uploadFileLogRepository.save(u);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("上报对象编号:"+keys[0]+",将过期的文件移到expire过期文件夹下:"+keys[2]+"失败",e);
|
|
|
- }
|
|
|
- // 清理缓存
|
|
|
- FileConstant.sdUploadCountMap.remove(entry.getKey());
|
|
|
- FileConstant.readyUploadFileMap.remove(entry.getKey());
|
|
|
-// FileConstant.fileContentMap.remove(entry.getKey());
|
|
|
- FileConstant.againUploadFileMap.remove(entry.getKey());
|
|
|
- }
|
|
|
- else{
|
|
|
- // 有效的文件
|
|
|
- fileterFileMap.put(entry.getKey(),u);
|
|
|
- }
|
|
|
- }
|
|
|
- else{
|
|
|
- // 有效的文件
|
|
|
- fileterFileMap.put(entry.getKey(),u);
|
|
|
- }
|
|
|
- }
|
|
|
- return fileterFileMap;
|
|
|
- }
|
|
|
-}
|