package com.fgkj.bts; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; import java.util.List; import com.fgkj.data.ComFn; import com.fgkj.data.FboData; import com.fgkj.data.FboData.DataType; /** * 充电数据 * @author DELL * */ public class BTS_ChargeData { public int parse_result = BTS_ComBase.PARSE_RESULT_NULL; public BTS_BattParam battParam; //电池参数 public BTS_ChargeParam chargeParam; //充电参数 public BTS_ChargeState charState; //充电状态 public List fbsDatas; //每次记录的单体数据 public BTS_ChargeData() { battParam = new BTS_BattParam(); chargeParam = new BTS_ChargeParam(); charState = new BTS_ChargeState(); fbsDatas = new ArrayList(); } /** * 解析充电数据 * @param filePath */ public void readChrFile(String filePath) { FileInputStream fis = null; try { File f = new File(filePath); if(!filePath.endsWith(".CHR") || !filePath.toUpperCase().endsWith(".CHR")) { //文件格式错误 parse_result = BTS_ComBase.PARSE_RESULT_FILETYPEERR; return; } if(!f.exists()) { //文件不存在 parse_result = BTS_ComBase.PARSE_RESULT_NOTFOUNDFILE; return; //System.out.println("文件不存在.........."); } fis = new FileInputStream(f); byte[] buf = new byte[2048]; if(fis.read(buf, 0, buf.length) == 2048) { if(this.checkTestDataHead(buf)) { parse_result = BTS_ComBase.PARSE_RESULT_SUCCESS; }else { parse_result = BTS_ComBase.PARSE_RESULT_FILEERROR; } BTS_DataType dataType = new BTS_DataType(); while(true) { int tag = dataType.checkDataHead(fis); if((0xFD == tag) || (0xFC == tag) || (0xFB == tag)) { System.out.println("#################"); byte[] databuf = new byte[BTS_FbsData.BASIC_LEN + (battParam.EachGroupBattCount*battParam.BattGroupCount)*2 -4]; if(fis.read(databuf) == databuf.length) { ByteBuffer bf = ByteBuffer.allocate(databuf.length + BTS_DataType.BYTE_LEN); bf.order(ByteOrder.LITTLE_ENDIAN); bf.put(dataType.getDataTypeByte()); bf.put(databuf); bf.flip(); bf.position(0); System.out.println(ComFn.bytesToHexString(databuf, databuf.length)); BTS_FbsData fbsData = new BTS_FbsData(); if(fbsData.putByteBuffer(bf, 0, 0)) { fbsDatas.add(fbsData); } } } if(tag == 1) break; } }else { parse_result = BTS_ComBase.PARSE_RESULT_FILEERROR; } } catch (IOException e) { e.printStackTrace(); } finally { if(null != fis) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 解析文件头数据 * @return */ public boolean checkTestDataHead(byte[] buf) { boolean checkOk = true; ByteBuffer buffer = ByteBuffer.allocate(buf.length); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.put(buf); buffer.flip(); buffer.position(0); checkOk = checkOk && battParam.putByteBuffer(buffer); System.out.println("checkTestDataHead:"+checkOk); checkOk = checkOk && chargeParam.putByteBuffer(buffer); System.out.println("checkTestDataHead:"+checkOk); charState.putByteBuffer(buffer); return checkOk; } }