Added some stuff to the overview

This commit is contained in:
jbr7rr 2023-05-25 20:14:51 +02:00
parent 404365aed2
commit 5a6d5b41d4
5 changed files with 182 additions and 8 deletions

View file

@ -75,6 +75,14 @@ class MedtrumPump @Inject constructor(
set(value) {
_lastBasalRate.value = value
}
private val _reservoir = MutableStateFlow(0.0)
val reservoirFlow: StateFlow<Double> = _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<BasalType> = MutableStateFlow(BasalType.NONE)
val lastBasalTypeFlow: StateFlow<BasalType> = _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<BasalType>()[sp.getInt(R.string.key_last_basal_type, 0)]
lastBasalType = enumValues<BasalType>()[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

View file

@ -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)
}

View file

@ -51,10 +51,22 @@ class MedtrumOverviewViewModel @Inject constructor(
val isPatchActivated: LiveData<Boolean>
get() = _isPatchActivated
private val _pumpState = SingleLiveEvent<String>()
val pumpState: LiveData<String>
get() = _pumpState
private val _basalType = SingleLiveEvent<String>()
val basalType: LiveData<String>
get() = _basalType
private val _runningBasalRate = SingleLiveEvent<String>()
val runningBasalRate: LiveData<String>
get() = _runningBasalRate
private val _reservoir = SingleLiveEvent<String>()
val reservoir: LiveData<String>
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() {

View file

@ -100,6 +100,84 @@
android:layout_marginBottom="5dp"
android:background="@color/list_delimiter" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="3dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:gravity="end"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:text="@string/pump_state_label"
android:textSize="16sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:text=":"
android:textSize="16sp" />
<TextView
android:id="@+id/pump_state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:text="@{viewmodel.pumpState}"
android:textColor="@android:color/white"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="3dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:gravity="end"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:text="@string/current_basal_type_label"
android:textSize="16sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:text=":"
android:textSize="16sp" />
<TextView
android:id="@+id/current_basal_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:text="@{viewmodel.basalType}"
android:textColor="@android:color/white"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -139,6 +217,45 @@
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="3dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:gravity="end"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:text="@string/reservoir_label"
android:textSize="16sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:text=":"
android:textSize="16sp" />
<TextView
android:id="@+id/reservoir_level"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:text="@{viewmodel.reservoir}"
android:textColor="@android:color/white"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>

View file

@ -18,7 +18,11 @@
<!-- overview fragment -->
<string name="medtrum_ble_status">BLE Status</string>
<string name="current_basal_label">Active basal</string>
<string name="pump_state_label">Pump state</string>
<string name="reservoir_label"> Reservoir</string>
<string name="reservoir_level"> %.2f U</string>
<string name="current_basal_type_label">Basal type</string>
<string name="current_basal_label">Basal rate</string>
<string name="current_basal_rate"> %.2f U/h</string>
<string name="string_new_patch">Start new patch</string>
<string name="string_stop_patch">Stop patch</string>