Search in sources :

Example 11 with Treatments

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

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 12 with Treatments

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

the class ListenerService method getWearTreatmentsData.

private synchronized DataMap getWearTreatmentsData(int count, long last_send_time, int min_count) {
    forceGoogleApiConnect();
    Log.d(TAG, "getWearTreatmentsData last_send_time:" + JoH.dateTimeText(last_send_time) + " max count=" + count + " min_count=" + min_count);
    Treatments last_log = Treatments.lastSystime();
    if (last_log != null) {
        Log.d(TAG, "getWearTreatmentsData last systimestamp: " + last_log.systimestamp + " " + JoH.dateTimeText((long) last_log.systimestamp));
    }
    if (last_log != null && last_log.systimestamp > 0 && last_send_time <= last_log.systimestamp) {
        // startTime
        long last_send_success = last_send_time;
        Log.d(TAG, "getWearTreatmentsData last_send_time < last_log.timestamp:" + JoH.dateTimeText((long) last_log.systimestamp));
        List<Treatments> logs = Treatments.latestForGraphSystime(count, last_send_time);
        if (!logs.isEmpty() && logs.size() > min_count) {
            // Log.d(TAG, "getWearLogData count = " + logs.size());
            DataMap entries = dataMap(last_log);
            final ArrayList<DataMap> dataMaps = new ArrayList<>(logs.size());
            for (Treatments log : logs) {
                dataMaps.add(dataMap(log));
                last_send_success = (long) log.systimestamp;
            // Log.d(TAG, "getWearTreatmentsData set last_send_sucess:" + JoH.dateTimeText(last_send_sucess) + " Log:" + log.toString());
            }
            // MOST IMPORTANT LINE FOR TIMESTAMP
            entries.putLong("time", new Date().getTime());
            entries.putDataMapArrayList("entries", dataMaps);
            Log.i(TAG, "getWearTreatmentsData SYNCED treatments up to " + JoH.dateTimeText(last_send_success) + " count = " + logs.size());
            return entries;
        } else
            Log.i(TAG, "getWearTreatmentsData SYNCED treatments up to " + JoH.dateTimeText(last_send_success) + " count = 0");
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) Treatments(com.eveningoutpost.dexdrip.Models.Treatments) Date(java.util.Date) DataMap(com.google.android.gms.wearable.DataMap)

Example 13 with Treatments

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

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 14 with Treatments

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

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 15 with Treatments

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

the class TreatmentsTable method getData.

private void getData() {
    // 3 days
    final long startTime = new Date().getTime() - (60000 * 60 * 24 * 3);
    final List<Treatments> latest = Treatments.latestForGraph(60, startTime);
    ListAdapter adapter = new thisAdapter(this, latest);
    this.setListAdapter(adapter);
    String msg = "";
    int size = 0;
    if (latest != null)
        size = latest.size();
    if (size == 0) {
        msg = getResources().getString(R.string.notify_table_size, "Treatments", size);
        JoH.static_toast(xdrip.getAppContext(), msg, Toast.LENGTH_SHORT);
    }
}
Also used : Treatments(com.eveningoutpost.dexdrip.Models.Treatments) Date(java.util.Date) ListAdapter(android.widget.ListAdapter)

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