Search in sources :

Example 1 with Treatments

use of com.eveningoutpost.dexdrip.Models.Treatments in project xDrip by NightscoutFoundation.

the class ListenerService method createTreatment.

public static synchronized void createTreatment(DataMap dataMap, Context context) {
    Log.d(TAG, "createTreatment dataMap=" + dataMap);
    double timeoffset = dataMap.getDouble("timeoffset", 0);
    double carbs = dataMap.getDouble("carbs", 0);
    double insulin = dataMap.getDouble("insulin", 0);
    double bloodtest = dataMap.getDouble("bloodtest", 0);
    String notes = dataMap.getString("notes", "");
    long timestamp_ms = Treatments.getTimeStampWithOffset(timeoffset);
    Treatments treatment = Treatments.create(carbs, insulin, notes, timestamp_ms);
    if (bloodtest > 0) {
        Log.d(TAG, "createTreatment bloodtest=" + bloodtest);
        BloodTest.createFromCal(bloodtest, timeoffset, "Manual Entry", treatment.uuid);
    } else
        Log.d(TAG, "createTreatment bloodtest=0 " + bloodtest);
    showTreatments(context, "all");
    SendData(context, SYNC_TREATMENTS_PATH, null);
// requestData(context);//send to phone if connected
}
Also used : Treatments(com.eveningoutpost.dexdrip.Models.Treatments)

Example 2 with Treatments

use of com.eveningoutpost.dexdrip.Models.Treatments in project xDrip by NightscoutFoundation.

the class ListenerService method getTreatments.

public static DataMap getTreatments(long startTime) {
    Treatments last = Treatments.last();
    if (last != null) {
        Log.d(TAG, "getTreatments last.timestamp:" + JoH.dateTimeText(last.timestamp));
    }
    List<Treatments> graph = Treatments.latestForGraph(60, startTime);
    if (!graph.isEmpty()) {
        Log.d(TAG, "getTreatments graph size=" + graph.size());
        final ArrayList<DataMap> dataMaps = new ArrayList<>(graph.size());
        DataMap entries = null;
        // if (includeTreatment(last)) entries = dataMapForWatchface(last);
        for (Treatments data : graph) {
            if (includeTreatment(data)) {
                if (entries == null) {
                    entries = dataMapForWatchface(data);
                    dataMaps.add(dataMapForWatchface(data));
                } else
                    dataMaps.add(dataMapForWatchface(data));
            }
        }
        if (entries != null) {
            entries.putDataMapArrayList("entries", dataMaps);
            Log.d(TAG, "getTreatments entries=" + entries);
        }
        return entries;
    } else
        return null;
}
Also used : ArrayList(java.util.ArrayList) Treatments(com.eveningoutpost.dexdrip.Models.Treatments) DataMap(com.google.android.gms.wearable.DataMap)

Example 3 with Treatments

use of com.eveningoutpost.dexdrip.Models.Treatments in project xDrip by NightscoutFoundation.

the class ListenerService method syncTreatmentsData.

private synchronized void syncTreatmentsData(DataMap dataMap, Context context) {
    Log.d(TAG, "syncTreatmentsData");
    boolean changed = false;
    String action = dataMap.getString("action");
    if (action.equals("delete")) {
        Log.d(TAG, "syncTreatmentsData Delete Treatments");
        deleteTreatment(dataMap);
        showTreatments(context, "treats");
    } else {
        ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
        if (entries != null) {
            Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new DateTypeAdapter()).serializeSpecialFloatingPointValues().create();
            Log.d(TAG, "syncTreatmentsData add Treatments Table entries count=" + entries.size());
            // ensure database has already been initialized
            Sensor.InitDb(context);
            for (DataMap entry : entries) {
                if (entry != null) {
                    String record = entry.getString("data");
                    if (record != null) {
                        Treatments data = gson.fromJson(record, Treatments.class);
                        Treatments exists = Treatments.byuuid(data.uuid);
                        if (exists != null) {
                            Log.d(TAG, "syncTreatmentsData save existing Treatments for action insert uuid=" + data.uuid + " timestamp=" + data.timestamp + " timeString=" + JoH.dateTimeText(data.timestamp) + " carbs=" + data.carbs + " insulin=" + data.insulin + " exists.systime=" + JoH.dateTimeText(exists.systimestamp));
                            if (exists.timestamp != data.timestamp) {
                                // currently only tracking timestamp on watch
                                changed = true;
                            }
                            exists.enteredBy = data.enteredBy;
                            exists.eventType = data.eventType;
                            exists.insulin = data.insulin;
                            exists.carbs = data.carbs;
                            exists.created_at = data.created_at;
                            exists.notes = data.notes;
                            exists.timestamp = data.timestamp;
                            exists.systimestamp = exists.systimestamp > 0 ? exists.systimestamp : data.timestamp < last_send_previous_treatments ? data.timestamp : last_send_previous_treatments > 0 ? last_send_previous_treatments - 1 : JoH.tsl();
                            exists.save();
                        } else {
                            changed = true;
                            data.systimestamp = data.timestamp < last_send_previous_treatments ? data.timestamp : last_send_previous_treatments > 0 ? last_send_previous_treatments - 1 : JoH.tsl();
                            data.save();
                            Log.d(TAG, "syncTreatmentsData create new treatment for action insert uuid=" + data.uuid + " timestamp=" + data.timestamp + " timeString=" + JoH.dateTimeText(data.timestamp) + " carbs=" + data.carbs + " insulin=" + data.insulin + " systime=" + JoH.dateTimeText(data.systimestamp));
                        }
                    }
                }
            }
            if (changed) {
                showTreatments(context, "treats");
            }
        }
    }
}
Also used : DateTypeAdapter(com.google.gson.internal.bind.DateTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Treatments(com.eveningoutpost.dexdrip.Models.Treatments) DataMap(com.google.android.gms.wearable.DataMap)

Example 4 with Treatments

use of com.eveningoutpost.dexdrip.Models.Treatments in project xDrip by NightscoutFoundation.

the class UploaderTask method doInBackground.

public Void doInBackground(String... urls) {
    try {
        final List<Long> circuits = new ArrayList<>();
        final List<String> types = new ArrayList<>();
        types.add(BgReading.class.getSimpleName());
        types.add(Calibration.class.getSimpleName());
        types.add(BloodTest.class.getSimpleName());
        types.add(Treatments.class.getSimpleName());
        if (Pref.getBooleanDefaultFalse("wear_sync")) {
            circuits.add(UploaderQueue.WATCH_WEARAPI);
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_mongodb_enable")) {
            circuits.add(UploaderQueue.MONGO_DIRECT);
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_api_enable")) {
            if ((Pref.getBoolean("cloud_storage_api_use_mobile", true) || (JoH.isLANConnected()))) {
                circuits.add(UploaderQueue.NIGHTSCOUT_RESTAPI);
            } else {
                Log.e(TAG, "Skipping Nightscout upload due to mobile data only");
            }
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_influxdb_enable")) {
            circuits.add(UploaderQueue.INFLUXDB_RESTAPI);
        }
        for (long THIS_QUEUE : circuits) {
            final List<BgReading> bgReadings = new ArrayList<>();
            final List<Calibration> calibrations = new ArrayList<>();
            final List<BloodTest> bloodtests = new ArrayList<>();
            final List<Treatments> treatmentsAdd = new ArrayList<>();
            final List<String> treatmentsDel = new ArrayList<>();
            final List<UploaderQueue> items = new ArrayList<>();
            for (String type : types) {
                final List<UploaderQueue> bgups = UploaderQueue.getPendingbyType(type, THIS_QUEUE);
                if (bgups != null) {
                    for (UploaderQueue up : bgups) {
                        switch(up.action) {
                            case "insert":
                            case "update":
                            case "create":
                                items.add(up);
                                if (type.equals(BgReading.class.getSimpleName())) {
                                    final BgReading this_bg = BgReading.byid(up.reference_id);
                                    if (this_bg != null) {
                                        bgReadings.add(this_bg);
                                    } else {
                                        Log.wtf(TAG, "BgReading with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(Calibration.class.getSimpleName())) {
                                    final Calibration this_cal = Calibration.byid(up.reference_id);
                                    if ((this_cal != null) && (this_cal.isValid())) {
                                        calibrations.add(this_cal);
                                    } else {
                                        Log.wtf(TAG, "Calibration with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(BloodTest.class.getSimpleName())) {
                                    final BloodTest this_bt = BloodTest.byid(up.reference_id);
                                    if (this_bt != null) {
                                        bloodtests.add(this_bt);
                                    } else {
                                        Log.wtf(TAG, "Bloodtest with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(Treatments.class.getSimpleName())) {
                                    final Treatments this_treat = Treatments.byid(up.reference_id);
                                    if (this_treat != null) {
                                        treatmentsAdd.add(this_treat);
                                    } else {
                                        Log.wtf(TAG, "Treatments with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                }
                                break;
                            case "delete":
                                if ((THIS_QUEUE == UploaderQueue.WATCH_WEARAPI || THIS_QUEUE == UploaderQueue.NIGHTSCOUT_RESTAPI) && type.equals(Treatments.class.getSimpleName())) {
                                    items.add(up);
                                    Log.wtf(TAG, "Delete Treatments with ID: " + up.reference_uuid);
                                    treatmentsDel.add(up.reference_uuid);
                                } else if (up.reference_uuid != null) {
                                    Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " delete not yet implemented: " + up.reference_uuid);
                                    // mark as completed so as not to tie up the queue for now
                                    up.completed(THIS_QUEUE);
                                }
                                break;
                            default:
                                Log.e(TAG, "Unsupported operation type for " + type + " " + up.action);
                                break;
                        }
                    }
                }
            }
            if ((bgReadings.size() > 0) || (calibrations.size() > 0) || (bloodtests.size() > 0) || (treatmentsAdd.size() > 0 || treatmentsDel.size() > 0) || (UploaderQueue.getPendingbyType(Treatments.class.getSimpleName(), THIS_QUEUE, 1).size() > 0)) {
                Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " Processing: " + bgReadings.size() + " BgReadings and " + calibrations.size() + " Calibrations " + bloodtests.size() + " bloodtests " + treatmentsAdd.size() + " treatmentsAdd " + treatmentsDel.size() + " treatmentsDel");
                boolean uploadStatus = false;
                if (THIS_QUEUE == UploaderQueue.MONGO_DIRECT) {
                    final NightscoutUploader uploader = new NightscoutUploader(xdrip.getAppContext());
                    uploadStatus = uploader.uploadMongo(bgReadings, calibrations, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.NIGHTSCOUT_RESTAPI) {
                    final NightscoutUploader uploader = new NightscoutUploader(xdrip.getAppContext());
                    uploadStatus = uploader.uploadRest(bgReadings, bloodtests, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.INFLUXDB_RESTAPI) {
                    final InfluxDBUploader influxDBUploader = new InfluxDBUploader(xdrip.getAppContext());
                    uploadStatus = influxDBUploader.upload(bgReadings, calibrations, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.WATCH_WEARAPI) {
                    uploadStatus = WatchUpdaterService.sendWearUpload(bgReadings, calibrations, bloodtests, treatmentsAdd, treatmentsDel);
                }
                // TODO some kind of fail counter?
                if (uploadStatus) {
                    for (UploaderQueue up : items) {
                        // approve all types for this queue
                        up.completed(THIS_QUEUE);
                    }
                    Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " Marking: " + items.size() + " Items as successful");
                    if (PersistentStore.getBoolean(BACKFILLING_BOOSTER)) {
                        Log.d(TAG, "Scheduling boosted repeat query");
                        SyncService.startSyncService(2000);
                    }
                }
            } else {
                Log.d(TAG, "Nothing to upload for: " + UploaderQueue.getCircuitName(THIS_QUEUE));
                if (PersistentStore.getBoolean(BACKFILLING_BOOSTER)) {
                    PersistentStore.setBoolean(BACKFILLING_BOOSTER, false);
                    Log.d(TAG, "Switched off backfilling booster");
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "caught exception", e);
        exception = e;
        return null;
    }
    return null;
}
Also used : BloodTest(com.eveningoutpost.dexdrip.Models.BloodTest) ArrayList(java.util.ArrayList) Calibration(com.eveningoutpost.dexdrip.Models.Calibration) InfluxDBUploader(com.eveningoutpost.dexdrip.InfluxDB.InfluxDBUploader) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Treatments(com.eveningoutpost.dexdrip.Models.Treatments)

Example 5 with Treatments

use of com.eveningoutpost.dexdrip.Models.Treatments in project xDrip-plus by jamorham.

the class NightscoutBackfillActivity method backfillRun.

public synchronized void backfillRun(View v) {
    locked = JoH.tsl();
    doitButton.setVisibility(View.INVISIBLE);
    JoH.static_toast_long("Please wait..");
    new Thread(new Runnable() {

        @Override
        public void run() {
            final PowerManager.WakeLock wl = JoH.getWakeLock("nightscout-backfill", 600000);
            try {
                final List<BgReading> the_readings = BgReading.latestForGraphAsc(500000, calendar.getTimeInMillis(), JoH.tsl());
                if ((the_readings != null) && (the_readings.size() > 0)) {
                    PersistentStore.setBoolean(UploaderTask.BACKFILLING_BOOSTER, true);
                    long bgcount = the_readings.size();
                    long trcount = 0;
                    for (BgReading bg : the_readings) {
                        UploaderQueue.newEntry("update", bg);
                    }
                    final List<Treatments> the_treatments = Treatments.latestForGraph(50000, calendar.getTimeInMillis(), JoH.tsl());
                    if ((the_treatments != null) && (the_treatments.size() > 0)) {
                        trcount = the_treatments.size();
                        for (Treatments tr : the_treatments) {
                            UploaderQueue.newEntry("update", tr);
                        }
                    }
                    // TODO Calibrations? Blood tests?
                    JoH.static_toast_long("Queued " + bgcount + " glucose readings and " + trcount + " treatments!");
                    SyncService.startSyncService(500);
                    // clear lock
                    locked = 0;
                } else {
                    JoH.static_toast_long("Didn't find any glucose readings in that time period");
                }
            } finally {
                JoH.releaseWakeLock(wl);
            }
        }
    }).start();
    finish();
}
Also used : PowerManager(android.os.PowerManager) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Treatments(com.eveningoutpost.dexdrip.Models.Treatments)

Aggregations

Treatments (com.eveningoutpost.dexdrip.Models.Treatments)26 ArrayList (java.util.ArrayList)12 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)8 DataMap (com.google.android.gms.wearable.DataMap)8 Date (java.util.Date)8 BloodTest (com.eveningoutpost.dexdrip.Models.BloodTest)6 Calibration (com.eveningoutpost.dexdrip.Models.Calibration)6 IOException (java.io.IOException)6 URISyntaxException (java.net.URISyntaxException)6 JSONException (org.json.JSONException)6 MongoClientURI (com.mongodb.MongoClientURI)4 ResponseBody (com.squareup.okhttp.ResponseBody)4 URI (java.net.URI)4 JSONArray (org.json.JSONArray)4 JSONObject (org.json.JSONObject)4 PowerManager (android.os.PowerManager)2 ListAdapter (android.widget.ListAdapter)2 AddCalibration (com.eveningoutpost.dexdrip.AddCalibration)2 InfluxDBUploader (com.eveningoutpost.dexdrip.InfluxDB.InfluxDBUploader)2 Forecast (com.eveningoutpost.dexdrip.Models.Forecast)2