Search in sources :

Example 1 with MqttClient

use of org.eclipse.paho.client.mqttv3.MqttClient in project camel by apache.

the class PahoEndpoint method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    client = new MqttClient(getBrokerUrl(), getClientId(), resolvePersistence());
    client.connect(resolveMqttConnectOptions());
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient)

Example 2 with MqttClient

use of org.eclipse.paho.client.mqttv3.MqttClient in project camel by apache.

the class PahoProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    MqttClient client = getEndpoint().getClient();
    String topic = getEndpoint().getTopic();
    int qos = exchange.getIn().getHeader(PahoConstants.CAMEL_PAHO_MSG_QOS, getEndpoint().getQos(), Integer.class);
    boolean retained = exchange.getIn().getHeader(PahoConstants.CAMEL_PAHO_MSG_RETAINED, getEndpoint().isRetained(), Boolean.class);
    byte[] payload = exchange.getIn().getBody(byte[].class);
    MqttMessage message = new MqttMessage(payload);
    message.setQos(qos);
    message.setRetained(retained);
    client.publish(topic, message);
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage)

Example 3 with MqttClient

use of org.eclipse.paho.client.mqttv3.MqttClient in project SEPA by arces-wot.

the class MQTTWebThing method start.

public boolean start() {
    try {
        mqttClient = new MqttClient(serverURI, clientID);
    } catch (MqttException e) {
        logger.fatal("Failed to create MQTT client " + e.getMessage());
        return created;
    }
    try {
        MqttConnectOptions options = new MqttConnectOptions();
        if (sslEnabled) {
            SSLSecurityManager sm;
            try {
                sm = new SSLSecurityManager("TLSv1", "sepa.jks", "sepa2017", "sepa2017");
            } catch (UnrecoverableKeyException | KeyManagementException | KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
                logger.error(e.getMessage());
                return false;
            }
            try {
                options.setSocketFactory(sm.getSSLContext().getSocketFactory());
            } catch (KeyManagementException | NoSuchAlgorithmException e) {
                logger.error(e.getMessage());
                return false;
            }
        }
        mqttClient.connect(options);
    } catch (MqttException e) {
        logger.fatal(e.getMessage());
        return created;
    }
    mqttClient.setCallback(this);
    try {
        mqttClient.subscribe(topicsFilter);
    } catch (MqttException e) {
        logger.fatal("Failed to subscribe " + e.getMessage());
        return created;
    }
    String topics = "";
    for (int i = 0; i < topicsFilter.length; i++) topics += "\"" + topicsFilter[i] + "\" ";
    logger.info("MQTT client " + clientID + " subscribed to " + serverURI + " Topic filter " + topics);
    created = true;
    return created;
}
Also used : CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) UnrecoverableKeyException(java.security.UnrecoverableKeyException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) SSLSecurityManager(it.unibo.arces.wot.sepa.commons.protocol.SSLSecurityManager)

Example 4 with MqttClient

use of org.eclipse.paho.client.mqttv3.MqttClient in project SEPA by arces-wot.

the class MQTTSmartifier method start.

public boolean start(boolean simulate) {
    // Subscribe to observation-topic mapping
    Response ret = subscribe(null);
    if (ret.isError()) {
        logger.fatal("Failed to subscribe: " + ret);
        return false;
    }
    SubscribeResponse results = (SubscribeResponse) ret;
    onAddedResults(results.getBindingsResults());
    if (simulate)
        simulator();
    else {
        // MQTT: begin
        JsonObject mqtt = getApplicationProfile().getExtendedData().get("mqtt").getAsJsonObject();
        String url = mqtt.get("url").getAsString();
        int port = mqtt.get("port").getAsInt();
        JsonArray topics = mqtt.get("topics").getAsJsonArray();
        topicsFilter = new String[topics.size()];
        int i = 0;
        for (JsonElement topic : topics) {
            topicsFilter[i] = topic.getAsString();
            i++;
        }
        boolean sslEnabled = false;
        if (mqtt.get("ssl") != null)
            sslEnabled = mqtt.get("ssl").getAsBoolean();
        String serverURI = null;
        if (sslEnabled) {
            serverURI = "ssl://" + url + ":" + String.format("%d", port);
        } else {
            serverURI = "tcp://" + url + ":" + String.format("%d", port);
        }
        // Create client
        logger.info("Creating MQTT client...");
        String clientID = MqttClient.generateClientId();
        logger.info("Client ID: " + clientID);
        logger.info("Server URI: " + serverURI);
        try {
            mqttClient = new MqttClient(serverURI, clientID);
        } catch (MqttException e) {
            logger.error(e.getMessage());
            return false;
        }
        // Connect
        logger.info("Connecting...");
        MqttConnectOptions options = new MqttConnectOptions();
        if (sslEnabled) {
            SSLSecurityManager sm;
            try {
                sm = new SSLSecurityManager("TLSv1", "sepa.jks", "sepa2017", "sepa2017");
            } catch (UnrecoverableKeyException | KeyManagementException | KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
                logger.error(e.getMessage());
                return false;
            }
            logger.info("Set SSL security");
            try {
                options.setSocketFactory(sm.getSSLContext().getSocketFactory());
            } catch (KeyManagementException | NoSuchAlgorithmException e) {
                logger.error(e.getMessage());
                return false;
            }
        }
        try {
            mqttClient.connect(options);
        } catch (MqttException e) {
            logger.error(e.getMessage());
        }
        // Subscribe
        mqttClient.setCallback(this);
        logger.info("Subscribing...");
        try {
            mqttClient.subscribe(topicsFilter);
        } catch (MqttException e) {
            logger.error(e.getMessage());
            return false;
        }
        for (String topic : topicsFilter) logger.info("MQTT client " + clientID + " subscribed to " + serverURI + " Topic filter " + topic);
    // MQTT: end
    }
    return true;
}
Also used : JsonObject(com.google.gson.JsonObject) CertificateException(java.security.cert.CertificateException) SubscribeResponse(it.unibo.arces.wot.sepa.commons.response.SubscribeResponse) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) Response(it.unibo.arces.wot.sepa.commons.response.Response) SubscribeResponse(it.unibo.arces.wot.sepa.commons.response.SubscribeResponse) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse) JsonArray(com.google.gson.JsonArray) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) UnrecoverableKeyException(java.security.UnrecoverableKeyException) JsonElement(com.google.gson.JsonElement) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) SSLSecurityManager(it.unibo.arces.wot.sepa.commons.protocol.SSLSecurityManager)

Example 5 with MqttClient

use of org.eclipse.paho.client.mqttv3.MqttClient in project SEPA by arces-wot.

the class MQTTAdapter method start.

public boolean start() {
    // MQTT
    JsonObject mqtt = getApplicationProfile().getExtendedData().get("mqtt").getAsJsonObject();
    String url = mqtt.get("url").getAsString();
    int port = mqtt.get("port").getAsInt();
    JsonArray topics = mqtt.get("topics").getAsJsonArray();
    topicsFilter = new String[topics.size()];
    int i = 0;
    for (JsonElement topic : topics) {
        topicsFilter[i] = topic.getAsString();
        i++;
    }
    boolean sslEnabled = false;
    if (mqtt.get("ssl") != null)
        sslEnabled = mqtt.get("ssl").getAsBoolean();
    if (sslEnabled) {
        serverURI = "ssl://" + url + ":" + String.format("%d", port);
    } else {
        serverURI = "tcp://" + url + ":" + String.format("%d", port);
    }
    // Create client
    logger.info("Creating MQTT client...");
    String clientID = MqttClient.generateClientId();
    logger.info("Client ID: " + clientID);
    logger.info("Server URI: " + serverURI);
    try {
        mqttClient = new MqttClient(serverURI, clientID);
    } catch (MqttException e) {
        logger.error(e.getMessage());
        return false;
    }
    // Connect
    logger.info("Connecting...");
    MqttConnectOptions options = new MqttConnectOptions();
    if (sslEnabled) {
        SSLSecurityManager sm;
        try {
            sm = new SSLSecurityManager("TLSv1", "sepa.jks", "sepa2017", "sepa2017");
        } catch (UnrecoverableKeyException | KeyManagementException | KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
            logger.error(e.getMessage());
            return false;
        }
        logger.info("Set SSL security");
        try {
            options.setSocketFactory(sm.getSSLContext().getSocketFactory());
        } catch (KeyManagementException | NoSuchAlgorithmException e) {
            logger.error(e.getMessage());
            return false;
        }
    }
    try {
        mqttClient.connect(options);
    } catch (MqttException e) {
        logger.error(e.getMessage());
    }
    // Subscribe
    mqttClient.setCallback(this);
    logger.info("Subscribing...");
    try {
        mqttClient.subscribe(topicsFilter);
    } catch (MqttException e) {
        logger.error(e.getMessage());
        return false;
    }
    String printTopics = "Topic filter ";
    for (String s : topicsFilter) {
        printTopics += s + " ";
    }
    logger.info("MQTT client " + clientID + " subscribed to " + serverURI + printTopics);
    return true;
}
Also used : JsonObject(com.google.gson.JsonObject) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) JsonArray(com.google.gson.JsonArray) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) UnrecoverableKeyException(java.security.UnrecoverableKeyException) JsonElement(com.google.gson.JsonElement) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) SSLSecurityManager(it.unibo.arces.wot.sepa.commons.protocol.SSLSecurityManager)

Aggregations

MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)46 MqttException (org.eclipse.paho.client.mqttv3.MqttException)37 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)23 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)23 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)18 Test (org.junit.Test)12 Properties (java.util.Properties)6 IMqttClient (org.eclipse.paho.client.mqttv3.IMqttClient)6 MqttDefaultFilePersistence (org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence)5 IOException (java.io.IOException)4 IMqttDeliveryToken (org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)4 MqttCallback (org.eclipse.paho.client.mqttv3.MqttCallback)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 JsonArray (com.google.gson.JsonArray)3 JsonElement (com.google.gson.JsonElement)3 JsonObject (com.google.gson.JsonObject)3 SSLSecurityManager (it.unibo.arces.wot.sepa.commons.protocol.SSLSecurityManager)3 KeyManagementException (java.security.KeyManagementException)3 KeyStoreException (java.security.KeyStoreException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3