You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
backend/src/main/java/com/xydl/cac/util/IcdXmlUtil.java

285 lines
11 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.xydl.cac.util;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.xydl.cac.entity.IcdConfigType;
import com.xydl.cac.entity.IcdFile;
import com.xydl.cac.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import java.util.*;
public class IcdXmlUtil {
public static IcdFile loadIcdType(String xml) throws Exception {
XmlMapper xmlMapper = XmlMapper.builder()
.build();
JsonNode root;
try {
root = xmlMapper.readTree(xml);
} catch (Exception ex) {
throw new BusinessException("ICD文件解析失败请上传正确的xml类型文件");
}
IcdFile icdFile = IcdFile.builder()
.xml(xml)
.build();
LinkedHashMap<String, IcdConfigType> iedMap = processTypeRoot(root);
icdFile.setConfigList(new ArrayList<>(iedMap.values()));
return icdFile;
}
private static LinkedHashMap<String, IcdConfigType> processTypeRoot(JsonNode root) {
LinkedHashMap<String, IcdConfigType> iedMap = new LinkedHashMap<>();
Map<String, JsonNode> mapLNodeType = buildLNodeTypeMap(root);
Map<String, JsonNode> mapDOType = buildDOTypeMap(root);
Map<String, JsonNode> mapDAType = buildDATypeMap(root);
List<JsonNode> iedList = findNodes(root, "IED");
for (JsonNode iedNode : iedList) {
processIEDNode(iedMap, iedNode,
mapLNodeType, mapDOType, mapDAType);
}
List<JsonNode> apList = findNodes(root, "ConnectedAP");
for (JsonNode apNode : apList) {
String iedName = apNode.get("iedName").asText();
IcdConfigType config = iedMap.get(iedName);
if (config != null) {
processAPNode(apNode, config);
}
}
return iedMap;
}
private static void processAPNode(JsonNode apNode, IcdConfigType config) {
List<JsonNode> pList = findNodes(apNode, "P");
for (JsonNode pNode : pList) {
String type = pNode.get("type").asText();
if ("IP".equalsIgnoreCase(type)) {
config.setIp(pNode.asText());
} else if ("OSI-AP-Title".equalsIgnoreCase(type)) {
config.setApTitle(pNode.asText());
}
}
}
private static void processIEDNode(LinkedHashMap<String, IcdConfigType> iedMap, JsonNode iedNode,
Map<String, JsonNode> mapLNodeType, Map<String, JsonNode> mapDOType, Map<String, JsonNode> mapDAType) {
String iedName = iedNode.get("name").asText();
List<JsonNode> devList = findNodes(iedNode, "LDevice");
for (JsonNode dev : devList) {
processTypeDeviceNode(iedMap, iedName, dev,
mapLNodeType, mapDOType, mapDAType);
}
}
private static void processTypeDeviceNode(LinkedHashMap<String, IcdConfigType> iedMap, String iedName, JsonNode deviceNode,
Map<String, JsonNode> mapLNodeType, Map<String, JsonNode> mapDOType, Map<String, JsonNode> mapDAType) {
String ldeviceInst = deviceNode.get("inst").asText();
Map<String, JsonNode> mapLN = buildLNMap(deviceNode);
List<JsonNode> fcdaList = findNodes(deviceNode, "FCDA");
for (JsonNode fcdaNode : fcdaList) {
String lnClass = fcdaNode.get("lnClass").asText();
String lnInst = fcdaNode.get("lnInst").asText();
String doName = fcdaNode.get("doName").asText();
String fc = fcdaNode.get("fc").asText();
JsonNode lnNode = mapLN.get(lnClass + lnInst);
String lnType = lnNode.get("lnType").asText();
JsonNode nodeLnDesc = lnNode.get("desc");
String lnDesc = null;
if (nodeLnDesc != null) {
lnDesc = nodeLnDesc.asText();
}
JsonNode nodeLNodeType = mapLNodeType.get(lnType);
JsonNode nodeDO = findLNodeType_DO_Node(nodeLNodeType, doName);
String doType = nodeDO.get("type").asText();
JsonNode nodeDoDesc = nodeDO.get("desc");
String doDesc = null;
if (nodeDoDesc != null) {
doDesc = nodeDoDesc.asText();
}
JsonNode nodeDOType = mapDOType.get(doType);
String lastname = findLastname(nodeDOType, fc, mapDAType);
String key = iedName + ldeviceInst + "/" + lnClass;
if ("MX".equals(fc)) {
IcdConfigType config = iedMap.get(key);
if (config == null) {
config = IcdConfigType.builder()
.iedName(iedName)
.ldeviceInst(ldeviceInst)
.lnClass(lnClass)
.lnDesc(lnDesc)
.build();
iedMap.put(key, config);
}
String param = fc + "$" + doName + "$" + lastname;
config.addInst(lnInst);
config.addAtt(doName, doDesc, param, lnInst);
} else if ("ST".equals(fc)) {
IcdConfigType config = iedMap.get(key);
if (config == null) {
config = IcdConfigType.builder()
.iedName(iedName)
.ldeviceInst(ldeviceInst)
.lnClass(lnClass)
.build();
iedMap.put(key, config);
}
String param = fc + "$" + doName + "$" + lastname;
config.addInst(lnInst);
config.addAtt(doName, doDesc, param, lnInst);
}
}
}
private static Map<String, JsonNode> buildLNMap(JsonNode deviceNode) {
Map<String, JsonNode> map = new LinkedHashMap<>();
List<JsonNode> list = findNodes(deviceNode, "LN");
for (JsonNode node : list) {
String lnClass = node.get("lnClass").asText();
String inst = node.get("inst").asText();
map.put(lnClass + inst, node);
}
return map;
}
private static Map<String, JsonNode> buildDOTypeMap(JsonNode root) {
Map<String, JsonNode> map = new LinkedHashMap<>();
List<JsonNode> list = findNodes(root, "DOType");
for (JsonNode node : list) {
String id = node.get("id").asText();
map.put(id, node);
}
return map;
}
private static Map<String, JsonNode> buildDATypeMap(JsonNode root) {
Map<String, JsonNode> map = new LinkedHashMap<>();
List<JsonNode> list = findNodes(root, "DAType");
for (JsonNode node : list) {
String id = node.get("id").asText();
map.put(id, node);
}
return map;
}
private static Map<String, JsonNode> buildLNodeTypeMap(JsonNode root) {
Map<String, JsonNode> map = new LinkedHashMap<>();
List<JsonNode> listLNodeType = findNodes(root, "LNodeType");
for (JsonNode node : listLNodeType) {
String id = node.get("id").asText();
map.put(id, node);
}
return map;
}
private static JsonNode findLNodeType_DO_Node(JsonNode lnNode, String doName) {
JsonNode result = null;
List<JsonNode> doiList = findNodes(lnNode, "DO");
for (JsonNode doiNode : doiList) {
String doiName = doiNode.get("name").asText();
if (doiName.equals(doName)) {
result = doiNode;
break;
}
}
return result;
}
private static String findLastname(JsonNode node, String fcname, Map<String, JsonNode> mapDAType) {
String result = "";
List<JsonNode> daList = findNodes(node, "DA");
for (JsonNode daNode : daList) {
String fc = daNode.get("fc").asText();
String bType = daNode.get("bType").asText();
JsonNode dchgNode = daNode.get("dchg");
if (bType.equals("Struct") && fc.equals(fcname)) {
result = daNode.get("name").asText();
JsonNode typeNode = daNode.get("type");
if (typeNode != null && StringUtils.isNotBlank(typeNode.asText())) {
JsonNode nodeDAType = mapDAType.get(typeNode.asText());
if (nodeDAType != null) {
List<JsonNode> bdaList = findNodes(nodeDAType, "BDA");
if (!CollectionUtils.isEmpty(bdaList)) {
String name = bdaList.get(0).get("name").asText();
result = result + "$" + name;
}
}
}
break;
} else if (dchgNode != null && dchgNode.asText().equals("true") && fc.equals(fcname)) {
result = daNode.get("name").asText();
JsonNode typeNode = daNode.get("type");
if (typeNode != null && StringUtils.isNotBlank(typeNode.asText())) {
JsonNode nodeDAType = mapDAType.get(typeNode.asText());
if (nodeDAType != null) {
List<JsonNode> bdaList = findNodes(nodeDAType, "BDA");
if (!CollectionUtils.isEmpty(bdaList)) {
String name = bdaList.get(0).get("name").asText();
result = result + "$" + name;
}
}
}
}
}
return result;
}
private static String findLN_DOI_DAI(JsonNode lnNode, String doName) {
String result = "";
List<JsonNode> doiList = findNodes(lnNode, "DOI");
for (JsonNode doiNode : doiList) {
String doiName = doiNode.get("name").asText();
if (doiName.equals(doName)) {
JsonNode pnode = doiNode;
List<JsonNode> sdiList = findNodes(doiNode, "SDI");
if (sdiList.size() > 0) {
JsonNode sdiNode = sdiList.get(0);
result = sdiNode.get("name").asText();
pnode = sdiNode;
}
List<JsonNode> daiList = findNodes(pnode, "DAI");
if (daiList.size() > 0) {
JsonNode daiNode = daiList.get(0);
String daiName = daiNode.get("name").asText();
if (StringUtils.isNotBlank(result)) {
result = result + "$" + daiName;
} else {
result = daiName;
}
}
break;
}
}
return result;
}
private static List<JsonNode> findNodes(JsonNode root, String fieldName) {
List<JsonNode> result = new ArrayList<>();
List<JsonNode> list = root.findValues(fieldName);
for (JsonNode node : list) {
if (node instanceof ObjectNode) {
result.add(node);
}
if (node instanceof ArrayNode) {
ArrayNode array = (ArrayNode) node;
Iterator<JsonNode> elements = array.elements();
while (elements.hasNext()) {
JsonNode item = elements.next();
result.add(item);
}
}
}
return result;
}
}