update with data from pump

This commit is contained in:
AdrianLxM 2018-07-02 06:54:02 +02:00
parent b39fa514c2
commit 297fe487f7
4 changed files with 49 additions and 11 deletions

View file

@ -26,6 +26,7 @@ import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLab
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.JsonHelper;
import info.nightscout.utils.ToastUtils;
@DatabaseTable(tableName = Treatment.TABLE_TREATMENTS)
public class Treatment implements DataPointWithLabelInterface {
@ -135,16 +136,21 @@ public class Treatment implements DataPointWithLabelInterface {
/*
* mealBolus, _id and isSMB cannot be known coming from pump. Only compare rest
* TODO: remove debug toasts
*/
public boolean equalsRePumpHistory(Treatment other) {
if (date != other.date)
if (date != other.date) {
ToastUtils.showToastInUiThread(MainApp.instance(), "date: " + date + " vs " + other.date);
return false;
if (insulin != other.insulin)
}
if (insulin != other.insulin) {
ToastUtils.showToastInUiThread(MainApp.instance(), "insulin: " + insulin + " vs " + other.insulin);
return false;
if (carbs != other.carbs)
return false;
if (pumpId != other.pumpId)
}
if (carbs != other.carbs) {
ToastUtils.showToastInUiThread(MainApp.instance(), "carbs: " + carbs + " vs " + other.carbs);
return false;
}
return true;
}
@ -158,6 +164,13 @@ public class Treatment implements DataPointWithLabelInterface {
isSMB = t.isSMB;
}
public void copyBasics(Treatment t) {
date = t.date;
insulin = t.insulin;
carbs = t.carbs;
pumpId = t.pumpId;
}
// ----------------- DataPointInterface --------------------
@Override
public double getX() {

View file

@ -250,10 +250,32 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
// check for changed from pump change in NS
Treatment existingTreatment = getPumpRecordById(treatment.pumpId);
if (existingTreatment != null) {
// do nothing, pump history record cannot be changed
log.debug("TREATMENT: Pump record already found in database: " + treatment.toString());
//return new UpdateReturn(true, false);
return new UpdateReturn(existingTreatment.equalsRePumpHistory(treatment), false);
boolean equalRePumpHistory = existingTreatment.equalsRePumpHistory(treatment);
if(!equalRePumpHistory) {
// another treatment exists. Update it with the treatment coming from the pump
log.debug("TREATMENT: Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString());
long oldDate = existingTreatment.date;
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(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);
long oldDate = existingTreatment.date;
log.debug("TREATMENT: Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString());
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, false);
}
getDao().create(treatment);
log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString());

View file

@ -497,10 +497,13 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
if (!allowUpdate && !creatOrUpdateResult.success) {
log.error("Treatment could not be added to DB", new Exception());
String status = String.format(MainApp.gs(R.string.error_adding_treatment_message), treatment.insulin, (int) treatment.carbs, DateUtil.dateAndTimeString(treatment.date));
Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class);
i.putExtra("soundid", R.raw.error);
i.putExtra("title", MainApp.gs(R.string.error_adding_treatment_title));
i.putExtra("status", String.format(MainApp.gs(R.string.error_adding_treatment_message), treatment.insulin, (int) treatment.carbs));
i.putExtra("status", status);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainApp.instance().startActivity(i);
}

View file

@ -1181,7 +1181,7 @@
<string name="allow_hardware_pump_text">Attention: If you activate and connect to a hardware pump, AndroidAPS will copy the basal settings from the profile to the pump, overwriting the existing basal rate stored on the pump. Make sure you have the correct basal setting in AndroidAPS. If you are not sure or don\'t want to overwrite the basal settings on your pump, press cancel and repeat switching to the pump at a later time.</string>
<string name="error_adding_treatment_title">Treatment data incomplete</string>
<!-- TODO convert to proper style -->
<string name="error_adding_treatment_message" formatted="false">A treatment (insulin: %.2f, carbs: %d) could not be added to treatments. Please check and manually add a record as appropriate.</string>
<string name="error_adding_treatment_message" formatted="false">A treatment (insulin: %.2f, carbs: %d, at: %s) could not be added to treatments. Please check and manually add a record as appropriate.</string>
<plurals name="objective_days">
<item quantity="one">%d day</item>