Browse Source

add TbCheckMessageNode

pull/1437/head
ShvaykaD 7 years ago
parent
commit
ddc2c73717
  1. 93
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/filter/TbCheckMessageNode.java
  2. 19
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/filter/TbCheckMessageNodeConfiguration.java
  3. 8
      rule-engine/rule-engine-components/src/main/resources/public/static/rulenode/rulenode-core-config.js

93
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/filter/TbCheckMessageNode.java

@ -1,12 +1,12 @@
/**
* Copyright © 2018 The Thingsboard Authors
* <p>
* Copyright © 2016-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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
*
* 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.
@ -15,7 +15,6 @@
*/
package org.thingsboard.rule.engine.filter;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
@ -29,20 +28,22 @@ import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.msg.TbMsg;
import java.util.List;
import java.util.Map;
@Slf4j
@RuleNode(
type = ComponentType.FILTER,
name = "check key",
name = "has Hobotok",
relationTypes = {"True", "False"},
configClazz = TbCheckMessageNodeConfiguration.class,
nodeDescription = "Checks the existence of the selected key in the message payload.",
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")
nodeDescription = "Checks the existence of the selected keys from message data and metadata.",
nodeDetails = "If selected checkbox 'Check that all selected keys are present'\" and all keys in message data and metadata are exist - send Message via <b>True</b> chain, otherwise <b>False</b> chain is used.\n" +
"Else if the checkbox is not selected, and at least one of the keys from data or metadata of the message exists - send Message via <b>True</b> chain, otherwise, <b>False</b> chain is used. ",
uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbFilterNodeCheckMessageConfig")
public class TbCheckMessageNode implements TbNode {
private static final Gson gson = new Gson();
private static final JsonParser parser = new JsonParser();
private TbCheckMessageNodeConfiguration config;
private List<String> messageNamesList;
@ -57,42 +58,72 @@ public class TbCheckMessageNode implements TbNode {
@Override
public void onMsg(TbContext ctx, TbMsg msg) {
JsonObject data = new JsonParser().parse(msg.getData()).getAsJsonObject();
JsonObject metadata = gson.toJsonTree(msg.getMetaData().getData()).getAsJsonObject();
try {
ctx.tellNext(msg, processAllMessageData(data) && processAllMessageMetadata(metadata) ? "True" : "False");
if (config.isCheckAllKeys()) {
ctx.tellNext(msg, allDataKeysExist(msg) && allMetadataKeysExist(msg) ? "True" : "False");
} else {
ctx.tellNext(msg, atLeastOneDataKeyExist(msg) || atLeastOneMetadataKeyExist(msg) ? "True" : "False");
}
} catch (Exception e) {
ctx.tellFailure(msg, e);
}
}
@Override
public void destroy() {
private boolean allDataKeysExist(TbMsg msg) {
if (!messageNamesList.isEmpty()) {
JsonObject data = parser.parse(msg.getData()).getAsJsonObject();
for (String field : messageNamesList) {
if (!data.has(field)) {
return false;
}
}
return true;
}
return true;
}
private boolean processAllMessageData(JsonObject data) {
if (config.isCheckMessageData()) {
return process(data, messageNamesList);
} else {
return true;
private boolean atLeastOneDataKeyExist(TbMsg msg) {
if (!messageNamesList.isEmpty()) {
JsonObject data = parser.parse(msg.getData()).getAsJsonObject();
for (String field : messageNamesList) {
if (data.has(field)) {
return true;
}
}
return false;
}
return false;
}
private boolean processAllMessageMetadata(JsonObject metadata) {
if (config.isCheckMessageMetadata()) {
return process(metadata, metadataNamesList);
} else {
private boolean allMetadataKeysExist(TbMsg msg) {
if (!metadataNamesList.isEmpty()) {
Map<String, String> metadata = msg.getMetaData().getData();
for (String field : metadataNamesList) {
if (!metadata.containsKey(field)) {
return false;
}
}
return true;
}
return true;
}
private boolean process(JsonObject object, List<String> fieldsList) {
for (String field : fieldsList) {
if (!object.has(field)) {
return false;
private boolean atLeastOneMetadataKeyExist(TbMsg msg) {
if (!metadataNamesList.isEmpty()) {
Map<String, String> metadata = msg.getMetaData().getData();
for (String field : metadataNamesList) {
if (metadata.containsKey(field)) {
return true;
}
}
return false;
}
return true;
return false;
}
@Override
public void destroy() {
}
}

19
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/filter/TbCheckMessageNodeConfiguration.java

@ -1,3 +1,18 @@
/**
* Copyright © 2016-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.filter;
import lombok.Data;
@ -12,8 +27,6 @@ public class TbCheckMessageNodeConfiguration implements NodeConfiguration {
private List<String> messageNames;
private List<String> metadataNames;
private boolean checkMessageData;
private boolean checkMessageMetadata;
private boolean checkAllKeys;
@ -22,8 +35,6 @@ public class TbCheckMessageNodeConfiguration implements NodeConfiguration {
TbCheckMessageNodeConfiguration configuration = new TbCheckMessageNodeConfiguration();
configuration.setMessageNames(Collections.emptyList());
configuration.setMetadataNames(Collections.emptyList());
configuration.setCheckMessageData(true);
configuration.setCheckMessageMetadata(false);
configuration.setCheckAllKeys(true);
return configuration;
}

8
rule-engine/rule-engine-components/src/main/resources/public/static/rulenode/rulenode-core-config.js

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save