Search in sources :

Example 66 with DataMap

use of com.google.android.gms.wearable.DataMap in project xDrip-plus by jamorham.

the class ListenerService method syncCalibrationData.

private synchronized void syncCalibrationData(DataMap dataMap, Context context) {
    // KS
    Log.d(TAG, "syncCalibrationData");
    boolean changed = false;
    ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
    if (entries != null) {
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new DateTypeAdapter()).serializeSpecialFloatingPointValues().create();
        Log.d(TAG, "syncCalibrationData add Calibration Table entries count=" + entries.size());
        // ensure database has already been initialized
        Sensor.InitDb(context);
        Sensor sensor = Sensor.currentSensor();
        if (sensor != null) {
            for (DataMap entry : entries) {
                if (entry != null) {
                    String bgrecord = entry.getString("bgs");
                    if (bgrecord != null) {
                        Calibration bgData = gson.fromJson(bgrecord, Calibration.class);
                        Calibration exists = Calibration.findByUuid(bgData.uuid);
                        bgData.sensor = sensor;
                        if (exists != null) {
                            Log.d(TAG, "syncCalibrationData Calibration exists for uuid=" + bgData.uuid + " bg=" + bgData.bg + " timestamp=" + bgData.timestamp + " timeString=" + JoH.dateTimeText(bgData.timestamp));
                            if (exists.slope != bgData.slope || exists.slope_confidence != bgData.slope_confidence || exists.timestamp != bgData.timestamp || exists.bg != bgData.bg) {
                                // slope* indicates if shown on graph
                                changed = true;
                            }
                            exists.adjusted_raw_value = bgData.adjusted_raw_value;
                            exists.bg = bgData.bg;
                            exists.check_in = bgData.check_in;
                            exists.distance_from_estimate = bgData.distance_from_estimate;
                            exists.estimate_bg_at_time_of_calibration = bgData.estimate_bg_at_time_of_calibration;
                            exists.estimate_raw_at_time_of_calibration = bgData.estimate_raw_at_time_of_calibration;
                            exists.first_decay = bgData.first_decay;
                            exists.first_intercept = bgData.first_intercept;
                            exists.first_scale = bgData.first_scale;
                            exists.first_slope = bgData.first_slope;
                            exists.intercept = bgData.intercept;
                            exists.possible_bad = bgData.possible_bad;
                            exists.raw_timestamp = bgData.raw_timestamp;
                            exists.raw_value = bgData.raw_value;
                            exists.second_decay = bgData.second_decay;
                            exists.second_intercept = bgData.second_intercept;
                            exists.second_scale = bgData.second_scale;
                            exists.second_slope = bgData.second_slope;
                            exists.sensor = sensor;
                            exists.sensor_age_at_time_of_estimation = bgData.sensor_age_at_time_of_estimation;
                            exists.sensor_confidence = bgData.sensor_confidence;
                            exists.sensor_uuid = bgData.sensor_uuid;
                            exists.slope = bgData.slope;
                            exists.slope_confidence = bgData.slope_confidence;
                            exists.timestamp = bgData.timestamp;
                            exists.save();
                        } else {
                            changed = true;
                            bgData.save();
                            // final boolean adjustPast = mPrefs.getBoolean("rewrite_history", true);
                            Log.d(TAG, "syncCalibrationData Calibration does not exist for uuid=" + bgData.uuid + " timestamp=" + bgData.timestamp + " timeString=" + JoH.dateTimeText(bgData.timestamp));
                        // Calibration.adjustRecentBgReadings(adjustPast ? 30 : 2);
                        }
                        exists = Calibration.findByUuid(bgData.uuid);
                        if (exists != null)
                            Log.d(TAG, "syncCalibrationData Calibration GSON saved BG: " + exists.toS());
                        else
                            Log.d(TAG, "syncCalibrationData Calibration GSON NOT saved");
                    }
                }
            }
        } else {
            Log.d(TAG, "syncCalibrationData No Active Sensor!! Request WEARABLE_INITDB_PATH");
            sendData(WEARABLE_INITDB_PATH, null);
        }
        if (changed) {
            showTreatments(context, "cals");
        }
    }
}
Also used : DateTypeAdapter(com.google.gson.internal.bind.DateTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Calibration(com.eveningoutpost.dexdrip.Models.Calibration) DataMap(com.google.android.gms.wearable.DataMap) Sensor(com.eveningoutpost.dexdrip.Models.Sensor)

Example 67 with DataMap

use of com.google.android.gms.wearable.DataMap 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 68 with DataMap

use of com.google.android.gms.wearable.DataMap in project xDrip-plus by jamorham.

the class ListenerService method syncBgData.

private synchronized void syncBgData(DataMap dataMap, Context context) {
    // KS
    Log.d(TAG, "syncBGData");
    boolean changed = false;
    int battery = dataMap.getInt("battery");
    ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
    Log.d(TAG, "syncBGData add BgReading Table battery=" + battery);
    if (entries != null) {
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new DateTypeAdapter()).serializeSpecialFloatingPointValues().create();
        Log.d(TAG, "syncBGData add BgReading Table entries count=" + entries.size());
        // ensure database has already been initialized
        Sensor.InitDb(context);
        Sensor sensor = Sensor.currentSensor();
        if (sensor != null) {
            for (DataMap entry : entries) {
                if (entry != null) {
                    String bgrecord = entry.getString("bgs");
                    if (bgrecord != null) {
                        BgReading bgData = gson.fromJson(bgrecord, BgReading.class);
                        /*    // TODO this is a hack to use display glucose but it is incomplete regarding delta
                            if (bgData.dg_mgdl > 0) {
                                bgData.calculated_value = bgData.dg_mgdl;
                                bgData.calculated_value_slope = bgData.dg_slope;
                                // TODO delta missing???
                            }
*/
                        BgReading exists = BgReading.getForTimestampExists(bgData.timestamp);
                        exists = exists != null ? exists : BgReading.findByUuid(bgData.uuid);
                        String calibrationUuid = entry.getString("calibrationUuid");
                        if (exists != null) {
                            Log.d(TAG, "syncBGData BG already exists for uuid=" + bgData.uuid + " timestamp=" + bgData.timestamp + " timeString=" + JoH.dateTimeText(bgData.timestamp));
                            Log.d(TAG, "syncBGData exists timeString=" + JoH.dateTimeText(exists.timestamp) + "  exists.calibration.uuid=" + exists.calibration.uuid + " exists=" + exists.toS());
                            exists.filtered_calculated_value = bgData.filtered_calculated_value;
                            exists.calculated_value = bgData.calculated_value;
                            exists.hide_slope = bgData.hide_slope;
                            exists.filtered_data = bgData.filtered_data;
                            exists.raw_data = bgData.raw_data;
                            exists.raw_calculated = bgData.raw_calculated;
                            exists.calculated_value_slope = bgData.calculated_value_slope;
                            exists.age_adjusted_raw_value = bgData.age_adjusted_raw_value;
                            exists.calibration_flag = bgData.calibration_flag;
                            exists.ignoreForStats = bgData.ignoreForStats;
                            exists.time_since_sensor_started = bgData.time_since_sensor_started;
                            exists.ra = bgData.ra;
                            exists.rb = bgData.rb;
                            exists.rc = bgData.rc;
                            exists.a = bgData.a;
                            exists.b = bgData.b;
                            exists.c = bgData.c;
                            exists.noise = bgData.noise;
                            exists.time_since_sensor_started = bgData.time_since_sensor_started;
                            Calibration calibration = Calibration.byuuid(calibrationUuid);
                            calibration = calibration != null ? calibration : Calibration.byuuid(exists.calibration_uuid);
                            if (calibration != null) {
                                exists.calibration = calibration;
                                exists.calibration_uuid = calibration.uuid;
                                exists.sensor = sensor;
                                exists.sensor_uuid = sensor.uuid;
                                if (exists.calculated_value != bgData.calculated_value) {
                                    changed = true;
                                }
                                exists.save();
                            } else {
                                Log.e(TAG, "syncBGData existing BgReading calibrationUuid not found by byuuid; calibrationUuid=" + calibrationUuid + " bgData.calibration_uuid=" + bgData.calibration_uuid + " bgData.uuid=" + bgData.uuid + " timeString=" + JoH.dateTimeText(bgData.timestamp));
                            }
                        } else {
                            Calibration calibration = Calibration.byuuid(calibrationUuid);
                            calibration = calibration != null ? calibration : Calibration.byuuid(bgData.calibration_uuid);
                            if (calibration != null) {
                                Log.d(TAG, "syncBGData add BG; does NOT exist for uuid=" + bgData.uuid + " timestamp=" + bgData.timestamp + " timeString=" + JoH.dateTimeText(bgData.timestamp));
                                bgData.calibration = calibration;
                                bgData.calibration_uuid = calibration.uuid;
                                bgData.sensor = sensor;
                                bgData.sensor_uuid = sensor.uuid;
                                changed = true;
                                bgData.save();
                            } else {
                                Log.e(TAG, "syncBGData new BgReading calibrationUuid not found by byuuid; calibrationUuid=" + calibrationUuid + " bgData.calibration_uuid=" + bgData.calibration_uuid + " bgData.uuid=" + bgData.uuid + " timeString=" + JoH.dateTimeText(bgData.timestamp));
                            }
                        }
                    }
                }
            }
        } else {
            Log.d(TAG, "syncBGData No Active Sensor!! Request WEARABLE_INITDB_PATH");
            sendData(WEARABLE_INITDB_PATH, null);
        }
    }
    if (changed) {
        // otherwise, wait for doBackground ACTION_RESEND
        Log.d(TAG, "syncBGData BG data has changed, refresh watchface, phone battery=" + battery);
        resendData(getApplicationContext(), battery);
        CustomComplicationProviderService.refresh();
    } else
        Log.d(TAG, "syncBGData BG data has NOT changed, do not refresh watchface, phone battery=" + battery);
}
Also used : DateTypeAdapter(com.google.gson.internal.bind.DateTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Calibration(com.eveningoutpost.dexdrip.Models.Calibration) DataMap(com.google.android.gms.wearable.DataMap) Sensor(com.eveningoutpost.dexdrip.Models.Sensor)

Example 69 with DataMap

use of com.google.android.gms.wearable.DataMap in project xDrip-plus by jamorham.

the class ListenerService method getWearStepSensorData.

private synchronized DataMap getWearStepSensorData(int count, long last_send_time, int min_count) {
    // final int sensorType, final int accuracy, final long timestamp, final float[] values) {
    forceGoogleApiConnect();
    Log.d(TAG, "getWearStepSensorData last_send_time:" + JoH.dateTimeText(last_send_time));
    PebbleMovement last_log = PebbleMovement.last();
    if (last_log != null) {
        Log.d(TAG, "getWearStepSensorData last_log.timestamp:" + JoH.dateTimeText((long) last_log.timestamp));
    } else {
        Log.d(TAG, "getWearStepSensorData PebbleMovement.last() = null:");
    }
    if (last_log != null && last_send_time <= last_log.timestamp) {
        // startTime
        long last_send_success = last_send_time;
        Log.d(TAG, "getWearStepSensorData last_send_time < last_bg.timestamp:" + JoH.dateTimeText((long) last_log.timestamp));
        List<PebbleMovement> logs = PebbleMovement.latestForGraph(count, last_send_time);
        if (!logs.isEmpty() && logs.size() > min_count) {
            // Log.d(TAG, "getWearStepSensorData count = " + logs.size());
            DataMap entries = dataMap(last_log);
            final ArrayList<DataMap> dataMaps = new ArrayList<>(logs.size());
            for (PebbleMovement log : logs) {
                dataMaps.add(dataMap(log));
                last_send_success = (long) log.timestamp;
            // Log.d(TAG, "getWearStepSensorData set last_send_sucess:" + JoH.dateTimeText(last_send_success) + " pw.metric: " + log.metric + " pw.timestamp: " + JoH.dateTimeText(log.timestamp));
            }
            // MOST IMPORTANT LINE FOR TIMESTAMP
            entries.putLong("time", new Date().getTime());
            entries.putDataMapArrayList("entries", dataMaps);
            // Log.d(TAG, "getWearStepSensorData  entries:" + entries);
            Log.i(TAG, "getWearStepSensorData SYNCED steps up to " + JoH.dateTimeText(last_send_success) + " count = " + logs.size());
            return entries;
        } else
            Log.i(TAG, "getWearStepSensorData SYNCED steps up to " + JoH.dateTimeText(last_send_success) + " count = 0");
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) PebbleMovement(com.eveningoutpost.dexdrip.Models.PebbleMovement) Date(java.util.Date) DataMap(com.google.android.gms.wearable.DataMap)

Example 70 with DataMap

use of com.google.android.gms.wearable.DataMap in project xDrip-plus by jamorham.

the class BIGChart method addToWatchSetTreats.

public void addToWatchSetTreats(DataMap dataMap, ArrayList<BgWatchData> dataList) {
    if (d)
        Log.d(TAG, "addToWatchSetTreats dataList.size()=" + (dataList != null ? dataList.size() : "0"));
    // necessary since treatments, bloodtest and calibrations can be deleted/invalidated
    dataList.clear();
    ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
    if (entries != null) {
        if (d)
            Log.d(TAG, "addToWatchSetTreats entries.size()=" + entries.size() + " entries=" + entries);
        for (DataMap entry : entries) {
            addDataMapTreats(entry, dataList);
        }
    }
    if (d)
        Log.d(TAG, "addToWatchSetTreats dataList.size()=" + dataList.size());
}
Also used : DataMap(com.google.android.gms.wearable.DataMap)

Aggregations

DataMap (com.google.android.gms.wearable.DataMap)147 Date (java.util.Date)38 ArrayList (java.util.ArrayList)36 Gson (com.google.gson.Gson)18 Intent (android.content.Intent)17 GsonBuilder (com.google.gson.GsonBuilder)16 DateTypeAdapter (com.google.gson.internal.bind.DateTypeAdapter)16 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)12 Sensor (com.eveningoutpost.dexdrip.Models.Sensor)12 PendingIntent (android.app.PendingIntent)11 Context (android.content.Context)8 Calibration (com.eveningoutpost.dexdrip.Models.Calibration)8 Treatments (com.eveningoutpost.dexdrip.Models.Treatments)8 Bundle (android.os.Bundle)6 HeartRate (com.eveningoutpost.dexdrip.Models.HeartRate)6 Paint (android.graphics.Paint)5 BloodTest (com.eveningoutpost.dexdrip.Models.BloodTest)5 BroadcastReceiver (android.content.BroadcastReceiver)4 SharedPreferences (android.content.SharedPreferences)4 Point (android.graphics.Point)4