package com.fgkj.alm; 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.Date; import java.util.List; import com.fgkj.bres.FileDataParseInfo_Interface; import com.fgkj.data.ComBase; public class Alarm_DataInfo implements FileDataParseInfo_Interface{ public AlarmDataHead almDataHead; public List almDatas; public int parse_result = PARSE_RESULT_NULL; //文件解析结果 public final int file_type = FILE_TYPE_ALM; //文件类型 public Alarm_DataInfo() { almDataHead = new AlarmDataHead(); almDatas = new ArrayList(); } @Override public void readFileData(String filePath) { FileInputStream fis = null; try { File f = new File(filePath); if(!filePath.endsWith(".alm") && !filePath.endsWith(".ALM")) { System.out.println("文件格式错误"); parse_result = PARSE_RESULT_FILETYPEERR; return; } if(!f.exists()) { parse_result = PARSE_RESULT_NOTFOUNDFILE; System.out.println("文件不存在.........."); return; } fis = new FileInputStream(f); byte[] buf = new byte[4]; if(fis.read(buf, 0, buf.length) == AlarmDataHead.BYTE_LEN) { if(this.almDataHead.setHeadData(buf)) { parse_result = PARSE_RESULT_SUCCESS; }else { parse_result = PARSE_RESULT_FILEERROR; } while(true) { if(almDataHead.checkDataHead(fis)) { byte[] databuf = new byte[AlarmData.BYTE_LEN]; if(fis.read(databuf) == databuf.length) { AlarmData almData = new AlarmData(); if(almData.setData(databuf)) { //System.out.println(resData); almDatas.add(almData); } } } if(fis.available() <1) { //System.out.println("解析完成"); break; } } }else { parse_result = PARSE_RESULT_FILEERROR; } } catch (IOException e) { e.printStackTrace(); } finally { if(null != fis) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } //告警头部数据 public class AlarmDataHead{ public static final int BYTE_LEN = 4; public int[] sYNCode = new int[2]; //固定值0xAA 0xAA public int record_line; public boolean setHeadData(byte[] databuf) { if(databuf.length < BYTE_LEN) { return false; } ByteBuffer bf = ByteBuffer.allocate(databuf.length); bf.order(ByteOrder.LITTLE_ENDIAN); bf.put(databuf); bf.position(0); for(int i=0;i= 2) { check_ok = true; break; } if(true == file_end) { break; } } } catch (IOException e) { //e.printStackTrace(); } return check_ok; } } //告警详情 public class AlarmData{ public static final int BYTE_LEN = 18; public static final int DATABYTE_LEN = 20; public int[] sYNCode = new int[2]; //固定值0xAA 0xAA public int alarmType; //告警类型 public int groupIndex; //组号索引 public int battIndex; //单体号索引 public float alarmValue; //告警值 public DATE_TIME startTime; //告警开始时间 public DATE_TIME endTime; //告警结束时间 public boolean setData(byte[] databuf) { ByteBuffer bf = ByteBuffer.allocate(BYTE_LEN); bf.order(ByteOrder.LITTLE_ENDIAN); //System.out.println(databuf.length); bf.put(databuf); if(databuf.length < BYTE_LEN) { //System.out.println("长度错误"); return false; } //System.err.println(ComFn.bytesToHexString(databuf, databuf.length)); bf.position(0); alarmType = ComBase.changeByteToInt(bf.get()); groupIndex = ComBase.changeByteToInt(bf.get()); battIndex = ComBase.changeShortToInt(bf.getShort()); alarmValue = ComBase.changeShortToFloat(bf.getShort()); startTime.setData(bf); endTime.setData(bf); return true; } } //时间 public class DATE_TIME{ public int year; public int month; public int day; public int hour; public int minute; public int second; public Date time; public boolean setData(ByteBuffer bf) { if(bf.remaining() >= 6) { year = ComBase.changeByteToInt(bf.get()); month = ComBase.changeByteToInt(bf.get()); day = ComBase.changeByteToInt(bf.get()); hour = ComBase.changeByteToInt(bf.get()); minute = ComBase.changeByteToInt(bf.get()); second = ComBase.changeByteToInt(bf.get()); return true; } return false; } } }