- Added separate method for TreatmentService.createOrUpdate for Medtronic (createOrUpdateMedtronic)

- MedtronicUtil added method to detect if pump is Medtronic (reads ConfigBuilder.getActivePump().deviceID)
- TreatementService call from NS checks pump type and calls correct method
- TreatmentPlugin checks pumptype and calls correct createOrUpdate method
This commit is contained in:
Andy Rozman 2019-07-13 14:28:24 +01:00
parent 0b1ef81d40
commit 8c8fba5522
3 changed files with 160 additions and 5 deletions

View file

@ -18,6 +18,7 @@ import java.util.Map;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.general.overview.dialogs.MessageHelperActivity; import info.nightscout.androidaps.plugins.general.overview.dialogs.MessageHelperActivity;
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
@ -91,6 +92,10 @@ public class MedtronicUtil extends RileyLinkUtil {
return k; return k;
} }
public static boolean isMedtronicPump() {
return ConfigBuilderPlugin.getPlugin().getActivePump().deviceID().equals("Medtronic");
}
public static byte[] getByteArrayFromUnsignedShort(int shortValue, boolean returnFixedSize) { public static byte[] getByteArrayFromUnsignedShort(int shortValue, boolean returnFixedSize) {
byte highByte = (byte) (shortValue >> 8 & 0xFF); byte highByte = (byte) (shortValue >> 8 & 0xFF);

View file

@ -244,8 +244,12 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
public void createTreatmentFromJsonIfNotExists(JSONObject json) { public void createTreatmentFromJsonIfNotExists(JSONObject json) {
try { try {
Treatment treatment = Treatment.createFromJson(json); Treatment treatment = Treatment.createFromJson(json);
if (treatment != null) if (treatment != null) {
createOrUpdate(treatment, true); if (!MedtronicUtil.isMedtronicPump())
createOrUpdate(treatment);
else
createOrUpdateMedtronic(treatment, false);
}
else else
log.error("Date is null: " + treatment.toString()); log.error("Date is null: " + treatment.toString());
} catch (JSONException e) { } catch (JSONException e) {
@ -254,7 +258,137 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
} }
public UpdateReturn createOrUpdate(Treatment treatment, boolean fromNightScout) { // return true if new record is created
public UpdateReturn createOrUpdate(Treatment treatment) {
try {
Treatment old;
treatment.date = DatabaseHelper.roundDateToSec(treatment.date);
if (treatment.source == Source.PUMP) {
// check for changed from pump change in NS
Treatment existingTreatment = getPumpRecordById(treatment.pumpId);
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;
}
getDao().delete(existingTreatment); // need to delete/create because date may change too
existingTreatment.copyBasics(treatment);
getDao().create(existingTreatment);
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(equalRePumpHistory, false);
}
existingTreatment = getDao().queryForId(treatment.date);
if (existingTreatment != null) {
// another treatment exists with different pumpID. Update it with the treatment coming from the pump
boolean equalRePumpHistory = existingTreatment.equalsRePumpHistory(treatment);
boolean sameSource = existingTreatment.source == treatment.source;
long oldDate = existingTreatment.date;
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;
}
getDao().delete(existingTreatment); // need to delete/create because date may change too
existingTreatment.copyFrom(treatment);
getDao().create(existingTreatment);
DatabaseHelper.updateEarliestDataChange(oldDate);
DatabaseHelper.updateEarliestDataChange(existingTreatment.date);
scheduleTreatmentChange(treatment);
return new UpdateReturn(equalRePumpHistory || sameSource, false);
}
getDao().create(treatment);
if (L.isEnabled(L.DATATREATMENTS))
log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString());
DatabaseHelper.updateEarliestDataChange(treatment.date);
scheduleTreatmentChange(treatment);
return new UpdateReturn(true, true);
}
if (treatment.source == Source.NIGHTSCOUT) {
old = getDao().queryForId(treatment.date);
if (old != null) {
if (!old.isEqual(treatment)) {
boolean historyChange = old.isDataChanging(treatment);
long oldDate = old.date;
getDao().delete(old); // need to delete/create because date may change too
old.copyFrom(treatment);
getDao().create(old);
if (L.isEnabled(L.DATATREATMENTS))
log.debug("Updating record by date from: " + Source.getString(treatment.source) + " " + old.toString());
if (historyChange) {
DatabaseHelper.updateEarliestDataChange(oldDate);
DatabaseHelper.updateEarliestDataChange(old.date);
}
scheduleTreatmentChange(treatment);
return new UpdateReturn(true, true);
}
if (L.isEnabled(L.DATATREATMENTS))
log.debug("Equal record by date from: " + Source.getString(treatment.source) + " " + old.toString());
return new UpdateReturn(true, false);
}
// find by NS _id
if (treatment._id != null) {
old = findByNSId(treatment._id);
if (old != null) {
if (!old.isEqual(treatment)) {
boolean historyChange = old.isDataChanging(treatment);
long oldDate = old.date;
getDao().delete(old); // need to delete/create because date may change too
old.copyFrom(treatment);
getDao().create(old);
if (L.isEnabled(L.DATATREATMENTS))
log.debug("Updating record by _id from: " + Source.getString(treatment.source) + " " + old.toString());
if (historyChange) {
DatabaseHelper.updateEarliestDataChange(oldDate);
DatabaseHelper.updateEarliestDataChange(old.date);
}
scheduleTreatmentChange(treatment);
return new UpdateReturn(true, true);
}
if (L.isEnabled(L.DATATREATMENTS))
log.debug("Equal record by _id from: " + Source.getString(treatment.source) + " " + old.toString());
return new UpdateReturn(true, false);
}
}
getDao().create(treatment);
if (L.isEnabled(L.DATATREATMENTS))
log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString());
DatabaseHelper.updateEarliestDataChange(treatment.date);
scheduleTreatmentChange(treatment);
return new UpdateReturn(true, true);
}
if (treatment.source == Source.USER) {
getDao().create(treatment);
if (L.isEnabled(L.DATATREATMENTS))
log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString());
DatabaseHelper.updateEarliestDataChange(treatment.date);
scheduleTreatmentChange(treatment);
return new UpdateReturn(true, true);
}
} catch (SQLException e) {
log.error("Unhandled exception", e);
}
return new UpdateReturn(false, false);
}
public UpdateReturn createOrUpdateMedtronic(Treatment treatment, boolean fromNightScout) {
try { try {
treatment.date = DatabaseHelper.roundDateToSec(treatment.date); treatment.date = DatabaseHelper.roundDateToSec(treatment.date);

View file

@ -49,6 +49,7 @@ import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNo
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil;
import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin;
import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAveragePlugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAveragePlugin;
import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DateUtil;
@ -518,6 +519,8 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
// return true if new record is created // return true if new record is created
@Override @Override
public boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo, boolean allowUpdate) { public boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo, boolean allowUpdate) {
boolean medtronicPump = MedtronicUtil.isMedtronicPump();
Treatment treatment = new Treatment(); Treatment treatment = new Treatment();
treatment.date = detailedBolusInfo.date; treatment.date = detailedBolusInfo.date;
treatment.source = detailedBolusInfo.source; treatment.source = detailedBolusInfo.source;
@ -530,7 +533,13 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
treatment.source = detailedBolusInfo.source; treatment.source = detailedBolusInfo.source;
treatment.mealBolus = treatment.carbs > 0; treatment.mealBolus = treatment.carbs > 0;
treatment.boluscalc = detailedBolusInfo.boluscalc != null ? detailedBolusInfo.boluscalc.toString() : null; treatment.boluscalc = detailedBolusInfo.boluscalc != null ? detailedBolusInfo.boluscalc.toString() : null;
TreatmentService.UpdateReturn creatOrUpdateResult = getService().createOrUpdate(treatment, false); TreatmentService.UpdateReturn creatOrUpdateResult;
if (!medtronicPump)
creatOrUpdateResult = getService().createOrUpdate(treatment);
else
creatOrUpdateResult = getService().createOrUpdateMedtronic(treatment, false);
boolean newRecordCreated = creatOrUpdateResult.newRecord; boolean newRecordCreated = creatOrUpdateResult.newRecord;
//log.debug("Adding new Treatment record" + treatment.toString()); //log.debug("Adding new Treatment record" + treatment.toString());
if (detailedBolusInfo.carbTime != 0) { if (detailedBolusInfo.carbTime != 0) {
@ -540,7 +549,10 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
carbsTreatment.date = detailedBolusInfo.date + detailedBolusInfo.carbTime * 60 * 1000L + 1000L; // add 1 sec to make them different records carbsTreatment.date = detailedBolusInfo.date + detailedBolusInfo.carbTime * 60 * 1000L + 1000L; // add 1 sec to make them different records
carbsTreatment.carbs = detailedBolusInfo.carbs; carbsTreatment.carbs = detailedBolusInfo.carbs;
carbsTreatment.source = detailedBolusInfo.source; carbsTreatment.source = detailedBolusInfo.source;
getService().createOrUpdate(carbsTreatment, false); if (!medtronicPump)
getService().createOrUpdate(carbsTreatment);
else
getService().createOrUpdateMedtronic(carbsTreatment, false);
//log.debug("Adding new Treatment record" + carbsTreatment); //log.debug("Adding new Treatment record" + carbsTreatment);
} }
if (newRecordCreated && detailedBolusInfo.isValid) if (newRecordCreated && detailedBolusInfo.isValid)
@ -567,6 +579,10 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
return newRecordCreated; return newRecordCreated;
} }
private boolean isMedtronicPump() {
return ConfigBuilderPlugin.getPlugin().getActivePump().deviceID().equals("Medtronic");
}
@Override @Override
public long oldestDataAvailable() { public long oldestDataAvailable() {
long oldestTime = System.currentTimeMillis(); long oldestTime = System.currentTimeMillis();