|
|
|
@ -38,6 +38,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
import org.thingsboard.rule.engine.api.ScriptEngine; |
|
|
|
import org.thingsboard.script.api.js.JsInvokeService; |
|
|
|
import org.thingsboard.script.api.mvel.MvelInvokeService; |
|
|
|
import org.thingsboard.server.actors.ActorSystemContext; |
|
|
|
import org.thingsboard.server.actors.tenant.DebugTbRateLimits; |
|
|
|
import org.thingsboard.server.common.data.EventInfo; |
|
|
|
@ -60,6 +61,7 @@ import org.thingsboard.server.common.data.rule.RuleChainImportResult; |
|
|
|
import org.thingsboard.server.common.data.rule.RuleChainMetaData; |
|
|
|
import org.thingsboard.server.common.data.rule.RuleChainOutputLabelsUsage; |
|
|
|
import org.thingsboard.server.common.data.rule.RuleChainType; |
|
|
|
import org.thingsboard.server.common.data.script.ScriptLanguage; |
|
|
|
import org.thingsboard.server.common.msg.TbMsg; |
|
|
|
import org.thingsboard.server.common.msg.TbMsgDataType; |
|
|
|
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|
|
|
@ -67,6 +69,7 @@ import org.thingsboard.server.dao.event.EventService; |
|
|
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
|
|
|
import org.thingsboard.server.service.rule.TbRuleChainService; |
|
|
|
import org.thingsboard.server.service.script.RuleNodeJsScriptEngine; |
|
|
|
import org.thingsboard.server.service.script.RuleNodeMvelScriptEngine; |
|
|
|
import org.thingsboard.server.service.security.permission.Operation; |
|
|
|
import org.thingsboard.server.service.security.permission.Resource; |
|
|
|
|
|
|
|
@ -117,10 +120,10 @@ public class RuleChainController extends BaseController { |
|
|
|
private static final String RULE_CHAIN_DESCRIPTION = "The rule chain object is lightweight and contains general information about the rule chain. " + |
|
|
|
"List of rule nodes and their connection is stored in a separate 'metadata' object."; |
|
|
|
private static final String RULE_CHAIN_METADATA_DESCRIPTION = "The metadata object contains information about the rule nodes and their connections."; |
|
|
|
private static final String TEST_JS_FUNCTION = "Execute the JavaScript function and return the result. The format of request: \n\n" |
|
|
|
private static final String TEST_SCRIPT_FUNCTION = "Execute the Script function and return the result. The format of request: \n\n" |
|
|
|
+ MARKDOWN_CODE_BLOCK_START |
|
|
|
+ "{\n" + |
|
|
|
" \"script\": \"Your JS Function as String\",\n" + |
|
|
|
" \"script\": \"Your Function as String\",\n" + |
|
|
|
" \"scriptType\": \"One of: update, generate, filter, switch, json, string\",\n" + |
|
|
|
" \"argNames\": [\"msg\", \"metadata\", \"type\"],\n" + |
|
|
|
" \"msg\": \"{\\\"temperature\\\": 42}\", \n" + |
|
|
|
@ -140,7 +143,10 @@ public class RuleChainController extends BaseController { |
|
|
|
private EventService eventService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private JsInvokeService scriptInvokeService; |
|
|
|
private JsInvokeService jsInvokeService; |
|
|
|
|
|
|
|
@Autowired(required = false) |
|
|
|
private MvelInvokeService mvelInvokeService; |
|
|
|
|
|
|
|
@Autowired(required = false) |
|
|
|
private ActorSystemContext actorContext; |
|
|
|
@ -148,6 +154,9 @@ public class RuleChainController extends BaseController { |
|
|
|
@Value("${actors.rule.chain.debug_mode_rate_limits_per_tenant.enabled}") |
|
|
|
private boolean debugPerTenantEnabled; |
|
|
|
|
|
|
|
@Value("${mvel.enabled:true}") |
|
|
|
private boolean mvelEnabled; |
|
|
|
|
|
|
|
@ApiOperation(value = "Get Rule Chain (getRuleChainById)", |
|
|
|
notes = "Fetch the Rule Chain object based on the provided Rule Chain Id. " + RULE_CHAIN_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH) |
|
|
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
|
|
|
@ -369,13 +378,23 @@ public class RuleChainController extends BaseController { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ApiOperation(value = "Is MVEL script executor enabled", |
|
|
|
notes = "Returns 'True' if the MVEL script execution is enabled" + TENANT_AUTHORITY_PARAGRAPH) |
|
|
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|
|
|
@RequestMapping(value = "/ruleChain/mvelEnabled", method = RequestMethod.GET) |
|
|
|
@ResponseBody |
|
|
|
public Boolean isMvelEnabled() { |
|
|
|
return mvelEnabled; |
|
|
|
} |
|
|
|
|
|
|
|
@ApiOperation(value = "Test JavaScript function", |
|
|
|
notes = TEST_JS_FUNCTION + TENANT_AUTHORITY_PARAGRAPH) |
|
|
|
@ApiOperation(value = "Test Script function", |
|
|
|
notes = TEST_SCRIPT_FUNCTION + TENANT_AUTHORITY_PARAGRAPH) |
|
|
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|
|
|
@RequestMapping(value = "/ruleChain/testScript", method = RequestMethod.POST) |
|
|
|
@ResponseBody |
|
|
|
public JsonNode testScript( |
|
|
|
@ApiParam(value = "Script language: JS or MVEL") |
|
|
|
@RequestParam(required = false) ScriptLanguage scriptLang, |
|
|
|
@ApiParam(value = "Test JS request. See API call description above.") |
|
|
|
@RequestBody JsonNode inputParams) throws ThingsboardException { |
|
|
|
try { |
|
|
|
@ -393,7 +412,17 @@ public class RuleChainController extends BaseController { |
|
|
|
String errorText = ""; |
|
|
|
ScriptEngine engine = null; |
|
|
|
try { |
|
|
|
engine = new RuleNodeJsScriptEngine(getTenantId(), scriptInvokeService, script, argNames); |
|
|
|
if (scriptLang == null) { |
|
|
|
scriptLang = ScriptLanguage.JS; |
|
|
|
} |
|
|
|
if (ScriptLanguage.JS.equals(scriptLang)) { |
|
|
|
engine = new RuleNodeJsScriptEngine(getTenantId(), jsInvokeService, script, argNames); |
|
|
|
} else { |
|
|
|
if (mvelInvokeService == null) { |
|
|
|
throw new IllegalArgumentException("MVEL script engine is disabled!"); |
|
|
|
} |
|
|
|
engine = new RuleNodeMvelScriptEngine(getTenantId(), mvelInvokeService, script, argNames); |
|
|
|
} |
|
|
|
TbMsg inMsg = TbMsg.newMsg(msgType, null, new TbMsgMetaData(metadata), TbMsgDataType.JSON, data); |
|
|
|
switch (scriptType) { |
|
|
|
case "update": |
|
|
|
|