Skip to content

Commit

Permalink
* Adding more crash reports around MQTT connections
Browse files Browse the repository at this point in the history
* Bubbling up specific error messages for the MQTT client
* Removed reporting of error message to the alert due to memory issues
  • Loading branch information
thanksmister committed Jun 15, 2019
1 parent 4abc621 commit 082c8d7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 50 deletions.
2 changes: 1 addition & 1 deletion WallPanelApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repositories {
def versionMajor = 0
def versionMinor = 8
def versionPatch = 7
def versionBuild = 2 // bump for dog food builds, public betas, etc.
def versionBuild = 3 // bump for dog food builds, public betas, etc.

android {
kapt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class MQTTModule (base: Context?, var mqttOptions: MQTTOptions, private val list
}
}

fun resetMQttOptions(mqttOptions: MQTTOptions) {
/*fun resetMQttOptions(mqttOptions: MQTTOptions) {
this.mqttOptions = mqttOptions
if (mqttService != null) {
try {
Expand All @@ -91,7 +91,7 @@ class MQTTModule (base: Context?, var mqttOptions: MQTTOptions, private val list
Timber.e("Could not create MQTTPublisher: " + t.message)
}
}
}
}*/

override fun subscriptionMessage(id: String, topic: String, payload: String) {
Timber.d("topic: " + topic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package com.thanksmister.iot.wallpanel.network
import android.R.id.message
import android.content.Context
import android.text.TextUtils
import com.crashlytics.android.Crashlytics
import com.thanksmister.iot.wallpanel.BuildConfig
import com.thanksmister.iot.wallpanel.R
import com.thanksmister.iot.wallpanel.utils.MqttUtils
import com.thanksmister.iot.wallpanel.utils.StringUtils
Expand Down Expand Up @@ -109,9 +111,7 @@ class MQTTService(private var context: Context, options: MQTTOptions,
sendMessage(mqttOptions?.getBaseTopic() + command, mqttMessage)
}
} catch (e: MqttException) {
if (listener != null) {
listener!!.handleMqttException("Exception while subscribing: " + e.message)
}
listener?.handleMqttException("Exception while subscribing to topics")
}

}
Expand Down Expand Up @@ -180,62 +180,68 @@ class MQTTService(private var context: Context, options: MQTTOptions,
disconnectedBufferOptions.isDeleteOldestMessages = false
mqttClient?.setBufferOpts(disconnectedBufferOptions)
listener?.handleMqttConnected()
mReady.set(true)
}

override fun onFailure(asyncActionToken: IMqttToken, exception: Throwable) {
mqttOptions.let {
Timber.e("Failed to connect to: " + it.brokerUrl + " exception: " + exception)
listener?.handleMqttException(context.getString(R.string.error_mqtt_subscription))
}
override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {
Timber.e("Failed to connect to: " + mqttOptions.brokerUrl + " exception: " + exception)
listener?.handleMqttException(context.getString(R.string.error_mqtt_subscription))
mReady.set(false)
}
})
} catch (e: NullPointerException) {
Timber.e(e, e.message)
e.printStackTrace()
mReady.set(false)
} catch (e: MqttException) {
listener?.handleMqttException("" + e.message)
mReady.set(false)
listener?.handleMqttException("Error initialize MQTT client")
}
mReady.set(true)
}

} catch (e: IllegalArgumentException) {
e.printStackTrace()
} catch (e: NullPointerException) {
e.printStackTrace()
} catch (e: Exception) {
e.printStackTrace()
listener?.handleMqttException("" + e.message)
listener?.handleMqttException("Error setting up MQTT client")
}
}

@Throws(MqttException::class)
private fun sendMessage(mqttTopic: String?, mqttMessage: MqttMessage) {
Timber.d("sendMessage")
if (isReady && mqttClient != null && mqttClient!!.isConnected) {
try {
mqttClient?.publish(mqttTopic, mqttMessage)
Timber.d("Command Topic: $mqttTopic Payload: $message")
} catch (e: NullPointerException) {
Timber.e(e.message)
} catch (e: MqttException) {
Timber.e("Error Sending Command: " + e.message)
e.printStackTrace()
listener?.handleMqttException(context.getString(R.string.error_mqtt_subscription))
try {
mqttClient?.let {
if (isReady && it.isConnected) {
it.publish(mqttTopic, mqttMessage)
Timber.d("Command Topic: $mqttTopic Payload: $message")
}
}
} catch (e: NullPointerException) {
Timber.e(e.message)
} catch (e: MqttException) {
Timber.e("Error Sending Command: " + e.message)
listener?.handleMqttException(context.getString(R.string.error_mqtt_subscription))
}
}

private fun subscribeToTopics(topicFilters: Array<String>?) {
Timber.d("Subscribe to Topics: " + StringUtils.convertArrayToString(topicFilters))
try {
if (isReady && mqttClient != null) {
mqttClient?.subscribe(topicFilters, MqttUtils.getQos(topicFilters!!.size),
MqttUtils.getMqttMessageListeners(topicFilters.size, listener))
mqttClient?.let {
if(it.isConnected && isReady) {
it.subscribe(topicFilters, MqttUtils.getQos(topicFilters!!.size),
MqttUtils.getMqttMessageListeners(topicFilters.size, listener))
}
}
} catch (e: NullPointerException) {
Timber.e(e.message)
if(!BuildConfig.DEBUG) {
Crashlytics.logException(e)
}
} catch (e: MqttException) {
listener?.handleMqttException("Exception while subscribing: " + e.message)
if(!BuildConfig.DEBUG) {
Crashlytics.logException(e)
}
listener?.handleMqttException("Exception while subscribing to topics.")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {
if (hasNetwork()) {
if (!mqttAlertMessageShown && !mqttConnected && Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
mqttAlertMessageShown = true
sendAlertMessage(getString(R.string.error_mqtt_exception))
sendAlertMessage(message)
reconnectHandler.postDelayed(restartMqttRunnable, 180000)
}
}
Expand All @@ -353,9 +353,7 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {
}

private fun publishMessage(command: String, message: String) {
if (mqttModule != null) {
mqttModule!!.publish(command, message)
}
mqttModule?.publish(command, message)
}

private fun configureCamera() {
Expand All @@ -375,20 +373,20 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {

private fun configureAudioPlayer() {
audioPlayer = MediaPlayer()
audioPlayer!!.setOnPreparedListener { audioPlayer ->
audioPlayer?.setOnPreparedListener { audioPlayer ->
Timber.d("audioPlayer: File buffered, playing it now")
audioPlayerBusy = false
audioPlayer.start()
}
audioPlayer!!.setOnCompletionListener { audioPlayer ->
audioPlayer?.setOnCompletionListener { audioPlayer ->
Timber.d("audioPlayer: Cleanup")
if (audioPlayer.isPlaying) { // should never happen, just in case
audioPlayer.stop()
}
audioPlayer.reset()
audioPlayerBusy = false
}
audioPlayer!!.setOnErrorListener { audioPlayer, i, i1 ->
audioPlayer?.setOnErrorListener { audioPlayer, i, i1 ->
Timber.d("audioPlayer: Error playing file")
audioPlayerBusy = false
false
Expand All @@ -399,17 +397,17 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {
if (httpServer == null && configuration.httpEnabled) {
Timber.d("startHttp")
httpServer = AsyncHttpServer()
httpServer!!.addAction("*", "*") { request, response ->
httpServer?.addAction("*", "*") { request, response ->
Timber.i("Unhandled Request Arrived")
response.code(404)
response.send("")
}
httpServer!!.listen(AsyncServer.getDefault(), configuration.httpPort)
httpServer?.listen(AsyncServer.getDefault(), configuration.httpPort)
Timber.i("Started HTTP server on " + configuration.httpPort)
}

if (httpServer != null && configuration.httpRestEnabled) {
httpServer!!.addAction("POST", "/api/command") { request, response ->
httpServer?.addAction("POST", "/api/command") { request, response ->
var result = false
if (request.body is JSONObjectBody) {
Timber.i("POST Json Arrived (command)")
Expand All @@ -427,7 +425,7 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {
response.send(j)
}

httpServer!!.addAction("GET", "/api/state") { request, response ->
httpServer?.addAction("GET", "/api/state") { request, response ->
Timber.i("GET Arrived (/api/state)")
response.send(state)
}
Expand All @@ -436,7 +434,7 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {

if (httpServer != null && configuration.httpMJPEGEnabled) {
startMJPEG()
httpServer!!.addAction("GET", "/camera/stream") { _, response ->
httpServer?.addAction("GET", "/camera/stream") { _, response ->
Timber.i("GET Arrived (/camera/stream)")
startMJPEG(response)
}
Expand All @@ -446,9 +444,9 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {

private fun stopHttp() {
Timber.d("stopHttp")
if (httpServer != null) {
httpServer?.let {
stopMJPEG()
httpServer!!.stop()
it.stop()
httpServer = null
}
}
Expand Down Expand Up @@ -483,9 +481,7 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {
Timber.d("stopMJPEG Called")
mJpegSockets.clear()
cameraReader.getJpeg().removeObservers(this)
if(httpServer != null) {
httpServer!!.removeAction("GET", "/camera/stream")
}
httpServer?.removeAction("GET", "/camera/stream")
}

private fun startMJPEG(response: AsyncHttpServerResponse) {
Expand Down Expand Up @@ -624,7 +620,7 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {
}

private fun speakMessage(message: String) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (textToSpeechModule != null) {
Timber.d("speakMessage $message")
textToSpeechModule!!.speakText(message)
Expand Down

0 comments on commit 082c8d7

Please sign in to comment.