Search in sources :

Example 1 with ConfigurationInitializationException

use of de.fraunhofer.iosb.ilt.faaast.service.exception.ConfigurationInitializationException in project FAAAST-Service by FraunhoferIOSB.

the class MqttAssetConnectionTest method invokeValueProvider.

public String invokeValueProvider(ContentFormat contentFormat, DataElementValue newValue, String query) throws AssetConnectionException, InterruptedException, MqttException, ConfigurationInitializationException {
    MqttAssetConnection assetConnection = newConnection(MqttValueProviderConfig.builder().contentFormat(contentFormat).query(query).build());
    MqttClient client = newMqttClient();
    final AtomicReference<String> response = new AtomicReference<>();
    CountDownLatch condition = new CountDownLatch(1);
    client.setCallback(new MqttCallback() {

        @Override
        public void connectionLost(Throwable throwable) {
        }

        @Override
        public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
            response.set(new String(mqttMessage.getPayload()));
            condition.countDown();
        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        }
    });
    client.subscribe(DEFAULT_TOPIC);
    assetConnection.getValueProviders().get(DEFAULT_REFERENCE).setValue(newValue);
    condition.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
    return response.get();
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttCallback(org.eclipse.paho.client.mqttv3.MqttCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken) JSONException(org.json.JSONException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) ConfigurationInitializationException(de.fraunhofer.iosb.ilt.faaast.service.exception.ConfigurationInitializationException) ValueFormatException(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.ValueFormatException) IOException(java.io.IOException)

Example 2 with ConfigurationInitializationException

use of de.fraunhofer.iosb.ilt.faaast.service.exception.ConfigurationInitializationException in project FAAAST-Service by FraunhoferIOSB.

the class OpcUaAssetConnection method init.

@Override
public void init(CoreConfig coreConfig, OpcUaAssetConnectionConfig config, ServiceContext context) throws ConfigurationInitializationException {
    this.serviceContext = context;
    this.config = config;
    try {
        connect();
        for (var provider : config.getValueProviders().entrySet()) {
            registerValueProvider(provider.getKey(), provider.getValue());
        }
        for (var provider : config.getOperationProviders().entrySet()) {
            registerOperationProvider(provider.getKey(), provider.getValue());
        }
        for (var provider : config.getSubscriptionProviders().entrySet()) {
            registerSubscriptionProvider(provider.getKey(), provider.getValue());
        }
    } catch (AssetConnectionException e) {
        throw new ConfigurationInitializationException("initializaing OPC UA asset connection failed", e);
    }
}
Also used : AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) ConfigurationInitializationException(de.fraunhofer.iosb.ilt.faaast.service.exception.ConfigurationInitializationException)

Example 3 with ConfigurationInitializationException

use of de.fraunhofer.iosb.ilt.faaast.service.exception.ConfigurationInitializationException in project FAAAST-Service by FraunhoferIOSB.

the class MqttAssetConnection method init.

/**
 * {@inheritDoc}
 *
 * @throws IllegalArgumentException if coreConfig is null
 * @throws IllegalArgumentException if config is null
 * @throws IllegalArgumentException if serviceContext if null
 */
@Override
public void init(CoreConfig coreConfig, MqttAssetConnectionConfig config, ServiceContext serviceContext) throws ConfigurationInitializationException {
    if (coreConfig == null) {
        throw new IllegalArgumentException("coreConfig must be non-null");
    }
    if (config == null) {
        throw new IllegalArgumentException("config must be non-null");
    }
    if (serviceContext == null) {
        throw new IllegalArgumentException("serviceContext must be non-null");
    }
    this.config = config;
    this.serviceContext = serviceContext;
    try {
        client = new MqttClient(config.getServerUri(), config.getClientId(), new MemoryPersistence());
        client.setCallback(new MqttCallback() {

            @Override
            public void connectionLost(Throwable throwable) {
                LOGGER.warn("MQTT asset connection lost (url: {}, reason: {})", config.getServerUri(), throwable.getMessage(), throwable);
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken imdt) {
            // intentionally left empty
            }

            @Override
            public void messageArrived(String string, MqttMessage mm) throws Exception {
            // intentionally left empty
            }
        });
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(true);
        client.connect(options);
        for (var providerConfig : config.getValueProviders().entrySet()) {
            registerValueProvider(providerConfig.getKey(), providerConfig.getValue());
        }
        for (var providerConfig : config.getOperationProviders().entrySet()) {
            registerOperationProvider(providerConfig.getKey(), providerConfig.getValue());
        }
        for (var providerConfig : config.getSubscriptionProviders().entrySet()) {
            registerSubscriptionProvider(providerConfig.getKey(), providerConfig.getValue());
        }
    } catch (MqttException | AssetConnectionException e) {
        throw new ConfigurationInitializationException("initializaing MQTT asset connection failed", e);
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) MqttCallback(org.eclipse.paho.client.mqttv3.MqttCallback) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken) MqttException(org.eclipse.paho.client.mqttv3.MqttException) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) ConfigurationInitializationException(de.fraunhofer.iosb.ilt.faaast.service.exception.ConfigurationInitializationException) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) MqttException(org.eclipse.paho.client.mqttv3.MqttException) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) ConfigurationInitializationException(de.fraunhofer.iosb.ilt.faaast.service.exception.ConfigurationInitializationException)

Aggregations

AssetConnectionException (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException)3 ConfigurationInitializationException (de.fraunhofer.iosb.ilt.faaast.service.exception.ConfigurationInitializationException)3 IMqttDeliveryToken (org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)2 MqttCallback (org.eclipse.paho.client.mqttv3.MqttCallback)2 MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)2 MqttException (org.eclipse.paho.client.mqttv3.MqttException)2 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)2 ValueFormatException (de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.ValueFormatException)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)1 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)1 JSONException (org.json.JSONException)1