whycrzg
2021-02-23 351b9a53cb9ecebdf8f79db0117f540d9c42c2a4
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package com.fgkj.fbo;
 
import com.fgkj.dto.Com;
import com.fgkj.mapper.BattTestData;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
import java.io.*;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
 
import static com.fgkj.mapper.BattTestData.BATT_MON_COUNT_MAX;
 
 
public class CsvData {
    public static final int DevType_Csv = 0x45;
    public static final int DevType_FBR = 0x46;
    
    public boolean m_DataOk = true;
    public String m_BattGroupNameFull = "";
    public int m_STDAH;                        //���������
    public int m_BattSum;                      //��ظ�����
    public float m_MonomerVol = 2;
    public int m_DevType = 0;
    public int m_TestType = 0;
    public Date m_TestTime = new Date();
    public float m_GroupVol;
    public float m_TestCurr;
    
    public int m_MinNum = 0;
    public float m_MinValue = 0;
    
    public float[] MonomerVol = new float[BATT_MON_COUNT_MAX];
    public float[] MonomerRes = new float[BattTestData.BATT_MON_COUNT_MAX];
    public float[] MonomerSer = new float[BattTestData.BATT_MON_COUNT_MAX];
    public float[] MonomerTmp = new float[BattTestData.BATT_MON_COUNT_MAX];
    public float[] MonomerConn = new float[BattTestData.BATT_MON_COUNT_MAX];
    
    public void setData(File file)
    {
        //System.out.println(111111111);
        m_DataOk = true;
        BufferedReader reader = null;
        try {
            m_TestType = BattTestData.BATTDATA_SER;
            m_DevType = DevType_Csv;
            
            reader = new BufferedReader(new FileReader(file));
            //-------------------------------------------------------------------//
            //-------------------------------------------------------------------//
            String tmpstr = reader.readLine();
            //-------------------------------------------------------------------//
            //-------------------------------------------------------------------//
            tmpstr = reader.readLine();
            for(int n=0; n<4; n++) {
                tmpstr = tmpstr.substring(tmpstr.indexOf(",")+1, tmpstr.length());
            }
            if(tmpstr.contains(",")) {
                tmpstr = tmpstr.substring(0, tmpstr.indexOf(","));
            } else if(tmpstr.contains("\r\n")) {
                tmpstr = tmpstr.substring(0, tmpstr.indexOf("\r\n"));
            }
            //System.out.println(tmpstr);
            m_BattSum = Integer.parseInt(tmpstr.trim());
            //-------------------------------------------------------------------//
            //-------------------------------------------------------------------//
            tmpstr = reader.readLine();
            for(int n=0; n<4; n++) {
                tmpstr = tmpstr.substring(tmpstr.indexOf(",")+1, tmpstr.length());
            }
            tmpstr = tmpstr.substring(0, tmpstr.indexOf(","));
            m_MonomerVol = Float.parseFloat(tmpstr.trim());
            if((m_MonomerVol>10.1) && (m_MonomerVol<20)) {
                m_MonomerVol = 12;
            } else if((m_MonomerVol>5.4) && (m_MonomerVol<7.8)) {
                m_MonomerVol = 6;
            } else if((m_MonomerVol>2.7) && (m_MonomerVol<4.0)) {
                m_MonomerVol = 4;
            } else {
                m_MonomerVol = 2;
            } 
            //-------------------------------------------------------------------//
            //-------------------------------------------------------------------//
            tmpstr = reader.readLine();
            for(int n=0; n<4; n++) {
                tmpstr = tmpstr.substring(tmpstr.indexOf(",")+1, tmpstr.length());
            }
            int month = Integer.parseInt(tmpstr.substring(0, tmpstr.indexOf("/")).trim());
            tmpstr = tmpstr.substring(tmpstr.indexOf("/")+1, tmpstr.length());
            
            int day = Integer.parseInt(tmpstr.substring(0, tmpstr.indexOf("/")).trim());
            tmpstr = tmpstr.substring(tmpstr.indexOf("/")+1, tmpstr.length());
            
            int year = Integer.parseInt(tmpstr.substring(0, tmpstr.indexOf(",")).trim());
            tmpstr = tmpstr.substring(tmpstr.indexOf(",")+1, tmpstr.length());
            
            int hour = Integer.parseInt(tmpstr.substring(0, tmpstr.indexOf(":")).trim());
            tmpstr = tmpstr.substring(tmpstr.indexOf(":")+1, tmpstr.length());
            
            int minute = Integer.parseInt(tmpstr.substring(0, 2).trim());
            tmpstr = tmpstr.substring(3, tmpstr.length());
            if(tmpstr.substring(0, tmpstr.indexOf(",")).trim().equals("PM"))
            {
                hour += 12;
            }
            GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
            gc.set(GregorianCalendar.YEAR, year);
            gc.set(GregorianCalendar.MONTH, month-1);
            gc.set(GregorianCalendar.DATE, day);
            gc.set(GregorianCalendar.HOUR_OF_DAY, hour);
            gc.set(GregorianCalendar.MINUTE, minute);
            gc.set(GregorianCalendar.SECOND, 0);
            m_TestTime = gc.getTime();
            //-------------------------------------------------------------------//
            //-------------------------------------------------------------------//
            tmpstr = reader.readLine();
            reader.mark(0);
            //-------------------------------------------------------------------//
            //-------------------------------------------------------------------//
            tmpstr = reader.readLine() + reader.readLine() + reader.readLine() + reader.readLine();
            int sub_con_count = 1;
            if (tmpstr.contains("J1D")) {
                sub_con_count = 4;
            } else if (tmpstr.contains("J1C")) {
                sub_con_count = 3;
            } else if (tmpstr.contains("J1B")) {
                sub_con_count = 2;
            }
            m_BattSum /= sub_con_count;
            //-------------------------------------------------------------------//
            reader.reset();
            //-------------------------------------------------------------------//
            m_MinValue = 900000;
            for(int n=0; n<m_BattSum; n++) {
                float tmpser = 0;
                float tmpvol = 0;
                for(int cnt=0; cnt<sub_con_count; cnt++) {
                    tmpstr = reader.readLine();
                    if(null != tmpstr) {
                        tmpstr = tmpstr.substring(tmpstr.indexOf(",")+1, tmpstr.length());
                        tmpser += Float.parseFloat(tmpstr.substring(0, tmpstr.indexOf(",")));
                        
                        tmpstr = tmpstr.substring(tmpstr.indexOf(",")+1, tmpstr.length());
                        tmpvol += Float.parseFloat(tmpstr.substring(0, tmpstr.indexOf(",")));
                    } else {
                        m_DataOk = false;
                        break;
                    }
                }
                
                if(false == m_DataOk) {
                    break;
                }
                
                MonomerSer[n] = tmpser / sub_con_count;
                MonomerVol[n] = tmpvol / sub_con_count ;
                
                MonomerRes[n] = 0;
                if(MonomerSer[n] > 1) {
                    MonomerRes[n] = 1000 / (MonomerSer[n] / (m_MonomerVol/2));
                }
                
                if(MonomerSer[n] < m_MinValue) {
                    m_MinValue = MonomerSer[n];
                    m_MinNum = n+1;
                }
            }
            //-------------------------------------------------------------------//
        } catch (Exception e) {
            m_DataOk = false;
            System.err.println("Class 'CsvData' function setData(): " + e.getMessage());
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    public void checkCsvFile(File file)
    {
        setData(file);
    }
    
    private String getNodeTextEx(Object ob, Element rootnode, String node_name) {
        String text = String.valueOf(ob);
        try 
        {
            Element node = rootnode.element(node_name);
            text = node.getTextTrim();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        
        return text;
    }
    
    public void checkXmlFile(File file)
    {
        m_DataOk = false;        
        try {
            SAXReader reader = new SAXReader();
            Document document = reader.read(file);
            Element rootnode = document.getRootElement();
            
            m_TestType = BattTestData.BATTDATA_RES;
            m_DevType = DevType_FBR;
            
            String test_time = getNodeTextEx("1990-01-01_00:00:00", rootnode, "TEST_TIME");
            if(test_time.contains("1990-01-01")) {
                m_DataOk = false;
                return;
            }
            
            m_TestTime = Com.get_DT_FromStr(test_time.replace("_", " ").trim(), Com.DTF_YMDhms);
            
            //String data_version = Config.getNodeTextEx("V1.0", rootnode, "VERSION");
            
            Element sub_node = rootnode.element("BATT_PARAM");
            m_BattSum = Integer.parseInt(getNodeTextEx("0", sub_node, "batt_count"));
            m_MonomerVol = Float.parseFloat(getNodeTextEx("0.0", sub_node, "batt_vol"));
            m_STDAH = Integer.parseInt(getNodeTextEx("0", sub_node, "batt_cap"));
            int concount_pb = Integer.parseInt(getNodeTextEx("1", sub_node, "concount_pb"));
                
            
            m_MinValue = 900000;
            for(int n=0; n<m_BattSum; n++) {
                @SuppressWarnings("unchecked")
                List<Element> childElements = rootnode.elements();
                int cnt = 0;
                for(cnt=0; cnt<childElements.size(); cnt++) {
                    Element child = childElements.get(cnt);
                    if(null == child.attributeValue("batt_num")) {
                        continue;
                    }
                    if(Integer.parseInt(child.attributeValue("batt_num").trim()) == (n+1)) {
                        float tmp_var_res = 0;
                        float tmp_var_vol = 0;
                        float tmp_var_ser = 0;
                        float tmp_var_con = 0;
                        for(int c=0; c<concount_pb; c++) {
                            tmp_var_res += Float.parseFloat(getNodeTextEx("0.0", child, "br_" + (c+1)));
                            tmp_var_vol += Float.parseFloat(getNodeTextEx("0.0", child, "bv_" + (c+1)));
                            tmp_var_ser += Float.parseFloat(getNodeTextEx("0.0", child, "bs_" + (c+1)));
                            tmp_var_con += Float.parseFloat(getNodeTextEx("0.0", child, "cr_" + (c+1)));
                        }
                        tmp_var_res /= concount_pb;
                        tmp_var_vol /= concount_pb;
                        tmp_var_ser /= concount_pb;
                        tmp_var_con /= concount_pb;
                        MonomerRes[n] = tmp_var_res;
                        MonomerVol[n] = tmp_var_vol;
                        MonomerSer[n] = tmp_var_ser;
                        MonomerConn[n] = tmp_var_con;
                        break;
                    }
                }
                
                if(cnt < childElements.size()) {
                    m_DataOk = true;
                }
                
                if(MonomerSer[n] < m_MinValue) {
                    m_MinValue = MonomerSer[n];
                    m_MinNum = n+1;
                }
            }
        } catch (NumberFormatException e) {
            m_DataOk = false;
            e.printStackTrace();
        } catch (DocumentException e) {
            m_DataOk = false;
            e.printStackTrace();
        }
    }
    
    
    
    public void checkFgxlsFile(File file)
    {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            HSSFWorkbook workbook = new HSSFWorkbook(fis);
            HSSFSheet rs = workbook.getSheetAt(0);
            m_BattGroupNameFull=rs.getRow(0).getCell(0).getStringCellValue().trim().replace("---", "-");
            String bg_inf = rs.getRow(0).getCell(1).getStringCellValue().trim().replace("---", "-");
            //m_BattGroupNameFull = rs.getCell(0, 0).getContents().trim().replace("---", "-");
            //System.out.println(m_BattGroupNameFull);
            //String bg_inf = rs.getCell(0, 1).getContents().trim();
            //System.out.println(bg_inf);
            m_TestType = DevType_Csv;
            m_DevType = DevType_Csv;
            m_MonomerVol = Float.parseFloat(bg_inf.substring(bg_inf.indexOf("�����ѹ:")+5, 
                                                bg_inf.indexOf("�ο��絼ֵ")-1).trim());
            m_BattSum = Integer.parseInt(bg_inf.substring(bg_inf.indexOf("������:")+4, 
                                                            bg_inf.indexOf("����ʱ��:")-1).trim());
            m_TestTime = Com.get_DT_FromStr(bg_inf.substring(bg_inf.indexOf("����ʱ��:")+5, 
                                                bg_inf.length()).trim(), Com.DTF_YMDhms);
            
            if(m_BattSum > 500) {
                m_BattSum = 500;
            }
            
            m_MinValue = 900000;
            for(int n=0; n<m_BattSum; n++) {
                MonomerSer[n] = Float.parseFloat(rs.getRow(1).getCell(n+3).getStringCellValue().trim());
                MonomerSer[n] = Float.parseFloat(rs.getRow(2).getCell(n+3).getStringCellValue().trim());
                //MonomerSer[n] = Float.parseFloat(rs.getCell(1, n+3).getContents().trim());
                //MonomerVol[n] = Float.parseFloat(rs.getCell(2, n+3).getContents().trim());
                MonomerRes[n] = 0;
                if(MonomerSer[n] > 1) {
                    MonomerRes[n] = 1000 / (MonomerSer[n] / (m_MonomerVol/2));
                }
                
                if(MonomerSer[n] < m_MinValue) {
                    m_MinValue = MonomerSer[n];
                    m_MinNum = n+1;
                }
            }
        } catch (IOException  e) {
            e.printStackTrace();
        } finally {
            if(null != fis)
            {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } 
    }
}