Search in sources :

Example 16 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage 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 MqttMessage

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

the class ApplozicMqttService method messageArrived.

@Override
public void messageArrived(String s, final MqttMessage mqttMessage) throws Exception {
    Utils.printLog(context, TAG, "Received MQTT message: " + new String(mqttMessage.getPayload()));
    try {
        if (!TextUtils.isEmpty(s) && s.startsWith(TYPINGTOPIC)) {
            String[] typingResponse = mqttMessage.toString().split(",");
            String applicationId = typingResponse[0];
            String userId = typingResponse[1];
            String isTypingStatus = typingResponse[2];
            BroadcastService.sendUpdateTypingBroadcast(context, BroadcastService.INTENT_ACTIONS.UPDATE_TYPING_STATUS.toString(), applicationId, userId, isTypingStatus);
        } else {
            final MqttMessageResponse mqttMessageResponse = (MqttMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), MqttMessageResponse.class);
            if (mqttMessageResponse != null) {
                if (MobiComPushReceiver.processPushNotificationId(mqttMessageResponse.getId())) {
                    return;
                }
                final SyncCallService syncCallService = SyncCallService.getInstance(context);
                MobiComPushReceiver.addPushNotificationId(mqttMessageResponse.getId());
                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        Utils.printLog(context, TAG, "MQTT message type: " + mqttMessageResponse.getType());
                        if (NOTIFICATION_TYPE.MESSAGE_RECEIVED.getValue().equals(mqttMessageResponse.getType()) || "MESSAGE_RECEIVED".equals(mqttMessageResponse.getType())) {
                            GcmMessageResponse messageResponse = (GcmMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), GcmMessageResponse.class);
                            if (messageResponse == null) {
                                return;
                            }
                            Message message = messageResponse.getMessage();
                            if (message.getGroupId() != null) {
                                Channel channel = ChannelService.getInstance(context).getChannelByChannelKey(message.getGroupId());
                                if (channel != null && Channel.GroupType.OPEN.getValue().equals(channel.getType())) {
                                    if (!MobiComUserPreference.getInstance(context).getDeviceKeyString().equals(message.getDeviceKeyString())) {
                                        syncCallService.syncMessages(message.getKeyString(), message);
                                    }
                                } else {
                                    syncCallService.syncMessages(null);
                                }
                            } else {
                                syncCallService.syncMessages(null);
                            }
                        }
                        if (NOTIFICATION_TYPE.MESSAGE_DELIVERED.getValue().equals(mqttMessageResponse.getType()) || "MT_MESSAGE_DELIVERED".equals(mqttMessageResponse.getType())) {
                            String[] splitKeyString = (mqttMessageResponse.getMessage()).toString().split(",");
                            String keyString = splitKeyString[0];
                            // String userId = splitKeyString[1];
                            syncCallService.updateDeliveryStatus(keyString);
                        }
                        if (NOTIFICATION_TYPE.MESSAGE_DELIVERED_AND_READ.getValue().equals(mqttMessageResponse.getType()) || "MT_MESSAGE_DELIVERED_READ".equals(mqttMessageResponse.getType())) {
                            String[] splitKeyString = (mqttMessageResponse.getMessage()).toString().split(",");
                            String keyString = splitKeyString[0];
                            syncCallService.updateReadStatus(keyString);
                        }
                        if (NOTIFICATION_TYPE.CONVERSATION_DELIVERED_AND_READ.getValue().equals(mqttMessageResponse.getType())) {
                            String contactId = mqttMessageResponse.getMessage().toString();
                            syncCallService.updateDeliveryStatusForContact(contactId, true);
                        }
                        if (NOTIFICATION_TYPE.CONVERSATION_READ.getValue().equals(mqttMessageResponse.getType())) {
                            syncCallService.updateConversationReadStatus(mqttMessageResponse.getMessage().toString(), false);
                        }
                        if (NOTIFICATION_TYPE.GROUP_CONVERSATION_READ.getValue().equals(mqttMessageResponse.getType())) {
                            InstantMessageResponse instantMessageResponse = (InstantMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), InstantMessageResponse.class);
                            syncCallService.updateConversationReadStatus(instantMessageResponse.getMessage(), true);
                        }
                        if (NOTIFICATION_TYPE.USER_CONNECTED.getValue().equals(mqttMessageResponse.getType())) {
                            syncCallService.updateConnectedStatus(mqttMessageResponse.getMessage().toString(), new Date(), true);
                        }
                        if (NOTIFICATION_TYPE.USER_DISCONNECTED.getValue().equals(mqttMessageResponse.getType())) {
                            // disconnect comes with timestamp, ranjeet,1449866097000
                            String[] parts = mqttMessageResponse.getMessage().toString().split(",");
                            String userId = parts[0];
                            Date lastSeenAt = new Date();
                            if (parts.length >= 2 && !parts[1].equals("null")) {
                                lastSeenAt = new Date(Long.valueOf(parts[1]));
                            }
                            syncCallService.updateConnectedStatus(userId, lastSeenAt, false);
                        }
                        if (NOTIFICATION_TYPE.CONVERSATION_DELETED.getValue().equals(mqttMessageResponse.getType())) {
                            syncCallService.deleteConversationThread(mqttMessageResponse.getMessage().toString());
                            BroadcastService.sendConversationDeleteBroadcast(context, BroadcastService.INTENT_ACTIONS.DELETE_CONVERSATION.toString(), mqttMessageResponse.getMessage().toString(), 0, "success");
                        }
                        if (NOTIFICATION_TYPE.GROUP_CONVERSATION_DELETED.getValue().equals(mqttMessageResponse.getType())) {
                            InstantMessageResponse instantMessageResponse = (InstantMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), InstantMessageResponse.class);
                            syncCallService.deleteChannelConversationThread(instantMessageResponse.getMessage());
                            BroadcastService.sendConversationDeleteBroadcast(context, BroadcastService.INTENT_ACTIONS.DELETE_CONVERSATION.toString(), null, Integer.valueOf(instantMessageResponse.getMessage()), "success");
                        }
                        if (NOTIFICATION_TYPE.MESSAGE_DELETED.getValue().equals(mqttMessageResponse.getType())) {
                            String messageKey = mqttMessageResponse.getMessage().toString().split(",")[0];
                            syncCallService.deleteMessage(messageKey);
                            BroadcastService.sendMessageDeleteBroadcast(context, BroadcastService.INTENT_ACTIONS.DELETE_MESSAGE.toString(), messageKey, null);
                        }
                        if (NOTIFICATION_TYPE.MESSAGE_SENT.getValue().equals(mqttMessageResponse.getType())) {
                            GcmMessageResponse messageResponse = (GcmMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), GcmMessageResponse.class);
                            Message sentMessageSync = messageResponse.getMessage();
                            syncCallService.syncMessages(sentMessageSync.getKeyString());
                        }
                        if (NOTIFICATION_TYPE.USER_BLOCKED.getValue().equals(mqttMessageResponse.getType()) || NOTIFICATION_TYPE.USER_UN_BLOCKED.getValue().equals(mqttMessageResponse.getType())) {
                            syncCallService.syncBlockUsers();
                        }
                        if (NOTIFICATION_TYPE.USER_DETAIL_CHANGED.getValue().equals(mqttMessageResponse.getType()) || NOTIFICATION_TYPE.USER_DELETE_NOTIFICATION.getValue().equals(mqttMessageResponse.getType())) {
                            String userId = mqttMessageResponse.getMessage().toString();
                            syncCallService.syncUserDetail(userId);
                        }
                        if (NOTIFICATION_TYPE.MESSAGE_METADATA_UPDATE.getValue().equals(mqttMessageResponse.getType())) {
                            String keyString = null;
                            String deviceKey = null;
                            try {
                                GcmMessageResponse messageResponse = (GcmMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), GcmMessageResponse.class);
                                keyString = messageResponse.getMessage().getKeyString();
                                deviceKey = messageResponse.getMessage().getDeviceKeyString();
                            } catch (Exception e) {
                                try {
                                    InstantMessageResponse response = (InstantMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), InstantMessageResponse.class);
                                    keyString = response.getMessage();
                                    Message message = new MessageDatabaseService(context).getMessage(keyString);
                                    Utils.printLog(context, TAG, "Message from db : " + message);
                                    if (message != null) {
                                        deviceKey = message.getDeviceKeyString();
                                    }
                                } catch (Exception e1) {
                                    e1.printStackTrace();
                                }
                            }
                            if (deviceKey != null && deviceKey.equals(MobiComUserPreference.getInstance(context).getDeviceKeyString())) {
                                return;
                            }
                            syncCallService.syncMessageMetadataUpdate(keyString, false);
                        }
                        if (NOTIFICATION_TYPE.USER_MUTE_NOTIFICATION.getValue().equals(mqttMessageResponse.getType())) {
                            try {
                                InstantMessageResponse response = (InstantMessageResponse) GsonUtils.getObjectFromJson(mqttMessage.toString(), InstantMessageResponse.class);
                                if (response.getMessage() != null) {
                                    String muteFlag = String.valueOf(response.getMessage().charAt(response.getMessage().length() - 1));
                                    if ("1".equals(muteFlag)) {
                                        syncCallService.syncMutedUserList(false, null);
                                    } else if ("0".equals(muteFlag)) {
                                        String userId = response.getMessage().substring(0, response.getMessage().length() - 2);
                                        syncCallService.syncMutedUserList(false, userId);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                thread.start();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) Message(com.applozic.mobicomkit.api.conversation.Message) MqttMessageResponse(com.applozic.mobicomkit.feed.MqttMessageResponse) GcmMessageResponse(com.applozic.mobicomkit.feed.GcmMessageResponse) Channel(com.applozic.mobicommons.people.channel.Channel) InstantMessageResponse(com.applozic.mobicomkit.feed.InstantMessageResponse) Date(java.util.Date) MqttException(org.eclipse.paho.client.mqttv3.MqttException) SyncCallService(com.applozic.mobicomkit.api.conversation.SyncCallService) MessageDatabaseService(com.applozic.mobicomkit.api.conversation.database.MessageDatabaseService)

Example 18 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project pentaho-kettle by pentaho.

the class MQTTStreamSourceTest method publish.

private void publish(String topic, String... messages) throws MqttException {
    MqttClient pub = null;
    try {
        pub = new MqttClient("tcp://127.0.0.1:" + port, "producer", new MemoryPersistence());
        pub.connect();
        for (String msg : messages) {
            pub.publish(topic, new MqttMessage(msg.getBytes(UTF_8)));
        }
    } finally {
        assert pub != null;
        pub.disconnect();
        pub.close();
    }
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) Matchers.anyString(org.mockito.Matchers.anyString)

Example 19 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project pentaho-kettle by pentaho.

the class MQTTProducerTest method testSendBinaryToProducer.

@Test
public void testSendBinaryToProducer() throws Exception {
    TransMeta transMeta = new TransMeta(getClass().getResource("/ProduceFourBinaryRows.ktr").getPath());
    Trans binaryTrans = new Trans(transMeta);
    binaryTrans.prepareExecution(new String[] {});
    when(mqttClient.isConnected()).thenReturn(true);
    handleAsSecondRow(binaryTrans);
    doAnswer(invocation -> {
        String topic = (String) invocation.getArguments()[0];
        MqttMessage message = (MqttMessage) invocation.getArguments()[1];
        assertEquals("TestLosing", topic);
        assertEquals(0, message.getQos());
        assertEquals("#losing", new String(message.getPayload(), UTF_8));
        return null;
    }).when(mqttClient).publish(any(), any());
    binaryTrans.startThreads();
    binaryTrans.waitUntilFinished();
    verify(mqttClient).disconnect();
    assertEquals(4, binaryTrans.getSteps().get(1).step.getLinesOutput());
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) TransMeta(org.pentaho.di.trans.TransMeta) Trans(org.pentaho.di.trans.Trans) Test(org.junit.Test)

Example 20 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project iaf by ibissource.

the class MqttSender method sendMessage.

public Message sendMessage(Message message, PipeLineSession session, String soapHeader) throws SenderException, TimeoutException {
    try {
        if (!client.isConnected()) {
            super.open();
        }
        log.debug(message);
        MqttMessage MqttMessage = new MqttMessage();
        MqttMessage.setPayload(message.asByteArray());
        MqttMessage.setQos(getQos());
        client.publish(getTopic(), MqttMessage);
    } catch (Exception e) {
        throw new SenderException(e);
    }
    return message;
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) SenderException(nl.nn.adapterframework.core.SenderException) SenderException(nl.nn.adapterframework.core.SenderException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException)

Aggregations

MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)65 MqttException (org.eclipse.paho.client.mqttv3.MqttException)28 MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)19 IMqttDeliveryToken (org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)16 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)16 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)15 Test (org.junit.Test)14 MqttCallback (org.eclipse.paho.client.mqttv3.MqttCallback)8 CountDownLatch (java.util.concurrent.CountDownLatch)4 JsonParser (com.google.gson.JsonParser)3 IOException (java.io.IOException)3 MqttAsyncClient (org.eclipse.paho.client.mqttv3.MqttAsyncClient)3 JSONObject (org.json.JSONObject)3 Test (org.testng.annotations.Test)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 MISTEdge (edu.snu.mist.common.graph.MISTEdge)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 CamelContext (org.apache.camel.CamelContext)2