From 7f61f45b78d808b2fea2411b11a0aaadee5c3a4e Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Thu, 2 May 2019 20:22:44 +0100 Subject: [PATCH] - RileyStatusGeneral fixed in case fimrware is not set (no connection to RL yet, when page is opened) - Crashalytics - Possible fix for SMB problem - Possible fix for Treatments saving - If last History entry has no date set, system doesn't crash (it crashed because it tryied to parse date and setting wrong values --- .../dialog/RileyLinkStatusGeneral.java | 21 +++--- .../pump/common/utils/DateTimeUtil.java | 14 +++- .../medtronic/data/MedtronicHistoryData.java | 66 +++++++++++-------- .../plugins/treatments/TreatmentService.java | 37 ++++++++--- 4 files changed, 89 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneral.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneral.java index 0d40210b06..386fb196fe 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneral.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneral.java @@ -1,9 +1,5 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.dialog; -import java.util.Locale; - -import org.joda.time.LocalDateTime; - import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; @@ -11,10 +7,15 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import org.joda.time.LocalDateTime; + +import java.util.Locale; + import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.pump.common.dialog.RefreshableInterface; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkFirmwareVersion; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkTargetDevice; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData; import info.nightscout.androidaps.plugins.pump.common.utils.StringUtil; @@ -98,13 +99,15 @@ public class RileyLinkStatusGeneral extends Fragment implements RefreshableInter if (rileyLinkServiceData != null) { this.configuredAddress.setText(rileyLinkServiceData.rileylinkAddress); this.connectionError.setText(rileyLinkServiceData.errorCode == null ? // - "-" - : MainApp.gs(rileyLinkServiceData.errorCode.getResourceId(targetDevice))); + "-" + : MainApp.gs(rileyLinkServiceData.errorCode.getResourceId(targetDevice))); + + + RileyLinkFirmwareVersion firmwareVersion = rileyLinkServiceData.versionCC110; this.firmwareVersion.setText("BLE113: " + // - rileyLinkServiceData.versionBLE113 == null ? "-" : rileyLinkServiceData.versionBLE113 + // - "\nCC110: " + rileyLinkServiceData.versionCC110 == null ? "-" : rileyLinkServiceData.versionCC110 - .toString()); + rileyLinkServiceData.versionBLE113 == null ? "-" : rileyLinkServiceData.versionBLE113 + // + "\nCC110: " + firmwareVersion == null ? "-" : firmwareVersion.toString()); } // TODO add handling for Omnipod pump status diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java index 5077b58f9f..deac533ab1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java @@ -4,12 +4,14 @@ package info.nightscout.androidaps.plugins.pump.common.utils; * Created by andy on 10/25/18. */ +import android.util.Log; + +import org.joda.time.LocalDateTime; + import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; -import org.joda.time.LocalDateTime; - /** * This is simple version of ATechDate, limited only to one format (yyyymmddHHMIss) */ @@ -39,7 +41,13 @@ public class DateTimeUtil { int second = (int)atechDateTime; - return new LocalDateTime(year, month, dayOfMonth, hourOfDay, minute, second); + try { + return new LocalDateTime(year, month, dayOfMonth, hourOfDay, minute, second); + } catch (Exception ex) { + Log.e("DateTimeUtil", String.format("Error creating LocalDateTime from values [atechDateTime=%d, year=%d, month=%d, day=%d, hour=%d, minute=%d, second=%d]", atechDateTime, year, month, dayOfMonth, hourOfDay, minute, second)); + //return null; + throw ex; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java index d6c79428d5..00c8d583dd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java @@ -174,26 +174,38 @@ public class MedtronicHistoryData { this.setLastHistoryRecordTime(pheLast.atechDateTime); - LocalDateTime dt = DateTimeUtil.toLocalDateTime(pheLast.atechDateTime); - dt = dt.minusDays(1); // we keep 24 hours + LocalDateTime dt = null; - long dtRemove = DateTimeUtil.toATechDate(dt); - - List removeList = new ArrayList<>(); - - for (PumpHistoryEntry pumpHistoryEntry : allHistory) { - - if (!pumpHistoryEntry.isAfter(dtRemove)) { - removeList.add(pumpHistoryEntry); - } + try { + dt = DateTimeUtil.toLocalDateTime(pheLast.atechDateTime); + } catch (Exception ex) { + LOG.error("Problem decoding date from last record: {}" + pheLast); } - this.allHistory.removeAll(removeList); + if (dt != null) { - this.sort(this.allHistory); + dt = dt.minusDays(1); // we keep 24 hours - LOG.debug("All History records [afterFilterCount={}, removedItemsCount={}, newItemsCount={}]", - allHistory.size(), removeList.size(), newHistory.size()); + long dtRemove = DateTimeUtil.toATechDate(dt); + + List removeList = new ArrayList<>(); + + for (PumpHistoryEntry pumpHistoryEntry : allHistory) { + + if (!pumpHistoryEntry.isAfter(dtRemove)) { + removeList.add(pumpHistoryEntry); + } + } + + this.allHistory.removeAll(removeList); + + this.sort(this.allHistory); + + LOG.debug("All History records [afterFilterCount={}, removedItemsCount={}, newItemsCount={}]", + allHistory.size(), removeList.size(), newHistory.size()); + } else { + LOG.error("Since we couldn't determine date, we don't clean full history. This is just workaround."); + } this.newHistory.clear(); @@ -203,12 +215,12 @@ public class MedtronicHistoryData { public boolean hasRelevantConfigurationChanged() { return getStateFromFilteredList( // - PumpHistoryEntryType.ChangeBasalPattern, // - PumpHistoryEntryType.ClearSettings, // - PumpHistoryEntryType.SaveSettings, // - PumpHistoryEntryType.ChangeMaxBolus, // - PumpHistoryEntryType.ChangeMaxBasal, // - PumpHistoryEntryType.ChangeTempBasalType); + PumpHistoryEntryType.ChangeBasalPattern, // + PumpHistoryEntryType.ClearSettings, // + PumpHistoryEntryType.SaveSettings, // + PumpHistoryEntryType.ChangeMaxBolus, // + PumpHistoryEntryType.ChangeMaxBasal, // + PumpHistoryEntryType.ChangeTempBasalType); } @@ -229,10 +241,10 @@ public class MedtronicHistoryData { PumpHistoryEntryType pumpHistoryEntryType = items.get(0).getEntryType(); boolean isSuspended = !(pumpHistoryEntryType == PumpHistoryEntryType.TempBasalCombined || // - pumpHistoryEntryType == PumpHistoryEntryType.BasalProfileStart || // - pumpHistoryEntryType == PumpHistoryEntryType.Bolus || // - pumpHistoryEntryType == PumpHistoryEntryType.PumpResume || // - pumpHistoryEntryType == PumpHistoryEntryType.Prime); + pumpHistoryEntryType == PumpHistoryEntryType.BasalProfileStart || // + pumpHistoryEntryType == PumpHistoryEntryType.Bolus || // + pumpHistoryEntryType == PumpHistoryEntryType.PumpResume || // + pumpHistoryEntryType == PumpHistoryEntryType.Prime); LOG.debug("isPumpSuspended. Last entry type={}, isSuspended={}", pumpHistoryEntryType, isSuspended); @@ -344,7 +356,7 @@ public class MedtronicHistoryData { List suspends = getSuspends(); LOG.debug("ProcessHistoryData: FakeTBRs (suspend/resume) [count={}, items={}]", suspends.size(), - gsonPretty.toJson(suspends)); + gsonPretty.toJson(suspends)); if (suspends.size() > 0) { // processSuspends(treatments); @@ -369,7 +381,7 @@ public class MedtronicHistoryData { TDD tddDbEntry = findTDD(tdd.atechDateTime, tddsDb); - DailyTotalsDTO totalsDTO = (DailyTotalsDTO)tdd.getDecodedData().get("Object"); + DailyTotalsDTO totalsDTO = (DailyTotalsDTO) tdd.getDecodedData().get("Object"); LOG.debug("DailtyTotals: {}", totalsDTO); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java index d41b2aec68..6d459d36d2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java @@ -255,6 +255,7 @@ public class TreatmentService extends OrmLiteBaseService { try { Treatment old; treatment.date = DatabaseHelper.roundDateToSec(treatment.date); + boolean changed = false; if (treatment.source == Source.PUMP) { // check for changed from pump change in NS @@ -262,16 +263,15 @@ public class TreatmentService extends OrmLiteBaseService { if (existingTreatment != null) { boolean equalRePumpHistory = existingTreatment.equalsRePumpHistory(treatment); boolean sameSource = existingTreatment.source == treatment.source; + if (!equalRePumpHistory) { // another treatment exists. Update it with the treatment coming from the pump if (L.isEnabled(L.DATATREATMENTS)) log.debug("Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); long oldDate = existingTreatment.date; - //preserve carbs - if (existingTreatment.isValid && existingTreatment.carbs > 0 && treatment.carbs == 0) { - treatment.carbs = existingTreatment.carbs; - } + // preserve carbs & SMB + changed = preserveCarbsAndSMB(treatment, existingTreatment); getDao().delete(existingTreatment); // need to delete/create because date may change too existingTreatment.copyBasics(treatment); @@ -279,8 +279,9 @@ public class TreatmentService extends OrmLiteBaseService { DatabaseHelper.updateEarliestDataChange(oldDate); DatabaseHelper.updateEarliestDataChange(existingTreatment.date); scheduleTreatmentChange(treatment); - return new UpdateReturn(sameSource, false); //updating a pump treatment with another one from the pump is not counted as clash + return new UpdateReturn(sameSource || changed, false); //updating a pump treatment with another one from the pump is not counted as clash } + return new UpdateReturn(equalRePumpHistory, false); } existingTreatment = getDao().queryForId(treatment.date); @@ -292,10 +293,8 @@ public class TreatmentService extends OrmLiteBaseService { if (L.isEnabled(L.DATATREATMENTS)) log.debug("Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); - //preserve carbs - if (existingTreatment.isValid && existingTreatment.carbs > 0 && treatment.carbs == 0) { - treatment.carbs = existingTreatment.carbs; - } + // preserve carbs & SMB + changed = preserveCarbsAndSMB(treatment, existingTreatment); getDao().delete(existingTreatment); // need to delete/create because date may change too existingTreatment.copyFrom(treatment); @@ -303,7 +302,7 @@ public class TreatmentService extends OrmLiteBaseService { DatabaseHelper.updateEarliestDataChange(oldDate); DatabaseHelper.updateEarliestDataChange(existingTreatment.date); scheduleTreatmentChange(treatment); - return new UpdateReturn(equalRePumpHistory || sameSource, false); + return new UpdateReturn(equalRePumpHistory || sameSource || changed, false); } getDao().create(treatment); if (L.isEnabled(L.DATATREATMENTS)) @@ -379,12 +378,28 @@ public class TreatmentService extends OrmLiteBaseService { return new UpdateReturn(false, false); } + private boolean preserveCarbsAndSMB(Treatment treatment, Treatment existingTreatment) { + if (existingTreatment.isValid) { + if (existingTreatment.carbs > 0 && treatment.carbs == 0) { + treatment.carbs = existingTreatment.carbs; + } + + treatment.isSMB = (existingTreatment.isSMB || treatment.isSMB); + + return true; + } + + return false; + } + private void treatmentCopy(Treatment oldTreatment, Treatment newTreatment, boolean fromNightScout) { log.debug("treatmentCopy [old={}, new={}]", oldTreatment.toString(), newTreatment.toString()); if (fromNightScout) { long pumpId_old = oldTreatment.pumpId; + boolean isSMB = (oldTreatment.isSMB || newTreatment.isSMB); + oldTreatment.copyFrom(newTreatment); if (pumpId_old != 0) { @@ -395,6 +410,8 @@ public class TreatmentService extends OrmLiteBaseService { oldTreatment.source = Source.PUMP; } + oldTreatment.isSMB = isSMB; + } else { oldTreatment.copyFrom(newTreatment); }