|
|
|
@ -16,15 +16,20 @@ |
|
|
|
package org.thingsboard.rule.engine.api.util; |
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
import org.thingsboard.rule.engine.api.TbNodeConfiguration; |
|
|
|
import org.thingsboard.rule.engine.api.TbNodeException; |
|
|
|
import org.thingsboard.server.common.msg.TbMsg; |
|
|
|
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.regex.Matcher; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
|
@ -34,8 +39,11 @@ public class TbNodeUtils { |
|
|
|
|
|
|
|
private static final ObjectMapper mapper = new ObjectMapper(); |
|
|
|
|
|
|
|
private static final String VARIABLE_TEMPLATE = "${%s}"; |
|
|
|
private static final String METADATA_VARIABLE_TEMPLATE = "${%s}"; |
|
|
|
|
|
|
|
private static final Pattern DATA_PATTERN = Pattern.compile("(\\$\\[)(.*?)(\\])"); |
|
|
|
|
|
|
|
private static final String DATA_VARIABLE_TEMPLATE = "$[%s]"; |
|
|
|
|
|
|
|
public static <T> T convert(TbNodeConfiguration configuration, Class<T> clazz) throws TbNodeException { |
|
|
|
try { |
|
|
|
@ -45,6 +53,44 @@ public class TbNodeUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static List<String> processPatterns(List<String> patterns, TbMsg tbMsg) { |
|
|
|
if (!CollectionUtils.isEmpty(patterns)) { |
|
|
|
return patterns.stream().map(p -> processPattern(p, tbMsg)).collect(Collectors.toList()); |
|
|
|
} |
|
|
|
return Collections.emptyList(); |
|
|
|
} |
|
|
|
|
|
|
|
public static String processPattern(String pattern, TbMsg tbMsg) { |
|
|
|
try { |
|
|
|
String result = processPattern(pattern, tbMsg.getMetaData()); |
|
|
|
JsonNode json = mapper.readTree(tbMsg.getData()); |
|
|
|
if (json.isObject()) { |
|
|
|
Matcher matcher = DATA_PATTERN.matcher(result); |
|
|
|
|
|
|
|
while (matcher.find()) { |
|
|
|
String group = matcher.group(2); |
|
|
|
String[] keys = group.split("\\."); |
|
|
|
JsonNode jsonNode = json; |
|
|
|
for (String key : keys) { |
|
|
|
if (StringUtils.isNotEmpty(key) && jsonNode != null) { |
|
|
|
jsonNode = jsonNode.get(key); |
|
|
|
} else { |
|
|
|
jsonNode = null; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (jsonNode != null && !jsonNode.isObject() && !jsonNode.isArray()) { |
|
|
|
result = result.replace(String.format(DATA_VARIABLE_TEMPLATE, group), jsonNode.asText()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RuntimeException("Failed to process pattern!", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static List<String> processPatterns(List<String> patterns, TbMsgMetaData metaData) { |
|
|
|
if (!CollectionUtils.isEmpty(patterns)) { |
|
|
|
return patterns.stream().map(p -> processPattern(p, metaData)).collect(Collectors.toList()); |
|
|
|
@ -53,15 +99,15 @@ public class TbNodeUtils { |
|
|
|
} |
|
|
|
|
|
|
|
public static String processPattern(String pattern, TbMsgMetaData metaData) { |
|
|
|
String result = new String(pattern); |
|
|
|
for (Map.Entry<String,String> keyVal : metaData.values().entrySet()) { |
|
|
|
String result = pattern; |
|
|
|
for (Map.Entry<String, String> keyVal : metaData.values().entrySet()) { |
|
|
|
result = processVar(result, keyVal.getKey(), keyVal.getValue()); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
private static String processVar(String pattern, String key, String val) { |
|
|
|
String varPattern = String.format(VARIABLE_TEMPLATE, key); |
|
|
|
String varPattern = String.format(METADATA_VARIABLE_TEMPLATE, key); |
|
|
|
return pattern.replace(varPattern, val); |
|
|
|
} |
|
|
|
|
|
|
|
|