- startted adding 35 minut - before reading functionality

- some refactoring
This commit is contained in:
Andy Rozman 2021-05-02 13:45:56 +01:00
parent 67a2c0dca3
commit 49f345d425
7 changed files with 81 additions and 51 deletions

View file

@ -466,7 +466,7 @@ class MedtronicPumpPlugin @Inject constructor(
} }
private val basalProfiles: Unit private val basalProfiles: Unit
private get() { get() {
val medtronicUITask = rileyLinkMedtronicService!!.medtronicUIComm.executeCommand(MedtronicCommandType.GetBasalProfileSTD) val medtronicUITask = rileyLinkMedtronicService!!.medtronicUIComm.executeCommand(MedtronicCommandType.GetBasalProfileSTD)
if (medtronicUITask.responseType === MedtronicUIResponseType.Error) { if (medtronicUITask.responseType === MedtronicUIResponseType.Error) {
rileyLinkMedtronicService!!.medtronicUIComm.executeCommand(MedtronicCommandType.GetBasalProfileSTD) rileyLinkMedtronicService!!.medtronicUIComm.executeCommand(MedtronicCommandType.GetBasalProfileSTD)
@ -816,7 +816,7 @@ class MedtronicPumpPlugin @Inject constructor(
medtronicHistoryData.processLastBasalProfileChange(pumpDescription.pumpType, medtronicPumpStatus) medtronicHistoryData.processLastBasalProfileChange(pumpDescription.pumpType, medtronicPumpStatus)
} }
val previousState = pumpState val previousState = pumpState
if (medtronicHistoryData.isPumpSuspended) { if (medtronicHistoryData.isPumpSuspended()) {
pumpState = PumpDriverState.Suspended pumpState = PumpDriverState.Suspended
aapsLogger.debug(LTag.PUMP, logPrefix + "isPumpSuspended: true") aapsLogger.debug(LTag.PUMP, logPrefix + "isPumpSuspended: true")
} else { } else {
@ -852,20 +852,19 @@ class MedtronicPumpPlugin @Inject constructor(
lastHistoryRecordTime = lastHistoryRecordTime.minusHours(12) // we get last 12 hours of history to lastHistoryRecordTime = lastHistoryRecordTime.minusHours(12) // we get last 12 hours of history to
// determine pump state // determine pump state
// (we don't process that data), we process only // (we don't process that data), we process only
if (timeMinus36h.isAfter(lastHistoryRecordTime)) {
targetDate = timeMinus36h
}
targetDate = if (timeMinus36h.isAfter(lastHistoryRecordTime)) timeMinus36h else lastHistoryRecordTime targetDate = if (timeMinus36h.isAfter(lastHistoryRecordTime)) timeMinus36h else lastHistoryRecordTime
if (debugHistory) aapsLogger.debug(LTag.PUMP, logPrefix + "readPumpHistoryLogic(): targetDate: " + targetDate) if (debugHistory) aapsLogger.debug(LTag.PUMP, logPrefix + "readPumpHistoryLogic(): targetDate: " + targetDate)
} }
} else { // all other reads } else { // all other reads
if (debugHistory) aapsLogger.debug(LTag.PUMP, logPrefix + "readPumpHistoryLogic(): lastPumpHistoryEntry: not null - " + medtronicUtil.gsonInstance.toJson(lastPumpHistoryEntry)) if (debugHistory) aapsLogger.debug(LTag.PUMP, logPrefix + "readPumpHistoryLogic(): lastPumpHistoryEntry: not null - " + medtronicUtil.gsonInstance.toJson(lastPumpHistoryEntry))
medtronicHistoryData.setIsInInit(false) medtronicHistoryData.setIsInInit(false)
// we need to read 35 minutes in the past so that we can repair any TBR or Bolus values if neeeded
targetDate = LocalDateTime(DateTimeUtil.getMillisFromATDWithAddedMinutes(lastPumpHistoryEntry!!.atechDateTime!!, -35))
} }
//aapsLogger.debug(LTag.PUMP, "HST: Target Date: " + targetDate); //aapsLogger.debug(LTag.PUMP, "HST: Target Date: " + targetDate);
val responseTask2 = rileyLinkMedtronicService!!.medtronicUIComm.executeCommand(MedtronicCommandType.GetHistoryData, val responseTask2 = rileyLinkMedtronicService!!.medtronicUIComm.executeCommand(MedtronicCommandType.GetHistoryData,
arrayListOf(lastPumpHistoryEntry, targetDate) as ArrayList<Any>?) arrayListOf(/*lastPumpHistoryEntry*/ null, targetDate) as ArrayList<Any>?)
if (debugHistory) aapsLogger.debug(LTag.PUMP, "HST: After task") if (debugHistory) aapsLogger.debug(LTag.PUMP, "HST: After task")
val historyResult = responseTask2.result as PumpHistoryResult? val historyResult = responseTask2.result as PumpHistoryResult?
if (debugHistory) aapsLogger.debug(LTag.PUMP, "HST: History Result: " + historyResult.toString()) if (debugHistory) aapsLogger.debug(LTag.PUMP, "HST: History Result: " + historyResult.toString())
@ -949,7 +948,6 @@ class MedtronicPumpPlugin @Inject constructor(
HashMap(statusRefreshMap) HashMap(statusRefreshMap)
} }
else -> null
} }
} }

View file

@ -35,7 +35,7 @@ abstract class MedtronicHistoryDecoder<T : MedtronicHistoryEntry?> : MedtronicHi
// TODO_ extend this to also use bigger pages (for now we support only 1024 pages) // TODO_ extend this to also use bigger pages (for now we support only 1024 pages)
@Throws(RuntimeException::class) @Throws(RuntimeException::class)
private fun checkPage(page: RawHistoryPage, partial: Boolean): MutableList<Byte> { private fun checkPage(page: RawHistoryPage): MutableList<Byte> {
//val byteList: MutableList<Byte> = mutableListOf() //val byteList: MutableList<Byte> = mutableListOf()
if (medtronicUtil.medtronicPumpModel == null) { if (medtronicUtil.medtronicPumpModel == null) {
@ -51,8 +51,14 @@ abstract class MedtronicHistoryDecoder<T : MedtronicHistoryEntry?> : MedtronicHi
} }
} }
fun processPageAndCreateRecords(rawHistoryPage: RawHistoryPage): List<T> { fun processPageAndCreateRecords(rawHistoryPage: RawHistoryPage): MutableList<T> {
return processPageAndCreateRecords(rawHistoryPage, false) val dataClear = checkPage(rawHistoryPage)
val records: MutableList<T> = createRecords(dataClear)
for (record in records) {
decodeRecord(record)
}
runPostDecodeTasks()
return records
} }
protected fun prepareStatistics() { protected fun prepareStatistics() {
@ -117,13 +123,4 @@ abstract class MedtronicHistoryDecoder<T : MedtronicHistoryEntry?> : MedtronicHi
return StringUtil.getFormatedValueUS(value, decimals) return StringUtil.getFormatedValueUS(value, decimals)
} }
private fun processPageAndCreateRecords(rawHistoryPage: RawHistoryPage, partial: Boolean): MutableList<T> {
val dataClear = checkPage(rawHistoryPage, partial)
val records: MutableList<T> = createRecords(dataClear)
for (record in records) {
decodeRecord(record)
}
runPostDecodeTasks()
return records
}
} }

View file

@ -72,9 +72,10 @@ class PumpHistoryEntry : MedtronicHistoryEntry() {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
if (other !is PumpHistoryEntry) return false if (other !is PumpHistoryEntry) return false
val that = other val that = other as PumpHistoryEntry
return entryType == that.entryType && // return this.pumpId === that.pumpId
atechDateTime === that.atechDateTime // && // // return entryType == that.entryType && //
// atechDateTime === that.atechDateTime // && //
// Objects.equals(this.decodedData, that.decodedData); // Objects.equals(this.decodedData, that.decodedData);
} }
@ -111,4 +112,12 @@ class PumpHistoryEntry : MedtronicHistoryEntry() {
set(pumpId) { set(pumpId) {
super.pumpId = pumpId super.pumpId = pumpId
} }
fun hasBolusOrTBRDataChanged(entry: PumpHistoryEntry) : Boolean {
if (entryType!=null && (entryType!! == PumpHistoryEntryType.Bolus || entryType!! == PumpHistoryEntryType.TempBasalCombined)) {
return false // TODO needs to be implemented
}
return false
}
} }

View file

@ -55,6 +55,7 @@ class MedtronicUIPostprocessor @Inject constructor(
//aapsLogger.debug("D: basal profile on read: " + basalProfile); //aapsLogger.debug("D: basal profile on read: " + basalProfile);
try { try {
// TODO need to refactor
val profilesByHour = basalProfile!!.getProfilesByHour(medtronicPumpPlugin.pumpDescription.pumpType) val profilesByHour = basalProfile!!.getProfilesByHour(medtronicPumpPlugin.pumpDescription.pumpType)
if (profilesByHour != null) { if (profilesByHour != null) {
medtronicPumpStatus.basalsByHour = profilesByHour medtronicPumpStatus.basalsByHour = profilesByHour

View file

@ -101,7 +101,8 @@ class MedtronicUITask {
result = communicationManager.cancelTBR() result = communicationManager.cancelTBR()
} }
MedtronicCommandType.SetBasalProfileSTD, MedtronicCommandType.SetBasalProfileA -> { MedtronicCommandType.SetBasalProfileSTD,
MedtronicCommandType.SetBasalProfileA -> {
val profile = parameters!![0] as BasalProfile val profile = parameters!![0] as BasalProfile
result = communicationManager.setBasalProfile(profile) result = communicationManager.setBasalProfile(profile)
} }
@ -128,7 +129,7 @@ class MedtronicUITask {
} }
private fun getTbrSettings(): TempBasalPair? { private fun getTbrSettings(): TempBasalPair? {
return TempBasalPair(getDoubleFromParameters(0), // return TempBasalPair(getDoubleFromParameters(0)!!, //
false, // false, //
getIntegerFromParameters(1)) getIntegerFromParameters(1))
} }
@ -137,8 +138,8 @@ class MedtronicUITask {
return parameters!![index] as Float return parameters!![index] as Float
} }
fun getDoubleFromParameters(index: Int): Double { fun getDoubleFromParameters(index: Int): Double? {
return parameters!![index] as Double return parameters!![index] as Double?
} }
private fun getIntegerFromParameters(index: Int): Int { private fun getIntegerFromParameters(index: Int): Int {

View file

@ -43,9 +43,10 @@ import javax.inject.Singleton
// all times that time changed (TZ, DST, etc.). Data needs to be returned in batches (time_changed batches, so that we can // all times that time changed (TZ, DST, etc.). Data needs to be returned in batches (time_changed batches, so that we can
// handle it. It would help to assign sort_ids to items (from oldest (1) to newest (x) // handle it. It would help to assign sort_ids to items (from oldest (1) to newest (x)
// //
// TODO New Database changes, we need to read 35 minutes from history on each read and then compare if items have the same // TODO New Database changes:
// amounts, if not send them back to database changes. ALSO we need to remove and invalidate TBRs that are cancels from // + we need to read 35 minutes from history on each read
// PumpSyncStorage // - compare all read items if they have the same amounts, if not send them back to database changes.
// - we need to remove and invalidate TBRs that are cancels from PumpSyncStorage
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
@Singleton @Singleton
class MedtronicHistoryData @Inject constructor( class MedtronicHistoryData @Inject constructor(
@ -82,12 +83,35 @@ class MedtronicHistoryData @Inject constructor(
for (validEntry in validEntries) { for (validEntry in validEntries) {
if (!allHistory.contains(validEntry)) { if (!allHistory.contains(validEntry)) {
newEntries.add(validEntry) newEntries.add(validEntry)
} else {
val entryByPumpId = getEntryByPumpId(validEntry.pumpId!!)
// TODO not implemented
if (entryByPumpId!=null && entryByPumpId.hasBolusOrTBRDataChanged(validEntry)) {
newEntries.add(validEntry)
allHistory.remove(entryByPumpId)
}
} }
} }
newHistory = newEntries newHistory = newEntries
showLogs("List of history (before filtering): [" + newHistory.size + "]", gson.toJson(newHistory)) showLogs("List of history (before filtering): [" + newHistory.size + "]", gson.toJson(newHistory))
} }
private fun getEntryByPumpId(pumpId: Long): PumpHistoryEntry? {
val findFirst = this.allHistory.stream()
.filter { f -> f.pumpId!! == pumpId }
.findFirst()
return if (findFirst.isPresent()) findFirst.get() else null
//
// for (pumpHistoryEntry in allHistory) {
// if (pumpHistoryEntry.pumpId == pumpId) {
// return pumpHistoryEntry
// }
// }
// return null
}
private fun showLogs(header: String?, data: String) { private fun showLogs(header: String?, data: String) {
if (header != null) { if (header != null) {
aapsLogger.debug(LTag.PUMP, header) aapsLogger.debug(LTag.PUMP, header)
@ -223,8 +247,7 @@ class MedtronicHistoryData @Inject constructor(
} }
val isPumpSuspended: Boolean fun isPumpSuspended(): Boolean {
get() {
val items = getDataForPumpSuspends() val items = getDataForPumpSuspends()
showLogs("isPumpSuspended: ", gson.toJson(items)) showLogs("isPumpSuspended: ", gson.toJson(items))
return if (isCollectionNotEmpty(items)) { return if (isCollectionNotEmpty(items)) {
@ -246,7 +269,7 @@ class MedtronicHistoryData @Inject constructor(
newAndAll.addAll(allHistory) newAndAll.addAll(allHistory)
} }
if (isCollectionNotEmpty(newHistory)) { if (isCollectionNotEmpty(newHistory)) {
for (pumpHistoryEntry in newHistory!!) { for (pumpHistoryEntry in newHistory) {
if (!newAndAll.contains(pumpHistoryEntry)) { if (!newAndAll.contains(pumpHistoryEntry)) {
newAndAll.add(pumpHistoryEntry) newAndAll.add(pumpHistoryEntry)
} }
@ -563,7 +586,7 @@ class MedtronicHistoryData @Inject constructor(
var processDTO: TempBasalProcessDTO? = null var processDTO: TempBasalProcessDTO? = null
val processList: MutableList<TempBasalProcessDTO> = ArrayList() val processList: MutableList<TempBasalProcessDTO> = ArrayList()
for (treatment in entryList) { for (treatment in entryList) {
val tbr2 = treatment!!.getDecodedDataEntry("Object") as TempBasalPair? val tbr2 = treatment.getDecodedDataEntry("Object") as TempBasalPair?
if (tbr2!!.isCancelTBR) { if (tbr2!!.isCancelTBR) {
if (processDTO != null) { if (processDTO != null) {
processDTO.itemTwo = treatment processDTO.itemTwo = treatment

View file

@ -112,16 +112,17 @@ class RileyLinkStatusDeviceMedtronic : DaggerFragment(), RefreshableInterface {
var view = viewIn var view = viewIn
val viewHolder: ViewHolder val viewHolder: ViewHolder
// General ListView optimization code. // General ListView optimization code.
if (view == null) { // if (view == null) {
view = mInflator.inflate(R.layout.rileylink_status_device_item, null) view = mInflator.inflate(R.layout.rileylink_status_device_item, null)
viewHolder = ViewHolder() viewHolder = ViewHolder()
viewHolder.itemTime = view.findViewById(R.id.rileylink_history_time) viewHolder.itemTime = view.findViewById(R.id.rileylink_history_time)
viewHolder.itemSource = view.findViewById(R.id.rileylink_history_source) viewHolder.itemSource = view.findViewById(R.id.rileylink_history_source)
viewHolder.itemDescription = view.findViewById(R.id.rileylink_history_description) viewHolder.itemDescription = view.findViewById(R.id.rileylink_history_description)
view.tag = viewHolder view.tag = viewHolder
} else { // }
viewHolder = view.tag as ViewHolder // else {
} // viewHolder = view.tag as ViewHolder
// }
val item = historyItemList[i] val item = historyItemList[i]
viewHolder.itemTime!!.text = StringUtil.toDateTimeString(dateUtil, item.dateTime) viewHolder.itemTime!!.text = StringUtil.toDateTimeString(dateUtil, item.dateTime)
viewHolder.itemSource!!.text = "Riley Link" // for now viewHolder.itemSource!!.text = "Riley Link" // for now