whyclxw
2025-03-28 cc86bb05bba08200a2e2bc301bb835f52a708a1f
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package com.whyc.dto;
 
 
import com.whyc.pojo.*;
import com.whyc.util.MathUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
 
import java.io.*;
import java.text.ParseException;
import java.util.*;
 
public class XmlFileOpreate {
    //解析FBR-6000NT基础2.7
    public static FileInfo readFBR_6000NT_jc(String xmlFilePath){
        FileInfo fileInfo=new FileInfo();//文件信息
        FileParam fparam=fileInfo.getFileParam();//文件参数
        boolean res = true;
        try
        {
            SAXReader reader = new SAXReader();
            FileInputStream  fiso=new FileInputStream(xmlFilePath);
            Document document = reader.read(fiso);
            Element rootnode = document.getRootElement();
 
            Element node;//根节点
            Element file_node;//文件节点
 
            //------------------------------------------------------------//
            node = rootnode.element("TEST_TIME");
            String testTime=node.getTextTrim();
            testTime=testTime.replace("_"," ");
            fparam.setTestTime(ActionUtil.sdfwithALL.parse(testTime));
            //------------------------------------------------------------//
            node = rootnode.element("VERSION");
            fparam.setVersion(node.getTextTrim());
            //------------------------------------------------------------//
            node = rootnode.element("BATT_PARAM");
            file_node=node.element("batt_group_name");
            fparam.setBattGroupName(file_node.getTextTrim());
 
            file_node=node.element("batt_producer");
            if(file_node!=null){
                fparam.setBattProducer(file_node.getTextTrim());
            }
            file_node=node.element("batt_cap");
            fparam.setBattCap(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("batt_count");
            fparam.setBattCount(Integer.parseInt(file_node.getTextTrim()));
 
            file_node=node.element("batt_vol");
            fparam.setBattVol(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("batt_vol_good");
            if(file_node!=null){
                fparam.setBattVolGood(Float.parseFloat(file_node.getTextTrim()));
            }
            file_node=node.element("batt_vol_bad");
            if(file_node!=null){
                fparam.setBattVolBad(Float.parseFloat(file_node.getTextTrim()));
            }
 
            file_node=node.element("batt_res");
            if(file_node!=null){
                fparam.setBattRes(Float.parseFloat(file_node.getTextTrim()));
            }else{
                fparam.setBattRes(0f);
            }
 
            file_node=node.element("batt_res_good");
            if(file_node!=null){
                fparam.setBattResGood(Float.parseFloat(file_node.getTextTrim()));
            }
            file_node=node.element("batt_res_bad");
            if(file_node!=null){
                fparam.setBattResBad(Float.parseFloat(file_node.getTextTrim()));
            }
 
            file_node=node.element("batt_ser");
            fparam.setBattSer(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("batt_ser_good");
            if(file_node!=null){
                fparam.setBattSerGood(Float.parseFloat(file_node.getTextTrim()));
            }
            file_node=node.element("batt_ser_bad");
            if(file_node!=null){
                fparam.setBattSerBad(Float.parseFloat(file_node.getTextTrim()));
            }
 
            file_node=node.element("concount_pb");
            fparam.setConcountPb(Integer.parseInt(file_node.getTextTrim()));
 
            file_node=node.element("conn_res");
            if(file_node!=null){
                fparam.setConnRes(Float.parseFloat(file_node.getTextTrim()));
            }
            file_node=node.element("conn_res_good");
            if(file_node!=null){
                fparam.setConnResGood(Float.parseFloat(file_node.getTextTrim()));
            }
            file_node=node.element("conn_res_bad");
            if(file_node!=null){
                fparam.setConnResBad(Float.parseFloat(file_node.getTextTrim()));
            }
 
            float battCap=fparam.getBattCap();//标称容量
            float battRes=fparam.getBattRes();//标称内阻
            //-----根据groupNum的值读取测试数据---------------------------//
            Iterator nodes = rootnode.elementIterator("node_batt_num");
            BattgroupInfo battInfo=new BattgroupInfo();
            List<BattgroupData> battDataList=new ArrayList();
            //List<Float> list=new ArrayList();//存放所有的内阻数据
            List<Float> avglist=new ArrayList();//存放有效的内阻数据用于计算偏差率(当标称内阻为0时,用平内阻)
            int avgNum=0;
            if(nodes!=null){
                while (nodes.hasNext()){
                    node= (Element) nodes.next();
                    BattgroupData battData=new BattgroupData();
                    battData.setMonNum(Integer.parseInt(node.attributeValue("batt_num")));
                    file_node=node.element("bv_1");
                    battData.setBv(file_node.getTextTrim());
                    file_node=node.element("br_1");
                    String br=file_node.getTextTrim();
                    battData.setBr(br);
                    /*float esCap=0f;
                    if(battRes==0){
                        esCap=1;
                    }else{
                        esCap=battRes/Float.valueOf(br);
                    }
                    battData.setEstimatedCap(esCap*battCap);*/
                    /* float preCapPercent=MathUtil.getPreCapTest(Float.valueOf(br),battRes);
                    //容量百分比修改
                    battData.setPreCapPercent(preCapPercent);
                    //预估容量
                    battData.setEstimatedCap(preCapPercent*battCap);
                   //内阻偏差率设置默认值为0
                    if(battRes==0){
                        battData.setResDevRate(Float.POSITIVE_INFINITY);
                    }else{
                        float resDevRate=(Float.valueOf(br)-battRes)/battRes;
                        battData.setResDevRate(resDevRate);
                    }*/
                    file_node=node.element("bs_1");
                    battData.setBs(file_node.getTextTrim());
                    file_node=node.element("cr_1");
                    battData.setCr(file_node.getTextTrim());
                    battDataList.add(battData);
                    //list.add(Float.valueOf(battData.getBr()));
                    //获取有效内阻
                    if(Float.parseFloat(battData.getBv())>0&&Float.parseFloat(battData.getBr())>0){
                        avgNum++;
                        avglist.add(Float.parseFloat(battData.getBr()));
                    }
                }
            }
            battInfo.setBattDataList(battDataList);
            battInfo.setTestTime(ActionUtil.sdfwithALL.parse(testTime));
            fileInfo.getBattInfoList().add(battInfo);
            //当标称内阻为0时,计算最低三分之一单体数内阻数据的平均值
            if(fparam.getBattRes()==0){
                /*if(list!=null&&list.size()>0){
                    int battNum= (int) Math.ceil(list.size()/3);
                    float resSum=0f;
                    Collections.sort(list);
                    for(int i=0;i<battNum;i++){
                        resSum=resSum+list.get(i);
                    }
                    String avgRes=String.format("%.2f",resSum/battNum);
                    fparam.setBattRes(Float.valueOf(avgRes));
                }*/
                //计算平局内阻
                if(avglist!=null&&avglist.size()>0){
                    Collections.sort(avglist);
                    System.err.println(avglist.toString());
                    float sunBr=0f;
                    avgNum= (int) Math.ceil(0.01*avgNum*(100-fparam.getSamplePercent()));
                    int effNum=0;
                    for (int i=0;i<avglist.size();i++) {
                        if(i!=0&&i<=avgNum){
                            sunBr+=avglist.get(i);
                            effNum++;
                        }
                    }
                    String avgRes=String.format("%.2f",sunBr/effNum);
                    fparam.setBattRes(Float.valueOf(avgRes));
                }
            }
            float effRes=fparam.getBattRes();  //有效的标称内阻
            for (BattgroupData battData:battInfo.getBattDataList()) {
                float resDevRate=(Float.valueOf(battData.getBr())-effRes)/effRes;
                battData.setResDevRate(resDevRate);
                float preCapPercent=MathUtil.getPreCapTest(Float.valueOf(battData.getBr()),effRes);
                //容量百分比修改
                battData.setPreCapPercent(preCapPercent);
                //预估容量
                battData.setEstimatedCap(preCapPercent*battCap);
            }
            fileInfo.setFileParam(fparam);
            fiso.close();
        } catch (NullPointerException | NumberFormatException | DocumentException | ParseException | FileNotFoundException e) {
            res = false;
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(false == res)
                return null;
        }
        return fileInfo;
    }
    //解析FBR-6000NT带标记版本2.0
    public static FileInfo readFBR_6000NT_bj(String xmlFilePath){
        boolean res = true;
        FileInfo fileInfo=new FileInfo();//文件信息
        FileParam fparam=fileInfo.getFileParam();//文件参数
        try
        {
            SAXReader reader = new SAXReader();
            FileInputStream  fiso=new FileInputStream(xmlFilePath);
            Document document = reader.read(fiso);
            Element rootnode = document.getRootElement();
 
            Element node;//根节点
            Element file_node;//文件节点
            Element batt_node;//电池节点
 
            //------------------------------------------------------------//
            node = rootnode.element("TEST_TIME");
            String testTime=node.getTextTrim();
            testTime.replace("_"," ");
            fparam.setTestTime(ActionUtil.sdfwithALL.parse(testTime));
            //------------------------------------------------------------//
            node = rootnode.element("VERSION");
            fparam.setVersion(node.getTextTrim());
            //------------------------------------------------------------//
            node = rootnode.element("BATT_PARAM");
            file_node=node.element("uploadTime");
            String uploadTime=file_node.getTextTrim();
            if(!uploadTime.equals("null")){
                fparam.setUploadTime(ActionUtil.sdfwithALL.parse(uploadTime));
            }
            file_node=node.element("uploadflag");
            fparam.setUploadFlag(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("sysID");
            fparam.setSysId(file_node.getTextTrim());
            file_node=node.element("batt_group_name");
            fparam.setBattGroupName(file_node.getTextTrim());
            file_node=node.element("battBrand");
            fparam.setBattBrand(file_node.getTextTrim());
            file_node=node.element("battModel");
            fparam.setBattModel(file_node.getTextTrim());
            file_node=node.element("battBatch");
            fparam.setBattBatch(file_node.getTextTrim());
            file_node=node.element("battStation");
            fparam.setBattStation(file_node.getTextTrim());
            file_node=node.element("battlineName");
            fparam.setBattlineName(file_node.getTextTrim());
            file_node=node.element("battTHA");
            fparam.setBattTha(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("battErrFlag");
            fparam.setBattErrflag(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("battTestNum");
            fparam.setBattTestnum(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("battFaultNum");
            fparam.setBattFaultnum(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("batt_cap");
            fparam.setBattCap(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("batt_count");
            fparam.setBattCount(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("batt_vol");
            fparam.setBattVol(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("batt_res");
            if(file_node!=null){
                fparam.setBattRes(Float.parseFloat(file_node.getTextTrim()));
            }else{
                fparam.setBattRes(0f);
            }
 
            file_node=node.element("gropNum");
            fparam.setGroupNum(Integer.parseInt(file_node.getTextTrim()));
 
            //------------------------------------------------------------//
            node = rootnode.element("TEST_PARAM");
            file_node=node.element("VolLowCoeK1");
            fparam.setVolLowCoeK1(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("VolHighCoeK2");
            fparam.setVolHighCoeK2(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("ResGoodCoeK3");
            fparam.setResGoodCoeK3(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("ResBadCoeK4");
            fparam.setResBadCoeK4(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("sample_percent");
            fparam.setSamplePercent(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("HighTempAlarm");
            fparam.setHighTempAlarm(Integer.parseInt(file_node.getTextTrim()));
            file_node=node.element("ChainRes");
            fparam.setChainRes(Float.parseFloat(file_node.getTextTrim()));
            file_node=node.element("evaluation_mode");
            fparam.setEvaluationMode(Integer.parseInt(file_node.getTextTrim()));
 
            //查询出当前最大电池组id
            //System.out.println("groupName:"+fparam.getGroupNum()+"   battCount:"+fparam.getBattCount());
 
            //-----根据groupNum的值读取测试数据---------------------------//
            Iterator nodes = rootnode.elementIterator("node_group_num");
 
            float battCap=fparam.getBattCap();//标称容量
            float battRes=fparam.getBattRes();//标称内阻
 
            if(nodes!=null){
                while (nodes.hasNext()){
                    node= (Element) nodes.next();
                    Iterator iterator1 = node.elementIterator();
                    BattgroupInfo battInfo=new BattgroupInfo();
                    List<BattgroupData> battDataList=new ArrayList();
                    //List<Float> list=new ArrayList();//存放所有的内阻数据
                    List<Float> avglist=new ArrayList();//存放有效的内阻数据用于计算偏差率(当标称内阻为0时,用平内阻)
                    int avgNum=0;
                    while (iterator1.hasNext()){
                        file_node= (Element) iterator1.next();
                        BattgroupData battData=new BattgroupData();
                        battData.setMonNum(Integer.parseInt(file_node.attributeValue("batt_num")));
                        batt_node=file_node.element("bv_1");
                        battData.setBv(batt_node.getTextTrim());
                        batt_node=file_node.element("br_1");
                        String br=batt_node.getTextTrim();
                        battData.setBr(br);
                       /* float esCap=0f;
                        if(battRes==0){
                            esCap=1;
                        }else{
                            esCap=battRes/Float.valueOf(br);
                        }
                        battData.setEstimatedCap(esCap*battCap);*/
                        /*float preCapPercent=MathUtil.getPreCapTest(Float.valueOf(br),battRes);
                        //容量百分比修改
                        battData.setPreCapPercent(preCapPercent);
                        //预估容量
                        battData.setEstimatedCap(preCapPercent*battCap);
                        //内阻偏差率
                        if(battRes==0){
                            battData.setResDevRate(Float.POSITIVE_INFINITY);
                        }else{
                            float resDevRate=(Float.valueOf(br)-battRes)/battRes;
                            battData.setResDevRate(resDevRate);
                        }*/
 
                        batt_node=file_node.element("bs_1");
                        battData.setBs(batt_node.getTextTrim());
                        batt_node=file_node.element("cr_1");
                        battData.setCr(batt_node.getTextTrim());
                        batt_node=file_node.element("er_1");
                        battData.setEr(Integer.parseInt(batt_node.getTextTrim()));
                        battDataList.add(battData);
                        //list.add(Float.valueOf(battData.getBr()));
                        //获取有效内阻
                        if(Float.parseFloat(battData.getBv())>0&&Float.parseFloat(battData.getBr())>0){
                            avgNum++;
                            avglist.add(Float.parseFloat(battData.getBr()));
                        }
                    }
                    battInfo.setTestTime(ActionUtil.sdfwithALL.parse(testTime));
                    battInfo.setBattDataList(battDataList);
                    fileInfo.getBattInfoList().add(battInfo);
                    //当标称内阻为0时,计算最低三分之一单体数内阻数据的平均值
                    /*if(fparam.getBattRes()==0){
                        if(list!=null&&list.size()>0){
                            int battNum= (int) Math.ceil(list.size()/3);
                            float resSum=0f;
                            Collections.sort(list);
                            for(int i=0;i<battNum;i++){
                                resSum=resSum+list.get(i);
                            }
                            String avgRes=String.format("%.2f",resSum/battNum);
                            fparam.setBattRes(Float.valueOf(avgRes));
                        }
                    }*/
                    if(fparam.getBattRes()==0){
                        //计算平局内阻
                        if(avglist!=null&&avglist.size()>0){
                            Collections.sort(avglist);
                            System.err.println(avglist.toString());
                            float sunBr=0f;
                            avgNum= (int) Math.ceil(0.01*avgNum*(100-fparam.getSamplePercent()));
                            int effNum=0;
                            for (int i=0;i<avglist.size();i++) {
                                if(i!=0&&i<=avgNum){
                                    sunBr+=avglist.get(i);
                                    effNum++;
                                }
                            }
                            String avgRes=String.format("%.2f",sunBr/effNum);
                            fparam.setBattRes(Float.valueOf(avgRes));
                        }
                    }
                    float effRes=fparam.getBattRes();  //有效的标称内阻
                    for (BattgroupData battData:battInfo.getBattDataList()) {
                        float resDevRate=(Float.valueOf(battData.getBr())-effRes)/effRes;
                        battData.setResDevRate(resDevRate);
                        float preCapPercent=MathUtil.getPreCapTest(Float.valueOf(battData.getBr()),effRes);
                        //容量百分比修改
                        battData.setPreCapPercent(preCapPercent);
                        //预估容量
                        battData.setEstimatedCap(preCapPercent*battCap);
                    }
                }
            }
            fileInfo.setFileParam(fparam);
            fiso.close();
        } catch (NullPointerException | NumberFormatException | DocumentException | ParseException | FileNotFoundException e) {
            res = false;
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(false == res)
                return null;
        }
        return  fileInfo;
    }
 
 
    //解析xml文件
    public static FileInfo readXml(String xmlFilePath)
    {
        FileInfo fileInfo=new FileInfo();
        try
        {
            SAXReader reader = new SAXReader();
            FileInputStream  fiso=new FileInputStream(xmlFilePath);
            Document document = reader.read(fiso);
            Element rootnode = document.getRootElement();
 
            //------------------------------------------------------------//
            Element node = rootnode.element("VERSION");
            if(node!=null){
                String version=node.getTextTrim();
                switch (version){
                    case "V2.0": fileInfo=readFBR_6000NT_bj(xmlFilePath);break;
                    case "V2.7": fileInfo=readFBR_6000NT_jc(xmlFilePath);break;
                    default:fileInfo=readFBR_6000NT_bj(xmlFilePath);break;
                }
            }
            fiso.close();
        } catch (NullPointerException | NumberFormatException | DocumentException  | FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  fileInfo;
    }
 
    public static boolean writeXml( Map<String,String> map,String xmlFilePath){
        boolean res=true;
        try {
            SAXReader reader = new SAXReader();
            FileInputStream fiso= new FileInputStream(xmlFilePath);
            Document document = reader.read(fiso);
 
            for (Map.Entry<String, String> entry:map.entrySet()) {
                String mapKey = entry.getKey();
                String mapValue = entry.getValue();
                //System.out.println(mapKey+":"+mapValue);
                //得到第一个mapKey节点
                Node node=document.selectSingleNode("//"+mapKey);
                if(node!=null){
                    node.setText(mapValue);
                }
            }
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");//默认的编码就是UTF-8
            XMLWriter writer = new XMLWriter( new FileOutputStream(xmlFilePath), format );
            writer.write( document );
            writer.close();
            fiso.close();
        } catch (DocumentException | FileNotFoundException | UnsupportedEncodingException e) {
            res = false;
            e.printStackTrace();
        } catch (IOException e) {
            res = false;
            e.printStackTrace();
        }
        return res;
    }
 
    public static void main(String[] args) {
        String filePath="D:\\fileTest\\FBR-5000CT -原数据20221031\\\\2022-10-31_09-14-48.xml";
        Map map=new HashMap() ;
        FileInfo fileInfo=XmlFileOpreate.readXml(filePath);
        System.out.println(fileInfo);
        map.put("ChainRes", "50.0");
        map.put("battTestNum","9");
        map.put("battModel","SD-001");
        map.put("batt_res","4.0");
        map.put("ResBadCoeK4","1.6");
        map.put("battlineName","一号线");
        map.put("evaluation_mode","0");
        map.put("TEST_TIME","2022-06-13 16:06:19");
        map.put("battErrFlag","1");
        map.put("gropNum","2");
        map.put("VolHighCoeK2","1.2");
        map.put("battBatch","20170602");
        map.put("batt_vol","12.0");
        map.put("batt_cap","100.0");
        map.put("sysID","B20220613160151");
        map.put("batt_count","24");
        map.put("uploadflag","0");
        map.put("uploadTime","null");
        map.put("battTHA","0");
        map.put("VolLowCoeK1","0.8");
        map.put("battFaultNum","2");
        map.put("HighTempAlarm","50");
        map.put("VERSION","V2.0");
        map.put("ResGoodCoeK3","1.25");
        map.put("sample_percent","30");
        map.put("VERSION","V2.0");
        map.put("battBrand","双登lxw");
        map.put("batt_group_name","后备电源室1#UPS系统lxw");
        map.put("battStation","鼓楼站lxw");
 
        //XmlFileOpreate.writeXml(map,filePath);
    }
}