diff --git a/WallPanelApp/build.gradle b/WallPanelApp/build.gradle index 07c2575..ec35f9d 100644 --- a/WallPanelApp/build.gradle +++ b/WallPanelApp/build.gradle @@ -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 { diff --git a/WallPanelApp/src/main/java/com/thanksmister/iot/wallpanel/modules/MQTTModule.kt b/WallPanelApp/src/main/java/com/thanksmister/iot/wallpanel/modules/MQTTModule.kt index 15b8b56..6d73624 100644 --- a/WallPanelApp/src/main/java/com/thanksmister/iot/wallpanel/modules/MQTTModule.kt +++ b/WallPanelApp/src/main/java/com/thanksmister/iot/wallpanel/modules/MQTTModule.kt @@ -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 { @@ -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) diff --git a/WallPanelApp/src/main/java/com/thanksmister/iot/wallpanel/network/MQTTService.kt b/WallPanelApp/src/main/java/com/thanksmister/iot/wallpanel/network/MQTTService.kt index a636403..98b1d74 100644 --- a/WallPanelApp/src/main/java/com/thanksmister/iot/wallpanel/network/MQTTService.kt +++ b/WallPanelApp/src/main/java/com/thanksmister/iot/wallpanel/network/MQTTService.kt @@ -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 @@ -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") } } @@ -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?) { 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.") } } diff --git a/WallPanelApp/src/main/java/com/thanksmister/iot/wallpanel/network/WallPanelService.kt b/WallPanelApp/src/main/java/com/thanksmister/iot/wallpanel/network/WallPanelService.kt index 9f3b5e0..91efd1e 100644 --- a/WallPanelApp/src/main/java/com/thanksmister/iot/wallpanel/network/WallPanelService.kt +++ b/WallPanelApp/src/main/java/com/thanksmister/iot/wallpanel/network/WallPanelService.kt @@ -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) } } @@ -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() { @@ -375,12 +373,12 @@ 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() @@ -388,7 +386,7 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener { audioPlayer.reset() audioPlayerBusy = false } - audioPlayer!!.setOnErrorListener { audioPlayer, i, i1 -> + audioPlayer?.setOnErrorListener { audioPlayer, i, i1 -> Timber.d("audioPlayer: Error playing file") audioPlayerBusy = false false @@ -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)") @@ -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) } @@ -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) } @@ -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 } } @@ -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) { @@ -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)