Search in sources :

Example 41 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project transporter by wang4ever.

the class MqttProducerCallback method deliveryComplete.

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
    try {
        MqttMessage msg = token.getMessage();
        if (msg == null)
            return;
        String hex = Hex.encodeHexString(msg.getPayload());
        logger.info("Mqtt producer deliveryComplete(). token.getMqttMessage()=[messageId={}, qos={}, retained={}, dup={}, payload={}]", msg.getId(), msg.getQos(), msg.isDuplicate(), msg.isRetained(), hex);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage)

Example 42 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project android-mdm-agent by flyve-mdm.

the class PoliciesController method unenroll.

/**
 * Unenroll the device
 */
public boolean unenroll() {
    // Send message with unenroll
    String topic = mTopic + "/Status/Unenroll";
    String payload = "{\"unenroll\": \"unenrolled\"}";
    byte[] encodedPayload = new byte[0];
    try {
        encodedPayload = payload.getBytes(UTF_8);
        MqttMessage message = new MqttMessage(encodedPayload);
        client.publish(topic, message);
        broadcastReceivedLog(Helpers.broadCastMessage(MQTT_SEND, "Unenroll", "Unenroll success"));
        // clear cache
        cache.clearSettings();
        // send message
        Helpers.sendBroadcast(Helpers.broadCastMessage("action", "open", "splash"), Helpers.BROADCAST_MSG, this.context);
        // show offline
        Helpers.sendBroadcast(false, Helpers.BROADCAST_STATUS, this.context);
        return true;
    } catch (Exception ex) {
        FlyveLog.e(ex.getMessage());
        broadcastReceivedLog(Helpers.broadCastMessage(ERROR, "Error on unenroll", ex.getMessage()));
        return false;
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage)

Example 43 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project android-mdm-agent by flyve-mdm.

the class PoliciesController method sendOnlineStatus.

/**
 * Send the Status version of the agent
 * payload: {"online": "true"}
 */
public void sendOnlineStatus(Boolean status) {
    String topic = mTopic + "/Status/Online";
    String payload = "{\"online\": \"" + Boolean.toString(status) + "\"}";
    byte[] encodedPayload = new byte[0];
    try {
        encodedPayload = payload.getBytes(UTF_8);
        MqttMessage message = new MqttMessage(encodedPayload);
        IMqttDeliveryToken token = client.publish(topic, message);
        broadcastReceivedLog(Helpers.broadCastMessage(MQTT_SEND, "Send Online Status", "ID: " + token.getMessageId()));
    } catch (Exception ex) {
        broadcastReceivedLog(Helpers.broadCastMessage(ERROR, "Error on sendStatusVersion", ex.getMessage()));
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)

Example 44 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project nifi by apache.

the class TestConsumeMqttCommon method testOnStoppedFinish.

@Test
public void testOnStoppedFinish() throws Exception {
    testRunner.setProperty(ConsumeMQTT.PROP_QOS, "2");
    testRunner.assertValid();
    MqttMessage innerMessage = new MqttMessage();
    innerMessage.setPayload(ByteBuffer.wrap("testMessage".getBytes()).array());
    innerMessage.setQos(2);
    MQTTQueueMessage testMessage = new MQTTQueueMessage("testTopic", innerMessage);
    ConsumeMQTT consumeMQTT = (ConsumeMQTT) testRunner.getProcessor();
    consumeMQTT.onScheduled(testRunner.getProcessContext());
    reconnect(consumeMQTT);
    Thread.sleep(PUBLISH_WAIT_MS);
    assertTrue(isConnected(consumeMQTT));
    consumeMQTT.processSessionFactory = testRunner.getProcessSessionFactory();
    Field f = ConsumeMQTT.class.getDeclaredField("mqttQueue");
    f.setAccessible(true);
    LinkedBlockingQueue<MQTTQueueMessage> queue = (LinkedBlockingQueue<MQTTQueueMessage>) f.get(consumeMQTT);
    queue.add(testMessage);
    consumeMQTT.onUnscheduled(testRunner.getProcessContext());
    consumeMQTT.onStopped(testRunner.getProcessContext());
    testRunner.assertTransferCount(ConsumeMQTT.REL_MESSAGE, 1);
    assertProvenanceEvents(1);
    List<MockFlowFile> flowFiles = testRunner.getFlowFilesForRelationship(ConsumeMQTT.REL_MESSAGE);
    MockFlowFile flowFile = flowFiles.get(0);
    flowFile.assertContentEquals("testMessage");
    flowFile.assertAttributeEquals(BROKER_ATTRIBUTE_KEY, broker);
    flowFile.assertAttributeEquals(TOPIC_ATTRIBUTE_KEY, "testTopic");
    flowFile.assertAttributeEquals(QOS_ATTRIBUTE_KEY, "2");
    flowFile.assertAttributeEquals(IS_DUPLICATE_ATTRIBUTE_KEY, "false");
    flowFile.assertAttributeEquals(IS_RETAINED_ATTRIBUTE_KEY, "false");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) Field(java.lang.reflect.Field) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ConsumeMQTT(org.apache.nifi.processors.mqtt.ConsumeMQTT) Test(org.junit.Test)

Example 45 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project nifi by apache.

the class TestConsumeMQTT method internalPublish.

@Override
public void internalPublish(PublishMessage publishMessage) {
    MqttMessage mqttMessage = new MqttMessage();
    mqttMessage.setPayload(publishMessage.getPayload().array());
    mqttMessage.setRetained(publishMessage.isRetainFlag());
    mqttMessage.setQos(publishMessage.getQos().ordinal());
    try {
        mqttTestClient.publish(publishMessage.getTopicName(), mqttMessage);
    } catch (MqttException e) {
        fail("Should never get an MqttException when publishing using test client");
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

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