Commit 52d1b756fac543cb6136bdcac9c5d7b4be824192
Committed by
Andrew Shvayka
1 parent
9746df84
Fix for test script node function
Showing
3 changed files
with
28 additions
and
4 deletions
@@ -18,6 +18,7 @@ package org.thingsboard.server.controller; | @@ -18,6 +18,7 @@ package org.thingsboard.server.controller; | ||
18 | import com.fasterxml.jackson.core.type.TypeReference; | 18 | import com.fasterxml.jackson.core.type.TypeReference; |
19 | import com.fasterxml.jackson.databind.JsonNode; | 19 | import com.fasterxml.jackson.databind.JsonNode; |
20 | import com.fasterxml.jackson.databind.ObjectMapper; | 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
21 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
21 | import com.fasterxml.jackson.databind.node.ObjectNode; | 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
22 | import lombok.extern.slf4j.Slf4j; | 23 | import lombok.extern.slf4j.Slf4j; |
23 | import org.springframework.beans.factory.annotation.Autowired; | 24 | import org.springframework.beans.factory.annotation.Autowired; |
@@ -432,4 +433,22 @@ public class RuleChainController extends BaseController { | @@ -432,4 +433,22 @@ public class RuleChainController extends BaseController { | ||
432 | return objectMapper.writeValueAsString(msgData); | 433 | return objectMapper.writeValueAsString(msgData); |
433 | } | 434 | } |
434 | 435 | ||
436 | + private String msgToOutput(List<TbMsg> msgs) throws Exception { | ||
437 | + ArrayNode resultNode = objectMapper.createArrayNode(); | ||
438 | + for (TbMsg msg:msgs) { | ||
439 | + ObjectNode msgData = objectMapper.createObjectNode(); | ||
440 | + if (!StringUtils.isEmpty(msg.getData())) { | ||
441 | + msgData.set("msg", objectMapper.readTree(msg.getData())); | ||
442 | + } | ||
443 | + Map<String, String> metadata = msg.getMetaData().getData(); | ||
444 | + msgData.set("metadata", objectMapper.valueToTree(metadata)); | ||
445 | + msgData.put("msgType", msg.getType()); | ||
446 | + resultNode.add(msgData); | ||
447 | + } | ||
448 | + if (resultNode.size() == 1) { | ||
449 | + return objectMapper.writeValueAsString(resultNode.get(0)); | ||
450 | + } | ||
451 | + return objectMapper.writeValueAsString(resultNode); | ||
452 | + } | ||
453 | + | ||
435 | } | 454 | } |
@@ -108,13 +108,18 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S | @@ -108,13 +108,18 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S | ||
108 | } | 108 | } |
109 | 109 | ||
110 | @Override | 110 | @Override |
111 | - public TbMsg executeUpdate(TbMsg msg) throws ScriptException { | 111 | + public List<TbMsg> executeUpdate(TbMsg msg) throws ScriptException { |
112 | JsonNode result = executeScript(msg); | 112 | JsonNode result = executeScript(msg); |
113 | - if (!result.isObject()) { | 113 | + if (result.isObject()) { |
114 | + return Collections.singletonList(unbindMsg(result, msg)); | ||
115 | + } else if (result.isArray()){ | ||
116 | + List<TbMsg> res = new ArrayList<>(result.size()); | ||
117 | + result.forEach(jsonObject -> res.add(unbindMsg(jsonObject, msg))); | ||
118 | + return res; | ||
119 | + } else { | ||
114 | log.warn("Wrong result type: {}", result.getNodeType()); | 120 | log.warn("Wrong result type: {}", result.getNodeType()); |
115 | throw new ScriptException("Wrong result type: " + result.getNodeType()); | 121 | throw new ScriptException("Wrong result type: " + result.getNodeType()); |
116 | } | 122 | } |
117 | - return unbindMsg(result, msg); | ||
118 | } | 123 | } |
119 | 124 | ||
120 | @Override | 125 | @Override |
@@ -25,7 +25,7 @@ import java.util.Set; | @@ -25,7 +25,7 @@ import java.util.Set; | ||
25 | 25 | ||
26 | public interface ScriptEngine { | 26 | public interface ScriptEngine { |
27 | 27 | ||
28 | - TbMsg executeUpdate(TbMsg msg) throws ScriptException; | 28 | + List<TbMsg> executeUpdate(TbMsg msg) throws ScriptException; |
29 | 29 | ||
30 | ListenableFuture<List<TbMsg>> executeUpdateAsync(TbMsg msg); | 30 | ListenableFuture<List<TbMsg>> executeUpdateAsync(TbMsg msg); |
31 | 31 |