From 5a6d5b41d4ef55c84d3ca9ad95f7d018965dfd41 Mon Sep 17 00:00:00 2001 From: jbr7rr <> Date: Thu, 25 May 2023 20:14:51 +0200 Subject: [PATCH] Added some stuff to the overview --- .../nightscout/pump/medtrum/MedtrumPump.kt | 35 +++++- .../pump/medtrum/services/MedtrumService.kt | 2 +- .../ui/viewmodel/MedtrumOverviewViewModel.kt | 30 +++++ .../res/layout/fragment_medtrum_overview.xml | 117 ++++++++++++++++++ pump/medtrum/src/main/res/values/strings.xml | 6 +- 5 files changed, 182 insertions(+), 8 deletions(-) diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt index 0feda438df..0c8822c192 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt @@ -75,6 +75,14 @@ class MedtrumPump @Inject constructor( set(value) { _lastBasalRate.value = value } + + private val _reservoir = MutableStateFlow(0.0) + val reservoirFlow: StateFlow = _reservoir + var reservoir: Double + get() = _reservoir.value + set(value) { + _reservoir.value = value + } /** Stuff stored in SP */ private var _patchSessionToken = 0L @@ -118,12 +126,13 @@ class MedtrumPump @Inject constructor( sp.putString(R.string.key_actual_basal_profile, encodedString) } - private var _lastBasalType: BasalType = BasalType.NONE + private var _lastBasalType: MutableStateFlow = MutableStateFlow(BasalType.NONE) + val lastBasalTypeFlow: StateFlow = _lastBasalType var lastBasalType: BasalType - get() = _lastBasalType + get() = _lastBasalType.value set(value) { - _lastBasalType = value - sp.putInt(R.string.key_last_basal_type, value.ordinal) + _lastBasalType.value = value + sp.putInt(R.string.key_last_basal_type, value.ordinal) // TODO is this still needed in SP? } private var _pumpSN = 0L @@ -137,7 +146,6 @@ class MedtrumPump @Inject constructor( var patchStartTime = 0L // Time in seconds! var patchAge = 0L // Time in seconds! - var reservoir = 0.0 var batteryVoltage_A = 0.0 var batteryVoltage_B = 0.0 @@ -187,7 +195,7 @@ class MedtrumPump @Inject constructor( _currentSequenceNumber = sp.getInt(R.string.key_current_sequence_number, 0) _patchId = sp.getLong(R.string.key_patch_id, 0L) _syncedSequenceNumber = sp.getInt(R.string.key_synced_sequence_number, 0) - _lastBasalType = enumValues()[sp.getInt(R.string.key_last_basal_type, 0)] + lastBasalType = enumValues()[sp.getInt(R.string.key_last_basal_type, 0)] // TODO: is this nice? val encodedString = sp.getString(R.string.key_actual_basal_profile, "0") try { @@ -339,6 +347,21 @@ class MedtrumPump @Inject constructor( LTag.PUMPCOMM, "handleBasalStatusUpdate: ${if (newRecord) "**NEW** " else ""}EVENT TEMP_END ($basalType) ${dateUtil.dateAndTimeString(basalStartTime)} ($basalStartTime)" ) + } else if (basalType == BasalType.NONE && expectedTemporaryBasal?.pumpId != basalStartTime) { // Also some sort of suspend or unkown by pump + val newRecord = pumpSync.syncTemporaryBasalWithPumpId( + timestamp = basalStartTime, + rate = basalRate, + duration = T.mins(4800).msecs(), // TODO MAGIC NUMBER + isAbsolute = true, + type = PumpSync.TemporaryBasalType.PUMP_SUSPEND, + pumpId = basalStartTime, + pumpType = pumpType, + pumpSerial = pumpSN.toString(radix = 16) + ) + aapsLogger.debug( + LTag.PUMPCOMM, + "handleBasalStatusUpdate: ${if (newRecord) "**NEW** " else ""}EVENT TEMP_END ($basalType) ${dateUtil.dateAndTimeString(basalStartTime)} ($basalStartTime)" + ) } // Update medtrum pump state diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt index 8ab48a512e..907cbeadec 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt @@ -191,7 +191,7 @@ class MedtrumService : DaggerService(), BLECommCallback { val bolusDurationInMSec = (insulin * 60 * 1000) val expectedEnd = bolusStart + bolusDurationInMSec + 2000 - while (System.currentTimeMillis() < expectedEnd) { + while (System.currentTimeMillis() < expectedEnd && result == true) { SystemClock.sleep(1000) } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt index 26f1a57c15..51eb6503d9 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt @@ -51,10 +51,22 @@ class MedtrumOverviewViewModel @Inject constructor( val isPatchActivated: LiveData get() = _isPatchActivated + private val _pumpState = SingleLiveEvent() + val pumpState: LiveData + get() = _pumpState + + private val _basalType = SingleLiveEvent() + val basalType: LiveData + get() = _basalType + private val _runningBasalRate = SingleLiveEvent() val runningBasalRate: LiveData get() = _runningBasalRate + private val _reservoir = SingleLiveEvent() + val reservoir: LiveData + get() = _reservoir + init { scope.launch { medtrumPump.connectionStateFlow.collect { state -> @@ -90,6 +102,24 @@ class MedtrumOverviewViewModel @Inject constructor( _runningBasalRate.postValue(String.format(rh.gs(R.string.current_basal_rate), rate)) } } + scope.launch { + medtrumPump.pumpStateFlow.collect { state -> + aapsLogger.debug(LTag.PUMP, "MedtrumViewModel pumpStateFlow: $state") + _pumpState.postValue(state.toString()) + } + } + scope.launch { + medtrumPump.reservoirFlow.collect { reservoir -> + aapsLogger.debug(LTag.PUMP, "MedtrumViewModel reservoirFlow: $reservoir") + _reservoir.postValue(String.format(rh.gs(R.string.reservoir_level), reservoir)) + } + } + scope.launch { + medtrumPump.lastBasalTypeFlow.collect { basalType -> + aapsLogger.debug(LTag.PUMP, "MedtrumViewModel basalTypeFlow: $basalType") + _basalType.postValue(basalType.toString()) + } + } } override fun onCleared() { diff --git a/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml b/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml index cc629b9ff1..398c351044 100644 --- a/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml +++ b/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml @@ -100,6 +100,84 @@ android:layout_marginBottom="5dp" android:background="@color/list_delimiter" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pump/medtrum/src/main/res/values/strings.xml b/pump/medtrum/src/main/res/values/strings.xml index 1388653576..ef2b5de75e 100644 --- a/pump/medtrum/src/main/res/values/strings.xml +++ b/pump/medtrum/src/main/res/values/strings.xml @@ -18,7 +18,11 @@ BLE Status - Active basal + Pump state + Reservoir + %.2f U + Basal type + Basal rate %.2f U/h Start new patch Stop patch