alarm on clashes, not on identical treatments

This commit is contained in:
AdrianLxM 2018-07-02 02:11:25 +02:00
parent bf3b4502bf
commit b39fa514c2
3 changed files with 39 additions and 11 deletions

View file

@ -132,6 +132,22 @@ public class Treatment implements DataPointWithLabelInterface {
return true; return true;
} }
/*
* mealBolus, _id and isSMB cannot be known coming from pump. Only compare rest
*/
public boolean equalsRePumpHistory(Treatment other) {
if (date != other.date)
return false;
if (insulin != other.insulin)
return false;
if (carbs != other.carbs)
return false;
if (pumpId != other.pumpId)
return false;
return true;
}
public void copyFrom(Treatment t) { public void copyFrom(Treatment t) {
date = t.date; date = t.date;
_id = t._id; _id = t._id;

View file

@ -241,7 +241,7 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
} }
// return true if new record is created // return true if new record is created
public boolean createOrUpdate(Treatment treatment) { public UpdateReturn createOrUpdate(Treatment treatment) {
try { try {
Treatment old; Treatment old;
treatment.date = DatabaseHelper.roundDateToSec(treatment.date); treatment.date = DatabaseHelper.roundDateToSec(treatment.date);
@ -252,13 +252,14 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
if (existingTreatment != null) { if (existingTreatment != null) {
// do nothing, pump history record cannot be changed // do nothing, pump history record cannot be changed
log.debug("TREATMENT: Pump record already found in database: " + treatment.toString()); log.debug("TREATMENT: Pump record already found in database: " + treatment.toString());
return false; //return new UpdateReturn(true, false);
return new UpdateReturn(existingTreatment.equalsRePumpHistory(treatment), false);
} }
getDao().create(treatment); getDao().create(treatment);
log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString());
DatabaseHelper.updateEarliestDataChange(treatment.date); DatabaseHelper.updateEarliestDataChange(treatment.date);
scheduleTreatmentChange(treatment); scheduleTreatmentChange(treatment);
return true; return new UpdateReturn(true, true);
} }
if (treatment.source == Source.NIGHTSCOUT) { if (treatment.source == Source.NIGHTSCOUT) {
old = getDao().queryForId(treatment.date); old = getDao().queryForId(treatment.date);
@ -275,9 +276,9 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
DatabaseHelper.updateEarliestDataChange(old.date); DatabaseHelper.updateEarliestDataChange(old.date);
} }
scheduleTreatmentChange(treatment); scheduleTreatmentChange(treatment);
return true; return new UpdateReturn(true, true);
} }
return false; return new UpdateReturn(true, false);
} }
// find by NS _id // find by NS _id
if (treatment._id != null) { if (treatment._id != null) {
@ -295,7 +296,7 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
DatabaseHelper.updateEarliestDataChange(old.date); DatabaseHelper.updateEarliestDataChange(old.date);
} }
scheduleTreatmentChange(treatment); scheduleTreatmentChange(treatment);
return true; return new UpdateReturn(true, true);
} }
} }
} }
@ -303,19 +304,19 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString());
DatabaseHelper.updateEarliestDataChange(treatment.date); DatabaseHelper.updateEarliestDataChange(treatment.date);
scheduleTreatmentChange(treatment); scheduleTreatmentChange(treatment);
return true; return new UpdateReturn(true, true);
} }
if (treatment.source == Source.USER) { if (treatment.source == Source.USER) {
getDao().create(treatment); getDao().create(treatment);
log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString());
DatabaseHelper.updateEarliestDataChange(treatment.date); DatabaseHelper.updateEarliestDataChange(treatment.date);
scheduleTreatmentChange(treatment); scheduleTreatmentChange(treatment);
return true; return new UpdateReturn(true, true);
} }
} catch (SQLException e) { } catch (SQLException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }
return false; return new UpdateReturn(false, false);
} }
/** Returns the record for the given id, null if none, throws RuntimeException /** Returns the record for the given id, null if none, throws RuntimeException
@ -439,4 +440,14 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
return null; return null;
} }
public class UpdateReturn {
public UpdateReturn(boolean success, boolean newRecord){
this.success = success;
this.newRecord = newRecord;
}
boolean newRecord;
boolean success;
}
} }

View file

@ -479,7 +479,8 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
treatment.carbs = detailedBolusInfo.carbs; treatment.carbs = detailedBolusInfo.carbs;
treatment.source = detailedBolusInfo.source; treatment.source = detailedBolusInfo.source;
treatment.mealBolus = treatment.carbs > 0; treatment.mealBolus = treatment.carbs > 0;
boolean newRecordCreated = getService().createOrUpdate(treatment); TreatmentService.UpdateReturn creatOrUpdateResult = getService().createOrUpdate(treatment);
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) {
Treatment carbsTreatment = new Treatment(); Treatment carbsTreatment = new Treatment();
@ -494,7 +495,7 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
if (newRecordCreated && detailedBolusInfo.isValid) if (newRecordCreated && detailedBolusInfo.isValid)
NSUpload.uploadTreatmentRecord(detailedBolusInfo); NSUpload.uploadTreatmentRecord(detailedBolusInfo);
if (!allowUpdate && !newRecordCreated) { if (!allowUpdate && !creatOrUpdateResult.success) {
log.error("Treatment could not be added to DB", new Exception()); log.error("Treatment could not be added to DB", new Exception());
Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class);
i.putExtra("soundid", R.raw.error); i.putExtra("soundid", R.raw.error);