Skip to content

Commit

Permalink
Merge pull request #2 from christianrowlands/feature/overload-publish…
Browse files Browse the repository at this point in the history
…-message

Overload publishMessage
  • Loading branch information
christianrowlands authored Jul 17, 2023
2 parents c3a9d50 + 6cbbed5 commit 48673d2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
4 changes: 2 additions & 2 deletions MqttLibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ plugins {
}

group 'com.craxiom'
version '0.5.0'
version '0.6.0'

android {
compileSdkVersion 33
Expand All @@ -27,7 +27,7 @@ android {
defaultConfig {
minSdkVersion 24
targetSdkVersion 33
versionCode 11
versionCode 12
versionName version

setProperty("archivesBaseName", "$applicationName-$versionName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import com.craxiom.mqttlibrary.IConnectionStateListener;
import com.craxiom.mqttlibrary.IMqttService;
import com.craxiom.mqttlibrary.R;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.MessageOrBuilder;
import com.google.protobuf.util.JsonFormat;
import com.hivemq.client.internal.mqtt.lifecycle.mqtt3.Mqtt3ClientDisconnectedContextView;
import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.hivemq.client.mqtt.exceptions.ConnectionFailedException;
import com.hivemq.client.mqtt.lifecycle.MqttDisconnectSource;
import com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient;
import com.hivemq.client.mqtt.mqtt3.Mqtt3Client;
Expand All @@ -23,7 +23,6 @@
import com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAck;
import com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAckReturnCode;

import java.net.UnknownHostException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
Expand Down Expand Up @@ -249,18 +248,29 @@ protected void publishMessage(String mqttMessageTopic, MessageOrBuilder message)
{
try
{
final String messageJson = jsonFormatter.print(message);

if (mqtt3Client.getState().isConnectedOrReconnect())
{
mqtt3Client.publishWith().topic(mqttMessageTopic).qos(MqttQos.AT_LEAST_ONCE).payload(messageJson.getBytes()).send();
}
} catch (Exception e)
final String jsonMessage = jsonFormatter.print(message);
publishMessage(mqttMessageTopic, jsonMessage);
} catch (InvalidProtocolBufferException e)
{
Timber.e(e, "Caught an exception when trying to send an MQTT message");
}
}

/**
* Publishes the JSON string to the specified topic.
*
* @param mqttMessageTopic The MQTT topic to publish the message to.
* @param jsonMessage The JSON string to send to the MQTT broker.
* @since 0.6.0
*/
protected void publishMessage(String mqttMessageTopic, String jsonMessage)
{
if (mqtt3Client.getState().isConnectedOrReconnect())
{
mqtt3Client.publishWith().topic(mqttMessageTopic).qos(MqttQos.AT_LEAST_ONCE).payload(jsonMessage.getBytes()).send();
}
}

/**
* Adds an {@link IConnectionStateListener} so that it will be notified of all future MQTT connection state changes.
*
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ It is important to note that the pieces provided in the library cannot stand on


## Change log
##### 0.6.0 - 2023-07-17
* Overload the publishMessage method to enable sending plain JSON strings.

##### [0.5.0](https://github.com/christianrowlands/android-mqtt-connection-lib/releases/tag/v0.5.0) - 2022-10-25
* Change the onMdmOverride method to protected so it can be overridden

Expand Down

0 comments on commit 48673d2

Please sign in to comment.