Search in sources :

Example 11 with TbNodeException

use of org.thingsboard.rule.engine.api.TbNodeException in project thingsboard by thingsboard.

the class TbCheckAlarmStatusNode method onMsg.

@Override
public void onMsg(TbContext ctx, TbMsg msg) throws TbNodeException {
    try {
        Alarm alarm = mapper.readValue(msg.getData(), Alarm.class);
        ListenableFuture<Alarm> latest = ctx.getAlarmService().findAlarmByIdAsync(ctx.getTenantId(), alarm.getId());
        Futures.addCallback(latest, new FutureCallback<Alarm>() {

            @Override
            public void onSuccess(@Nullable Alarm result) {
                if (result != null) {
                    boolean isPresent = false;
                    for (AlarmStatus alarmStatus : config.getAlarmStatusList()) {
                        if (result.getStatus() == alarmStatus) {
                            isPresent = true;
                            break;
                        }
                    }
                    if (isPresent) {
                        ctx.tellNext(msg, "True");
                    } else {
                        ctx.tellNext(msg, "False");
                    }
                } else {
                    ctx.tellFailure(msg, new TbNodeException("No such alarm found."));
                }
            }

            @Override
            public void onFailure(Throwable t) {
                ctx.tellFailure(msg, t);
            }
        }, MoreExecutors.directExecutor());
    } catch (IOException e) {
        log.error("Failed to parse alarm: [{}]", msg.getData());
        throw new TbNodeException(e);
    }
}
Also used : TbNodeException(org.thingsboard.rule.engine.api.TbNodeException) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmStatus(org.thingsboard.server.common.data.alarm.AlarmStatus) IOException(java.io.IOException)

Example 12 with TbNodeException

use of org.thingsboard.rule.engine.api.TbNodeException in project thingsboard by thingsboard.

the class AbstractGeofencingNode method checkMatches.

protected boolean checkMatches(Perimeter perimeter, double latitude, double longitude) throws TbNodeException {
    if (perimeter.getPerimeterType() == PerimeterType.CIRCLE) {
        Coordinates entityCoordinates = new Coordinates(latitude, longitude);
        Coordinates perimeterCoordinates = new Coordinates(perimeter.getCenterLatitude(), perimeter.getCenterLongitude());
        return perimeter.getRange() > GeoUtil.distance(entityCoordinates, perimeterCoordinates, perimeter.getRangeUnit());
    } else if (perimeter.getPerimeterType() == PerimeterType.POLYGON) {
        return GeoUtil.contains(perimeter.getPolygonsDefinition(), new Coordinates(latitude, longitude));
    } else {
        throw new TbNodeException("Unsupported perimeter type: " + perimeter.getPerimeterType());
    }
}
Also used : TbNodeException(org.thingsboard.rule.engine.api.TbNodeException)

Example 13 with TbNodeException

use of org.thingsboard.rule.engine.api.TbNodeException in project thingsboard by thingsboard.

the class AbstractGeofencingNode method getPerimeters.

protected List<Perimeter> getPerimeters(TbMsg msg, JsonObject msgDataObj) throws TbNodeException {
    if (config.isFetchPerimeterInfoFromMessageMetadata()) {
        if (StringUtils.isEmpty(config.getPerimeterKeyName())) {
            // Old configuration before "perimeterKeyName" was introduced
            String perimeterValue = msg.getMetaData().getValue("perimeter");
            if (!StringUtils.isEmpty(perimeterValue)) {
                Perimeter perimeter = new Perimeter();
                perimeter.setPerimeterType(PerimeterType.POLYGON);
                perimeter.setPolygonsDefinition(perimeterValue);
                return Collections.singletonList(perimeter);
            } else if (!StringUtils.isEmpty(msg.getMetaData().getValue("centerLatitude"))) {
                Perimeter perimeter = new Perimeter();
                perimeter.setPerimeterType(PerimeterType.CIRCLE);
                perimeter.setCenterLatitude(Double.parseDouble(msg.getMetaData().getValue("centerLatitude")));
                perimeter.setCenterLongitude(Double.parseDouble(msg.getMetaData().getValue("centerLongitude")));
                perimeter.setRange(Double.parseDouble(msg.getMetaData().getValue("range")));
                perimeter.setRangeUnit(RangeUnit.valueOf(msg.getMetaData().getValue("rangeUnit")));
                return Collections.singletonList(perimeter);
            } else {
                throw new TbNodeException("Missing perimeter definition!");
            }
        } else {
            String perimeterValue = msg.getMetaData().getValue(config.getPerimeterKeyName());
            if (!StringUtils.isEmpty(perimeterValue)) {
                if (config.getPerimeterType().equals(PerimeterType.POLYGON)) {
                    Perimeter perimeter = new Perimeter();
                    perimeter.setPerimeterType(PerimeterType.POLYGON);
                    perimeter.setPolygonsDefinition(perimeterValue);
                    return Collections.singletonList(perimeter);
                } else {
                    var circleDef = JacksonUtil.toJsonNode(perimeterValue);
                    Perimeter perimeter = new Perimeter();
                    perimeter.setPerimeterType(PerimeterType.CIRCLE);
                    perimeter.setCenterLatitude(circleDef.get("latitude").asDouble());
                    perimeter.setCenterLongitude(circleDef.get("longitude").asDouble());
                    perimeter.setRange(circleDef.get("radius").asDouble());
                    perimeter.setRangeUnit(circleDef.has("radiusUnit") ? RangeUnit.valueOf(circleDef.get("radiusUnit").asText()) : RangeUnit.METER);
                    return Collections.singletonList(perimeter);
                }
            } else {
                throw new TbNodeException("Missing perimeter definition!");
            }
        }
    } else {
        Perimeter perimeter = new Perimeter();
        perimeter.setPerimeterType(config.getPerimeterType());
        perimeter.setCenterLatitude(config.getCenterLatitude());
        perimeter.setCenterLongitude(config.getCenterLongitude());
        perimeter.setRange(config.getRange());
        perimeter.setRangeUnit(config.getRangeUnit());
        perimeter.setPolygonsDefinition(config.getPolygonsDefinition());
        return Collections.singletonList(perimeter);
    }
}
Also used : TbNodeException(org.thingsboard.rule.engine.api.TbNodeException)

Example 14 with TbNodeException

use of org.thingsboard.rule.engine.api.TbNodeException in project thingsboard by thingsboard.

the class TbOriginatorTypeSwitchNode method onMsg.

@Override
public void onMsg(TbContext ctx, TbMsg msg) throws TbNodeException {
    String relationType;
    EntityType originatorType = msg.getOriginator().getEntityType();
    switch(originatorType) {
        case TENANT:
            relationType = "Tenant";
            break;
        case CUSTOMER:
            relationType = "Customer";
            break;
        case USER:
            relationType = "User";
            break;
        case DASHBOARD:
            relationType = "Dashboard";
            break;
        case ASSET:
            relationType = "Asset";
            break;
        case DEVICE:
            relationType = "Device";
            break;
        case ENTITY_VIEW:
            relationType = "Entity View";
            break;
        case EDGE:
            relationType = "Edge";
            break;
        case RULE_CHAIN:
            relationType = "Rule chain";
            break;
        case RULE_NODE:
            relationType = "Rule node";
            break;
        case ALARM:
            relationType = "Alarm";
            break;
        default:
            throw new TbNodeException("Unsupported originator type: " + originatorType);
    }
    ctx.tellNext(msg, relationType);
}
Also used : EntityType(org.thingsboard.server.common.data.EntityType) TbNodeException(org.thingsboard.rule.engine.api.TbNodeException)

Example 15 with TbNodeException

use of org.thingsboard.rule.engine.api.TbNodeException in project thingsboard by thingsboard.

the class TbRestApiCallNodeTest method initWithConfig.

private void initWithConfig(TbRestApiCallNodeConfiguration config) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
        restNode = new TbRestApiCallNode();
        restNode.init(ctx, nodeConfiguration);
    } catch (TbNodeException ex) {
        throw new IllegalStateException(ex);
    }
}
Also used : TbNodeException(org.thingsboard.rule.engine.api.TbNodeException) TbNodeConfiguration(org.thingsboard.rule.engine.api.TbNodeConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

TbNodeException (org.thingsboard.rule.engine.api.TbNodeException)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 TbNodeConfiguration (org.thingsboard.rule.engine.api.TbNodeConfiguration)4 AWSCredentials (com.amazonaws.auth.AWSCredentials)2 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)2 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)2 IOException (java.io.IOException)2 SSLException (javax.net.ssl.SSLException)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 Properties (java.util.Properties)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 TimeoutException (org.apache.kafka.common.errors.TimeoutException)1 CertPemCredentials (org.thingsboard.rule.engine.credentials.CertPemCredentials)1 ClientCredentials (org.thingsboard.rule.engine.credentials.ClientCredentials)1 TbMqttNodeConfiguration (org.thingsboard.rule.engine.mqtt.TbMqttNodeConfiguration)1 EntityType (org.thingsboard.server.common.data.EntityType)1