committed by
GitHub
3 changed files with 179 additions and 4 deletions
@ -0,0 +1,134 @@ |
|||
/** |
|||
* 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 com.google.gson.Gson; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
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.util.TbNodeUtils; |
|||
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 existence fields", |
|||
relationTypes = {"True", "False"}, |
|||
configClazz = TbCheckMessageNodeConfiguration.class, |
|||
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 TbCheckMessageNodeConfiguration config; |
|||
private List<String> messageNamesList; |
|||
private List<String> metadataNamesList; |
|||
|
|||
@Override |
|||
public void init(TbContext tbContext, TbNodeConfiguration configuration) throws TbNodeException { |
|||
this.config = TbNodeUtils.convert(configuration, TbCheckMessageNodeConfiguration.class); |
|||
messageNamesList = config.getMessageNames(); |
|||
metadataNamesList = config.getMetadataNames(); |
|||
} |
|||
|
|||
@Override |
|||
public void onMsg(TbContext ctx, TbMsg msg) { |
|||
try { |
|||
if (config.isCheckAllKeys()) { |
|||
ctx.tellNext(msg, allKeysData(msg) && allKeysMetadata(msg) ? "True" : "False"); |
|||
} else { |
|||
ctx.tellNext(msg, atLeastOneData(msg) || atLeastOneMetadata(msg) ? "True" : "False"); |
|||
} |
|||
} catch (Exception e) { |
|||
ctx.tellFailure(msg, e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void destroy() { |
|||
} |
|||
|
|||
private boolean allKeysData(TbMsg msg) { |
|||
if (!messageNamesList.isEmpty()) { |
|||
Map<String, String> dataMap = dataToMap(msg); |
|||
return processAllKeys(messageNamesList, dataMap); |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
private boolean allKeysMetadata(TbMsg msg) { |
|||
if (!metadataNamesList.isEmpty()) { |
|||
Map<String, String> metadataMap = metadataToMap(msg); |
|||
return processAllKeys(metadataNamesList, metadataMap); |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
private boolean atLeastOneData(TbMsg msg) { |
|||
if (!messageNamesList.isEmpty()) { |
|||
Map<String, String> dataMap = dataToMap(msg); |
|||
return processAtLeastOne(messageNamesList, dataMap); |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
private boolean atLeastOneMetadata(TbMsg msg) { |
|||
if (!metadataNamesList.isEmpty()) { |
|||
Map<String, String> metadataMap = metadataToMap(msg); |
|||
return processAtLeastOne(metadataNamesList, metadataMap); |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
private boolean processAllKeys(List<String> data, Map<String, String> map) { |
|||
for (String field : data) { |
|||
if (!map.containsKey(field)) { |
|||
return false; |
|||
} |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
private boolean processAtLeastOne(List<String> data, Map<String, String> map) { |
|||
for (String field : data) { |
|||
if (map.containsKey(field)) { |
|||
return true; |
|||
} |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
private Map<String, String> metadataToMap(TbMsg msg) { |
|||
return msg.getMetaData().getData(); |
|||
} |
|||
|
|||
@SuppressWarnings("unchecked") |
|||
private Map<String, String> dataToMap(TbMsg msg) { |
|||
return (Map<String, String>) gson.fromJson(msg.getData(), Map.class); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
/** |
|||
* 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; |
|||
import org.thingsboard.rule.engine.api.NodeConfiguration; |
|||
|
|||
import java.util.Collections; |
|||
import java.util.List; |
|||
|
|||
@Data |
|||
public class TbCheckMessageNodeConfiguration implements NodeConfiguration { |
|||
|
|||
private List<String> messageNames; |
|||
private List<String> metadataNames; |
|||
|
|||
private boolean checkAllKeys; |
|||
|
|||
|
|||
@Override |
|||
public TbCheckMessageNodeConfiguration defaultConfiguration() { |
|||
TbCheckMessageNodeConfiguration configuration = new TbCheckMessageNodeConfiguration(); |
|||
configuration.setMessageNames(Collections.emptyList()); |
|||
configuration.setMetadataNames(Collections.emptyList()); |
|||
configuration.setCheckAllKeys(true); |
|||
return configuration; |
|||
} |
|||
} |
|||
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue