From b46f7fa6d77852ae9e28a062da234a2ddf9405fb Mon Sep 17 00:00:00 2001 From: whyclxw <810412026@qq.com> Date: 星期一, 31 三月 2025 16:09:51 +0800 Subject: [PATCH] 2025-3-22定制修改导出去掉容量和连接条 --- src/main/java/com/whyc/dto/XmlFileOpreate.java | 450 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 395 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/whyc/dto/XmlFileOpreate.java b/src/main/java/com/whyc/dto/XmlFileOpreate.java index bd7ccb4..1911faf 100644 --- a/src/main/java/com/whyc/dto/XmlFileOpreate.java +++ b/src/main/java/com/whyc/dto/XmlFileOpreate.java @@ -2,9 +2,11 @@ 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; @@ -14,17 +16,213 @@ import java.util.*; public class XmlFileOpreate { + //瑙f瀽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(); - //瑙f瀽xml鏂囦欢 - public static FileInfo readXml(String xmlFilePath) - { + 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; + } + //瑙f瀽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(); - Document document = reader.read(new File(xmlFilePath)); + FileInputStream fiso=new FileInputStream(xmlFilePath); + Document document = reader.read(fiso); Element rootnode = document.getRootElement(); Element node;//鏍硅妭鐐� @@ -34,6 +232,7 @@ //------------------------------------------------------------// node = rootnode.element("TEST_TIME"); String testTime=node.getTextTrim(); + testTime.replace("_"," "); fparam.setTestTime(ActionUtil.sdfwithALL.parse(testTime)); //------------------------------------------------------------// node = rootnode.element("VERSION"); @@ -76,7 +275,12 @@ file_node=node.element("batt_vol"); fparam.setBattVol(Float.parseFloat(file_node.getTextTrim())); file_node=node.element("batt_res"); - fparam.setBattRes(Float.parseFloat(file_node.getTextTrim())); + 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())); @@ -104,33 +308,114 @@ //-----鏍规嵁groupNum鐨勫�艰鍙栨祴璇曟暟鎹�---------------------------// Iterator nodes = rootnode.elementIterator("node_group_num"); - while (nodes.hasNext()){ - node= (Element) nodes.next(); - Iterator iterator1 = node.elementIterator(); - BattgroupInfo battInfo=new BattgroupInfo(); - List<BattgroupData> battDataList=new ArrayList(); - 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(Float.parseFloat(batt_node.getTextTrim())); - batt_node=file_node.element("br_1"); - battData.setBr(Float.parseFloat(batt_node.getTextTrim())); - batt_node=file_node.element("bs_1"); - battData.setBs(Float.parseFloat(batt_node.getTextTrim())); - batt_node=file_node.element("cr_1"); - battData.setCr(Float.parseFloat(batt_node.getTextTrim())); - batt_node=file_node.element("er_1"); - battData.setEr(Integer.parseInt(batt_node.getTextTrim())); - battDataList.add(battData); + + 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); + } } - battInfo.setBattDataList(battDataList); - fileInfo.getBattInfoList().add(battInfo); } fileInfo.setFileParam(fparam); - } catch (NullPointerException | NumberFormatException | DocumentException | ParseException e) { + fiso.close(); + } catch (NullPointerException | NumberFormatException | DocumentException | ParseException | FileNotFoundException e) { res = false; + e.printStackTrace(); + } catch (IOException e) { e.printStackTrace(); } finally { if(false == res) @@ -139,32 +424,60 @@ return fileInfo; } - public static boolean writeXml(String xmlFilePath, Map<String,String> map){ + + //瑙f瀽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(); - Document document = reader.read(new File(xmlFilePath)); - //鑾峰彇鏍瑰厓绱� - Element rootElement = document.getRootElement(); - //鑾峰彇鏍瑰厓绱犱笅鐨勬墍鏈夊瓙鍏冪礌 - List<Element> list = rootElement.elements(); - if(list!=null&&map!=null){ - for (Element e:list) { - for (Map.Entry<String, String> entry:map.entrySet()) { - String mapKey = entry.getKey(); - String mapValue = entry.getValue(); - if(e.getName().equals(mapKey)){ - e.setText(mapValue); - } - } + 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); + //寰楀埌绗竴涓猰apKey鑺傜偣 + Node node=document.selectSingleNode("//"+mapKey); + if(node!=null){ + node.setText(mapValue); } - OutputFormat format = OutputFormat.createPrettyPrint(); - //format.setEncoding("UTF-8");//榛樿鐨勭紪鐮佸氨鏄疷TF-8 - XMLWriter writer = new XMLWriter( new FileOutputStream(xmlFilePath), format ); - writer.write( document ); - }else{ - res=false; } + OutputFormat format = OutputFormat.createPrettyPrint(); + format.setEncoding("UTF-8");//榛樿鐨勭紪鐮佸氨鏄疷TF-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(); @@ -176,13 +489,40 @@ } public static void main(String[] args) { - String filePath="D://2022-06-14 16-02-34.xml"; - String filePath1="D://test.xml"; + 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 map=new HashMap(); - map.put("test1","lxw"); - map.put("test2","hw"); - //XmlFileOpreate.writeXml(filePath1,map); + 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","榧撴ゼ绔檒xw"); + + //XmlFileOpreate.writeXml(map,filePath); } } -- Gitblit v1.9.1