feat: 增加icd文件分析功能
parent
cac633b9a0
commit
31a7a93585
@ -0,0 +1,34 @@
|
|||||||
|
package com.xydl.cac.controller;
|
||||||
|
|
||||||
|
import com.xydl.cac.model.Response;
|
||||||
|
import com.xydl.cac.service.IcdService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@Api(tags = {"Icd相关接口"})
|
||||||
|
@RequestMapping("icd")
|
||||||
|
@Slf4j
|
||||||
|
public class IcdController extends BasicController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
IcdService service;
|
||||||
|
|
||||||
|
@PostMapping("process")
|
||||||
|
@ApiOperation("处理icd文件")
|
||||||
|
public Response<List<String>> process(@RequestParam("file") MultipartFile file) throws Exception {
|
||||||
|
if (file != null && file.getInputStream() != null) {
|
||||||
|
List<String> paramList = service.loadXml(file.getInputStream());
|
||||||
|
return Response.success(paramList);
|
||||||
|
} else {
|
||||||
|
return Response.fail("缺少上传文件");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.xydl.cac.service;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface IcdService {
|
||||||
|
List<String> loadXml(InputStream input) throws Exception;
|
||||||
|
|
||||||
|
List<String> loadXml(String xml) throws Exception;
|
||||||
|
}
|
@ -0,0 +1,128 @@
|
|||||||
|
package com.xydl.cac.service.impl;
|
||||||
|
|
||||||
|
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.service.IcdService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class IcdServiceImpl implements IcdService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> loadXml(InputStream input) throws Exception {
|
||||||
|
XmlMapper xmlMapper = XmlMapper.builder()
|
||||||
|
.build();
|
||||||
|
JsonNode root = xmlMapper.readTree(input);
|
||||||
|
List<String> paramList = this.processRoot(root);
|
||||||
|
return paramList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> loadXml(String xml) throws Exception {
|
||||||
|
XmlMapper xmlMapper = XmlMapper.builder()
|
||||||
|
.build();
|
||||||
|
JsonNode root = xmlMapper.readTree(xml);
|
||||||
|
List<String> paramList = this.processRoot(root);
|
||||||
|
return paramList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> processRoot(JsonNode root) {
|
||||||
|
List<String> paramList = new ArrayList<>();
|
||||||
|
List<JsonNode> iedList = this.findNodes(root, "IED");
|
||||||
|
|
||||||
|
for (JsonNode iedNode : iedList) {
|
||||||
|
String iedName = iedNode.get("name").asText();
|
||||||
|
|
||||||
|
List<JsonNode> devList = this.findNodes(iedNode, "LDevice");
|
||||||
|
for (JsonNode dev : devList) {
|
||||||
|
this.processDeviceNode(dev, iedName, paramList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return paramList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processDeviceNode(JsonNode deviceNode, String iedName, List<String> paramList) {
|
||||||
|
String inst = deviceNode.get("inst").asText();
|
||||||
|
Map<String, JsonNode> lnMap = this.buildLNMap(deviceNode);
|
||||||
|
|
||||||
|
List<JsonNode> fcdaList = this.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 = lnMap.get(lnClass + lnInst);
|
||||||
|
String dai = this.findLN_DOI_DAI(lnNode, doName);
|
||||||
|
String param = iedName + inst + "/" + lnClass + lnInst + "$" + fc + "$" + doName + "$" + dai;
|
||||||
|
paramList.add(param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, JsonNode> buildLNMap(JsonNode deviceNode) {
|
||||||
|
Map<String, JsonNode> map = new HashMap<>();
|
||||||
|
List<JsonNode> lnList = this.findNodes(deviceNode, "LN");
|
||||||
|
for (JsonNode lnNode : lnList) {
|
||||||
|
String lnClass = lnNode.get("lnClass").asText();
|
||||||
|
String inst = lnNode.get("inst").asText();
|
||||||
|
map.put(lnClass + inst, lnNode);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String findLN_DOI_DAI(JsonNode lnNode, String doName) {
|
||||||
|
String result = "";
|
||||||
|
List<JsonNode> doiList = this.findNodes(lnNode, "DOI");
|
||||||
|
for (JsonNode doiNode : doiList) {
|
||||||
|
String doiName = doiNode.get("name").asText();
|
||||||
|
if (doiName.equals(doName)) {
|
||||||
|
JsonNode pnode = doiNode;
|
||||||
|
List<JsonNode> sdiList = this.findNodes(doiNode, "SDI");
|
||||||
|
if (sdiList.size() > 0) {
|
||||||
|
JsonNode sdiNode = sdiList.get(0);
|
||||||
|
result = sdiNode.get("name").asText();
|
||||||
|
pnode = sdiNode;
|
||||||
|
}
|
||||||
|
List<JsonNode> daiList = this.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 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue