From 91957930ed3d9b1684a35c2369613a173bec00a5 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Sun, 30 May 2021 22:49:35 +0200 Subject: [PATCH] display bluetooth connection status. display deliveryStatus. display lastConnection --- .../omnipod/dash/OmnipodDashPumpPlugin.kt | 4 +- .../dash/driver/comm/OmnipodDashBleManager.kt | 4 +- .../driver/comm/OmnipodDashBleManagerImpl.kt | 19 ++--- .../dash/driver/comm/session/Connection.kt | 17 +++- .../driver/comm/status/ConnectionStatus.kt | 10 --- .../pod/state/OmnipodDashPodStateManager.kt | 5 ++ .../state/OmnipodDashPodStateManagerImpl.kt | 11 ++- .../dash/ui/OmnipodDashOverviewFragment.kt | 44 +++++++---- ...omnipod_dash_overview_bluetooth_status.xml | 78 +++++++++---------- 9 files changed, 109 insertions(+), 83 deletions(-) delete mode 100644 omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/status/ConnectionStatus.kt 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 d4013821dd..721907f831 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 @@ -407,8 +407,8 @@ class OmnipodDashPumpPlugin @Inject constructor( } else -> - throw IllegalArgumentException( - "Don't know how to sync confirmed command of type: $historyEntry and " + + aapsLogger.warn(LTag.PUMP, + "Will not sync confirmed command of type: $historyEntry and " + "succes: ${confirmation.success}" ) } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManager.kt index f3708c8031..b2400713c7 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManager.kt @@ -1,6 +1,6 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.status.ConnectionStatus +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.session.ConnectionState import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.event.PodEvent import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.Command import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.Response @@ -11,7 +11,7 @@ interface OmnipodDashBleManager { fun sendCommand(cmd: Command, responseType: KClass): Observable - fun getStatus(): ConnectionStatus + fun getStatus(): ConnectionState fun connect(): Observable diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt index 0b9eee3a52..4637d52b5c 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt @@ -11,7 +11,6 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptio import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.pair.LTKExchanger import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.scan.PodScanner import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.session.* -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.status.ConnectionStatus import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.event.PodEvent import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.Command import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.Response @@ -34,7 +33,6 @@ class OmnipodDashBleManagerImpl @Inject constructor( context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager private val bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter private var connection: Connection? = null - private var status: ConnectionStatus = ConnectionStatus.IDLE private val ids = Ids(podState) override fun sendCommand(cmd: Command, responseType: KClass): Observable = @@ -85,13 +83,9 @@ class OmnipodDashBleManagerImpl @Inject constructor( ?: throw NotConnectedException("Missing session") } - override fun getStatus(): ConnectionStatus { - // TODO is this used? - var s: ConnectionStatus - synchronized(status) { - s = status - } - return s + override fun getStatus(): ConnectionState { + return connection?.let{ getStatus() } + ?: NotConnected } override fun connect(): Observable = Observable.create { emitter -> @@ -106,9 +100,10 @@ class OmnipodDashBleManagerImpl @Inject constructor( ?: throw FailedToConnectException("Missing bluetoothAddress, activate the pod first") val podDevice = bluetoothAdapter.getRemoteDevice(podAddress) val conn = connection - ?: Connection(podDevice, aapsLogger, context) + ?: Connection(podDevice, aapsLogger, context, podState) connection = conn if (conn.connectionState() is Connected) { + podState.lastConnection = System.currentTimeMillis() if (conn.session == null) { emitter.onNext(PodEvent.EstablishingSession) establishSession(1.toByte()) @@ -121,7 +116,7 @@ class OmnipodDashBleManagerImpl @Inject constructor( } conn.connect() emitter.onNext(PodEvent.BluetoothConnected(podAddress)) - + podState.lastConnection = System.currentTimeMillis() emitter.onNext(PodEvent.EstablishingSession) establishSession(1.toByte()) emitter.onNext(PodEvent.Connected) @@ -190,7 +185,7 @@ class OmnipodDashBleManagerImpl @Inject constructor( emitter.onNext(PodEvent.BluetoothConnecting) val podDevice = bluetoothAdapter.getRemoteDevice(podAddress) - val conn = Connection(podDevice, aapsLogger, context) + val conn = Connection(podDevice, aapsLogger, context, podState) connection = conn emitter.onNext(PodEvent.BluetoothConnected(podAddress)) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt index 104b7fc3a2..71bccf91b6 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt @@ -20,6 +20,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.io.CmdBl import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.io.DataBleIO import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.io.IncomingPackets import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageIO +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager sealed class ConnectionState @@ -29,7 +30,8 @@ object NotConnected : ConnectionState() class Connection( private val podDevice: BluetoothDevice, private val aapsLogger: AAPSLogger, - context: Context + context: Context, + private val podState: OmnipodDashPodStateManager ) : DisconnectHandler { private val incomingPackets = IncomingPackets() @@ -50,13 +52,15 @@ class Connection( aapsLogger.debug(LTag.PUMPBTCOMM, "Connecting to ${podDevice.address}") val autoConnect = false - + podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTING gattConnection = podDevice.connectGatt(context, autoConnect, bleCommCallbacks, BluetoothDevice.TRANSPORT_LE) // OnDisconnect can be called after this point!!! val state = waitForConnection() if (state !is Connected) { + podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED throw FailedToConnectException(podDevice.address) } + podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTED val discoverer = ServiceDiscoverer(aapsLogger, gattConnection, bleCommCallbacks) val discoveredCharacteristics = discoverer.discoverServices() cmdBleIO = CmdBleIO( @@ -90,13 +94,18 @@ class Connection( disconnect() } aapsLogger.debug("Connecting") + podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTING + if (!gattConnection.connect()) { throw FailedToConnectException("connect() returned false") } - if (waitForConnection() is NotConnected) { + if (waitForConnection() !is Connected) { + podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED throw FailedToConnectException(podDevice.address) } + podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTED + val discoverer = ServiceDiscoverer(aapsLogger, gattConnection, bleCommCallbacks) val discovered = discoverer.discoverServices() @@ -110,6 +119,8 @@ class Connection( fun disconnect() { aapsLogger.debug(LTag.PUMPBTCOMM, "Disconnecting") + podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED + gattConnection.disconnect() bleCommCallbacks.resetConnection() session = null diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/status/ConnectionStatus.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/status/ConnectionStatus.kt deleted file mode 100644 index 6977f342f3..0000000000 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/status/ConnectionStatus.kt +++ /dev/null @@ -1,10 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.status - -enum class ConnectionStatus { - IDLE, - BUSY, - CONNECTING, - ESTABLISHING_SESSION, - PAIRING, - RUNNING_COMMAND; -} diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt index 2d57a6a9ec..a1030d61eb 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt @@ -22,6 +22,7 @@ interface OmnipodDashPodStateManager { val isSuspended: Boolean val isPodRunning: Boolean var lastConnection: Long + var bluetoothConnectionState: BluetoothConnectionState val lastUpdatedSystem: Long // System.currentTimeMillis() val lastStatusResponseReceived: Long @@ -79,4 +80,8 @@ interface OmnipodDashPodStateManager { ) // TODO: set created to "now" on boot data class TempBasal(val startTime: Long, val rate: Double, val durationInMinutes: Short) : Serializable + + enum class BluetoothConnectionState { + CONNECTING, CONNECTED, DISCONNECTED + } } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt index af2ef287e8..f00b0d1dae 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt @@ -160,6 +160,14 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( override val lastStatusResponseReceived: Long get() = podState.lastStatusResponseReceived + override var bluetoothConnectionState: OmnipodDashPodStateManager.BluetoothConnectionState + get() = podState.bluetoothConnectionState + set(bluetoothConnectionState) { + podState.bluetoothConnectionState = bluetoothConnectionState + rxBus.send(EventOmnipodDashPumpValuesChanged()) + // do not store + } + override fun increaseMessageSequenceNumber() { podState.messageSequenceNumber = ((podState.messageSequenceNumber.toInt() + 1) and 0x0f).toShort() store() @@ -380,7 +388,8 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( var lastConnection: Long = 0 var lastUpdatedSystem: Long = 0 var lastStatusResponseReceived: Long = 0 - + var bluetoothConnectionState: OmnipodDashPodStateManager.BluetoothConnectionState = + OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED var messageSequenceNumber: Short = 0 var sequenceNumberOfLastProgrammingCommand: Short? = null var activationTime: Long? = null diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt index 95fdf5b155..3fe453d235 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt @@ -13,6 +13,7 @@ import info.nightscout.androidaps.Constants import info.nightscout.androidaps.activities.ErrorHelperActivity import info.nightscout.androidaps.events.EventPreferenceChange import info.nightscout.androidaps.interfaces.CommandQueueProvider +import info.nightscout.androidaps.interfaces.PumpSync import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification import info.nightscout.androidaps.plugins.general.overview.notifications.Notification @@ -41,6 +42,7 @@ import info.nightscout.androidaps.utils.ui.UIRunnable import io.reactivex.disposables.CompositeDisposable import io.reactivex.rxkotlin.plusAssign import org.apache.commons.lang3.StringUtils +import org.joda.time.DateTime import org.joda.time.Duration import java.util.* import javax.inject.Inject @@ -58,6 +60,8 @@ class OmnipodDashOverviewFragment : DaggerFragment() { @Inject lateinit var sp: SP @Inject lateinit var dateUtil: DateUtil @Inject lateinit var aapsSchedulers: AapsSchedulers + @Inject lateinit var pumpSync: PumpSync + companion object { @@ -211,11 +215,26 @@ class OmnipodDashOverviewFragment : DaggerFragment() { private fun updateUi() { // TODO update bluetooth status + updateBluetoothStatus() updateOmnipodStatus() updatePodActionButtons() updateQueueStatus() } + private fun updateBluetoothStatus(){ + bluetoothStatusBinding.omnipodDashBluetoothAddress.text = podStateManager.bluetoothAddress + ?: PLACEHOLDER + bluetoothStatusBinding.omnipodDashBluetoothStatus.text = + when (podStateManager.bluetoothConnectionState) { + OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTED -> + "{fa-bluetooth}" + OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED -> + "{fa-bluetooth-b}" + OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTING -> + "{fa-bluetooth-b spin}" + } + } + private fun updateOmnipodStatus() { updateLastConnection() updateLastBolus() @@ -246,10 +265,9 @@ class OmnipodDashOverviewFragment : DaggerFragment() { podStateManager.firmwareVersion.toString(), podStateManager.bluetoothVersion.toString() ) - + podInfoBinding.timeOnPod.text = podStateManager.minutesSinceActivation.toString() + " minutes" // TODO /* - podInfoBinding.timeOnPod.text = readableZonedTime(podStateManager.time) podInfoBinding.timeOnPod.setTextColor(if (podStateManager.timeDeviatesMoreThan(OmnipodConstants.TIME_DEVIATION_THRESHOLD)) { Color.RED } else { @@ -373,7 +391,8 @@ class OmnipodDashOverviewFragment : DaggerFragment() { if (podStateManager.isSuspended) { resourceHelper.gs(R.string.omnipod_common_pod_status_suspended) } else { - resourceHelper.gs(R.string.omnipod_common_pod_status_running) + resourceHelper.gs(R.string.omnipod_common_pod_status_running) + + podStateManager.deliveryStatus?.let{ " " + podStateManager.deliveryStatus.toString() } } // TODO /* @@ -421,10 +440,11 @@ class OmnipodDashOverviewFragment : DaggerFragment() { } private fun updateTempBasal() { - if (podStateManager.isActivationCompleted && podStateManager.tempBasalActive) { - val startTime = podStateManager.tempBasal!!.startTime - val rate = podStateManager.tempBasal!!.rate - val duration = podStateManager.tempBasal!!.durationInMinutes + val tempBasal = podStateManager.tempBasal + if (podStateManager.isActivationCompleted && podStateManager.tempBasalActive && tempBasal != null) { + val startTime = tempBasal.startTime + val rate = tempBasal.rate + val duration = tempBasal.durationInMinutes val minutesRunning = 0 // TODO @@ -560,12 +580,8 @@ class OmnipodDashOverviewFragment : DaggerFragment() { } */ - private fun readableDuration(time: Long): String { - // TODO - return "TODO" - - /* - val duration = Duration(dateTime, DateTime.now()) + private fun readableDuration(dateTime: Long): String { + val duration = Duration(dateTime, System.currentTimeMillis()) val hours = duration.standardHours.toInt() val minutes = duration.standardMinutes.toInt() val seconds = duration.standardSeconds.toInt() @@ -599,7 +615,7 @@ class OmnipodDashOverviewFragment : DaggerFragment() { return resourceHelper.gs(R.string.omnipod_common_time_ago, resourceHelper.gq(R.plurals.omnipod_common_days, days, days)) } } - */ + } private fun isQueueEmpty(): Boolean { diff --git a/omnipod-dash/src/main/res/layout/omnipod_dash_overview_bluetooth_status.xml b/omnipod-dash/src/main/res/layout/omnipod_dash_overview_bluetooth_status.xml index d9d95b3a60..8d7ac5e903 100644 --- a/omnipod-dash/src/main/res/layout/omnipod_dash_overview_bluetooth_status.xml +++ b/omnipod-dash/src/main/res/layout/omnipod_dash_overview_bluetooth_status.xml @@ -1,44 +1,6 @@ - - - - - - - - - - - + + + + + + + + +