Fix for test script node function
This commit is contained in:
		
							parent
							
								
									9746df84ea
								
							
						
					
					
						commit
						52d1b756fa
					
				@ -18,6 +18,7 @@ package org.thingsboard.server.controller;
 | 
			
		||||
import com.fasterxml.jackson.core.type.TypeReference;
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
import com.fasterxml.jackson.databind.node.ArrayNode;
 | 
			
		||||
import com.fasterxml.jackson.databind.node.ObjectNode;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
@ -432,4 +433,22 @@ public class RuleChainController extends BaseController {
 | 
			
		||||
        return objectMapper.writeValueAsString(msgData);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private String msgToOutput(List<TbMsg> msgs) throws Exception {
 | 
			
		||||
        ArrayNode resultNode = objectMapper.createArrayNode();
 | 
			
		||||
        for (TbMsg msg:msgs) {
 | 
			
		||||
            ObjectNode msgData = objectMapper.createObjectNode();
 | 
			
		||||
            if (!StringUtils.isEmpty(msg.getData())) {
 | 
			
		||||
                msgData.set("msg", objectMapper.readTree(msg.getData()));
 | 
			
		||||
            }
 | 
			
		||||
            Map<String, String> metadata = msg.getMetaData().getData();
 | 
			
		||||
            msgData.set("metadata", objectMapper.valueToTree(metadata));
 | 
			
		||||
            msgData.put("msgType", msg.getType());
 | 
			
		||||
            resultNode.add(msgData);
 | 
			
		||||
        }
 | 
			
		||||
        if (resultNode.size() == 1) {
 | 
			
		||||
            return objectMapper.writeValueAsString(resultNode.get(0));
 | 
			
		||||
        }
 | 
			
		||||
        return objectMapper.writeValueAsString(resultNode);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -108,13 +108,18 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public TbMsg executeUpdate(TbMsg msg) throws ScriptException {
 | 
			
		||||
    public List<TbMsg> executeUpdate(TbMsg msg) throws ScriptException {
 | 
			
		||||
        JsonNode result = executeScript(msg);
 | 
			
		||||
        if (!result.isObject()) {
 | 
			
		||||
        if (result.isObject()) {
 | 
			
		||||
            return Collections.singletonList(unbindMsg(result, msg));
 | 
			
		||||
        } else if (result.isArray()){
 | 
			
		||||
            List<TbMsg> res = new ArrayList<>(result.size());
 | 
			
		||||
            result.forEach(jsonObject -> res.add(unbindMsg(jsonObject, msg)));
 | 
			
		||||
            return res;
 | 
			
		||||
        } else {
 | 
			
		||||
            log.warn("Wrong result type: {}", result.getNodeType());
 | 
			
		||||
            throw new ScriptException("Wrong result type: " + result.getNodeType());
 | 
			
		||||
        }
 | 
			
		||||
        return unbindMsg(result, msg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 | 
			
		||||
@ -25,7 +25,7 @@ import java.util.Set;
 | 
			
		||||
 | 
			
		||||
public interface ScriptEngine {
 | 
			
		||||
 | 
			
		||||
    TbMsg executeUpdate(TbMsg msg) throws ScriptException;
 | 
			
		||||
    List<TbMsg> executeUpdate(TbMsg msg) throws ScriptException;
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<List<TbMsg>> executeUpdateAsync(TbMsg msg);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user