Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated TbKeyFilterNode to be versioned node && removed destroy method usages because it is identical to its super method #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<main.dir>${basedir}</main.dir>
<thingsboard.version>3.5.0</thingsboard.version>
<thingsboard.version>3.5.2-SNAPSHOT</thingsboard.version>
<lombok.version>1.18.18</lombok.version>
<guava.version>31.1-jre</guava.version>
<!-- TEST SCOPE -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,4 @@ public void onMsg(TbContext ctx, TbMsg msg) {
}
}

@Override
public void destroy() {

}
}
Original file line number Diff line number Diff line change
@@ -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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <b>True</b> chain, otherwise <b>False</b> 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<Boolean, JsonNode> 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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
public class TbKeyFilterNodeConfiguration implements NodeConfiguration<TbKeyFilterNodeConfiguration> {

private String key;
private FilterSource filterSource;

@Override
public TbKeyFilterNodeConfiguration defaultConfiguration() {
TbKeyFilterNodeConfiguration configuration = new TbKeyFilterNodeConfiguration();
configuration.setKey(null);
configuration.setFilterSource(FilterSource.DATA);
return configuration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,4 @@ long getNow() {
return System.currentTimeMillis();
}

@Override
public void destroy() {
}
}