...
|
...
|
@@ -107,95 +107,76 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S |
107
|
107
|
}
|
108
|
108
|
|
109
|
109
|
@Override
|
110
|
|
- public List<TbMsg> executeUpdate(TbMsg msg) throws ScriptException {
|
111
|
|
- JsonNode result = executeScript(msg);
|
112
|
|
- if (result.isObject()) {
|
113
|
|
- return Collections.singletonList(unbindMsg(result, msg));
|
114
|
|
- } else if (result.isArray()){
|
115
|
|
- List<TbMsg> res = new ArrayList<>(result.size());
|
116
|
|
- result.forEach(jsonObject -> res.add(unbindMsg(jsonObject, msg)));
|
117
|
|
- return res;
|
118
|
|
- } else {
|
119
|
|
- log.warn("Wrong result type: {}", result.getNodeType());
|
120
|
|
- throw new ScriptException("Wrong result type: " + result.getNodeType());
|
121
|
|
- }
|
122
|
|
- }
|
123
|
|
-
|
124
|
|
- @Override
|
125
|
110
|
public ListenableFuture<List<TbMsg>> executeUpdateAsync(TbMsg msg) {
|
126
|
111
|
ListenableFuture<JsonNode> result = executeScriptAsync(msg);
|
127
|
|
- return Futures.transformAsync(result, json -> {
|
128
|
|
- if (json.isObject()) {
|
129
|
|
- return Futures.immediateFuture(Collections.singletonList(unbindMsg(json, msg)));
|
130
|
|
- } else if (json.isArray()){
|
131
|
|
- List<TbMsg> res = new ArrayList<>(json.size());
|
132
|
|
- json.forEach(jsonObject -> res.add(unbindMsg(jsonObject, msg)));
|
133
|
|
- return Futures.immediateFuture(res);
|
134
|
|
- }
|
135
|
|
- else{
|
136
|
|
- log.warn("Wrong result type: {}", json.getNodeType());
|
137
|
|
- return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + json.getNodeType()));
|
138
|
|
- }
|
139
|
|
- }, MoreExecutors.directExecutor());
|
|
112
|
+ return Futures.transformAsync(result,
|
|
113
|
+ json -> executeUpdateTransform(msg, json),
|
|
114
|
+ MoreExecutors.directExecutor());
|
|
115
|
+ }
|
|
116
|
+
|
|
117
|
+ ListenableFuture<List<TbMsg>> executeUpdateTransform(TbMsg msg, JsonNode json) {
|
|
118
|
+ if (json.isObject()) {
|
|
119
|
+ return Futures.immediateFuture(Collections.singletonList(unbindMsg(json, msg)));
|
|
120
|
+ } else if (json.isArray()) {
|
|
121
|
+ List<TbMsg> res = new ArrayList<>(json.size());
|
|
122
|
+ json.forEach(jsonObject -> res.add(unbindMsg(jsonObject, msg)));
|
|
123
|
+ return Futures.immediateFuture(res);
|
|
124
|
+ }
|
|
125
|
+ log.warn("Wrong result type: {}", json.getNodeType());
|
|
126
|
+ return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + json.getNodeType()));
|
140
|
127
|
}
|
141
|
128
|
|
142
|
129
|
@Override
|
143
|
130
|
public ListenableFuture<TbMsg> executeGenerateAsync(TbMsg prevMsg) {
|
144
|
|
- log.trace("execute generate async, prevMsg {}", prevMsg);
|
145
|
|
- return Futures.transformAsync(executeScriptAsync(prevMsg), result -> {
|
146
|
|
- if (!result.isObject()) {
|
147
|
|
- log.warn("Wrong result type: {}", result.getNodeType());
|
148
|
|
- Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + result.getNodeType()));
|
149
|
|
- }
|
150
|
|
- return Futures.immediateFuture(unbindMsg(result, prevMsg));
|
151
|
|
- }, MoreExecutors.directExecutor());
|
152
|
|
-
|
|
131
|
+ return Futures.transformAsync(executeScriptAsync(prevMsg),
|
|
132
|
+ result -> executeGenerateTransform(prevMsg, result),
|
|
133
|
+ MoreExecutors.directExecutor());
|
153
|
134
|
}
|
154
|
135
|
|
155
|
|
- @Override
|
156
|
|
- public JsonNode executeJson(TbMsg msg) throws ScriptException {
|
157
|
|
- return executeScript(msg);
|
|
136
|
+ ListenableFuture<TbMsg> executeGenerateTransform(TbMsg prevMsg, JsonNode result) {
|
|
137
|
+ if (!result.isObject()) {
|
|
138
|
+ log.warn("Wrong result type: {}", result.getNodeType());
|
|
139
|
+ Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + result.getNodeType()));
|
|
140
|
+ }
|
|
141
|
+ return Futures.immediateFuture(unbindMsg(result, prevMsg));
|
158
|
142
|
}
|
159
|
143
|
|
160
|
144
|
@Override
|
161
|
|
- public ListenableFuture<JsonNode> executeJsonAsync(TbMsg msg) throws ScriptException {
|
|
145
|
+ public ListenableFuture<JsonNode> executeJsonAsync(TbMsg msg) {
|
162
|
146
|
return executeScriptAsync(msg);
|
163
|
147
|
}
|
164
|
148
|
|
165
|
149
|
@Override
|
166
|
|
- public String executeToString(TbMsg msg) throws ScriptException {
|
167
|
|
- JsonNode result = executeScript(msg);
|
168
|
|
- if (!result.isTextual()) {
|
169
|
|
- log.warn("Wrong result type: {}", result.getNodeType());
|
170
|
|
- throw new ScriptException("Wrong result type: " + result.getNodeType());
|
171
|
|
- }
|
172
|
|
- return result.asText();
|
|
150
|
+ public ListenableFuture<String> executeToStringAsync(TbMsg msg) {
|
|
151
|
+ return Futures.transformAsync(executeScriptAsync(msg),
|
|
152
|
+ this::executeToStringTransform,
|
|
153
|
+ MoreExecutors.directExecutor());
|
173
|
154
|
}
|
174
|
155
|
|
175
|
|
- @Override
|
176
|
|
- public boolean executeFilter(TbMsg msg) throws ScriptException {
|
177
|
|
- JsonNode result = executeScript(msg);
|
178
|
|
- if (!result.isBoolean()) {
|
179
|
|
- log.warn("Wrong result type: {}", result.getNodeType());
|
180
|
|
- throw new ScriptException("Wrong result type: " + result.getNodeType());
|
|
156
|
+ ListenableFuture<String> executeToStringTransform(JsonNode result) {
|
|
157
|
+ if (result.isTextual()) {
|
|
158
|
+ return Futures.immediateFuture(result.asText());
|
181
|
159
|
}
|
182
|
|
- return result.asBoolean();
|
|
160
|
+ log.warn("Wrong result type: {}", result.getNodeType());
|
|
161
|
+ return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + result.getNodeType()));
|
183
|
162
|
}
|
184
|
163
|
|
185
|
164
|
@Override
|
186
|
165
|
public ListenableFuture<Boolean> executeFilterAsync(TbMsg msg) {
|
187
|
|
- ListenableFuture<JsonNode> result = executeScriptAsync(msg);
|
188
|
|
- return Futures.transformAsync(result, json -> {
|
189
|
|
- if (!json.isBoolean()) {
|
190
|
|
- log.warn("Wrong result type: {}", json.getNodeType());
|
191
|
|
- return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + json.getNodeType()));
|
192
|
|
- } else {
|
193
|
|
- return Futures.immediateFuture(json.asBoolean());
|
194
|
|
- }
|
195
|
|
- }, MoreExecutors.directExecutor());
|
|
166
|
+ return Futures.transformAsync(executeScriptAsync(msg),
|
|
167
|
+ this::executeFilterTransform,
|
|
168
|
+ MoreExecutors.directExecutor());
|
|
169
|
+ }
|
|
170
|
+
|
|
171
|
+ ListenableFuture<Boolean> executeFilterTransform(JsonNode json) {
|
|
172
|
+ if (json.isBoolean()) {
|
|
173
|
+ return Futures.immediateFuture(json.asBoolean());
|
|
174
|
+ }
|
|
175
|
+ log.warn("Wrong result type: {}", json.getNodeType());
|
|
176
|
+ return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + json.getNodeType()));
|
196
|
177
|
}
|
197
|
178
|
|
198
|
|
- ListenableFuture<Set<String>> executeSwitchPostProcessAsyncFunction(JsonNode result) {
|
|
179
|
+ ListenableFuture<Set<String>> executeSwitchTransform(JsonNode result) {
|
199
|
180
|
if (result.isTextual()) {
|
200
|
181
|
return Futures.immediateFuture(Collections.singleton(result.asText()));
|
201
|
182
|
}
|
...
|
...
|
@@ -217,34 +198,19 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S |
217
|
198
|
|
218
|
199
|
@Override
|
219
|
200
|
public ListenableFuture<Set<String>> executeSwitchAsync(TbMsg msg) {
|
220
|
|
- log.trace("execute switch async, msg {}", msg);
|
221
|
201
|
return Futures.transformAsync(executeScriptAsync(msg),
|
222
|
|
- this::executeSwitchPostProcessAsyncFunction,
|
|
202
|
+ this::executeSwitchTransform,
|
223
|
203
|
MoreExecutors.directExecutor()); //usually runs in a callbackExecutor
|
224
|
204
|
}
|
225
|
205
|
|
226
|
|
- private JsonNode executeScript(TbMsg msg) throws ScriptException {
|
227
|
|
- try {
|
228
|
|
- String[] inArgs = prepareArgs(msg);
|
229
|
|
- String eval = sandboxService.invokeFunction(tenantId, msg.getCustomerId(), this.scriptId, inArgs[0], inArgs[1], inArgs[2]).get().toString();
|
230
|
|
- return mapper.readTree(eval);
|
231
|
|
- } catch (ExecutionException e) {
|
232
|
|
- if (e.getCause() instanceof ScriptException) {
|
233
|
|
- throw (ScriptException) e.getCause();
|
234
|
|
- } else if (e.getCause() instanceof RuntimeException) {
|
235
|
|
- throw new ScriptException(e.getCause().getMessage());
|
236
|
|
- } else {
|
237
|
|
- throw new ScriptException(e);
|
238
|
|
- }
|
239
|
|
- } catch (Exception e) {
|
240
|
|
- throw new ScriptException(e);
|
241
|
|
- }
|
242
|
|
- }
|
243
|
|
-
|
244
|
|
- private ListenableFuture<JsonNode> executeScriptAsync(TbMsg msg) {
|
|
206
|
+ ListenableFuture<JsonNode> executeScriptAsync(TbMsg msg) {
|
245
|
207
|
log.trace("execute script async, msg {}", msg);
|
246
|
208
|
String[] inArgs = prepareArgs(msg);
|
247
|
|
- return Futures.transformAsync(sandboxService.invokeFunction(tenantId, msg.getCustomerId(), this.scriptId, inArgs[0], inArgs[1], inArgs[2]),
|
|
209
|
+ return executeScriptAsync(msg.getCustomerId(), inArgs[0], inArgs[1], inArgs[2]);
|
|
210
|
+ }
|
|
211
|
+
|
|
212
|
+ ListenableFuture<JsonNode> executeScriptAsync(CustomerId customerId, Object... args) {
|
|
213
|
+ return Futures.transformAsync(sandboxService.invokeFunction(tenantId, customerId, this.scriptId, args),
|
248
|
214
|
o -> {
|
249
|
215
|
try {
|
250
|
216
|
return Futures.immediateFuture(mapper.readTree(o.toString()));
|
...
|
...
|
|