return mapper

This commit is contained in:
Yuriy Lytvynchuk 2022-11-01 16:54:25 +02:00
parent 691339c5c5
commit 1d8733e55a

View File

@ -15,7 +15,10 @@
*/ */
package org.thingsboard.rule.engine.metadata; package org.thingsboard.rule.engine.metadata;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
@ -31,12 +34,14 @@ import org.thingsboard.rule.engine.api.util.TbNodeUtils;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry; import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.BasicTsKvEntry; import org.thingsboard.server.common.data.kv.BasicTsKvEntry;
import org.thingsboard.server.common.data.kv.DataType;
import org.thingsboard.server.common.data.kv.JsonDataEntry; import org.thingsboard.server.common.data.kv.JsonDataEntry;
import org.thingsboard.server.common.data.kv.KvEntry; import org.thingsboard.server.common.data.kv.KvEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry; import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.common.msg.TbMsg; import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgMetaData; import org.thingsboard.server.common.msg.TbMsgMetaData;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -54,6 +59,8 @@ import static org.thingsboard.server.common.data.DataConstants.SHARED_SCOPE;
public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeConfiguration, T extends EntityId> implements TbNode { public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeConfiguration, T extends EntityId> implements TbNode {
private static ObjectMapper mapper = new ObjectMapper();
private static final String VALUE = "value"; private static final String VALUE = "value";
private static final String TS = "ts"; private static final String TS = "ts";
@ -65,6 +72,8 @@ public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeC
@Override @Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException { public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
this.config = loadGetAttributesNodeConfig(configuration); this.config = loadGetAttributesNodeConfig(configuration);
mapper.configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
this.fetchToData = config.isFetchToData(); this.fetchToData = config.isFetchToData();
this.getLatestValueWithTs = config.isGetLatestValueWithTs(); this.getLatestValueWithTs = config.isGetLatestValueWithTs();
this.isTellFailureIfAbsent = BooleanUtils.toBooleanDefaultIfNull(this.config.isTellFailureIfAbsent(), true); this.isTellFailureIfAbsent = BooleanUtils.toBooleanDefaultIfNull(this.config.isTellFailureIfAbsent(), true);
@ -91,7 +100,7 @@ public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeC
ctx.tellNext(msg, FAILURE); ctx.tellNext(msg, FAILURE);
return; return;
} }
JsonNode msgDataNode = JacksonUtil.toJsonNode(msg.getData()); JsonNode msgDataNode = toJsonNode(msg.getData());
if (fetchToData) { if (fetchToData) {
if (!msgDataNode.isObject()) { if (!msgDataNode.isObject()) {
ctx.tellFailure(msg, new IllegalArgumentException("Msg body is not an object!")); ctx.tellFailure(msg, new IllegalArgumentException("Msg body is not an object!"));
@ -115,7 +124,7 @@ public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeC
String prefix = getPrefix(keyScope); String prefix = getPrefix(keyScope);
kvEntryList.forEach(kvEntry -> { kvEntryList.forEach(kvEntry -> {
if (fetchToData) { if (fetchToData) {
JacksonUtil.addKvEntry((ObjectNode) msgDataNode, kvEntry, prefix + kvEntry.getKey()); addKvEntryToJson((ObjectNode) msgDataNode, kvEntry, prefix + kvEntry.getKey());
} else { } else {
msgMetaData.putValue(prefix + kvEntry.getKey(), kvEntry.getValueAsString()); msgMetaData.putValue(prefix + kvEntry.getKey(), kvEntry.getValueAsString());
} }
@ -170,10 +179,10 @@ public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeC
} }
private TsKvEntry getValueWithTs(TsKvEntry tsKvEntry) { private TsKvEntry getValueWithTs(TsKvEntry tsKvEntry) {
ObjectNode value = JacksonUtil.newObjectNode(); ObjectNode value = mapper.createObjectNode();
value.put(TS, tsKvEntry.getTs()); value.put(TS, tsKvEntry.getTs());
JacksonUtil.addKvEntry(value, tsKvEntry, VALUE); addKvEntryToJson(value, tsKvEntry, VALUE);
return new BasicTsKvEntry(tsKvEntry.getTs(), new JsonDataEntry(tsKvEntry.getKey(), JacksonUtil.toString(value))); return new BasicTsKvEntry(tsKvEntry.getTs(), new JsonDataEntry(tsKvEntry.getKey(), value.toString()));
} }
private String getPrefix(String scope) { private String getPrefix(String scope) {
@ -192,6 +201,30 @@ public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeC
return prefix; return prefix;
} }
public static void addKvEntryToJson(ObjectNode entityNode, KvEntry kvEntry, String key) {
if (kvEntry.getDataType() == DataType.BOOLEAN) {
kvEntry.getBooleanValue().ifPresent(value -> entityNode.put(key, value));
} else if (kvEntry.getDataType() == DataType.DOUBLE) {
kvEntry.getDoubleValue().ifPresent(value -> entityNode.put(key, value));
} else if (kvEntry.getDataType() == DataType.LONG) {
kvEntry.getLongValue().ifPresent(value -> entityNode.put(key, value));
} else if (kvEntry.getDataType() == DataType.JSON) {
if (kvEntry.getJsonValue().isPresent()) {
entityNode.set(key, toJsonNode(kvEntry.getJsonValue().get()));
}
} else {
entityNode.put(key, kvEntry.getValueAsString());
}
}
public static JsonNode toJsonNode(String value) {
try {
return mapper.readTree(value);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
private List<String> getNotExistingKeys(List<AttributeKvEntry> existingAttributesKvEntry, List<String> allKeys) { private List<String> getNotExistingKeys(List<AttributeKvEntry> existingAttributesKvEntry, List<String> allKeys) {
List<String> existingKeys = existingAttributesKvEntry.stream().map(KvEntry::getKey).collect(Collectors.toList()); List<String> existingKeys = existingAttributesKvEntry.stream().map(KvEntry::getKey).collect(Collectors.toList());
return allKeys.stream().filter(key -> !existingKeys.contains(key)).collect(Collectors.toList()); return allKeys.stream().filter(key -> !existingKeys.contains(key)).collect(Collectors.toList());