combov2: Autodetect reservoir and battery changes and insert them as events

Signed-off-by: Carlos Rafael Giani <crg7475@mailbox.org>
This commit is contained in:
Carlos Rafael Giani 2022-11-20 21:04:53 +01:00
parent 8bef914eac
commit 5aeb401cc0
3 changed files with 60 additions and 9 deletions

View file

@ -730,22 +730,57 @@ class ComboV2Plugin @Inject constructor (
// levels are shown on the UI even when the driver connects to the
// pump again and resets the current pump state.
override var reservoirLevel: Double = 0.0
private set
override var batteryLevel: Int = 0
private set
private var _reservoirLevel: Double? = null
override val reservoirLevel: Double
get() = _reservoirLevel ?: 0.0
private var _batteryLevel: Int? = null
override val batteryLevel: Int
get() = _batteryLevel ?: 0
private fun updateLevels() {
pumpStatus?.availableUnitsInReservoir?.let {
reservoirLevel = it.toDouble()
pumpStatus?.availableUnitsInReservoir?.let { newLevel ->
_reservoirLevel?.let { currentLevel ->
aapsLogger.debug(LTag.PUMP, "Current/new reservoir levels: $currentLevel / $newLevel")
if (sp.getBoolean(R.string.key_combov2_automatic_reservoir_entry, true) && (newLevel > currentLevel)) {
aapsLogger.debug(LTag.PUMP, "Auto-inserting reservoir change therapy event")
pumpSync.insertTherapyEventIfNewWithTimestamp(
timestamp = System.currentTimeMillis(),
type = DetailedBolusInfo.EventType.INSULIN_CHANGE,
note = rh.gs(R.string.combov2_note_reservoir_change),
pumpId = null,
pumpType = PumpType.ACCU_CHEK_COMBO,
pumpSerial = serialNumber()
)
}
}
_reservoirLevel = newLevel.toDouble()
}
pumpStatus?.batteryState?.let {
batteryLevel = when (it) {
pumpStatus?.batteryState?.let { newState ->
val newLevel = when (newState) {
BatteryState.NO_BATTERY -> 5
BatteryState.LOW_BATTERY -> 25
BatteryState.FULL_BATTERY -> 100
}
_batteryLevel?.let { currentLevel ->
aapsLogger.debug(LTag.PUMP, "Current/new battery levels: $currentLevel / $newLevel")
if (sp.getBoolean(R.string.key_combov2_automatic_battery_entry, true) && (newLevel > currentLevel)) {
aapsLogger.debug(LTag.PUMP, "Auto-inserting battery change therapy event")
pumpSync.insertTherapyEventIfNewWithTimestamp(
timestamp = System.currentTimeMillis(),
type = DetailedBolusInfo.EventType.PUMP_BATTERY_CHANGE,
note = rh.gs(R.string.combov2_note_battery_change),
pumpId = null,
pumpType = PumpType.ACCU_CHEK_COMBO,
pumpSerial = serialNumber()
)
}
}
_batteryLevel = newLevel
}
}
@ -1134,7 +1169,7 @@ class ComboV2Plugin @Inject constructor (
get() = _pumpDescription
override fun shortStatus(veryShort: Boolean): String {
var lines = mutableListOf<String>()
val lines = mutableListOf<String>()
if (lastConnectionTimestamp != 0L) {
val agoMsec: Long = System.currentTimeMillis() - lastConnectionTimestamp

View file

@ -123,4 +123,10 @@ buttons at the same time to cancel pairing)\n
<string name="combov2_short_status_battery_state_low">low</string>
<string name="combov2_short_status_battery_state_full">full</string>
<string name="combov2_short_status_battery_state">Batt: %s</string>
<string name="key_combov2_automatic_reservoir_entry" translatable="false">combov2_automatic_reservoir_entry</string>
<string name="key_combov2_automatic_battery_entry" translatable="false">combov2_automatic_battery_entry</string>
<string name="combov2_automatic_reservoir_entry">Autodetect and automatically enter insulin reservoir change</string>
<string name="combov2_automatic_battery_entry">Autodetect and automatically enter battery change</string>
<string name="combov2_note_reservoir_change">Insulin reservoir change inserted automatically by combov2 driver</string>
<string name="combov2_note_battery_change">Battery change inserted automatically by combov2 driver</string>
</resources>

View file

@ -31,6 +31,16 @@
android:inputType="number"
android:defaultValue="300" />
<SwitchPreference
android:defaultValue="true"
android:key="@string/key_combov2_automatic_reservoir_entry"
android:title="@string/combov2_automatic_reservoir_entry" />
<SwitchPreference
android:defaultValue="true"
android:key="@string/key_combov2_automatic_battery_entry"
android:title="@string/combov2_automatic_battery_entry" />
<SwitchPreference
android:defaultValue="false"
android:key="@string/key_combov2_verbose_logging"