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

View file

@ -113,7 +113,22 @@ class OmnipodDashBleManagerImpl @Inject constructor(
emitter.onComplete() emitter.onComplete()
return@create 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.BluetoothConnected(podAddress))
emitter.onNext(PodEvent.EstablishingSession) emitter.onNext(PodEvent.EstablishingSession)
establishSession(1.toByte()) establishSession(1.toByte())
@ -218,7 +233,7 @@ class OmnipodDashBleManagerImpl @Inject constructor(
} }
companion object { companion object {
const val MAX_NUMBER_OF_CONNECTION_ATTEMPTS = 3
const val CONTROLLER_ID = 4242 // TODO read from preferences or somewhere else. 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 podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTING
gattConnection = podDevice.connectGatt(context, autoConnect, bleCommCallbacks, BluetoothDevice.TRANSPORT_LE) gattConnection = podDevice.connectGatt(context, autoConnect, bleCommCallbacks, BluetoothDevice.TRANSPORT_LE)
// OnDisconnect can be called after this point!!! // OnDisconnect can be called after this point!!!
val state = waitForConnection() val state = waitForConnection(2)
if (state !is Connected) { if (state !is Connected) {
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED
throw FailedToConnectException(podDevice.address) throw FailedToConnectException(podDevice.address)
@ -79,6 +79,7 @@ class Connection(
gattConnection, gattConnection,
bleCommCallbacks bleCommCallbacks
) )
val sendResult = cmdBleIO.hello() val sendResult = cmdBleIO.hello()
if (sendResult !is BleSendSuccess) { if (sendResult !is BleSendSuccess) {
throw FailedToConnectException("Could not send HELLO command to ${podDevice.address}") throw FailedToConnectException("Could not send HELLO command to ${podDevice.address}")
@ -89,7 +90,7 @@ class Connection(
val msgIO = MessageIO(aapsLogger, cmdBleIO, dataBleIO) val msgIO = MessageIO(aapsLogger, cmdBleIO, dataBleIO)
fun connect() { fun connect(timeoutMultiplier: Int) {
if (session != null) { if (session != null) {
disconnect() disconnect()
} }
@ -100,7 +101,7 @@ class Connection(
throw FailedToConnectException("connect() returned false") throw FailedToConnectException("connect() returned false")
} }
if (waitForConnection() !is Connected) { if (waitForConnection(timeoutMultiplier) !is Connected) {
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED
throw FailedToConnectException(podDevice.address) throw FailedToConnectException(podDevice.address)
} }
@ -111,6 +112,8 @@ class Connection(
dataBleIO.characteristic = discovered[CharacteristicType.DATA]!! dataBleIO.characteristic = discovered[CharacteristicType.DATA]!!
cmdBleIO.characteristic = discovered[CharacteristicType.CMD]!! cmdBleIO.characteristic = discovered[CharacteristicType.CMD]!!
// val ret = gattConnection.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH)
// aapsLogger.info(LTag.PUMPBTCOMM, "requestConnectionPriority: $ret")
cmdBleIO.hello() cmdBleIO.hello()
cmdBleIO.readyToRead() cmdBleIO.readyToRead()
dataBleIO.readyToRead() dataBleIO.readyToRead()
@ -125,9 +128,9 @@ class Connection(
session = null session = null
} }
private fun waitForConnection(): ConnectionState { private fun waitForConnection(timeoutMultiplier: Int): ConnectionState {
try { try {
bleCommCallbacks.waitForConnection(CONNECT_TIMEOUT_MS) bleCommCallbacks.waitForConnection(BASE_CONNECT_TIMEOUT_MS * timeoutMultiplier)
} catch (e: InterruptedException) { } catch (e: InterruptedException) {
// We are still going to check if connection was successful // We are still going to check if connection was successful
aapsLogger.info(LTag.PUMPBTCOMM, "Interrupted while waiting for connection") aapsLogger.info(LTag.PUMPBTCOMM, "Interrupted while waiting for connection")
@ -178,7 +181,6 @@ class Connection(
} }
companion object { companion object {
private const val BASE_CONNECT_TIMEOUT_MS = 6000
private const val CONNECT_TIMEOUT_MS = 12000
} }
} }

View file

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