Commit f3757ad127cd0029e347d7b5858a99e4eab62f93
1 parent
8915b279
js-script-engine-api: refactored executeSwitchAsync
Showing
1 changed file
with
12 additions
and
12 deletions
@@ -18,7 +18,6 @@ package org.thingsboard.server.service.script; | @@ -18,7 +18,6 @@ package org.thingsboard.server.service.script; | ||
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.google.common.collect.Sets; | ||
22 | import com.google.common.util.concurrent.Futures; | 21 | import com.google.common.util.concurrent.Futures; |
23 | import com.google.common.util.concurrent.ListenableFuture; | 22 | import com.google.common.util.concurrent.ListenableFuture; |
24 | import com.google.common.util.concurrent.MoreExecutors; | 23 | import com.google.common.util.concurrent.MoreExecutors; |
@@ -32,6 +31,7 @@ import org.thingsboard.server.common.msg.TbMsgMetaData; | @@ -32,6 +31,7 @@ import org.thingsboard.server.common.msg.TbMsgMetaData; | ||
32 | import javax.script.ScriptException; | 31 | import javax.script.ScriptException; |
33 | import java.util.ArrayList; | 32 | import java.util.ArrayList; |
34 | import java.util.Collections; | 33 | import java.util.Collections; |
34 | +import java.util.HashSet; | ||
35 | import java.util.List; | 35 | import java.util.List; |
36 | import java.util.Map; | 36 | import java.util.Map; |
37 | import java.util.Set; | 37 | import java.util.Set; |
@@ -145,7 +145,7 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S | @@ -145,7 +145,7 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S | ||
145 | return Futures.transformAsync(executeScriptAsync(prevMsg), result -> { | 145 | return Futures.transformAsync(executeScriptAsync(prevMsg), result -> { |
146 | if (!result.isObject()) { | 146 | if (!result.isObject()) { |
147 | log.warn("Wrong result type: {}", result.getNodeType()); | 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 | return Futures.immediateFuture(unbindMsg(result, prevMsg)); | 150 | return Futures.immediateFuture(unbindMsg(result, prevMsg)); |
151 | }, MoreExecutors.directExecutor()); | 151 | }, MoreExecutors.directExecutor()); |
@@ -195,31 +195,31 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S | @@ -195,31 +195,31 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S | ||
195 | }, MoreExecutors.directExecutor()); | 195 | }, MoreExecutors.directExecutor()); |
196 | } | 196 | } |
197 | 197 | ||
198 | - Set<String> executeSwitchPostProcessFunction(JsonNode result) throws ScriptException { | 198 | + ListenableFuture<Set<String>> executeSwitchPostProcessAsyncFunction(JsonNode result) { |
199 | if (result.isTextual()) { | 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 | for (JsonNode val : result) { | 204 | for (JsonNode val : result) { |
204 | if (!val.isTextual()) { | 205 | if (!val.isTextual()) { |
205 | log.warn("Wrong result type: {}", val.getNodeType()); | 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 | } else { | 208 | } else { |
208 | nextStates.add(val.asText()); | 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 | @Override | 218 | @Override |
219 | public ListenableFuture<Set<String>> executeSwitchAsync(TbMsg msg) { | 219 | public ListenableFuture<Set<String>> executeSwitchAsync(TbMsg msg) { |
220 | log.trace("execute switch async, msg {}", msg); | 220 | log.trace("execute switch async, msg {}", msg); |
221 | return Futures.transformAsync(executeScriptAsync(msg), | 221 | return Futures.transformAsync(executeScriptAsync(msg), |
222 | - result -> Futures.immediateFuture(executeSwitchPostProcessFunction(result)), | 222 | + this::executeSwitchPostProcessAsyncFunction, |
223 | MoreExecutors.directExecutor()); //usually runs in a callbackExecutor | 223 | MoreExecutors.directExecutor()); //usually runs in a callbackExecutor |
224 | } | 224 | } |
225 | 225 |