|
|
@@ -1,16 +1,17 @@
|
|
|
package com.energy.manage.service.test;
|
|
|
|
|
|
import com.energy.manage.service.domain.dto.analysis.AnalysisDto;
|
|
|
+import com.energy.manage.service.property.analysis.AlgorithmProperties;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
+import com.fasterxml.jackson.databind.node.IntNode;
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -20,6 +21,8 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
public class TestJson {
|
|
|
|
|
|
+ private static ObjectMapper mapper = new ObjectMapper();
|
|
|
+
|
|
|
|
|
|
public static void main(String[] args) throws JsonProcessingException {
|
|
|
String jsonString = "{\n" +
|
|
|
@@ -29,9 +32,6 @@ public class TestJson {
|
|
|
" },\n" +
|
|
|
" \"dataContract\": {\n" +
|
|
|
" \"autoOrManual\":\"automatic\",\n" +
|
|
|
- " \"dataSource\": {\n" +
|
|
|
- " \"scada\": \"second\"\n" +
|
|
|
- " },\n" +
|
|
|
" \"dataFilter\": {\n" +
|
|
|
" \"powerFarmID\": \"010-00001\",\n" +
|
|
|
" \"turbines\": [\n" +
|
|
|
@@ -65,31 +65,6 @@ public class TestJson {
|
|
|
" }\n" +
|
|
|
" },\n" +
|
|
|
" \"configAnalysis\": [\n" +
|
|
|
- " {\n" +
|
|
|
- " \"package\": \"algorithm.powerCurveAnalyst\",\n" +
|
|
|
- " \"className\": \"PowerCurveAnalyst\",\n" +
|
|
|
- " \"methodName\": \"executeAnalysis\"\n" +
|
|
|
- " },\n" +
|
|
|
- " {\n" +
|
|
|
- " \"package\": \"algorithm.powerScatter2DAnalyst\",\n" +
|
|
|
- " \"className\": \"PowerScatter2DAnayst\",\n" +
|
|
|
- " \"methodName\": \"executeAnalysis\"\n" +
|
|
|
- " },\n" +
|
|
|
- " {\n" +
|
|
|
- " \"package\": \"algorithm.powerScatterAnalyst\",\n" +
|
|
|
- " \"className\": \"PowerScatterAnalyst\",\n" +
|
|
|
- " \"methodName\": \"executeAnalysis\"\n" +
|
|
|
- " },\n" +
|
|
|
- " {\n" +
|
|
|
- " \"package\": \"algorithm.windSpeedFrequencyAnalyst\",\n" +
|
|
|
- " \"className\": \"WindSpeedFrequencyAnalyst\",\n" +
|
|
|
- " \"methodName\": \"executeAnalysis\"\n" +
|
|
|
- " },\n" +
|
|
|
- " {\n" +
|
|
|
- " \"package\": \"algorithm.generatorSpeedPowerAnalyst\",\n" +
|
|
|
- " \"className\": \"GeneratorSpeedPowerAnalyst\",\n" +
|
|
|
- " \"methodName\": \"executeAnalysis\"\n" +
|
|
|
- " }\n" +
|
|
|
" ],\n" +
|
|
|
" \"graphSets\": {\n" +
|
|
|
" \"generatorSpeed\": {\n" +
|
|
|
@@ -125,8 +100,6 @@ public class TestJson {
|
|
|
" }\n" +
|
|
|
" }\n" +
|
|
|
" }";
|
|
|
-
|
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
|
JsonNode rootNode = mapper.readTree(jsonString);
|
|
|
// AnalysisDto dto = new AnalysisDto();
|
|
|
// dto.setBeginTime("1234-12-12 00:00:12");
|
|
|
@@ -139,10 +112,13 @@ public class TestJson {
|
|
|
// }
|
|
|
|
|
|
AnalysisDto dto = new AnalysisDto();
|
|
|
+ dto.setAutoOrManual("manual");
|
|
|
dto.setBeginTime("1234-12-12 00:00:12");
|
|
|
dto.setExcludingMonths(Arrays.asList("1","2","3"));
|
|
|
+ dto.setTurbines(Arrays.asList("456","789"));
|
|
|
+ dto.setValueActivePower(Arrays.asList(1,2));
|
|
|
+ dto.setConfigAnalysis(Arrays.asList("algorithm.powerOscillationAnalyst|PowerOscillationAnalyst|executeAnalysis|minute","algorithm.pitchTSRCpAnalyst|PitchTSRCpAnalyst|executeAnalysis|second"));
|
|
|
setValue(dto,rootNode);
|
|
|
- System.out.println();
|
|
|
for (JsonNode node : rootNode) {
|
|
|
System.out.println("Found matching node: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node));
|
|
|
}
|
|
|
@@ -168,12 +144,21 @@ public class TestJson {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static void updateNodesByPropertyName(JsonNode node, String propertyName,String value) {
|
|
|
- if (node.isObject()) {
|
|
|
- node.fields().forEachRemaining(entry -> {
|
|
|
+ private static void updateNodesByPropertyName(JsonNode rootNode, String propertyName,Object value) {
|
|
|
+ if (rootNode.isObject()) {
|
|
|
+ rootNode.fields().forEachRemaining(entry -> {
|
|
|
// 匹配到属性名称,进行值的修改
|
|
|
if (entry.getKey().equals(propertyName)) {
|
|
|
- ((ObjectNode) node).put(propertyName, value);
|
|
|
+ //如果当前节点没有子节点直接复制
|
|
|
+ if(!entry.getValue().fields().hasNext() && !propertyName.equals("configAnalysis")){
|
|
|
+ ((ObjectNode) rootNode).put(propertyName, value.toString());
|
|
|
+ }else {
|
|
|
+ try {
|
|
|
+ assignmentByPropertyName(entry.getValue(), propertyName, value);
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
// 递归搜索子节点
|
|
|
updateNodesByPropertyName(entry.getValue(), propertyName, value);
|
|
|
@@ -186,18 +171,69 @@ public class TestJson {
|
|
|
* @param analysisDto
|
|
|
* @return
|
|
|
*/
|
|
|
- private static void setValue(AnalysisDto analysisDto,JsonNode jsonNode) {
|
|
|
+ private static void setValue(AnalysisDto analysisDto,JsonNode rootNode) {
|
|
|
//前端入参类
|
|
|
Field[] analysisFields = analysisDto.getClass().getDeclaredFields();
|
|
|
Arrays.asList(analysisFields).stream().forEach(field -> {
|
|
|
field.setAccessible(true);
|
|
|
try {
|
|
|
if(field.get(analysisDto) != null){
|
|
|
- updateNodesByPropertyName(jsonNode, field.getName(), field.get(analysisDto).toString());
|
|
|
+ updateNodesByPropertyName(rootNode, field.getName(), field.get(analysisDto));
|
|
|
}
|
|
|
} catch (IllegalAccessException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ private static void assignmentByPropertyName(JsonNode subNode, String propertyName, Object newValue) throws JsonProcessingException {
|
|
|
+ //图像设置特殊处理
|
|
|
+ if("valueActivePower".contains(propertyName)){
|
|
|
+// if(AlgorithmProperties.graphSetsProperties.contains(propertyName)){
|
|
|
+ List<Integer> list = (List) newValue;
|
|
|
+ AtomicInteger index = new AtomicInteger(0);
|
|
|
+ subNode.fields().forEachRemaining(entry -> {
|
|
|
+ System.out.println(entry.getKey() + "======");
|
|
|
+ ((ObjectNode) subNode).put(entry.getKey(), list.get(index.get()));
|
|
|
+ index.incrementAndGet();
|
|
|
+ });
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ //分析类型特殊处理
|
|
|
+ if("configAnalysis".equals(propertyName)){
|
|
|
+ dealConfigAnalysis(subNode, newValue);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static String json = "{\n" +
|
|
|
+ " \"package\": \"\",\n" +
|
|
|
+ " \"className\": \"\",\n" +
|
|
|
+ " \"methodName\": \"\",\n" +
|
|
|
+ " \"scada\": \"\"\n" +
|
|
|
+ "}";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分析类型处理
|
|
|
+ * @param subNode
|
|
|
+ * @param newValue
|
|
|
+ */
|
|
|
+ private static void dealConfigAnalysis(JsonNode subNode, Object newValue) throws JsonProcessingException {
|
|
|
+ ArrayNode arrayNode = (ArrayNode) subNode;
|
|
|
+ List<String> analysisTypes = (List) newValue;
|
|
|
+ for(int i = 0 ; i < analysisTypes.size() ; i++){
|
|
|
+ JsonNode childNode = mapper.readTree(json);
|
|
|
+ //分析类型解析
|
|
|
+ String[] types = analysisTypes.get(i).split("\\|");
|
|
|
+ AtomicInteger index = new AtomicInteger(0);
|
|
|
+ childNode.fields().forEachRemaining(entry-> {
|
|
|
+ ((ObjectNode)childNode).put(entry.getKey(), types[index.get()]);
|
|
|
+ index.incrementAndGet();
|
|
|
+ });
|
|
|
+ arrayNode.add(childNode);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|