From 59b91a55e12ea347a9a8f198e674127108de6b94 Mon Sep 17 00:00:00 2001 From: Administrator <409575046@qq.com> Date: Thu, 1 Apr 2021 14:54:19 +0800 Subject: [PATCH 1/2] v1 --- .../com/epower/tools/DatFileDataBase.java | 99 +++++++++++++++---- .../java/com/epower/tools/CfgFileTest.java | 25 ++++- 2 files changed, 101 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/epower/tools/DatFileDataBase.java b/src/main/java/com/epower/tools/DatFileDataBase.java index 4340907..ede3066 100644 --- a/src/main/java/com/epower/tools/DatFileDataBase.java +++ b/src/main/java/com/epower/tools/DatFileDataBase.java @@ -1,6 +1,7 @@ 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; @@ -54,29 +55,29 @@ public class DatFileDataBase { Integer anaCounts = cfgFile.getAnalogChannelInfoList().size(); Integer statusCounts = cfgFile.getStatusChannelInfoList().size(); //每个采样记录的字节数 - Integer oneRowBytes = anaCounts * 2 + Double.valueOf(Math.ceil(2 * statusCounts / 16)).intValue() + FOUR_BYTES + FOUR_BYTES; + Integer oneRowBytes = anaCounts * 2 + Double.valueOf(Math.ceil((double) statusCounts * 2 / 16)).intValue() + FOUR_BYTES + FOUR_BYTES; File file = new File(datFileLocation); logger.debug(String.format("字节总数:%d,模拟通道数量:%d,状态通道数量:%d,每个采样要求的字节数:%d", datFileLocation.length(), anaCounts, statusCounts, oneRowBytes)); logger.debug("文件:" + file.getAbsolutePath() + ",Size:" + file.length() + "Bytes,LastUpdateTime:" + DateUtil.format(new Date(file.lastModified()), "yyyy-MM-dd HH:mm:ss.SSS")); this.dataRows = new ArrayList<>(); InputStream inputStream = new FileInputStream(datFileLocation); +// DataInputStream inputStream=new DataInputStream(new BufferedInputStream(new FileInputStream(datFileLocation))); Integer totalRows = Double.valueOf(Math.ceil(file.length() / oneRowBytes)).intValue(); - //开始记录数据的时间微秒数 long startTimeMicroSeconds = cfgFile.toMicroSeconds(cfgFile.getFirstTimeText()); - for (int rowNo = 0; rowNo < totalRows; rowNo++) { + + byte[] content = getContent(datFileLocation); + int times = content.length / oneRowBytes; + for (int i = 0; i < times; i++) { //按每次采样开始读取 - int readBytes = 0; DatRowBean datRowBean = new DatRowBean(); //读取采样编号 - byte[] buffer = new byte[FOUR_BYTES]; - readBytes += inputStream.read(buffer); + byte[] buffer = ArrayUtil.sub(content, 0 + i * oneRowBytes, 4 + i * oneRowBytes); datRowBean.setSampleIndex(bytesToInt(buffer)); //读取时标 - buffer = new byte[FOUR_BYTES]; - readBytes += inputStream.read(buffer); + buffer = ArrayUtil.sub(content, 4 + i * oneRowBytes, 8 + i * oneRowBytes); //使用时间倍率因子计算 long timeStamp = startTimeMicroSeconds + Double.valueOf(Long.valueOf(bytesToInt(buffer)) * cfgFile.getTimemultDouble()).longValue(); //时标已经使用时间倍率因子及dat存储的timestamp乘积加上开始时间计算过了 @@ -85,24 +86,62 @@ public class DatFileDataBase { //读取模拟通道数据 int idx = 0; for (AnalogChannelInfo analogChannelInfo : cfgFile.getAnalogChannelInfoList()) { - buffer = new byte[TWO_BYTES]; - readBytes += inputStream.read(buffer); + buffer = ArrayUtil.sub(content, 8 + idx + i * oneRowBytes, 10 + idx + i * oneRowBytes); //原始采样值*系数 datRowBean.addData(bytesToInt(buffer) * analogChannelInfo.chanMultiRate()); //转向下一个点 - idx++; + idx += 2; } //读取状态通道数据 - buffer = new byte[oneRowBytes - readBytes]; - readBytes += inputStream.read(buffer); - idx = 0; - List byteBitStatusList = getByteArrayStatusList(buffer); - for (StatusChannelInfo statusChannelInfo : cfgFile.getStatusChannelInfoList()) { - datRowBean.addData(byteBitStatusList.get(idx++)); + buffer = ArrayUtil.sub(content, 10 + idx - 2 + i * oneRowBytes, oneRowBytes + i * oneRowBytes); + if (ArrayUtil.isNotEmpty(buffer)) { + idx = 0; + List byteBitStatusList = getByteArrayStatusList(buffer); + for (StatusChannelInfo statusChannelInfo : cfgFile.getStatusChannelInfoList()) { + datRowBean.addData(byteBitStatusList.get(idx++)); + } } - logger.debug(String.format("行%d,读取字节数:%d", rowNo, readBytes)); +// logger.debug(String.format("行%d,读取字节数:%d", rowNo, readBytes)); this.dataRows.add(datRowBean); } + +// for (int rowNo = 0; rowNo < totalRows; rowNo++) { +// //按每次采样开始读取 +// int readBytes = 0; +// DatRowBean datRowBean = new DatRowBean(); +// //读取采样编号 +// byte[] buffer = new byte[FOUR_BYTES]; +// readBytes += inputStream.read(buffer); +// datRowBean.setSampleIndex(bytesToInt(buffer)); +// //读取时标 +// buffer = new byte[FOUR_BYTES]; +// readBytes += inputStream.read(buffer); +// //使用时间倍率因子计算 +// 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()) { +// buffer = new byte[TWO_BYTES]; +// readBytes += inputStream.read(buffer); +// //原始采样值*系数 +// datRowBean.addData(bytesToInt(buffer) * analogChannelInfo.chanMultiRate()); +// //转向下一个点 +// idx++; +// } +// //读取状态通道数据 +// buffer = new byte[oneRowBytes - readBytes]; +// readBytes += inputStream.read(buffer); +// idx = 0; +// List byteBitStatusList = getByteArrayStatusList(buffer); +// for (StatusChannelInfo statusChannelInfo : cfgFile.getStatusChannelInfoList()) { +// datRowBean.addData(byteBitStatusList.get(idx++)); +// } +// logger.debug(String.format("行%d,读取字节数:%d", rowNo, readBytes)); +// this.dataRows.add(datRowBean); +// } } @@ -271,4 +310,28 @@ 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/test/java/com/epower/tools/CfgFileTest.java b/src/test/java/com/epower/tools/CfgFileTest.java index c445852..4d4a2f2 100644 --- a/src/test/java/com/epower/tools/CfgFileTest.java +++ b/src/test/java/com/epower/tools/CfgFileTest.java @@ -6,15 +6,28 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import org.junit.Test; +import java.sql.SQLOutput; + public class CfgFileTest { @Test public void loadDat() { try { + long startTime = System.currentTimeMillis(); /** * 装载配置文件 */ - CfgFile cfgFile = new CfgFile("D:\\JavaDebug\\comtrade\\DEVICE_0002_20210316_162900_000.cfg"); + //CfgFile cfgFile = new CfgFile("D:\\JavaDebug\\comtrade\\DEVICE_0002_20210316_162900_000.cfg"); +// 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"); + + /** + * 解析数据文件 + */ + // DatFileDataBase datFileDataBase = new DatFileDataBase("D:\\JavaDebug\\comtrade\\DEVICE_0003_20210316_163000_000.dat"); + DatFileDataBase datFileDataBase = new DatFileDataBase("E:\\ide\\workspace\\javaSpace\\comtrade-parser\\data\\DEVICE_0585_20210331_201344_000.dat"); + + //读取cfg文件,解析终端描述、通道相关信息 cfgFile.parse(); @@ -50,12 +63,12 @@ public class CfgFileTest { //倍率因子 System.out.println("时间倍率因子:" + cfgFile.getTimemultDouble()); - /** - * 解析数据文件 - */ - DatFileDataBase datFileDataBase = new DatFileDataBase("D:\\JavaDebug\\comtrade\\DEVICE_0003_20210316_163000_000.dat"); + //根据cfg文件解析dat数据文件 datFileDataBase.parse(cfgFile); + long endTime = System.currentTimeMillis(); + + System.out.println("解析文件耗时:=========================》" + (endTime - startTime) + "ms"); //数据总行数 System.out.println("数据总行数:" + datFileDataBase.getDataRows().size()); //查询GPS状态取“对时正常”的开关量录波第一个点 @@ -70,7 +83,9 @@ public class CfgFileTest { System.out.println("UA最大值信息:" + datFileDataBase.getMaxValue(cfgFile, "Ua")); System.out.println("UA最小值信息:" + datFileDataBase.getMinValue(cfgFile, "Ua")); //如果需要算具体时间点的值,请参考:firstTimeMicroSeconds再取具体的值 + long endTime1 = System.currentTimeMillis(); + System.out.println("总耗时:=========================》" + (endTime1 - startTime) + "ms"); } catch (Exception e) { e.printStackTrace(); } -- Gitee From 29a4afe7bef223b429595398416dd42454d71ac6 Mon Sep 17 00:00:00 2001 From: Administrator <409575046@qq.com> Date: Thu, 1 Apr 2021 15:34:16 +0800 Subject: [PATCH 2/2] v1 --- src/main/java/com/epower/tools/DatFileDataBase.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/epower/tools/DatFileDataBase.java b/src/main/java/com/epower/tools/DatFileDataBase.java index ede3066..963cd83 100644 --- a/src/main/java/com/epower/tools/DatFileDataBase.java +++ b/src/main/java/com/epower/tools/DatFileDataBase.java @@ -55,7 +55,13 @@ public class DatFileDataBase { Integer anaCounts = cfgFile.getAnalogChannelInfoList().size(); Integer statusCounts = cfgFile.getStatusChannelInfoList().size(); //每个采样记录的字节数 - Integer oneRowBytes = anaCounts * 2 + Double.valueOf(Math.ceil((double) statusCounts * 2 / 16)).intValue() + FOUR_BYTES + FOUR_BYTES; + int digitalBytesLen = 0; + if (statusCounts % 16 != 0) { + digitalBytesLen = 2 * ((statusCounts / 16) + 1); + } else { + digitalBytesLen = 2 * (statusCounts / 16); + } + Integer oneRowBytes = anaCounts * 2 + digitalBytesLen + FOUR_BYTES + FOUR_BYTES; File file = new File(datFileLocation); logger.debug(String.format("字节总数:%d,模拟通道数量:%d,状态通道数量:%d,每个采样要求的字节数:%d", datFileLocation.length(), anaCounts, statusCounts, oneRowBytes)); -- Gitee