Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle all possible MQTT disconnect exceptions #47

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,16 @@ class MQTT3Service(
listener?.handleMqttConnected()
}
mqttBuilder.addDisconnectedListener { context: MqttClientDisconnectedContext? ->
TypeSwitch.`when`(context!!.cause).`is`(
Mqtt3DisconnectException::class.java,
Consumer { disconnectException ->
val disconnect: Mqtt3Disconnect = disconnectException.mqttMessage
mReady.set(false)
listener?.handleMqttDisconnected()
mqttOptions.let {
Timber.e("Failed to connect to: %s, exception: %s", it.brokerUrl, disconnect.toString())
listener?.handleMqttException("Error establishing MQTT connection to MQTT broker with address ${mqttOptions.brokerUrl}.")
}
})

mReady.set(false)
listener?.handleMqttDisconnected()
mqttOptions.let {
Timber.e(
"Disconnected from: %s, exception: %s",
it.brokerUrl,
context?.cause?.message
)
listener?.handleMqttException("Error establishing MQTT connection to MQTT broker with address ${mqttOptions.brokerUrl}.")
}
}

mqtt3AsyncClient = mqttBuilder.useMqttVersion3().build().toAsync()
Expand Down Expand Up @@ -228,7 +226,6 @@ class MQTT3Service(
Timber.e(e)
} catch (e: Mqtt3MessageException) {
Timber.e("Error Sending Command: %s", e.message)
e.printStackTrace()
listener?.handleMqttException("Couldn't send message to the MQTT broker for topic ${mqttMessage.topic}, check the MQTT client settings or your connection to the broker.")
}
}
Expand All @@ -250,10 +247,9 @@ class MQTT3Service(
)
}
} catch (e: NullPointerException) {
e.printStackTrace()
Timber.e(e)
} catch (e: Mqtt3MessageException) {
e.printStackTrace()
Timber.e(e)
listener?.handleMqttException("Exception while subscribing: " + e.message)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,18 @@ class MQTT5Service(
listener?.handleMqttConnected()
}
mqttBuilder.addDisconnectedListener { context: MqttClientDisconnectedContext? ->
TypeSwitch.`when`(context!!.cause).`is`(
Mqtt5DisconnectException::class.java,
Consumer { disconnectException ->
val disconnect: Mqtt5Disconnect = disconnectException.mqttMessage
mReady.set(false)
listener?.handleMqttDisconnected()
mqttOptions.let {
Timber.e(
"Failed to connect to: %s, exception: %s",
it.brokerUrl,
disconnect.reasonString
)
listener?.handleMqttException("Error establishing MQTT connection to MQTT broker with address ${mqttOptions.brokerUrl}.")
}
})
mReady.set(false)
listener?.handleMqttDisconnected()
mqttOptions.let {
Timber.e(
"Disconnected from: %s, exception: %s",
it.brokerUrl,
context?.cause?.message
)
listener?.handleMqttException("Error establishing MQTT connection to MQTT broker with address ${mqttOptions.brokerUrl}.")
}
}

mqtt5AsyncClient = mqttBuilder.useMqttVersion5().build().toAsync()
val clientConnect = mqtt5AsyncClient!!.connectWith()
clientConnect.cleanStart(false)
Expand Down