Search in sources :

Example 1 with TbNodeException

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

the class TbAlarmNodeTest method initWithClearAlarmScript.

private void initWithClearAlarmScript() {
    try {
        TbClearAlarmNodeConfiguration config = new TbClearAlarmNodeConfiguration();
        config.setAlarmType("SomeType");
        config.setAlarmDetailsBuildJs("DETAILS");
        ObjectMapper mapper = new ObjectMapper();
        TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
        when(ctx.createJsScriptEngine("DETAILS")).thenReturn(detailsJs);
        when(ctx.getTenantId()).thenReturn(tenantId);
        when(ctx.getAlarmService()).thenReturn(alarmService);
        when(ctx.getDbCallbackExecutor()).thenReturn(dbExecutor);
        node = new TbClearAlarmNode();
        node.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)

Example 2 with TbNodeException

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

the class TbAlarmNodeTest method initWithCreateAlarmScript.

private void initWithCreateAlarmScript() {
    try {
        TbCreateAlarmNodeConfiguration config = new TbCreateAlarmNodeConfiguration();
        config.setPropagate(true);
        config.setSeverity(CRITICAL.name());
        config.setAlarmType("SomeType");
        config.setAlarmDetailsBuildJs("DETAILS");
        ObjectMapper mapper = new ObjectMapper();
        TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
        when(ctx.createJsScriptEngine("DETAILS")).thenReturn(detailsJs);
        when(ctx.getTenantId()).thenReturn(tenantId);
        when(ctx.getAlarmService()).thenReturn(alarmService);
        when(ctx.getDbCallbackExecutor()).thenReturn(dbExecutor);
        node = new TbCreateAlarmNode();
        node.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)

Example 3 with TbNodeException

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

the class TbKafkaNode method init.

@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
    this.config = TbNodeUtils.convert(configuration, TbKafkaNodeConfiguration.class);
    this.initError = null;
    Properties properties = new Properties();
    properties.put(ProducerConfig.CLIENT_ID_CONFIG, "producer-tb-kafka-node-" + ctx.getSelfId().getId().toString() + "-" + ctx.getServiceId());
    properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, config.getBootstrapServers());
    properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, config.getValueSerializer());
    properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, config.getKeySerializer());
    properties.put(ProducerConfig.ACKS_CONFIG, config.getAcks());
    properties.put(ProducerConfig.RETRIES_CONFIG, config.getRetries());
    properties.put(ProducerConfig.BATCH_SIZE_CONFIG, config.getBatchSize());
    properties.put(ProducerConfig.LINGER_MS_CONFIG, config.getLinger());
    properties.put(ProducerConfig.BUFFER_MEMORY_CONFIG, config.getBufferMemory());
    if (config.getOtherProperties() != null) {
        config.getOtherProperties().forEach(properties::put);
    }
    addMetadataKeyValuesAsKafkaHeaders = BooleanUtils.toBooleanDefaultIfNull(config.isAddMetadataKeyValuesAsKafkaHeaders(), false);
    toBytesCharset = config.getKafkaHeadersCharset() != null ? Charset.forName(config.getKafkaHeadersCharset()) : StandardCharsets.UTF_8;
    try {
        this.producer = new KafkaProducer<>(properties);
        Thread ioThread = (Thread) ReflectionUtils.getField(IO_THREAD_FIELD, producer);
        ioThread.setUncaughtExceptionHandler((thread, throwable) -> {
            if (throwable instanceof ThingsboardKafkaClientError) {
                initError = throwable;
                destroy();
            }
        });
    } catch (Exception e) {
        throw new TbNodeException(e);
    }
}
Also used : TbNodeException(org.thingsboard.rule.engine.api.TbNodeException) ThingsboardKafkaClientError(org.thingsboard.server.common.data.exception.ThingsboardKafkaClientError) Properties(java.util.Properties) TbNodeException(org.thingsboard.rule.engine.api.TbNodeException) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 4 with TbNodeException

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

the class TbRabbitMqNode method init.

@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
    this.config = TbNodeUtils.convert(configuration, TbRabbitMqNodeConfiguration.class);
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(this.config.getHost());
    factory.setPort(this.config.getPort());
    factory.setVirtualHost(this.config.getVirtualHost());
    factory.setUsername(this.config.getUsername());
    factory.setPassword(this.config.getPassword());
    factory.setAutomaticRecoveryEnabled(this.config.isAutomaticRecoveryEnabled());
    factory.setConnectionTimeout(this.config.getConnectionTimeout());
    factory.setHandshakeTimeout(this.config.getHandshakeTimeout());
    this.config.getClientProperties().forEach((k, v) -> factory.getClientProperties().put(k, v));
    try {
        this.connection = factory.newConnection();
        this.channel = this.connection.createChannel();
    } catch (Exception e) {
        throw new TbNodeException(e);
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) TbNodeException(org.thingsboard.rule.engine.api.TbNodeException) TbNodeException(org.thingsboard.rule.engine.api.TbNodeException)

Example 5 with TbNodeException

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

the class TbAzureIotHubNode method init.

@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
    try {
        this.mqttNodeConfiguration = TbNodeUtils.convert(configuration, TbMqttNodeConfiguration.class);
        mqttNodeConfiguration.setPort(8883);
        mqttNodeConfiguration.setCleanSession(true);
        ClientCredentials credentials = mqttNodeConfiguration.getCredentials();
        if (CredentialsType.CERT_PEM == credentials.getType()) {
            CertPemCredentials pemCredentials = (CertPemCredentials) credentials;
            if (pemCredentials.getCaCert() == null || pemCredentials.getCaCert().isEmpty()) {
                pemCredentials.setCaCert(AzureIotHubUtil.getDefaultCaCert());
            }
        }
        this.mqttClient = initClient(ctx);
    } catch (Exception e) {
        throw new TbNodeException(e);
    }
}
Also used : CertPemCredentials(org.thingsboard.rule.engine.credentials.CertPemCredentials) TbMqttNodeConfiguration(org.thingsboard.rule.engine.mqtt.TbMqttNodeConfiguration) TbNodeException(org.thingsboard.rule.engine.api.TbNodeException) ClientCredentials(org.thingsboard.rule.engine.credentials.ClientCredentials) TbNodeException(org.thingsboard.rule.engine.api.TbNodeException) SSLException(javax.net.ssl.SSLException)

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