diff --git a/src/main/java/com/epower/tools/CfgFile.java b/src/main/java/com/epower/tools/CfgFile.java index 95ee46e3942c578443b7dc7a077517ec87ba04de..8c5d4f4dd16fd2a1a6c21abe9e2f60216214587f 100644 --- a/src/main/java/com/epower/tools/CfgFile.java +++ b/src/main/java/com/epower/tools/CfgFile.java @@ -56,7 +56,7 @@ public class CfgFile { */ public void loadFile(byte[] content) throws Exception { String res = new java.lang.String(content, "gbk"); - String[] arrayStr= res.split("\\r?\\n"); + String[] arrayStr = res.split("\\r?\\n"); lines = Arrays.asList(arrayStr); // lines = FileUtil.readLines(filePath, GBK); } @@ -186,10 +186,6 @@ public class CfgFile { '}'; } - public void print() { - logger.debug(this); - } - /** * 终端的描述 diff --git a/src/main/java/com/epower/tools/DatFileDataBase.java b/src/main/java/com/epower/tools/DatFileDataBase.java index a6865a8ef414a77a8a5309a4162cbcd0d4308924..ea4fed70e20bc5b623034faddd30f4b798478836 100644 --- a/src/main/java/com/epower/tools/DatFileDataBase.java +++ b/src/main/java/com/epower/tools/DatFileDataBase.java @@ -1,16 +1,11 @@ package com.epower.tools; -import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.HexUtil; -import com.epower.tools.bean.AnalogChannelInfo; import com.epower.tools.bean.DatRowBean; import com.epower.tools.bean.PntDataBean; -import com.epower.tools.bean.StatusChannelInfo; -import org.apache.log4j.Logger; -import java.io.*; -import java.nio.ByteBuffer; +import java.math.BigInteger; import java.util.*; /** @@ -19,22 +14,10 @@ import java.util.*; * @create: 2021-03-21 22:58 */ public class DatFileDataBase { - static Logger logger = Logger.getLogger(DatFileDataBase.class); - static final String[] HighLetter = {"A", "B", "C", "D", "E", "F"}; - static Map hexCharIntMap = new HashMap<>(); - - static { - for (int i = 0; i <= 9; i++) { - hexCharIntMap.put(i + "", i); - } - for (int j = 10; j < HighLetter.length + 10; j++) { - hexCharIntMap.put(HighLetter[j - 10], j); - } - } // private final String datFileLocation; - private final byte[] content; + private byte[] content; /** @@ -48,7 +31,6 @@ public class DatFileDataBase { // public DatFileDataBase(String datFileLocation) { // this.datFileLocation = datFileLocation; // } - public DatFileDataBase(byte[] content) { this.content = content; } @@ -71,7 +53,8 @@ public class DatFileDataBase { // logger.debug("文件:" + file.getAbsolutePath() + ",Size:" + file.length() + "Bytes,LastUpdateTime:" + DateUtil.format(new Date(file.lastModified()), "yyyy-MM-dd HH:mm:ss.SSS")); //开始记录数据的时间微秒数 - long startTimeMicroSeconds = cfgFile.toMicroSeconds(cfgFile.getFirstTimeText()); + +// long startTimeMicroSeconds = cfgFile.toMicroSeconds(cfgFile.getFirstTimeText()); // byte[] content = getContent(datFileLocation); int lines = content.length / oneRowBytes; @@ -85,15 +68,22 @@ public class DatFileDataBase { //读取时标 buffer = ArrayUtil.sub(content, 4 + i * oneRowBytes, 8 + i * oneRowBytes); //使用时间倍率因子计算 - long timeStamp = startTimeMicroSeconds + Double.valueOf(Long.valueOf(bytesToInt(buffer)) * cfgFile.getTimemultDouble()).longValue(); - //时标已经使用时间倍率因子及dat存储的timestamp乘积加上开始时间计算过了 - datRowBean.setTimeStamp(timeStamp); +// long timeStamp = startTimeMicroSeconds + Double.valueOf(Long.valueOf(bytesToInt(buffer)) * cfgFile.getTimemultDouble()).longValue(); +// //时标已经使用时间倍率因子及dat存储的timestamp乘积加上开始时间计算过了 +// datRowBean.setTimeStamp(timeStamp); //读取模拟通道数据 int idx = 0; - for (AnalogChannelInfo analogChannelInfo : cfgFile.getAnalogChannelInfoList()) { +// for (AnalogChannelInfo analogChannelInfo : cfgFile.getAnalogChannelInfoList()) { +// buffer = ArrayUtil.sub(content, 8 + idx + i * oneRowBytes, 10 + idx + i * oneRowBytes); +// //原始采样值*系数 +// datRowBean.addData(bytesToInt(buffer) * analogChannelInfo.chanMultiRate()); +// //转向下一个点 +// idx += 2; +// } + for (int j = 0; j < anaCounts; j++) { buffer = ArrayUtil.sub(content, 8 + idx + i * oneRowBytes, 10 + idx + i * oneRowBytes); //原始采样值*系数 - datRowBean.addData(bytesToInt(buffer) * analogChannelInfo.chanMultiRate()); + datRowBean.addData(bytesToInt(buffer)); //转向下一个点 idx += 2; } @@ -117,7 +107,7 @@ public class DatFileDataBase { public Integer bytesToInt(byte[] bytes) { String s = HexUtil.encodeHexStr(bytes).toUpperCase(); String hexText = reverseHex(s); - return hexText.length() == 4 ? parseHex4(hexText) : covert(hexText); + return hexText.length() == 4 ? parseHex4(hexText) : parseHex8(hexText); } /** @@ -170,20 +160,15 @@ public class DatFileDataBase { ret = ((ret & 0x8000) > 0) ? (ret - 0x10000) : (ret); return ret; } - - public static Integer covert(String content) { - int number = 0; - - String[] str = new String[content.length()]; - for (int i = 0; i < str.length; i++) { - str[i] = content.substring(i, i + 1); + public static Integer parseHex8(String num) { + if (num.length() != 8) { + throw new NumberFormatException("Wrong length: " + num.length() + ", must be 8."); } - for (int i = 0; i < str.length; i++) { - number += hexCharIntMap.get(str[i]) * Math.pow(16, str.length - 1 - i); - } - return number; + BigInteger in = new BigInteger(num,16); + return in.intValue(); } + /** * 查询指定点在特定的值 * @@ -265,9 +250,9 @@ public class DatFileDataBase { return null; } Long timeStamp = this.dataRows.get(0).getTimeStamp(); - Double firstValue = (Double) this.dataRows.get(0).getValues().get(colNum); + Integer firstValue = (Integer) this.dataRows.get(0).getValues().get(colNum); for (DatRowBean datRowBean : this.dataRows) { - Double tmp = (Double) datRowBean.getValues().get(colNum); + Integer tmp = (Integer) datRowBean.getValues().get(colNum); if (isMax) { if (tmp.compareTo(firstValue) > 0) { firstValue = tmp; @@ -284,27 +269,4 @@ public class DatFileDataBase { return new PntDataBean(chId, timeStamp, firstValue); } - public byte[] getContent(String filePath) throws IOException { - File file = new File(filePath); - long fileSize = file.length(); - if (fileSize > Integer.MAX_VALUE) { - System.out.println("file too big..."); - return null; - } - FileInputStream fi = new FileInputStream(file); - byte[] buffer = new byte[(int) fileSize]; - int offset = 0; - int numRead = 0; - while (offset < buffer.length - && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) { - offset += numRead; - } - // 确保所有数据均被读取 - if (offset != buffer.length) { - throw new IOException("Could not completely read file " - + file.getName()); - } - fi.close(); - return buffer; - } } diff --git a/src/main/java/com/epower/tools/FileUtils.java b/src/main/java/com/epower/tools/FileUtils.java index 8482660ee616bd5c4d7b03828470c65639018068..69242b8ebc2f2dffd2464d439ce36ddf235f3b27 100644 --- a/src/main/java/com/epower/tools/FileUtils.java +++ b/src/main/java/com/epower/tools/FileUtils.java @@ -5,10 +5,12 @@ import org.apache.commons.compress.archivers.ArchiveStreamFactory; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import java.io.*; +import java.util.ArrayList; +import java.util.List; public class FileUtils { - public static byte[] readTarFile(File tarFile,String suffix) { + public static byte[] readTarFile(File tarFile, String suffix) { try { ArchiveInputStream archiveInputStream = new ArchiveStreamFactory() .createArchiveInputStream("tar", new BufferedInputStream(new FileInputStream(tarFile))); @@ -16,7 +18,28 @@ public class FileUtils { while ((entry = (TarArchiveEntry) archiveInputStream.getNextEntry()) != null) { if (entry.getSize() > 0) { if (entry.getName().endsWith(suffix)) { - return getContent(archiveInputStream); + return getContent(archiveInputStream, entry.getSize()); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static List readTarFile(File tarFile) { + List res = new ArrayList(); + try { + ArchiveInputStream archiveInputStream = new ArchiveStreamFactory() + .createArchiveInputStream("tar", new BufferedInputStream(new FileInputStream(tarFile))); + TarArchiveEntry entry = null; + while ((entry = (TarArchiveEntry) archiveInputStream.getNextEntry()) != null) { + if (entry.getSize() > 0) { + if (entry.getName().endsWith("cfg")) { + res.add(getContent(archiveInputStream, entry.getSize())); + }else if (entry.getName().endsWith("dat")) { + res.add(getContent(archiveInputStream, entry.getSize())); } } } @@ -44,4 +67,44 @@ public class FileUtils { } return baos.toByteArray(); } + + public static byte[] getContent(InputStream fi, long fileSize) throws IOException { + byte[] buffer = new byte[(int) fileSize]; + int offset = 0; + int numRead = 0; + while (offset < buffer.length + && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) { + offset += numRead; + } + // 确保所有数据均被读取 + if (offset != buffer.length) { + throw new IOException("Could not completely read file "); + } + fi.close(); + return buffer; + } + + public byte[] getContent(String filePath) throws IOException { + File file = new File(filePath); + long fileSize = file.length(); + if (fileSize > Integer.MAX_VALUE) { + System.out.println("file too big..."); + return null; + } + FileInputStream fi = new FileInputStream(file); + byte[] buffer = new byte[(int) fileSize]; + int offset = 0; + int numRead = 0; + while (offset < buffer.length + && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) { + offset += numRead; + } + // 确保所有数据均被读取 + if (offset != buffer.length) { + throw new IOException("Could not completely read file " + + file.getName()); + } + fi.close(); + return buffer; + } } diff --git a/src/test/java/com/epower/tools/CfgFileTest.java b/src/test/java/com/epower/tools/CfgFileTest.java index db1645d2dda8180cf53bf9f07c16f9b82f4b0fc8..051d5df03b15c5890b769e7a3dfbb0d95df3a830 100644 --- a/src/test/java/com/epower/tools/CfgFileTest.java +++ b/src/test/java/com/epower/tools/CfgFileTest.java @@ -23,79 +23,81 @@ public class CfgFileTest { // CfgFile cfgFile = new CfgFile("E:\\ide\\workspace\\javaSpace\\comtrade-parser\\data\\DEVICE_0002_20210316_162900_000.cfg"); // CfgFile cfgFile = new CfgFile("E:\\ide\\workspace\\javaSpace\\comtrade-parser\\data\\DEVICE_0585_20210331_201344_000.cfg"); long tarStart = System.currentTimeMillis(); - CfgFile cfgFile = new CfgFile(FileUtils.readTarFile( - new File(tarFIle),"cfg")); + + /** * 解析数据文件 */ // DatFileDataBase datFileDataBase = new DatFileDataBase("E:\\ide\\workspace\\javaSpace\\comtrade-parser\\data\\DEVICE_0585_20210331_201344_000.dat"); + CfgFile cfgFile = new CfgFile(FileUtils.readTarFile( + new File(tarFIle),"cfg")); DatFileDataBase datFileDataBase = new DatFileDataBase(FileUtils.readTarFile(new File( tarFIle),"dat")); long tarEnd = System.currentTimeMillis(); - System.out.println("加载tar耗时:================================>" + (tarEnd - tarStart) + "ms"); + System.out.println("加载tar 耗时:================================>" + (tarEnd - tarStart) + "ms"); //读取cfg文件,解析终端描述、通道相关信息 long cfgStart = System.currentTimeMillis(); cfgFile.parse(); long cfgEnd = System.currentTimeMillis(); - System.out.println("解析cfg耗时:================================>" + (cfgEnd - cfgStart) + "ms"); - - //全部通道列表 - System.out.println("通道列表:" + CollectionUtil.join(cfgFile.getAllPntNames(), ",")); - - //终端描述 - System.out.println("终端描述:" + cfgFile.getStationName()); - - //采样速率数 - System.out.println("采样速率数:" + cfgFile.getNrates()); - - //采样率 - System.out.println("采样率:" + cfgFile.getSampRateValue()); - - //总点数 - System.out.println("总点数:" + cfgFile.totalPntCount()); - - //模拟量通道数量 - System.out.println("模拟量通道数量:" + cfgFile.getAnalogChannelInfoList().size()); - - //状态量通道数量 - System.out.println("状态量通道数量:" + cfgFile.getStatusChannelInfoList().size()); - - - //第一个数据点时间 - long firstTimeMicroSeconds = cfgFile.toMicroSeconds(cfgFile.getFirstTimeText()); - System.out.println("第一个数据点时间:" + cfgFile.getFirstTimeText() + ",微秒:" + firstTimeMicroSeconds); - - //触发点时间 - long triggerTimeMicroSeconds = cfgFile.toMicroSeconds(cfgFile.getTriggerTimeText()); - System.out.println("触发点时间:" + cfgFile.getTriggerTimeText() + ",微秒:" + cfgFile.toMicroSeconds(cfgFile.getTriggerTimeText())); - - //倍率因子 - System.out.println("时间倍率因子:" + cfgFile.getTimemultDouble()); + System.out.println("解析cfg 耗时:================================>" + (cfgEnd - cfgStart) + "ms"); + +// //全部通道列表 +// System.out.println("通道列表:" + CollectionUtil.join(cfgFile.getAllPntNames(), ",")); +// +// //终端描述 +// System.out.println("终端描述:" + cfgFile.getStationName()); +// +// //采样速率数 +// System.out.println("采样速率数:" + cfgFile.getNrates()); +// +// //采样率 +// System.out.println("采样率:" + cfgFile.getSampRateValue()); +// +// //总点数 +// System.out.println("总点数:" + cfgFile.totalPntCount()); +// +// //模拟量通道数量 +// System.out.println("模拟量通道数量:" + cfgFile.getAnalogChannelInfoList().size()); +// +// //状态量通道数量 +// System.out.println("状态量通道数量:" + cfgFile.getStatusChannelInfoList().size()); +// +// +// //第一个数据点时间 +// long firstTimeMicroSeconds = cfgFile.toMicroSeconds(cfgFile.getFirstTimeText()); +// System.out.println("第一个数据点时间:" + cfgFile.getFirstTimeText() + ",微秒:" + firstTimeMicroSeconds); +// +// //触发点时间 +// long triggerTimeMicroSeconds = cfgFile.toMicroSeconds(cfgFile.getTriggerTimeText()); +// System.out.println("触发点时间:" + cfgFile.getTriggerTimeText() + ",微秒:" + cfgFile.toMicroSeconds(cfgFile.getTriggerTimeText())); +// +// //倍率因子 +// System.out.println("时间倍率因子:" + cfgFile.getTimemultDouble()); //根据cfg文件解析dat数据文件 long datStart = System.currentTimeMillis(); - System.out.println("解析cfg数据耗时:===============================》" + (datStart - cfgEnd) + "ms"); +// System.out.println("解析cfg数据耗时:===============================》" + (datStart - cfgEnd) + "ms"); datFileDataBase.parse(cfgFile); long datEnd = System.currentTimeMillis(); - System.out.println("解析dat文件耗时:===============================》" + (datEnd - datStart) + "ms"); + System.out.println("解析dat 耗时:===============================》" + (datEnd - datStart) + "ms"); //数据总行数 - System.out.println("数据总行数:" + datFileDataBase.getDataRows().size()); - //查询GPS状态取“对时正常”的开关量录波第一个点 - final String gpsPntTag = "对时正常"; - Integer valPos = cfgFile.findValPosition(gpsPntTag); - Object gpsStatus = datFileDataBase.queryRowItemValue(0, valPos); - System.out.println("GPS状态:" + (gpsStatus.equals(1) ? "正常" : "异常")); - - //UA UB UC IA IB IC的瞬时值的点 - System.out.println("UA第一个时间点的值:" + datFileDataBase.queryRowItemValue(0, cfgFile.findValPosition("Ua"))); - System.out.println("UA第一个时间点信息:" + datFileDataBase.queryRowItem(0, cfgFile, "Ua")); - System.out.println("UA最大值信息:" + datFileDataBase.getMaxValue(cfgFile, "Ua")); - System.out.println("UA最小值信息:" + datFileDataBase.getMinValue(cfgFile, "Ua")); +// System.out.println("数据总行数:" + datFileDataBase.getDataRows().size()); +// //查询GPS状态取“对时正常”的开关量录波第一个点 +// final String gpsPntTag = "对时正常"; +// Integer valPos = cfgFile.findValPosition(gpsPntTag); +// Object gpsStatus = datFileDataBase.queryRowItemValue(0, valPos); +// System.out.println("GPS状态:" + (gpsStatus.equals(1) ? "正常" : "异常")); +// +// //UA UB UC IA IB IC的瞬时值的点 +// System.out.println("UA第一个时间点的值:" + datFileDataBase.queryRowItemValue(0, cfgFile.findValPosition("Ua"))); +// System.out.println("UA第一个时间点信息:" + datFileDataBase.queryRowItem(0, cfgFile, "Ua")); +// System.out.println("UA最大值信息:" + datFileDataBase.getMaxValue(cfgFile, "Ua")); +// System.out.println("UA最小值信息:" + datFileDataBase.getMinValue(cfgFile, "Ua")); //如果需要算具体时间点的值,请参考:firstTimeMicroSeconds再取具体的值 long endTime = System.currentTimeMillis(); - System.out.println("解析dat数据耗时:===============================》" + (endTime - datEnd) + "ms"); +// System.out.println("解析dat数据耗时:===============================》" + (endTime - datEnd) + "ms"); System.out.println("总耗时:=========================》" + (endTime - startTime) + "ms"); } catch (Exception e) { e.printStackTrace();