This commit is contained in:
Andrei Vereha 2021-06-11 21:27:04 +02:00
parent 96e06cb33a
commit 898c1c7906
4 changed files with 36 additions and 15 deletions

View file

@ -11,7 +11,6 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definitio
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.*
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.androidaps.utils.rx.retryWithBackoff
import io.reactivex.Observable
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
@ -78,8 +77,7 @@ class OmnipodDashManagerImpl @Inject constructor(
private val observeConnectToPod: Observable<PodEvent>
get() = Observable.defer {
bleManager.connect()
.doOnError { throwable -> logger.warn(LTag.PUMPBTCOMM,"observeConnectToPod error=$throwable")}
.retryWithBackoff(retries = 2, delay = 6, timeUnit = TimeUnit.SECONDS, delayFactor = 1.2)
.doOnError { throwable -> logger.warn(LTag.PUMPBTCOMM, "observeConnectToPod error=$throwable") }
}
private val observePairNewPod: Observable<PodEvent>

View file

@ -113,7 +113,22 @@ class OmnipodDashBleManagerImpl @Inject constructor(
emitter.onComplete()
return@create
}
conn.connect()
// two retries
for (i in 1..MAX_NUMBER_OF_CONNECTION_ATTEMPTS) {
try {
// wait i * CONNECTION_TIMEOUT
conn.connect(i)
break
} catch (e: Exception) {
aapsLogger.warn(LTag.PUMPBTCOMM, "connect error=$e")
if (i == MAX_NUMBER_OF_CONNECTION_ATTEMPTS) {
emitter.onError(e)
return@create
}
}
}
emitter.onNext(PodEvent.BluetoothConnected(podAddress))
emitter.onNext(PodEvent.EstablishingSession)
establishSession(1.toByte())
@ -218,7 +233,7 @@ class OmnipodDashBleManagerImpl @Inject constructor(
}
companion object {
const val MAX_NUMBER_OF_CONNECTION_ATTEMPTS = 3
const val CONTROLLER_ID = 4242 // TODO read from preferences or somewhere else.
}
}

View file

@ -55,7 +55,7 @@ class Connection(
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTING
gattConnection = podDevice.connectGatt(context, autoConnect, bleCommCallbacks, BluetoothDevice.TRANSPORT_LE)
// OnDisconnect can be called after this point!!!
val state = waitForConnection()
val state = waitForConnection(2)
if (state !is Connected) {
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED
throw FailedToConnectException(podDevice.address)
@ -79,6 +79,7 @@ class Connection(
gattConnection,
bleCommCallbacks
)
val sendResult = cmdBleIO.hello()
if (sendResult !is BleSendSuccess) {
throw FailedToConnectException("Could not send HELLO command to ${podDevice.address}")
@ -89,7 +90,7 @@ class Connection(
val msgIO = MessageIO(aapsLogger, cmdBleIO, dataBleIO)
fun connect() {
fun connect(timeoutMultiplier: Int) {
if (session != null) {
disconnect()
}
@ -100,7 +101,7 @@ class Connection(
throw FailedToConnectException("connect() returned false")
}
if (waitForConnection() !is Connected) {
if (waitForConnection(timeoutMultiplier) !is Connected) {
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED
throw FailedToConnectException(podDevice.address)
}
@ -111,6 +112,8 @@ class Connection(
dataBleIO.characteristic = discovered[CharacteristicType.DATA]!!
cmdBleIO.characteristic = discovered[CharacteristicType.CMD]!!
// val ret = gattConnection.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH)
// aapsLogger.info(LTag.PUMPBTCOMM, "requestConnectionPriority: $ret")
cmdBleIO.hello()
cmdBleIO.readyToRead()
dataBleIO.readyToRead()
@ -125,9 +128,9 @@ class Connection(
session = null
}
private fun waitForConnection(): ConnectionState {
private fun waitForConnection(timeoutMultiplier: Int): ConnectionState {
try {
bleCommCallbacks.waitForConnection(CONNECT_TIMEOUT_MS)
bleCommCallbacks.waitForConnection(BASE_CONNECT_TIMEOUT_MS * timeoutMultiplier)
} catch (e: InterruptedException) {
// We are still going to check if connection was successful
aapsLogger.info(LTag.PUMPBTCOMM, "Interrupted while waiting for connection")
@ -178,7 +181,6 @@ class Connection(
}
companion object {
private const val CONNECT_TIMEOUT_MS = 12000
private const val BASE_CONNECT_TIMEOUT_MS = 6000
}
}

View file

@ -368,8 +368,13 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
private fun updateLastConnection() {
if (podStateManager.isUniqueIdSet) {
podInfoBinding.lastConnection.text = readableDuration(Duration(podStateManager.lastUpdatedSystem, System
.currentTimeMillis()))
podInfoBinding.lastConnection.text = readableDuration(
Duration(
podStateManager.lastUpdatedSystem,
System
.currentTimeMillis()
)
)
val lastConnectionColor =
if (omnipodDashPumpPlugin.isUnreachableAlertTimeoutExceeded(getPumpUnreachableTimeout().millis)) {
Color.RED
@ -380,7 +385,8 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
} else {
podInfoBinding.lastConnection.setTextColor(Color.WHITE)
podInfoBinding.lastConnection.text = readableDuration(
Duration(podStateManager.lastUpdatedSystem, System.currentTimeMillis()))
Duration(podStateManager.lastUpdatedSystem, System.currentTimeMillis())
)
}
}