Search in sources :

Example 21 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class TbMsgCountNode method scheduleTickMsg.

private void scheduleTickMsg(TbContext ctx, TbMsg msg) {
    long curTs = System.currentTimeMillis();
    if (lastScheduledTs == 0L) {
        lastScheduledTs = curTs;
    }
    lastScheduledTs = lastScheduledTs + delay;
    long curDelay = Math.max(0L, (lastScheduledTs - curTs));
    TbMsg tickMsg = ctx.newMsg(ServiceQueue.MAIN, TB_MSG_COUNT_NODE_MSG, ctx.getSelfId(), msg != null ? msg.getCustomerId() : null, new TbMsgMetaData(), "");
    nextTickId = tickMsg.getId();
    ctx.tellSelf(tickMsg, curDelay);
}
Also used : TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbMsg(org.thingsboard.server.common.msg.TbMsg)

Example 22 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class TbAbstractAlarmNode method buildAlarmDetails.

protected ListenableFuture<JsonNode> buildAlarmDetails(TbContext ctx, TbMsg msg, JsonNode previousDetails) {
    try {
        TbMsg dummyMsg = msg;
        if (previousDetails != null) {
            TbMsgMetaData metaData = msg.getMetaData().copy();
            metaData.putValue(PREV_ALARM_DETAILS, mapper.writeValueAsString(previousDetails));
            dummyMsg = ctx.transformMsg(msg, msg.getType(), msg.getOriginator(), metaData, msg.getData());
        }
        return buildDetailsJsEngine.executeJsonAsync(dummyMsg);
    } catch (Exception e) {
        return Futures.immediateFailedFuture(e);
    }
}
Also used : TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbMsg(org.thingsboard.server.common.msg.TbMsg) TbNodeException(org.thingsboard.rule.engine.api.TbNodeException)

Example 23 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class RuleChainController method testScript.

@ApiOperation(value = "Test JavaScript function", notes = TEST_JS_FUNCTION + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChain/testScript", method = RequestMethod.POST)
@ResponseBody
public JsonNode testScript(@ApiParam(value = "Test JS request. See API call description above.") @RequestBody JsonNode inputParams) throws ThingsboardException {
    try {
        String script = inputParams.get("script").asText();
        String scriptType = inputParams.get("scriptType").asText();
        JsonNode argNamesJson = inputParams.get("argNames");
        String[] argNames = objectMapper.treeToValue(argNamesJson, String[].class);
        String data = inputParams.get("msg").asText();
        JsonNode metadataJson = inputParams.get("metadata");
        Map<String, String> metadata = objectMapper.convertValue(metadataJson, new TypeReference<Map<String, String>>() {
        });
        String msgType = inputParams.get("msgType").asText();
        String output = "";
        String errorText = "";
        ScriptEngine engine = null;
        try {
            engine = new RuleNodeJsScriptEngine(getTenantId(), jsInvokeService, getCurrentUser().getId(), script, argNames);
            TbMsg inMsg = TbMsg.newMsg(msgType, null, new TbMsgMetaData(metadata), TbMsgDataType.JSON, data);
            switch(scriptType) {
                case "update":
                    output = msgToOutput(engine.executeUpdateAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS));
                    break;
                case "generate":
                    output = msgToOutput(engine.executeGenerateAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS));
                    break;
                case "filter":
                    boolean result = engine.executeFilterAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS);
                    output = Boolean.toString(result);
                    break;
                case "switch":
                    Set<String> states = engine.executeSwitchAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS);
                    output = objectMapper.writeValueAsString(states);
                    break;
                case "json":
                    JsonNode json = engine.executeJsonAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS);
                    output = objectMapper.writeValueAsString(json);
                    break;
                case "string":
                    output = engine.executeToStringAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS);
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported script type: " + scriptType);
            }
        } catch (Exception e) {
            log.error("Error evaluating JS function", e);
            errorText = e.getMessage();
        } finally {
            if (engine != null) {
                engine.destroy();
            }
        }
        ObjectNode result = objectMapper.createObjectNode();
        result.put("output", output);
        result.put("error", errorText);
        return result;
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RuleNodeJsScriptEngine(org.thingsboard.server.service.script.RuleNodeJsScriptEngine) JsonNode(com.fasterxml.jackson.databind.JsonNode) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) RuleNodeJsScriptEngine(org.thingsboard.server.service.script.RuleNodeJsScriptEngine) ScriptEngine(org.thingsboard.rule.engine.api.ScriptEngine) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) TbMsg(org.thingsboard.server.common.msg.TbMsg) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 24 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class DeviceController method getMetaDataForAssignedFrom.

private TbMsgMetaData getMetaDataForAssignedFrom(Tenant tenant) {
    TbMsgMetaData metaData = new TbMsgMetaData();
    metaData.putValue("assignedFromTenantId", tenant.getId().getId().toString());
    metaData.putValue("assignedFromTenantName", tenant.getName());
    return metaData;
}
Also used : TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData)

Example 25 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class DeviceEdgeProcessor method getTbMsgMetaData.

private TbMsgMetaData getTbMsgMetaData(Edge edge) {
    TbMsgMetaData metaData = new TbMsgMetaData();
    metaData.putValue("edgeId", edge.getId().toString());
    metaData.putValue("edgeName", edge.getName());
    return metaData;
}
Also used : TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData)

Aggregations

TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)89 TbMsg (org.thingsboard.server.common.msg.TbMsg)56 Test (org.junit.Test)49 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)26 Device (org.thingsboard.server.common.data.Device)17 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)17 List (java.util.List)16 DeviceProfileData (org.thingsboard.server.common.data.device.profile.DeviceProfileData)16 DeviceId (org.thingsboard.server.common.data.id.DeviceId)15 EntityId (org.thingsboard.server.common.data.id.EntityId)15 AlarmCondition (org.thingsboard.server.common.data.device.profile.AlarmCondition)14 AlarmConditionFilter (org.thingsboard.server.common.data.device.profile.AlarmConditionFilter)14 AlarmConditionFilterKey (org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey)14 AlarmRule (org.thingsboard.server.common.data.device.profile.AlarmRule)14 DeviceProfileAlarm (org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm)14 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)14 NumericFilterPredicate (org.thingsboard.server.common.data.query.NumericFilterPredicate)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)13 DynamicValue (org.thingsboard.server.common.data.query.DynamicValue)13 AttributeKvCompositeKey (org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey)13