|
|
@ -16,11 +16,11 @@ |
|
|
package org.thingsboard.rule.engine.api.util; |
|
|
package org.thingsboard.rule.engine.api.util; |
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
import org.thingsboard.rule.engine.api.TbNodeConfiguration; |
|
|
import org.thingsboard.rule.engine.api.TbNodeConfiguration; |
|
|
import org.thingsboard.rule.engine.api.TbNodeException; |
|
|
import org.thingsboard.rule.engine.api.TbNodeException; |
|
|
import org.thingsboard.server.common.data.StringUtils; |
|
|
import org.thingsboard.server.common.data.StringUtils; |
|
|
|
|
|
import org.thingsboard.server.common.data.util.CollectionsUtil; |
|
|
import org.thingsboard.server.common.msg.TbMsg; |
|
|
import org.thingsboard.server.common.msg.TbMsg; |
|
|
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|
|
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|
|
|
|
|
|
|
|
@ -29,15 +29,18 @@ import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
import java.util.regex.Matcher; |
|
|
import java.util.regex.Matcher; |
|
|
import java.util.regex.Pattern; |
|
|
import java.util.regex.Pattern; |
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
public final class TbNodeUtils { |
|
|
* Created by ashvayka on 19.01.18. |
|
|
|
|
|
*/ |
|
|
private TbNodeUtils() { |
|
|
public class TbNodeUtils { |
|
|
throw new IllegalStateException("Utility class"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private static final Pattern DATA_PATTERN = Pattern.compile("(\\$\\[)(.*?)(])"); |
|
|
private static final Pattern DATA_PATTERN = Pattern.compile("(\\$\\[)(.*?)(])"); |
|
|
|
|
|
|
|
|
|
|
|
private static final String ALL_DATA_TEMPLATE = "$[*]"; |
|
|
|
|
|
private static final String ALL_METADATA_TEMPLATE = "${*}"; |
|
|
|
|
|
|
|
|
public static <T> T convert(TbNodeConfiguration configuration, Class<T> clazz) throws TbNodeException { |
|
|
public static <T> T convert(TbNodeConfiguration configuration, Class<T> clazz) throws TbNodeException { |
|
|
try { |
|
|
try { |
|
|
return JacksonUtil.treeToValue(configuration.getData(), clazz); |
|
|
return JacksonUtil.treeToValue(configuration.getData(), clazz); |
|
|
@ -47,16 +50,19 @@ public class TbNodeUtils { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static List<String> processPatterns(List<String> patterns, TbMsg tbMsg) { |
|
|
public static List<String> processPatterns(List<String> patterns, TbMsg tbMsg) { |
|
|
if (!CollectionUtils.isEmpty(patterns)) { |
|
|
if (CollectionsUtil.isEmpty(patterns)) { |
|
|
return patterns.stream().map(p -> processPattern(p, tbMsg)).collect(Collectors.toList()); |
|
|
return Collections.emptyList(); |
|
|
} |
|
|
} |
|
|
return Collections.emptyList(); |
|
|
return patterns.stream().map(p -> processPattern(p, tbMsg)).toList(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static String processPattern(String pattern, TbMsg tbMsg) { |
|
|
public static String processPattern(String pattern, TbMsg tbMsg) { |
|
|
try { |
|
|
try { |
|
|
String result = processPattern(pattern, tbMsg.getMetaData()); |
|
|
String result = processPattern(pattern, tbMsg.getMetaData()); |
|
|
JsonNode json = JacksonUtil.toJsonNode(tbMsg.getData()); |
|
|
JsonNode json = JacksonUtil.toJsonNode(tbMsg.getData()); |
|
|
|
|
|
|
|
|
|
|
|
result = result.replace(ALL_DATA_TEMPLATE, JacksonUtil.toString(json)); |
|
|
|
|
|
|
|
|
if (json.isObject()) { |
|
|
if (json.isObject()) { |
|
|
Matcher matcher = DATA_PATTERN.matcher(result); |
|
|
Matcher matcher = DATA_PATTERN.matcher(result); |
|
|
while (matcher.find()) { |
|
|
while (matcher.find()) { |
|
|
@ -64,7 +70,7 @@ public class TbNodeUtils { |
|
|
String[] keys = group.split("\\."); |
|
|
String[] keys = group.split("\\."); |
|
|
JsonNode jsonNode = json; |
|
|
JsonNode jsonNode = json; |
|
|
for (String key : keys) { |
|
|
for (String key : keys) { |
|
|
if (!StringUtils.isEmpty(key) && jsonNode != null) { |
|
|
if (StringUtils.isNotEmpty(key) && jsonNode != null) { |
|
|
jsonNode = jsonNode.get(key); |
|
|
jsonNode = jsonNode.get(key); |
|
|
} else { |
|
|
} else { |
|
|
jsonNode = null; |
|
|
jsonNode = null; |
|
|
@ -83,15 +89,9 @@ public class TbNodeUtils { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Deprecated(since = "3.6.1", forRemoval = true) |
|
|
private static String processPattern(String pattern, TbMsgMetaData metaData) { |
|
|
public static List<String> processPatterns(List<String> patterns, TbMsgMetaData metaData) { |
|
|
String replacement = metaData.isEmpty() ? "{}" : JacksonUtil.toString(metaData.getData()); |
|
|
if (!CollectionUtils.isEmpty(patterns)) { |
|
|
pattern = pattern.replace(ALL_METADATA_TEMPLATE, replacement); |
|
|
return patterns.stream().map(p -> processPattern(p, metaData)).collect(Collectors.toList()); |
|
|
|
|
|
} |
|
|
|
|
|
return Collections.emptyList(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String processPattern(String pattern, TbMsgMetaData metaData) { |
|
|
|
|
|
return processTemplate(pattern, metaData.values()); |
|
|
return processTemplate(pattern, metaData.values()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -108,10 +108,11 @@ public class TbNodeUtils { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static String formatDataVarTemplate(String key) { |
|
|
static String formatDataVarTemplate(String key) { |
|
|
return "$[" + key + ']'; |
|
|
return "$[" + key + "]"; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static String formatMetadataVarTemplate(String key) { |
|
|
static String formatMetadataVarTemplate(String key) { |
|
|
return "${" + key + '}'; |
|
|
return "${" + key + "}"; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|