Search in sources :

Example 36 with BgReading

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

the class BgReadingTable method getData.

/*@Override
    public void onNavigationDrawerItemSelected(int position) {
        mNavigationDrawerFragment.swapContext(position);
    }*/
private void getData() {
    // 3 days
    final long startTime = new Date().getTime() - (60000 * 60 * 24 * 3);
    final List<BgReading> latest = BgReading.latestForGraph(216, startTime);
    ListAdapter adapter = new BgReadingAdapter(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, "BgReading", size);
        JoH.static_toast(xdrip.getAppContext(), msg, Toast.LENGTH_SHORT);
    }
}
Also used : BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Date(java.util.Date) ListAdapter(android.widget.ListAdapter)

Example 37 with BgReading

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

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)

Example 38 with BgReading

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

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 39 with BgReading

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

the class ListenerService method resetDataToLatest.

private boolean resetDataToLatest(DataMap dataMap, Context context) {
    // KS
    if (dataMap != null) {
        Double dmTimestamp = dataMap.getDouble("timestamp");
        Log.d(TAG, "resetDataToLatest dataMap.datetime=" + JoH.dateTimeText(dmTimestamp.longValue()) + " dataMap.sgvDouble=" + dataMap.getDouble("sgvDouble"));
        // ensure database has already been initialized
        Sensor.InitDb(context);
        final BgReading last = BgReading.last();
        if (last != null) {
            long bgTimestamp = last.timestamp;
            Log.d(TAG, "resetDataToLatest last.timestamp=" + JoH.dateTimeText(bgTimestamp) + " last.calculated_value=" + last.calculated_value);
            if (bgTimestamp > dmTimestamp) {
                dataMap(dataMap, last, mPrefs, new com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder(context));
                return true;
            }
        }
    }
    return false;
}
Also used : BgReading(com.eveningoutpost.dexdrip.Models.BgReading)

Example 40 with BgReading

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

the class WatchUpdaterService method syncTransmitterData.

private synchronized void syncTransmitterData(DataMap dataMap, boolean bBenchmark) {
    // KS
    Log.d(TAG, "syncTransmitterData");
    ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
    long timeOfLastBG = 0;
    Log.d(TAG, "syncTransmitterData add BgReading Table");
    if (entries != null) {
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new DateTypeAdapter()).serializeSpecialFloatingPointValues().create();
        int idx = 0;
        int count = entries.size();
        Log.d(TAG, "syncTransmitterData add BgReading Table entries count=" + count);
        for (DataMap entry : entries) {
            if (entry != null) {
                // Log.d(TAG, "syncTransmitterData add BgReading Table entry=" + entry);
                idx++;
                String bgrecord = entry.getString("bgs");
                if (bgrecord != null) {
                    // for (TransmitterData bgData : bgs) {
                    // Log.d(TAG, "syncTransmitterData add TransmitterData Table bgrecord=" + bgrecord);
                    TransmitterData bgData = gson.fromJson(bgrecord, TransmitterData.class);
                    // TransmitterData bgData = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().fromJson(bgrecord, TransmitterData.class);
                    TransmitterData exists = TransmitterData.getForTimestamp(bgData.timestamp);
                    TransmitterData uuidexists = TransmitterData.findByUuid(bgData.uuid);
                    timeOfLastBG = bgData.timestamp + 1;
                    if (exists != null || uuidexists != null) {
                        Log.d(TAG, "syncTransmitterData BG already exists for uuid=" + bgData.uuid + " timestamp=" + bgData.timestamp + " timeString=" + JoH.dateTimeText(bgData.timestamp) + " raw_data=" + bgData.raw_data);
                    } else {
                        Log.d(TAG, "syncTransmitterData add BG; does NOT exist for uuid=" + bgData.uuid + " timestamp=" + bgData.timestamp + " timeString=" + JoH.dateTimeText(bgData.timestamp) + " raw_data=" + bgData.raw_data);
                        if (!bBenchmark) {
                            bgData.save();
                            // Check
                            if (TransmitterData.findByUuid(bgData.uuid) != null)
                                Log.d(TAG, "syncTransmitterData: TransmitterData was saved for uuid:" + bgData.uuid);
                            else {
                                Log.e(TAG, "syncTransmitterData: TransmitterData was NOT saved for uuid:" + bgData.uuid);
                                return;
                            }
                            // KS the following is from G5CollectionService processNewTransmitterData()
                            Sensor sensor = Sensor.currentSensor();
                            if (sensor == null) {
                                Log.e(TAG, "syncTransmitterData: No Active Sensor, Data only stored in Transmitter Data");
                                return;
                            }
                            // TODO : LOG if unfiltered or filtered values are zero
                            Sensor.updateBatteryLevel(sensor, bgData.sensor_battery_level);
                            // android.util.Log.i
                            Log.i(TAG, "syncTransmitterData: BG timestamp create " + Long.toString(bgData.timestamp));
                            BgReading bgExists;
                            // KS TODO wear implements limited alerts, therefore continue to process all alerts on phone for last entry
                            if (count > 1 && idx < count) {
                                // Disable Notifications for bulk insert
                                bgExists = BgReading.create(bgData.raw_data, bgData.filtered_data, this, bgData.timestamp, true);
                            } else {
                                bgExists = BgReading.create(bgData.raw_data, bgData.filtered_data, this, bgData.timestamp);
                            }
                            if (bgExists != null)
                                Log.d(TAG, "syncTransmitterData BG GSON saved BG: " + bgExists.toS());
                            else
                                Log.e(TAG, "syncTransmitterData BG GSON NOT saved");
                        }
                    }
                }
            }
        }
        sendDataReceived(DATA_ITEM_RECEIVED_PATH, "DATA_RECEIVED_BGS count=" + entries.size(), timeOfLastBG, bBenchmark ? "BM" : "BG", -1);
    }
}
Also used : TransmitterData(com.eveningoutpost.dexdrip.Models.TransmitterData) DateTypeAdapter(com.google.gson.internal.bind.DateTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) DataMap(com.google.android.gms.wearable.DataMap) Sensor(com.eveningoutpost.dexdrip.Models.Sensor)

Aggregations

BgReading (com.eveningoutpost.dexdrip.Models.BgReading)66 Date (java.util.Date)26 Calibration (com.eveningoutpost.dexdrip.Models.Calibration)20 Sensor (com.eveningoutpost.dexdrip.Models.Sensor)16 ArrayList (java.util.ArrayList)14 DataMap (com.google.android.gms.wearable.DataMap)12 Intent (android.content.Intent)8 Treatments (com.eveningoutpost.dexdrip.Models.Treatments)8 IOException (java.io.IOException)8 JSONException (org.json.JSONException)8 PendingIntent (android.app.PendingIntent)6 Paint (android.graphics.Paint)6 BloodTest (com.eveningoutpost.dexdrip.Models.BloodTest)6 TransmitterData (com.eveningoutpost.dexdrip.Models.TransmitterData)6 BgGraphBuilder (com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder)6 CalibrationAbstract (com.eveningoutpost.dexdrip.calibrations.CalibrationAbstract)6 DecimalFormat (java.text.DecimalFormat)6 JSONArray (org.json.JSONArray)6 Point (android.graphics.Point)4 SpannableString (android.text.SpannableString)4