Auto-add battery changes to careportal

This commit is contained in:
Jakub Kuczys 2023-11-15 04:56:13 +01:00
parent 56937dc704
commit 52335bc7da
No known key found for this signature in database
GPG key ID: 9F02686F15FCBCD3
3 changed files with 45 additions and 1 deletions

View file

@ -297,7 +297,9 @@ class MedtronicPumpHistoryDecoder @Inject constructor(
} }
private fun decodeBatteryActivity(entry: PumpHistoryEntry) { private fun decodeBatteryActivity(entry: PumpHistoryEntry) {
entry.displayableValue = if (entry.head[0] == 0.toByte()) "Battery Removed" else "Battery Replaced" val isRemoved = entry.head[0] == 0.toByte()
entry.addDecodedData("isRemoved", isRemoved)
entry.displayableValue = if (isRemoved) "Battery Removed" else "Battery Replaced"
} }
private fun decodeBasalProfileStart(entry: PumpHistoryEntry): RecordDecodeStatus { private fun decodeBasalProfileStart(entry: PumpHistoryEntry): RecordDecodeStatus {

View file

@ -347,6 +347,18 @@ class MedtronicHistoryData @Inject constructor(
} }
} }
// BatteryChange
val batteryChangeRecords: MutableList<PumpHistoryEntry> = getFilteredItems(PumpHistoryEntryType.BatteryChange)
aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "ProcessHistoryData: BatteryChange [count=%d, items=%s]", batteryChangeRecords.size, gson.toJson(batteryChangeRecords)))
if (isCollectionNotEmpty(batteryChangeRecords)) {
try {
processBatteryChange(batteryChangeRecords)
} catch (ex: Exception) {
aapsLogger.error(LTag.PUMP, "ProcessHistoryData: Error processing BatteryChange entries: " + ex.message, ex)
throw ex
}
}
// TDD // TDD
val tdds: MutableList<PumpHistoryEntry> = getFilteredItems(setOf(PumpHistoryEntryType.EndResultTotals, getTDDType())) val tdds: MutableList<PumpHistoryEntry> = getFilteredItems(setOf(PumpHistoryEntryType.EndResultTotals, getTDDType()))
aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "ProcessHistoryData: TDD [count=%d, items=%s]", tdds.size, gson.toJson(tdds))) aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "ProcessHistoryData: TDD [count=%d, items=%s]", tdds.size, gson.toJson(tdds)))
@ -456,6 +468,35 @@ class MedtronicHistoryData @Inject constructor(
} }
} }
private fun processBatteryChange(batteryChangeRecords: List<PumpHistoryEntry>) {
val maxAllowedTimeInPast = DateTimeUtil.getATDWithAddedMinutes(GregorianCalendar(), -120)
var lastBatteryChangeRecordTime = 0L
var lastBatteryChangeRecord: PumpHistoryEntry? = null
for (batteryChangeRecord in batteryChangeRecords) {
val isRemoved = batteryChangeRecord.getDecodedDataEntry("isRemoved")
if (isRemoved != null && isRemoved as Boolean)
{
// we're interested in battery replacements, not battery removals
continue
}
if (batteryChangeRecord.atechDateTime > maxAllowedTimeInPast) {
if (lastBatteryChangeRecordTime < batteryChangeRecord.atechDateTime) {
lastBatteryChangeRecordTime = batteryChangeRecord.atechDateTime
lastBatteryChangeRecord = batteryChangeRecord
}
}
}
if (lastBatteryChangeRecord != null) {
uploadCareportalEventIfFoundInHistory(
lastBatteryChangeRecord,
MedtronicConst.Statistics.LastBatteryChange,
DetailedBolusInfo.EventType.PUMP_BATTERY_CHANGE
)
}
}
private fun uploadCareportalEventIfFoundInHistory(historyRecord: PumpHistoryEntry, eventSP: String, eventType: DetailedBolusInfo.EventType) { private fun uploadCareportalEventIfFoundInHistory(historyRecord: PumpHistoryEntry, eventSP: String, eventType: DetailedBolusInfo.EventType) {
val lastPrimeFromAAPS = sp.getLong(eventSP, 0L) val lastPrimeFromAAPS = sp.getLong(eventSP, 0L)
if (historyRecord.atechDateTime != lastPrimeFromAAPS) { if (historyRecord.atechDateTime != lastPrimeFromAAPS) {

View file

@ -30,5 +30,6 @@ object MedtronicConst {
const val LastPumpHistoryEntry = StatsPrefix + "pump_history_entry" const val LastPumpHistoryEntry = StatsPrefix + "pump_history_entry"
const val LastPrime = StatsPrefix + "last_sent_prime" const val LastPrime = StatsPrefix + "last_sent_prime"
const val LastRewind = StatsPrefix + "last_sent_rewind" const val LastRewind = StatsPrefix + "last_sent_rewind"
const val LastBatteryChange = StatsPrefix + "last_sent_battery_change"
} }
} }