- closeGatt on stopConnecting
 - fix stopBolus
This commit is contained in:
Andrei Vereha 2021-07-17 10:35:44 +02:00
parent f9aa967b46
commit bf7ad8e697
7 changed files with 38 additions and 22 deletions

View file

@ -78,6 +78,8 @@ class OmnipodDashPumpPlugin @Inject constructor(
commandQueue: CommandQueueProvider commandQueue: CommandQueueProvider
) : PumpPluginBase(pluginDescription, injector, aapsLogger, resourceHelper, commandQueue), Pump { ) : PumpPluginBase(pluginDescription, injector, aapsLogger, resourceHelper, commandQueue), Pump {
@Volatile var bolusCanceled = false @Volatile var bolusCanceled = false
@Volatile var bolusDeliveryInProgress = false
private val handler: Handler = Handler(Looper.getMainLooper()) private val handler: Handler = Handler(Looper.getMainLooper())
private lateinit var statusChecker: Runnable private lateinit var statusChecker: Runnable
var nextPodWarningCheck: Long = 0 var nextPodWarningCheck: Long = 0
@ -232,12 +234,13 @@ class OmnipodDashPumpPlugin @Inject constructor(
override fun disconnect(reason: String) { override fun disconnect(reason: String) {
aapsLogger.info(LTag.PUMP, "disconnect reason=$reason") aapsLogger.info(LTag.PUMP, "disconnect reason=$reason")
stopConnecting?.let { it.countDown() } stopConnecting?.let { it.countDown() }
omnipodManager.disconnect() omnipodManager.disconnect(false)
} }
override fun stopConnecting() { override fun stopConnecting() {
aapsLogger.info(LTag.PUMP, "stopConnecting") aapsLogger.info(LTag.PUMP, "stopConnecting")
stopConnecting?.let { it.countDown() } stopConnecting?.let { it.countDown() }
omnipodManager.disconnect(true)
} }
override fun getPumpStatus(reason: String) { override fun getPumpStatus(reason: String) {
@ -476,6 +479,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult {
try { try {
bolusDeliveryInProgress = true
aapsLogger.info(LTag.PUMP, "Delivering treatment: $detailedBolusInfo $bolusCanceled") aapsLogger.info(LTag.PUMP, "Delivering treatment: $detailedBolusInfo $bolusCanceled")
val beepsConfigurationKey = if (detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB) val beepsConfigurationKey = if (detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB)
R.string.key_omnipod_common_smb_beeps_enabled R.string.key_omnipod_common_smb_beeps_enabled
@ -563,6 +567,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
return ret return ret
} finally { } finally {
bolusCanceled = false bolusCanceled = false
bolusDeliveryInProgress = false
} }
} }
@ -697,7 +702,9 @@ class OmnipodDashPumpPlugin @Inject constructor(
override fun stopBolusDelivering() { override fun stopBolusDelivering() {
aapsLogger.info(LTag.PUMP, "stopBolusDelivering called") aapsLogger.info(LTag.PUMP, "stopBolusDelivering called")
bolusCanceled = true if (bolusDeliveryInProgress) {
bolusCanceled = true
}
} }
override fun setTempBasalAbsolute( override fun setTempBasalAbsolute(

View file

@ -41,7 +41,7 @@ interface OmnipodDashManager {
fun deactivatePod(): Observable<PodEvent> fun deactivatePod(): Observable<PodEvent>
fun disconnect() fun disconnect(closeGatt: Boolean=false)
fun connect(stop: CountDownLatch): Observable<PodEvent> fun connect(stop: CountDownLatch): Observable<PodEvent>
} }

View file

@ -78,8 +78,8 @@ class OmnipodDashManagerImpl @Inject constructor(
} }
} }
override fun disconnect() { override fun disconnect(closeGatt: Boolean) {
bleManager.disconnect() bleManager.disconnect(closeGatt)
} }
override fun connect(stop: CountDownLatch): Observable<PodEvent> { override fun connect(stop: CountDownLatch): Observable<PodEvent> {

View file

@ -23,5 +23,5 @@ interface OmnipodDashBleManager {
fun pairNewPod(): Observable<PodEvent> fun pairNewPod(): Observable<PodEvent>
fun disconnect() fun disconnect(closeGatt: Boolean=false)
} }

View file

@ -84,7 +84,7 @@ class OmnipodDashBleManagerImpl @Inject constructor(
} }
emitter.onComplete() emitter.onComplete()
} catch (ex: Exception) { } catch (ex: Exception) {
disconnect() disconnect(false)
emitter.tryOnError(ex) emitter.tryOnError(ex)
} finally { } finally {
busy.set(false) busy.set(false)
@ -142,7 +142,7 @@ class OmnipodDashBleManagerImpl @Inject constructor(
emitter.onComplete() emitter.onComplete()
} catch (ex: Exception) { } catch (ex: Exception) {
disconnect() disconnect(false)
emitter.tryOnError(ex) emitter.tryOnError(ex)
} finally { } finally {
busy.set(false) busy.set(false)
@ -226,15 +226,15 @@ class OmnipodDashBleManagerImpl @Inject constructor(
emitter.onNext(PodEvent.Connected) emitter.onNext(PodEvent.Connected)
emitter.onComplete() emitter.onComplete()
} catch (ex: Exception) { } catch (ex: Exception) {
disconnect() disconnect(false)
emitter.tryOnError(ex) emitter.tryOnError(ex)
} finally { } finally {
busy.set(false) busy.set(false)
} }
} }
override fun disconnect() { override fun disconnect(closeGatt: Boolean) {
connection?.disconnect() connection?.disconnect(closeGatt)
?: aapsLogger.info(LTag.PUMPBTCOMM, "Trying to disconnect a null connection") ?: aapsLogger.info(LTag.PUMPBTCOMM, "Trying to disconnect a null connection")
} }

View file

@ -40,7 +40,8 @@ class BleCommCallbacks(
if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) { if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) {
connected.countDown() connected.countDown()
} }
if (newState == BluetoothProfile.STATE_DISCONNECTED) { if (newState == BluetoothProfile.STATE_DISCONNECTED && status != BluetoothGatt.GATT_SUCCESS ) {
// If status == SUCCESS, it means that we initiated the disconnect.
disconnectHandler.onConnectionLost(status) disconnectHandler.onConnectionLost(status)
} }
} }
@ -54,12 +55,13 @@ class BleCommCallbacks(
} }
fun waitForConnection(timeoutMs: Long): Boolean { fun waitForConnection(timeoutMs: Long): Boolean {
val latch = connected
try { try {
connected.await(timeoutMs, TimeUnit.MILLISECONDS) latch.await(timeoutMs, TimeUnit.MILLISECONDS)
} catch (e: InterruptedException) { } catch (e: InterruptedException) {
aapsLogger.warn(LTag.PUMPBTCOMM, "Interrupted while waiting for Connection") aapsLogger.warn(LTag.PUMPBTCOMM, "Interrupted while waiting for Connection")
} }
return connected.count == 0L return latch.count == 0L
} }
fun startServiceDiscovery() { fun startServiceDiscovery() {
@ -67,12 +69,13 @@ class BleCommCallbacks(
} }
fun waitForServiceDiscovery(timeoutMs: Long): Boolean { fun waitForServiceDiscovery(timeoutMs: Long): Boolean {
val latch = serviceDiscoveryComplete
try { try {
serviceDiscoveryComplete.await(timeoutMs, TimeUnit.MILLISECONDS) latch.await(timeoutMs, TimeUnit.MILLISECONDS)
} catch (e: InterruptedException) { } catch (e: InterruptedException) {
aapsLogger.warn(LTag.PUMPBTCOMM, "Interrupted while waiting for ServiceDiscovery") aapsLogger.warn(LTag.PUMPBTCOMM, "Interrupted while waiting for ServiceDiscovery")
} }
return serviceDiscoveryComplete.count == 0L return latch.count == 0L
} }
fun confirmWrite(expectedPayload: ByteArray, expectedUUID: String, timeoutMs: Long): WriteConfirmation { fun confirmWrite(expectedPayload: ByteArray, expectedUUID: String, timeoutMs: Long): WriteConfirmation {
@ -208,6 +211,8 @@ class BleCommCallbacks(
fun resetConnection() { fun resetConnection() {
aapsLogger.debug(LTag.PUMPBTCOMM, "Reset connection") aapsLogger.debug(LTag.PUMPBTCOMM, "Reset connection")
connected?.countDown()
serviceDiscoveryComplete?.countDown()
connected = CountDownLatch(1) connected = CountDownLatch(1)
serviceDiscoveryComplete = CountDownLatch(1) serviceDiscoveryComplete = CountDownLatch(1)
flushConfirmationQueue() flushConfirmationQueue()

View file

@ -63,7 +63,7 @@ class Connection(
var msgIO: MessageIO? = null var msgIO: MessageIO? = null
fun connect(connectionWaitCond: ConnectionWaitCondition) { fun connect(connectionWaitCond: ConnectionWaitCondition) {
aapsLogger.debug("Connecting") aapsLogger.debug("Connecting connectionWaitCond=$connectionWaitCond")
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTING podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTING
val autoConnect = false val autoConnect = false
@ -115,11 +115,15 @@ class Connection(
dataBleIO.readyToRead() dataBleIO.readyToRead()
} }
fun disconnect() { fun disconnect(closeGatt: Boolean) {
aapsLogger.debug(LTag.PUMPBTCOMM, "Disconnecting") aapsLogger.debug(LTag.PUMPBTCOMM, "Disconnecting closeGatt=$closeGatt")
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED
if (closeGatt) {
gattConnection?.disconnect() gattConnection?.close()
gattConnection = null
} else {
gattConnection?.disconnect()
}
bleCommCallbacks.resetConnection() bleCommCallbacks.resetConnection()
session = null session = null
msgIO = null msgIO = null
@ -186,7 +190,7 @@ class Connection(
// This will be called from a different thread !!! // This will be called from a different thread !!!
override fun onConnectionLost(status: Int) { override fun onConnectionLost(status: Int) {
aapsLogger.info(LTag.PUMPBTCOMM, "Lost connection with status: $status") aapsLogger.info(LTag.PUMPBTCOMM, "Lost connection with status: $status")
disconnect() disconnect(false)
} }
companion object { companion object {