Skip to content

Commit

Permalink
feat: add debug logging for mqtt communication (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasgrill committed Aug 12, 2024
1 parent c021a68 commit c1b6ca5
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,19 @@ class XesarMqttClient(private val client: MqttAsyncClient) : IXesarMqttClient {
object : MqttCallback {

override fun connectionLost(cause: Throwable?) {
log.error("lost connection: $cause")
log.error(cause) { "lost connection" }
val exception = ConnectionFailedException("lost connection: $cause")
onDisconnect(exception)
}

override fun messageArrived(topic: String?, message: MqttMessage?) {
log.debug { "Message arrived: ${message?.payload?.decodeToString()}" }
onMessage(topic ?: "", message?.payload ?: ByteArray(0))
if (log.isDebugEnabled) {
log.debug("Message arrived: ${message?.payload?.decodeToString()}")
}
}

override fun deliveryComplete(token: IMqttDeliveryToken?) {
if (log.isDebugEnabled) {
log.debug(
"Received MQTT 'delivery complete' message of topic: ${token?.topics?.joinToString(",")}")
log.debug {
"Received MQTT 'delivery complete' message of topic: ${token?.topics?.joinToString(",")}"
}
}
})
Expand All @@ -58,6 +55,8 @@ class XesarMqttClient(private val client: MqttAsyncClient) : IXesarMqttClient {
val result = CompletableDeferred<Unit>()
val qosArr = IntArray(topics.size) { qos }

log.debug { "Subscribing to topics: ${topics.joinToString(",")} with qos $qos" }

client.subscribe(
topics,
qosArr,
Expand Down Expand Up @@ -95,6 +94,8 @@ class XesarMqttClient(private val client: MqttAsyncClient) : IXesarMqttClient {
override fun publishAsync(topic: String, message: String, qos: Int): Deferred<Unit> {
val deferredResult = CompletableDeferred<Unit>()

log.debug { "Publishing message to topic: $topic, qos $qos, message: $message" }

client.publish(
topic,
message.encodeToByteArray(),
Expand Down Expand Up @@ -122,11 +123,13 @@ class XesarMqttClient(private val client: MqttAsyncClient) : IXesarMqttClient {
* @param topics The topics to unsubscribe from.
*/
override fun unsubscribe(topics: Topics) {
log.debug { "Unsubscribing from topics: ${topics.topics.joinToString(",")}" }
client.unsubscribe(topics.topics)
}

/** Disconnects from the MQTT broker. */
override fun disconnect() {
log.debug { "Disconnecting from MQTT broker" }
val token = client.disconnect()

token.actionCallback =
Expand Down

0 comments on commit c1b6ca5

Please sign in to comment.