Search in sources :

Example 6 with InsulinInjection

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();
}
Also used : InsulinInjection(com.eveningoutpost.dexdrip.Models.InsulinInjection) ArrayList(java.util.ArrayList) Treatments(com.eveningoutpost.dexdrip.Models.Treatments) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point)

Aggregations

SuppressLint (android.annotation.SuppressLint)6 Paint (android.graphics.Paint)6 Point (android.graphics.Point)6 InsulinInjection (com.eveningoutpost.dexdrip.Models.InsulinInjection)6 ArrayList (java.util.ArrayList)4 AlertDialog (android.app.AlertDialog)2 Dialog (android.app.Dialog)2 PendingIntent (android.app.PendingIntent)2 BroadcastReceiver (android.content.BroadcastReceiver)2 Context (android.content.Context)2 Intent (android.content.Intent)2 ColorDrawable (android.graphics.drawable.ColorDrawable)2 Bundle (android.os.Bundle)2 RecognizerIntent (android.speech.RecognizerIntent)2 Toolbar (android.support.v7.widget.Toolbar)2 DisplayMetrics (android.util.DisplayMetrics)2 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 AdapterView (android.widget.AdapterView)2 TextView (android.widget.TextView)2