Search in sources :

Example 16 with MqttClient

use of org.eclipse.paho.client.mqttv3.MqttClient in project Applozic-Android-SDK by AppLozic.

the class ApplozicMqttService method connectPublish.

public synchronized void connectPublish(final String userKeyString, final String deviceKeyString, final String status) {
    try {
        final MqttClient client = connect();
        if (client == null || !client.isConnected()) {
            return;
        }
        MqttMessage message = new MqttMessage();
        message.setRetained(false);
        message.setPayload((userKeyString + "," + deviceKeyString + "," + status).getBytes());
        Utils.printLog(context, TAG, "UserKeyString,DeviceKeyString,status:" + userKeyString + "," + deviceKeyString + "," + status);
        message.setQos(0);
        client.publish(STATUS, message);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Example 17 with MqttClient

use of org.eclipse.paho.client.mqttv3.MqttClient in project Applozic-Android-SDK by AppLozic.

the class ApplozicMqttService method subscribe.

public synchronized void subscribe() {
    if (!Utils.isInternetAvailable(context)) {
        return;
    }
    final String deviceKeyString = MobiComUserPreference.getInstance(context).getDeviceKeyString();
    final String userKeyString = MobiComUserPreference.getInstance(context).getSuUserKeyString();
    if (TextUtils.isEmpty(deviceKeyString) || TextUtils.isEmpty(userKeyString)) {
        return;
    }
    try {
        final MqttClient client = connect();
        if (client == null || !client.isConnected()) {
            return;
        }
        connectPublish(userKeyString, deviceKeyString, "1");
        subscribeToConversation();
        if (client != null) {
            client.setCallback(ApplozicMqttService.this);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Example 18 with MqttClient

use of org.eclipse.paho.client.mqttv3.MqttClient in project Applozic-Android-SDK by AppLozic.

the class ApplozicMqttService method subscribeToOpenGroupTopic.

public synchronized void subscribeToOpenGroupTopic(Channel channel) {
    try {
        String currentId = null;
        if (channel != null) {
            currentId = String.valueOf(channel.getKey());
        }
        final MqttClient client = connect();
        if (client == null || !client.isConnected()) {
            return;
        }
        client.subscribe(OPEN_GROUP + getApplicationKey(context) + "-" + currentId, 0);
        Utils.printLog(context, TAG, "Subscribed to Open group: " + OPEN_GROUP + getApplicationKey(context) + "-" + currentId);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Example 19 with MqttClient

use of org.eclipse.paho.client.mqttv3.MqttClient in project Applozic-Android-SDK by AppLozic.

the class ApplozicMqttService method connect.

private MqttClient connect() {
    String userId = MobiComUserPreference.getInstance(context).getUserId();
    try {
        if (TextUtils.isEmpty(userId)) {
            return client;
        }
        if (client == null) {
            client = new MqttClient(getMqttBaseUrl(), userId + "-" + new Date().getTime(), memoryPersistence);
        }
        if (!client.isConnected()) {
            Utils.printLog(context, TAG, "Connecting to mqtt...");
            MqttConnectOptions options = new MqttConnectOptions();
            options.setConnectionTimeout(60);
            options.setWill(STATUS, (MobiComUserPreference.getInstance(context).getSuUserKeyString() + "," + MobiComUserPreference.getInstance(context).getDeviceKeyString() + "," + "0").getBytes(), 0, true);
            client.setCallback(ApplozicMqttService.this);
            client.connect(options);
        }
    } catch (MqttException e) {
        Utils.printLog(context, TAG, "Connecting already in progress.");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return client;
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) MqttException(org.eclipse.paho.client.mqttv3.MqttException) Date(java.util.Date) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Example 20 with MqttClient

use of org.eclipse.paho.client.mqttv3.MqttClient in project Applozic-Android-SDK by AppLozic.

the class ApplozicMqttService method unSubscribeToOpenGroupTopic.

public synchronized void unSubscribeToOpenGroupTopic(Channel channel) {
    try {
        String currentId = null;
        if (channel != null) {
            currentId = String.valueOf(channel.getKey());
        }
        final MqttClient client = connect();
        if (client == null || !client.isConnected()) {
            return;
        }
        client.unsubscribe(OPEN_GROUP + getApplicationKey(context) + "-" + currentId);
        Utils.printLog(context, TAG, "UnSubscribed to topic: " + OPEN_GROUP + getApplicationKey(context) + "-" + currentId);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

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