diff --git a/pom.xml b/pom.xml index 3169acc..f7647ba 100644 --- a/pom.xml +++ b/pom.xml @@ -32,7 +32,7 @@ UTF-8 ${basedir} - 3.5.0 + 3.5.2-SNAPSHOT 1.18.18 31.1-jre diff --git a/src/main/java/org/thingsboard/rule/engine/node/enrichment/TbGetSumIntoMetadata.java b/src/main/java/org/thingsboard/rule/engine/node/enrichment/TbGetSumIntoMetadata.java index 199bb34..349a6c9 100644 --- a/src/main/java/org/thingsboard/rule/engine/node/enrichment/TbGetSumIntoMetadata.java +++ b/src/main/java/org/thingsboard/rule/engine/node/enrichment/TbGetSumIntoMetadata.java @@ -83,8 +83,4 @@ public void onMsg(TbContext ctx, TbMsg msg) { } } - @Override - public void destroy() { - - } } diff --git a/src/main/java/org/thingsboard/rule/engine/node/filter/FilterSource.java b/src/main/java/org/thingsboard/rule/engine/node/filter/FilterSource.java new file mode 100644 index 0000000..1a4a6e5 --- /dev/null +++ b/src/main/java/org/thingsboard/rule/engine/node/filter/FilterSource.java @@ -0,0 +1,23 @@ +/** + * Copyright © 2018 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.rule.engine.node.filter; + +public enum FilterSource { + + DATA, + METADATA + +} diff --git a/src/main/java/org/thingsboard/rule/engine/node/filter/TbKeyFilterNode.java b/src/main/java/org/thingsboard/rule/engine/node/filter/TbKeyFilterNode.java index e4eb3ec..e831a16 100644 --- a/src/main/java/org/thingsboard/rule/engine/node/filter/TbKeyFilterNode.java +++ b/src/main/java/org/thingsboard/rule/engine/node/filter/TbKeyFilterNode.java @@ -15,54 +15,72 @@ */ package org.thingsboard.rule.engine.node.filter; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import lombok.extern.slf4j.Slf4j; +import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.rule.engine.api.RuleNode; import org.thingsboard.rule.engine.api.TbContext; -import org.thingsboard.rule.engine.api.TbNode; import org.thingsboard.rule.engine.api.TbNodeConfiguration; import org.thingsboard.rule.engine.api.TbNodeException; +import org.thingsboard.rule.engine.api.TbVersionedNode; import org.thingsboard.rule.engine.api.util.TbNodeUtils; import org.thingsboard.server.common.data.plugin.ComponentType; +import org.thingsboard.server.common.data.util.TbPair; import org.thingsboard.server.common.msg.TbMsg; -import java.io.IOException; - @Slf4j @RuleNode( type = ComponentType.FILTER, name = "check key", + version = 1, relationTypes = {"True", "False"}, configClazz = TbKeyFilterNodeConfiguration.class, - nodeDescription = "Checks the existence of the selected key in the message payload.", + nodeDescription = "Checks the existence of the selected key in the message or message metadata.", nodeDetails = "If the selected key exists - send Message via True chain, otherwise False chain is used.", uiResources = {"static/rulenode/custom-nodes-config.js"}, configDirective = "tbFilterNodeCheckKeyConfig") -public class TbKeyFilterNode implements TbNode { - - private static final ObjectMapper mapper = new ObjectMapper(); +public class TbKeyFilterNode implements TbVersionedNode { private TbKeyFilterNodeConfiguration config; private String key; + private FilterSource filterSource; @Override public void init(TbContext tbContext, TbNodeConfiguration configuration) throws TbNodeException { this.config = TbNodeUtils.convert(configuration, TbKeyFilterNodeConfiguration.class); key = config.getKey(); + filterSource = config.getFilterSource(); + if (filterSource == null) { + throw new TbNodeException("FilterSource cannot be null!"); + } } @Override public void onMsg(TbContext ctx, TbMsg msg) { - try { - ctx.tellNext(msg, mapper.readTree(msg.getData()).has(key) ? "True" : "False"); - } catch (IOException e) { - ctx.tellFailure(msg, e); + String nextRelationType; + if (FilterSource.DATA.equals(filterSource)) { + nextRelationType = JacksonUtil.toJsonNode(msg.getData()).has(key) ? "True" : "False"; + } else { + nextRelationType = msg.getMetaData().getValue(key) != null ? "True" : "False"; } + ctx.tellNext(msg, nextRelationType); } @Override - public void destroy() { + public TbPair upgrade(int fromVersion, JsonNode oldConfiguration) throws TbNodeException { + try { + if (fromVersion == 0) { + var newConfigObjectNode = (ObjectNode) oldConfiguration; + newConfigObjectNode.put("filterSource", FilterSource.DATA.name()); + return new TbPair<>(true, newConfigObjectNode); + } + return new TbPair<>(false, oldConfiguration); + } catch (Exception e) { + throw new TbNodeException(e); + } } + } diff --git a/src/main/java/org/thingsboard/rule/engine/node/filter/TbKeyFilterNodeConfiguration.java b/src/main/java/org/thingsboard/rule/engine/node/filter/TbKeyFilterNodeConfiguration.java index 35b7d22..a0966c3 100644 --- a/src/main/java/org/thingsboard/rule/engine/node/filter/TbKeyFilterNodeConfiguration.java +++ b/src/main/java/org/thingsboard/rule/engine/node/filter/TbKeyFilterNodeConfiguration.java @@ -22,11 +22,13 @@ public class TbKeyFilterNodeConfiguration implements NodeConfiguration { private String key; + private FilterSource filterSource; @Override public TbKeyFilterNodeConfiguration defaultConfiguration() { TbKeyFilterNodeConfiguration configuration = new TbKeyFilterNodeConfiguration(); configuration.setKey(null); + configuration.setFilterSource(FilterSource.DATA); return configuration; } } diff --git a/src/main/java/org/thingsboard/rule/engine/node/transform/TbCalculateSumNode.java b/src/main/java/org/thingsboard/rule/engine/node/transform/TbCalculateSumNode.java index 3943210..d56809e 100644 --- a/src/main/java/org/thingsboard/rule/engine/node/transform/TbCalculateSumNode.java +++ b/src/main/java/org/thingsboard/rule/engine/node/transform/TbCalculateSumNode.java @@ -84,7 +84,4 @@ long getNow() { return System.currentTimeMillis(); } - @Override - public void destroy() { - } }