Commit f3757ad127cd0029e347d7b5858a99e4eab62f93

Authored by Sergey Matvienko
1 parent 8915b279

js-script-engine-api: refactored executeSwitchAsync

... ... @@ -18,7 +18,6 @@ package org.thingsboard.server.service.script;
18 18 import com.fasterxml.jackson.core.type.TypeReference;
19 19 import com.fasterxml.jackson.databind.JsonNode;
20 20 import com.fasterxml.jackson.databind.ObjectMapper;
21   -import com.google.common.collect.Sets;
22 21 import com.google.common.util.concurrent.Futures;
23 22 import com.google.common.util.concurrent.ListenableFuture;
24 23 import com.google.common.util.concurrent.MoreExecutors;
... ... @@ -32,6 +31,7 @@ import org.thingsboard.server.common.msg.TbMsgMetaData;
32 31 import javax.script.ScriptException;
33 32 import java.util.ArrayList;
34 33 import java.util.Collections;
  34 +import java.util.HashSet;
35 35 import java.util.List;
36 36 import java.util.Map;
37 37 import java.util.Set;
... ... @@ -145,7 +145,7 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S
145 145 return Futures.transformAsync(executeScriptAsync(prevMsg), result -> {
146 146 if (!result.isObject()) {
147 147 log.warn("Wrong result type: {}", result.getNodeType());
148   - throw new ScriptException("Wrong result type: " + result.getNodeType());
  148 + Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + result.getNodeType()));
149 149 }
150 150 return Futures.immediateFuture(unbindMsg(result, prevMsg));
151 151 }, MoreExecutors.directExecutor());
... ... @@ -195,31 +195,31 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S
195 195 }, MoreExecutors.directExecutor());
196 196 }
197 197
198   - Set<String> executeSwitchPostProcessFunction(JsonNode result) throws ScriptException {
  198 + ListenableFuture<Set<String>> executeSwitchPostProcessAsyncFunction(JsonNode result) {
199 199 if (result.isTextual()) {
200   - return Collections.singleton(result.asText());
201   - } else if (result.isArray()) {
202   - Set<String> nextStates = Sets.newHashSet();
  200 + return Futures.immediateFuture(Collections.singleton(result.asText()));
  201 + }
  202 + if (result.isArray()) {
  203 + Set<String> nextStates = new HashSet<>();
203 204 for (JsonNode val : result) {
204 205 if (!val.isTextual()) {
205 206 log.warn("Wrong result type: {}", val.getNodeType());
206   - throw new ScriptException("Wrong result type: " + val.getNodeType());
  207 + return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + val.getNodeType()));
207 208 } else {
208 209 nextStates.add(val.asText());
209 210 }
210 211 }
211   - return nextStates;
212   - } else {
213   - log.warn("Wrong result type: {}", result.getNodeType());
214   - throw new ScriptException("Wrong result type: " + result.getNodeType());
  212 + return Futures.immediateFuture(nextStates);
215 213 }
  214 + log.warn("Wrong result type: {}", result.getNodeType());
  215 + return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + result.getNodeType()));
216 216 }
217 217
218 218 @Override
219 219 public ListenableFuture<Set<String>> executeSwitchAsync(TbMsg msg) {
220 220 log.trace("execute switch async, msg {}", msg);
221 221 return Futures.transformAsync(executeScriptAsync(msg),
222   - result -> Futures.immediateFuture(executeSwitchPostProcessFunction(result)),
  222 + this::executeSwitchPostProcessAsyncFunction,
223 223 MoreExecutors.directExecutor()); //usually runs in a callbackExecutor
224 224 }
225 225
... ...