Skip to content

Commit

Permalink
* Auto-restart MQTT service on network disconnect with minimal alert …
Browse files Browse the repository at this point in the history
…notifications
  • Loading branch information
thanksmister committed Nov 1, 2018
1 parent a3f1460 commit 5e30e76
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
4 changes: 2 additions & 2 deletions WallPanelApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ repositories {
def versionMajor = 0
def versionMinor = 8
def versionPatch = 4
def versionBuild = 2 // bump for dog food builds, public betas, etc.
def versionBuild = 3 // bump for dog food builds, public betas, etc.

android {
kapt {
generateStubs = true
correctErrorTypes = true
}
compileSdkVersion 27
buildToolsVersion "28.0.2"
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.thanksmister.iot.wallpanel"
minSdkVersion versions.min_sdk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.thanksmister.iot.wallpanel.network

import android.annotation.SuppressLint
import android.app.KeyguardManager
import android.arch.lifecycle.LifecycleService
import android.arch.lifecycle.Observer
Expand All @@ -28,7 +27,6 @@ import android.os.*
import android.provider.Settings
import android.support.v4.content.ContextCompat
import android.support.v4.content.LocalBroadcastManager
import android.widget.Toast
import com.koushikdutta.async.AsyncServer
import com.koushikdutta.async.ByteBufferList
import com.koushikdutta.async.http.body.JSONObjectBody
Expand Down Expand Up @@ -103,6 +101,7 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {
private val reconnectHandler = Handler()
private var appLaunchUrl: String? = null
private var localBroadCastManager: LocalBroadcastManager? = null
private var mqttAlertMessageShown = false

inner class WallPanelServiceBinder : Binder() {
val service: WallPanelService
Expand Down Expand Up @@ -314,6 +313,10 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {

override fun onMQTTConnect() {
Timber.w("onMQTTConnect")
if(mqttAlertMessageShown) {
clearAlertMessage() // clear any dialogs
mqttAlertMessageShown = false
}
publishMessage(COMMAND_STATE, state.toString())
clearFaceDetected()
clearMotionDetected()
Expand All @@ -322,16 +325,22 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {
override fun onMQTTDisconnect() {
Timber.e("onMQTTDisconnect")
if(hasNetwork()) {
sendAlertMessage(getString(R.string.error_mqtt_connection))
//reconnectHandler.postDelayed(restartMqttRunnable, 3000)
if(!mqttAlertMessageShown) {
mqttAlertMessageShown = true
sendAlertMessage(getString(R.string.error_mqtt_connection))
}
reconnectHandler.postDelayed(restartMqttRunnable, 30000)
}
}

override fun onMQTTException(message: String) {
Timber.e("onMQTTException: $message")
if(hasNetwork()) {
sendAlertMessage(getString(R.string.error_mqtt_exception))
//reconnectHandler.postDelayed(restartMqttRunnable, 3000)
if(!mqttAlertMessageShown) {
mqttAlertMessageShown = true
sendAlertMessage(getString(R.string.error_mqtt_exception))
}
reconnectHandler.postDelayed(restartMqttRunnable, 30000)
}
}

Expand Down Expand Up @@ -765,6 +774,13 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {
bm.sendBroadcast(intent)
}

private fun clearAlertMessage() {
Timber.d("clearAlertMessage")
val intent = Intent(BROADCAST_CLEAR_ALERT_MESSAGE)
val bm = LocalBroadcastManager.getInstance(applicationContext)
bm.sendBroadcast(intent)
}

private fun sendWakeScreen() {
Timber.d("sendWakeScreen")
val intent = Intent(BROADCAST_SCREEN_WAKE)
Expand Down Expand Up @@ -857,6 +873,7 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {
const val BROADCAST_EVENT_SCREEN_TOUCH = "BROADCAST_EVENT_SCREEN_TOUCH"
const val SCREEN_WAKE_TIME = 30000L
const val BROADCAST_ALERT_MESSAGE = "BROADCAST_ALERT_MESSAGE"
const val BROADCAST_CLEAR_ALERT_MESSAGE = "BROADCAST_CLEAR_ALERT_MESSAGE"
const val BROADCAST_TOAST_MESSAGE = "BROADCAST_TOAST_MESSAGE"
const val BROADCAST_SCREEN_WAKE = "BROADCAST_SCREEN_WAKE"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import android.widget.Toast
import com.thanksmister.iot.wallpanel.R
import com.thanksmister.iot.wallpanel.network.WallPanelService
import com.thanksmister.iot.wallpanel.network.WallPanelService.Companion.BROADCAST_ALERT_MESSAGE
import com.thanksmister.iot.wallpanel.network.WallPanelService.Companion.BROADCAST_CLEAR_ALERT_MESSAGE
import com.thanksmister.iot.wallpanel.network.WallPanelService.Companion.BROADCAST_TOAST_MESSAGE
import com.thanksmister.iot.wallpanel.persistence.Configuration
import com.thanksmister.iot.wallpanel.utils.DialogUtils
Expand Down Expand Up @@ -75,6 +76,8 @@ abstract class BrowserActivity : DaggerAppCompatActivity() {
} else if (BROADCAST_ALERT_MESSAGE == intent.action) {
val message = intent.getStringExtra(BROADCAST_ALERT_MESSAGE)
dialogUtils.showAlertDialog(this@BrowserActivity, message)
} else if (BROADCAST_CLEAR_ALERT_MESSAGE == intent.action) {
dialogUtils.clearDialogs()
}
}
}
Expand Down Expand Up @@ -111,6 +114,7 @@ abstract class BrowserActivity : DaggerAppCompatActivity() {
filter.addAction(BROADCAST_ACTION_JS_EXEC)
filter.addAction(BROADCAST_ACTION_CLEAR_BROWSER_CACHE)
filter.addAction(BROADCAST_ACTION_RELOAD_PAGE)
filter.addAction(BROADCAST_CLEAR_ALERT_MESSAGE)
filter.addAction(BROADCAST_ALERT_MESSAGE)
filter.addAction(BROADCAST_TOAST_MESSAGE)
val bm = LocalBroadcastManager.getInstance(this)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0-rc02'
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'io.fabric.tools:gradle:1.+'
}
Expand Down

0 comments on commit 5e30e76

Please sign in to comment.