Administrator
2023-02-07 3b359b5a250b6383ce111980f28a719e16446e25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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<AlarmData> 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<Alarm_DataInfo.AlarmData>();
    }
    
    
    @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<sYNCode.length;i++) {
                sYNCode[i] = ComBase.changeByteToInt(bf.get());
            }
            record_line = ComBase.changeShortToInt(bf.getShort());
            //System.out.println("行数:"+record_line);
            return true;
        }
        
        public boolean checkDataHead(FileInputStream fis)
        {
            boolean check_ok = false;
            boolean file_end = false;
            byte[] tag = new byte[1];        
            try {
                while(true)
                {
                    int n = 0;
                    for(n=0; n<2; n++)
                    {
                        if(1 != fis.read(tag, 0, 1))
                        {
                            file_end = true;
                            break;
                        }
                        if((0xAA != (tag[0]&0xFF)))
                        {
                            break;
                        }
                    }
                    
                    if(n >= 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;
        }
        
    }
 
    
}