refactor code
This commit is contained in:
parent
72f2f633a7
commit
7e8b0d59a4
@ -30,11 +30,10 @@ import org.thingsboard.server.common.msg.TbMsg;
|
|||||||
import org.thingsboard.server.common.msg.TbMsgMetaData;
|
import org.thingsboard.server.common.msg.TbMsgMetaData;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@ -44,8 +43,7 @@ import java.util.regex.Pattern;
|
|||||||
configClazz = TbCopyKeysNodeConfiguration.class,
|
configClazz = TbCopyKeysNodeConfiguration.class,
|
||||||
nodeDescription = "Copies the msg or metadata keys with specified key names selected in the list",
|
nodeDescription = "Copies the msg or metadata keys with specified key names selected in the list",
|
||||||
nodeDetails = "Will fetch fields values specified in list. If specified field is not part of msg or metadata fields it will be ignored." +
|
nodeDetails = "Will fetch fields values specified in list. If specified field is not part of msg or metadata fields it will be ignored." +
|
||||||
"If the msg is not a JSON object returns the incoming message as outbound message with <code>Failure</code> chain, " +
|
"Returns transformed messages via <code>Success</code> chain",
|
||||||
"otherwise returns transformed messages via <code>Success</code> chain",
|
|
||||||
uiResources = {"static/rulenode/rulenode-core-config.js"},
|
uiResources = {"static/rulenode/rulenode-core-config.js"},
|
||||||
configDirective = "tbTransformationNodeCopyKeysConfig",
|
configDirective = "tbTransformationNodeCopyKeysConfig",
|
||||||
icon = "content_copy"
|
icon = "content_copy"
|
||||||
@ -53,13 +51,14 @@ import java.util.regex.Pattern;
|
|||||||
public class TbCopyKeysNode implements TbNode {
|
public class TbCopyKeysNode implements TbNode {
|
||||||
|
|
||||||
TbCopyKeysNodeConfiguration config;
|
TbCopyKeysNodeConfiguration config;
|
||||||
List<Pattern> patternKeys = new ArrayList<>();
|
List<Pattern> patternKeys;
|
||||||
boolean fromMetadata;
|
boolean fromMetadata;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
|
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
|
||||||
this.config = TbNodeUtils.convert(configuration, TbCopyKeysNodeConfiguration.class);
|
this.config = TbNodeUtils.convert(configuration, TbCopyKeysNodeConfiguration.class);
|
||||||
this.fromMetadata = config.isFromMetadata();
|
this.fromMetadata = config.isFromMetadata();
|
||||||
|
this.patternKeys = new ArrayList<>();
|
||||||
config.getKeys().forEach(key -> {
|
config.getKeys().forEach(key -> {
|
||||||
this.patternKeys.add(Pattern.compile(key));
|
this.patternKeys.add(Pattern.compile(key));
|
||||||
});
|
});
|
||||||
@ -69,30 +68,33 @@ public class TbCopyKeysNode implements TbNode {
|
|||||||
public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
|
public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
|
||||||
TbMsgMetaData metaData = msg.getMetaData();
|
TbMsgMetaData metaData = msg.getMetaData();
|
||||||
String msgData = msg.getData();
|
String msgData = msg.getData();
|
||||||
AtomicBoolean msgChanged = new AtomicBoolean(false);
|
boolean msgChanged = false;
|
||||||
JsonNode dataNode = JacksonUtil.toJsonNode(msgData);
|
JsonNode dataNode = JacksonUtil.toJsonNode(msgData);
|
||||||
if (dataNode.isObject()) {
|
if (dataNode.isObject()) {
|
||||||
if (fromMetadata) {
|
if (fromMetadata) {
|
||||||
ObjectNode msgDataNode = (ObjectNode) dataNode;
|
ObjectNode msgDataNode = (ObjectNode) dataNode;
|
||||||
Map<String, String> metaDataMap = metaData.getData();
|
Map<String, String> metaDataMap = metaData.getData();
|
||||||
metaDataMap.forEach((keyMetaData, valueMetaData) -> {
|
for (Map.Entry<String, String> entry : metaDataMap.entrySet()) {
|
||||||
if (checkKey(keyMetaData)) {
|
|
||||||
msgChanged.set(true);
|
|
||||||
msgDataNode.put(keyMetaData, valueMetaData);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
msgData = JacksonUtil.toString(msgDataNode);
|
|
||||||
} else {
|
|
||||||
dataNode.fields().forEachRemaining(entry -> {
|
|
||||||
String keyData = entry.getKey();
|
String keyData = entry.getKey();
|
||||||
if (checkKey(keyData)) {
|
if (checkKey(keyData)) {
|
||||||
msgChanged.set(true);
|
msgChanged = true;
|
||||||
|
msgDataNode.put(keyData, entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
msgData = JacksonUtil.toString(msgDataNode);
|
||||||
|
} else {
|
||||||
|
Iterator<Map.Entry<String, JsonNode>> iteratorNode = dataNode.fields();
|
||||||
|
while (iteratorNode.hasNext()) {
|
||||||
|
Map.Entry<String, JsonNode> entry = iteratorNode.next();
|
||||||
|
String keyData = entry.getKey();
|
||||||
|
if (checkKey(keyData)) {
|
||||||
|
msgChanged = true;
|
||||||
metaData.putValue(keyData, JacksonUtil.toString(entry.getValue()));
|
metaData.putValue(keyData, JacksonUtil.toString(entry.getValue()));
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (msgChanged.get()) {
|
if (msgChanged) {
|
||||||
ctx.tellSuccess(TbMsg.transformMsg(msg, msg.getType(), msg.getOriginator(), metaData, msgData));
|
ctx.tellSuccess(TbMsg.transformMsg(msg, msg.getType(), msg.getOriginator(), metaData, msgData));
|
||||||
} else {
|
} else {
|
||||||
ctx.tellSuccess(msg);
|
ctx.tellSuccess(msg);
|
||||||
@ -100,10 +102,7 @@ public class TbCopyKeysNode implements TbNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean checkKey(String key) {
|
boolean checkKey(String key) {
|
||||||
Optional<Pattern> currentPattern = patternKeys.stream()
|
return patternKeys.stream().anyMatch(pattern -> pattern.matcher(key).matches());
|
||||||
.filter(pattern -> pattern.matcher(key).matches())
|
|
||||||
.findFirst();
|
|
||||||
return currentPattern.isPresent();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user