Merge pull request #3091 from Jackenmen/auto_add_medtronic_battery_change_to_careportal

Auto-add Medtronic battery changes to careportal
This commit is contained in:
Milos Kozak 2023-12-01 16:51:58 +01:00 committed by GitHub
commit 0ec3f4eae8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 1 deletions

View file

@ -297,7 +297,9 @@ class MedtronicPumpHistoryDecoder @Inject constructor(
}
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 {

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
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)))
@ -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) {
val lastPrimeFromAAPS = sp.getLong(eventSP, 0L)
if (historyRecord.atechDateTime != lastPrimeFromAAPS) {

View file

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