use of com.eveningoutpost.dexdrip.Models.InsulinInjection in project xDrip by NightscoutFoundation.
the class Home method processAndApproveTreatment.
private void processAndApproveTreatment() {
// preserve globals before threading off
final double myglucosenumber = thisglucosenumber;
double mytimeoffset = thistimeoffset;
// TODO Handle BG Tests here also
if (watchkeypad) {
// calculate absolute offset
long treatment_timestamp = watchkeypad_timestamp - (long) mytimeoffset;
mytimeoffset = JoH.tsl() - treatment_timestamp;
Log.d(TAG, "Watch Keypad timestamp is: " + JoH.dateTimeText(treatment_timestamp) + " Original offset: " + JoH.qs(thistimeoffset) + " New: " + JoH.qs(mytimeoffset));
if ((mytimeoffset > (DAY_IN_MS * 3)) || (mytimeoffset < -HOUR_IN_MS * 3)) {
Log.e(TAG, "Treatment timestamp out of range: " + mytimeoffset);
JoH.static_toast_long(gs(R.string.treatment_time_wrong));
WatchUpdaterService.sendWearLocalToast(gs(R.string.treatment_error), Toast.LENGTH_LONG);
} else {
JoH.static_toast_long(gs(R.string.treatment_processed));
WatchUpdaterService.sendWearLocalToast(gs(R.string.treatment_processed), Toast.LENGTH_LONG);
long time = Treatments.getTimeStampWithOffset(mytimeoffset);
// sanity check timestamp
final Treatments exists = Treatments.byTimestamp(time);
if (exists == null) {
ArrayList<InsulinInjection> injections = new ArrayList<InsulinInjection>();
for (int i = 0; i < maxInsulinProfiles; i++) if (insulinset[i] && thisinsulinprofile[i] != null) {
InsulinInjection injection = new InsulinInjection(thisinsulinprofile[i], thisinsulinnumber[i]);
injections.add(injection);
}
Log.d(TAG, "processAndApproveTreatment create watchkeypad Treatment carbs=" + thiscarbsnumber + " insulin=" + thisInsulinSumNumber + " timestamp=" + JoH.dateTimeText(time) + " uuid=" + thisuuid);
Treatments.create(thiscarbsnumber, thisInsulinSumNumber, injections, time, thisuuid);
// gruoner: changed pendiq handling 09/12/19 TODO remove duplicate code with helper function
// in case of multiple injections in a treatment, select the injection with the primary insulin profile defined in the profile editor; if not found, take 0
// in case of a single injection in a treatment, assume thats the #units to send to pendiq
double pendiqInsulin = 0;
if (MultipleInsulins.isEnabled() && injections.size() > 1) {
for (InsulinInjection i : injections) if (i.getProfile() == InsulinManager.getBolusProfile())
pendiqInsulin = i.getUnits();
} else
pendiqInsulin = thisInsulinSumNumber;
Pendiq.handleTreatment(pendiqInsulin);
} else {
Log.d(TAG, "processAndApproveTreatment Treatment already exists carbs=" + thiscarbsnumber + " insulin=" + thisInsulinSumNumber + " timestamp=" + JoH.dateTimeText(time));
}
}
} else {
WatchUpdaterService.sendWearToast(gs(R.string.treatment_processed), Toast.LENGTH_LONG);
ArrayList<InsulinInjection> injections = new ArrayList<InsulinInjection>();
for (int i = 0; i < maxInsulinProfiles; i++) if (insulinset[i] && thisinsulinprofile[i] != null) {
InsulinInjection injection = new InsulinInjection(thisinsulinprofile[i], thisinsulinnumber[i]);
injections.add(injection);
}
Treatments.create(thiscarbsnumber, thisInsulinSumNumber, injections, Treatments.getTimeStampWithOffset(mytimeoffset));
// gruoner: changed pendiq handling 09/12/19 TODO remove duplicate code with helper function
// in case of multiple injections in a treatment, select the injection with the primary insulin profile defined in the profile editor; if not found, take 0
// in case of a single injection in a treatment, assume thats the #units to send to pendiq
double pendiqInsulin = 0;
if (MultipleInsulins.isEnabled() && injections.size() > 1) {
for (InsulinInjection i : injections) if (i.getProfile() == InsulinManager.getBolusProfile())
pendiqInsulin = i.getUnits();
} else
pendiqInsulin = thisInsulinSumNumber;
Pendiq.handleTreatment(pendiqInsulin);
}
hideAllTreatmentButtons();
if (hideTreatmentButtonsIfAllDone()) {
updateCurrentBgInfo("approve button");
}
if (watchkeypad) {
if (myglucosenumber > 0) {
if ((mytimeoffset > (DAY_IN_MS * 3)) || (mytimeoffset < -HOUR_IN_MS * 3)) {
Log.e(TAG, "Treatment bloodtest timestamp out of range: " + mytimeoffset);
} else {
BloodTest.createFromCal(myglucosenumber, mytimeoffset, "Manual Entry", thisuuid);
}
}
watchkeypad = false;
watchkeypadset = false;
watchkeypad_timestamp = -1;
} else
processCalibrationNoUI(myglucosenumber, mytimeoffset);
staticRefreshBGCharts();
}
Aggregations