Browse Source

add TbCheckMessageNode

pull/1437/head
ShvaykaD 8 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 * Copyright © 2016-2018 The Thingsboard Authors
* <p> *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* <p> *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* <p> *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -15,7 +15,6 @@
*/ */
package org.thingsboard.rule.engine.filter; package org.thingsboard.rule.engine.filter;
import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j; 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 org.thingsboard.server.common.msg.TbMsg;
import java.util.List; import java.util.List;
import java.util.Map;
@Slf4j @Slf4j
@RuleNode( @RuleNode(
type = ComponentType.FILTER, type = ComponentType.FILTER,
name = "check key", name = "has Hobotok",
relationTypes = {"True", "False"}, relationTypes = {"True", "False"},
configClazz = TbCheckMessageNodeConfiguration.class, configClazz = TbCheckMessageNodeConfiguration.class,
nodeDescription = "Checks the existence of the selected key in the message payload.", nodeDescription = "Checks the existence of the selected keys from message data and metadata.",
nodeDetails = "If the selected key exists - send Message via <b>True</b> chain, otherwise <b>False</b> chain is used.", 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" +
uiResources = {"static/rulenode/custom-nodes-config.js"}, "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. ",
configDirective = "tbFilterNodeCheckKeyConfig") uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbFilterNodeCheckMessageConfig")
public class TbCheckMessageNode implements TbNode { public class TbCheckMessageNode implements TbNode {
private static final Gson gson = new Gson(); private static final JsonParser parser = new JsonParser();
private TbCheckMessageNodeConfiguration config; private TbCheckMessageNodeConfiguration config;
private List<String> messageNamesList; private List<String> messageNamesList;
@ -57,42 +58,72 @@ public class TbCheckMessageNode implements TbNode {
@Override @Override
public void onMsg(TbContext ctx, TbMsg msg) { public void onMsg(TbContext ctx, TbMsg msg) {
JsonObject data = new JsonParser().parse(msg.getData()).getAsJsonObject();
JsonObject metadata = gson.toJsonTree(msg.getMetaData().getData()).getAsJsonObject();
try { 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) { } catch (Exception e) {
ctx.tellFailure(msg, e); ctx.tellFailure(msg, e);
} }
} }
@Override private boolean allDataKeysExist(TbMsg msg) {
public void destroy() { 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) { private boolean atLeastOneDataKeyExist(TbMsg msg) {
if (config.isCheckMessageData()) { if (!messageNamesList.isEmpty()) {
return process(data, messageNamesList); JsonObject data = parser.parse(msg.getData()).getAsJsonObject();
} else { for (String field : messageNamesList) {
return true; if (data.has(field)) {
return true;
}
}
return false;
} }
return false;
} }
private boolean processAllMessageMetadata(JsonObject metadata) { private boolean allMetadataKeysExist(TbMsg msg) {
if (config.isCheckMessageMetadata()) { if (!metadataNamesList.isEmpty()) {
return process(metadata, metadataNamesList); Map<String, String> metadata = msg.getMetaData().getData();
} else { for (String field : metadataNamesList) {
if (!metadata.containsKey(field)) {
return false;
}
}
return true; return true;
} }
return true;
} }
private boolean process(JsonObject object, List<String> fieldsList) { private boolean atLeastOneMetadataKeyExist(TbMsg msg) {
for (String field : fieldsList) { if (!metadataNamesList.isEmpty()) {
if (!object.has(field)) { Map<String, String> metadata = msg.getMetaData().getData();
return false; 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; package org.thingsboard.rule.engine.filter;
import lombok.Data; import lombok.Data;
@ -12,8 +27,6 @@ public class TbCheckMessageNodeConfiguration implements NodeConfiguration {
private List<String> messageNames; private List<String> messageNames;
private List<String> metadataNames; private List<String> metadataNames;
private boolean checkMessageData;
private boolean checkMessageMetadata;
private boolean checkAllKeys; private boolean checkAllKeys;
@ -22,8 +35,6 @@ public class TbCheckMessageNodeConfiguration implements NodeConfiguration {
TbCheckMessageNodeConfiguration configuration = new TbCheckMessageNodeConfiguration(); TbCheckMessageNodeConfiguration configuration = new TbCheckMessageNodeConfiguration();
configuration.setMessageNames(Collections.emptyList()); configuration.setMessageNames(Collections.emptyList());
configuration.setMetadataNames(Collections.emptyList()); configuration.setMetadataNames(Collections.emptyList());
configuration.setCheckMessageData(true);
configuration.setCheckMessageMetadata(false);
configuration.setCheckAllKeys(true); configuration.setCheckAllKeys(true);
return configuration; 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