...
|
...
|
@@ -19,6 +19,8 @@ import com.fasterxml.jackson.core.type.TypeReference; |
19
|
19
|
import com.fasterxml.jackson.databind.JsonNode;
|
20
|
20
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
21
|
21
|
import com.google.common.collect.Sets;
|
|
22
|
+import com.google.common.util.concurrent.Futures;
|
|
23
|
+import com.google.common.util.concurrent.ListenableFuture;
|
22
|
24
|
import lombok.extern.slf4j.Slf4j;
|
23
|
25
|
import org.apache.commons.lang3.StringUtils;
|
24
|
26
|
import org.thingsboard.server.common.data.id.EntityId;
|
...
|
...
|
@@ -110,6 +112,19 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S |
110
|
112
|
}
|
111
|
113
|
|
112
|
114
|
@Override
|
|
115
|
+ public ListenableFuture<TbMsg> executeUpdateAsync(TbMsg msg) {
|
|
116
|
+ ListenableFuture<JsonNode> result = executeScriptAsync(msg);
|
|
117
|
+ return Futures.transformAsync(result, json -> {
|
|
118
|
+ if (!json.isObject()) {
|
|
119
|
+ log.warn("Wrong result type: {}", json.getNodeType());
|
|
120
|
+ return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + json.getNodeType()));
|
|
121
|
+ } else {
|
|
122
|
+ return Futures.immediateFuture(unbindMsg(json, msg));
|
|
123
|
+ }
|
|
124
|
+ });
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ @Override
|
113
|
128
|
public TbMsg executeGenerate(TbMsg prevMsg) throws ScriptException {
|
114
|
129
|
JsonNode result = executeScript(prevMsg);
|
115
|
130
|
if (!result.isObject()) {
|
...
|
...
|
@@ -145,6 +160,19 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S |
145
|
160
|
}
|
146
|
161
|
|
147
|
162
|
@Override
|
|
163
|
+ public ListenableFuture<Boolean> executeFilterAsync(TbMsg msg) {
|
|
164
|
+ ListenableFuture<JsonNode> result = executeScriptAsync(msg);
|
|
165
|
+ return Futures.transformAsync(result, json -> {
|
|
166
|
+ if (!json.isBoolean()) {
|
|
167
|
+ log.warn("Wrong result type: {}", json.getNodeType());
|
|
168
|
+ return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + json.getNodeType()));
|
|
169
|
+ } else {
|
|
170
|
+ return Futures.immediateFuture(json.asBoolean());
|
|
171
|
+ }
|
|
172
|
+ });
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ @Override
|
148
|
176
|
public Set<String> executeSwitch(TbMsg msg) throws ScriptException {
|
149
|
177
|
JsonNode result = executeScript(msg);
|
150
|
178
|
if (result.isTextual()) {
|
...
|
...
|
@@ -173,7 +201,7 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S |
173
|
201
|
return mapper.readTree(eval);
|
174
|
202
|
} catch (ExecutionException e) {
|
175
|
203
|
if (e.getCause() instanceof ScriptException) {
|
176
|
|
- throw (ScriptException)e.getCause();
|
|
204
|
+ throw (ScriptException) e.getCause();
|
177
|
205
|
} else if (e.getCause() instanceof RuntimeException) {
|
178
|
206
|
throw new ScriptException(e.getCause().getMessage());
|
179
|
207
|
} else {
|
...
|
...
|
@@ -184,6 +212,24 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S |
184
|
212
|
}
|
185
|
213
|
}
|
186
|
214
|
|
|
215
|
+ private ListenableFuture<JsonNode> executeScriptAsync(TbMsg msg) {
|
|
216
|
+ String[] inArgs = prepareArgs(msg);
|
|
217
|
+ return Futures.transformAsync(sandboxService.invokeFunction(this.scriptId, inArgs[0], inArgs[1], inArgs[2]),
|
|
218
|
+ o -> {
|
|
219
|
+ try {
|
|
220
|
+ return Futures.immediateFuture(mapper.readTree(o.toString()));
|
|
221
|
+ } catch (Exception e) {
|
|
222
|
+ if (e.getCause() instanceof ScriptException) {
|
|
223
|
+ return Futures.immediateFailedFuture(e.getCause());
|
|
224
|
+ } else if (e.getCause() instanceof RuntimeException) {
|
|
225
|
+ return Futures.immediateFailedFuture(new ScriptException(e.getCause().getMessage()));
|
|
226
|
+ } else {
|
|
227
|
+ return Futures.immediateFailedFuture(new ScriptException(e));
|
|
228
|
+ }
|
|
229
|
+ }
|
|
230
|
+ });
|
|
231
|
+ }
|
|
232
|
+
|
187
|
233
|
public void destroy() {
|
188
|
234
|
sandboxService.release(this.scriptId);
|
189
|
235
|
}
|
...
|
...
|
|