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);
}
}
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;
}
}
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()));
}
}
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");
}
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");
}
}
Aggregations