diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt index 7c9059c558..80508a90e7 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt @@ -156,12 +156,13 @@ class OmnipodDashPumpPlugin @Inject constructor( override fun isConnected(): Boolean { - return true + return !podStateManager.isPodRunning || + podStateManager.bluetoothConnectionState == OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTED } override fun isConnecting(): Boolean { // TODO - return false + return stopConnecting != null } override fun isHandshakeInProgress(): Boolean { @@ -182,7 +183,8 @@ class OmnipodDashPumpPlugin @Inject constructor( try { val stop = CountDownLatch(1) stopConnecting = stop - omnipodManager.connect(stop) + val error = omnipodManager.connect(stop).ignoreElements().blockingGet() + aapsLogger.info(LTag.PUMPCOMM, "connect error=$error") } finally { stopConnecting = null } @@ -190,6 +192,7 @@ class OmnipodDashPumpPlugin @Inject constructor( } override fun disconnect(reason: String) { + stopConnecting?.let { it.countDown() } omnipodManager.disconnect() } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManager.kt index a905016f99..24b09dfc79 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManager.kt @@ -42,5 +42,6 @@ interface OmnipodDashManager { fun deactivatePod(): Observable fun disconnect() - fun connect(stop: CountDownLatch) + + fun connect(stop: CountDownLatch): Observable } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt index 487ecf2c6c..e504f2a3b7 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt @@ -15,6 +15,7 @@ import io.reactivex.Observable import io.reactivex.functions.Action import io.reactivex.functions.Consumer import java.util.* +import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit import javax.inject.Inject import javax.inject.Singleton @@ -74,6 +75,25 @@ class OmnipodDashManagerImpl @Inject constructor( } } + override fun disconnect() { + bleManager.disconnect() + } + + override fun connect(stop: CountDownLatch): Observable { + return observeConnectToPodWithStop(stop) + // TODO these would be common for any observable returned in a public function in this class + .doOnNext(PodEventInterceptor()) + .doOnError(ErrorInterceptor()) + .subscribeOn(aapsSchedulers.io) + } + + private fun observeConnectToPodWithStop(stop: CountDownLatch): Observable { + return Observable.defer { + bleManager.connect(stop) + .doOnError { throwable -> logger.warn(LTag.PUMPBTCOMM, "observeConnectToPodWithStop error=$throwable") } + } + } + private val observeConnectToPod: Observable get() = Observable.defer { bleManager.connect()