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

224 lines
9.0 KiB
Java

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 org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import java.util.*;
public class IcdXmlUtil {
public static LinkedHashMap<String, IcdConfigType> loadIcdType(String xml) throws Exception {
XmlMapper xmlMapper = XmlMapper.builder()
.build();
JsonNode root = xmlMapper.readTree(xml);
LinkedHashMap<String, IcdConfigType> result = processTypeRoot(root);
return result;
}
private static LinkedHashMap<String, IcdConfigType> processTypeRoot(JsonNode root) {
LinkedHashMap<String, IcdConfigType> result = 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(result, iedNode,
mapLNodeType, mapDOType, mapDAType);
}
return result;
}
private static void processIEDNode(LinkedHashMap<String, IcdConfigType> result, 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(result, iedName, dev,
mapLNodeType, mapDOType, mapDAType);
}
}
private static void processTypeDeviceNode(LinkedHashMap<String, IcdConfigType> result, 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 nodeLNodeType = mapLNodeType.get(lnType);
String doType = findLNodeType_DO_Type(nodeLNodeType, doName);
JsonNode nodeDOType = mapDOType.get(doType);
String lastname = findLastname(nodeDOType, fc, mapDAType);
String key = iedName + ldeviceInst + "/" + lnClass;
if ("MX".equals(fc)) {
IcdConfigType config = result.get(key);
if (config == null) {
config = IcdConfigType.builder()
.iedName(iedName)
.ldeviceInst(ldeviceInst)
.lnClass(lnClass)
.build();
result.put(key, config);
}
String param = fc + "$" + doName + "$" + lastname;
config.addInst(lnInst);
config.addAtt(doName, param);
} else if ("ST".equals(fc)) {
// IcdConfigType config = result.get(key);
// if (config == null) {
// config = IcdConfigType.builder()
// .iedName(iedName)
// .ldeviceInst(ldeviceInst)
// .lnClass(lnClass)
// .build();
// result.put(key, config);
// }
// String param = fc + "$" + doName + "$" + lastname;
// config.addInst(lnInst);
// config.addAtt(doName, param);
}
}
}
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 String findLNodeType_DO_Type(JsonNode lnNode, String doName) {
String result = "";
List<JsonNode> doiList = findNodes(lnNode, "DO");
for (JsonNode doiNode : doiList) {
String doiName = doiNode.get("name").asText();
if (doiName.equals(doName)) {
result = doiNode.get("type").asText();
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) {
JsonNode dchgNode = daNode.get("dchg");
String fc = daNode.get("fc").asText();
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());
List<JsonNode> bdaList = findNodes(nodeDAType, "BDA");
if (!CollectionUtils.isEmpty(bdaList)) {
String name = bdaList.get(0).get("name").asText();
result = result + "$" + name;
}
}
break;
}
}
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;
}
}