Search in sources :

Example 11 with DateTypeAdapter

use of com.google.gson.internal.bind.DateTypeAdapter 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 12 with DateTypeAdapter

use of com.google.gson.internal.bind.DateTypeAdapter in project xDrip-plus by jamorham.

the class Sensor method toS.

public String toS() {
    // KS
    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new DateTypeAdapter()).serializeSpecialFloatingPointValues().create();
    Log.d("SENSOR", "Sensor toS uuid=" + this.uuid + " started_at=" + this.started_at + " active=" + this.isActive() + " battery=" + this.latest_battery_level + " location=" + this.sensor_location + " stopped_at=" + this.stopped_at);
    return gson.toJson(this);
}
Also used : DateTypeAdapter(com.google.gson.internal.bind.DateTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson)

Example 13 with DateTypeAdapter

use of com.google.gson.internal.bind.DateTypeAdapter 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 14 with DateTypeAdapter

use of com.google.gson.internal.bind.DateTypeAdapter in project xDrip by NightscoutFoundation.

the class ListenerService method syncBloodTestData.

private synchronized void syncBloodTestData(DataMap dataMap, Context context) {
    // KS
    Log.d(TAG, "syncBloodTestData");
    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, "syncBloodTestData add BloodTest 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) {
                    BloodTest data = gson.fromJson(record, BloodTest.class);
                    BloodTest exists = BloodTest.byUUID(data.uuid);
                    if (exists != null) {
                        Log.d(TAG, "syncBloodTestData save existing BloodTest for uuid=" + data.uuid + " timestamp=" + data.timestamp + " timeString=" + JoH.dateTimeText(data.timestamp) + " mgdl=" + data.mgdl + " state=" + data.state);
                        if (exists.mgdl != data.mgdl || exists.state != data.state || exists.timestamp != data.timestamp) {
                            // state indicates if deleted
                            changed = true;
                        }
                        exists.mgdl = data.mgdl;
                        exists.created_timestamp = data.created_timestamp;
                        exists.source = data.source;
                        exists.state = data.state;
                        exists.timestamp = data.timestamp;
                        exists.save();
                    } else {
                        changed = true;
                        data.save();
                        Log.d(TAG, "syncBloodTestData create new BloodTest for uuid=" + data.uuid + " timestamp=" + data.timestamp + " timeString=" + JoH.dateTimeText(data.timestamp) + " mgdl=" + data.mgdl + " state=" + data.state);
                    }
                }
            }
        }
        if (changed) {
            showTreatments(context, "bts");
        }
    }
}
Also used : DateTypeAdapter(com.google.gson.internal.bind.DateTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) BloodTest(com.eveningoutpost.dexdrip.Models.BloodTest) Gson(com.google.gson.Gson) DataMap(com.google.android.gms.wearable.DataMap)

Example 15 with DateTypeAdapter

use of com.google.gson.internal.bind.DateTypeAdapter in project xDrip by NightscoutFoundation.

the class ListenerService method syncAlertTypeData.

private void syncAlertTypeData(DataMap dataMap, Context context) {
    // KS
    Log.d(TAG, "syncAlertTypeData");
    ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
    if (entries != null) {
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new DateTypeAdapter()).serializeSpecialFloatingPointValues().create();
        Log.d(TAG, "syncAlertTypeData add AlertType Table entries count=" + entries.size());
        // ensure database has already been initialized
        Sensor.InitDb(context);
        AlertType.remove_all();
        for (DataMap entry : entries) {
            if (entry != null) {
                String alertrecord = entry.getString("alert");
                if (alertrecord != null) {
                    AlertType data = gson.fromJson(alertrecord, AlertType.class);
                    AlertType exists = AlertType.get_alert(data.uuid);
                    if (exists != null) {
                        Log.d(TAG, "syncAlertTypeData AlertType exists for uuid=" + data.uuid + " name=" + data.name);
                        exists.name = data.name;
                        exists.active = data.active;
                        exists.volume = data.volume;
                        exists.vibrate = data.vibrate;
                        exists.light = data.light;
                        exists.override_silent_mode = data.override_silent_mode;
                        exists.predictive = data.predictive;
                        exists.time_until_threshold_crossed = data.time_until_threshold_crossed;
                        exists.above = data.above;
                        exists.threshold = data.threshold;
                        exists.all_day = data.all_day;
                        exists.start_time_minutes = data.start_time_minutes;
                        exists.end_time_minutes = data.end_time_minutes;
                        exists.minutes_between = data.minutes_between;
                        exists.default_snooze = data.default_snooze;
                        exists.text = data.text;
                        exists.mp3_file = data.mp3_file;
                        exists.save();
                    } else {
                        data.save();
                        Log.d(TAG, "syncAlertTypeData AlertType does not exist for uuid=" + data.uuid);
                    }
                    exists = AlertType.get_alert(data.uuid);
                    if (exists != null)
                        Log.d(TAG, "syncAlertTypeData AlertType GSON saved BG: " + exists.toS());
                    else
                        Log.d(TAG, "syncAlertTypeData AlertType GSON NOT saved");
                }
            }
        }
    }
}
Also used : AlertType(com.eveningoutpost.dexdrip.Models.AlertType) DateTypeAdapter(com.google.gson.internal.bind.DateTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) DataMap(com.google.android.gms.wearable.DataMap)

Aggregations

Gson (com.google.gson.Gson)24 GsonBuilder (com.google.gson.GsonBuilder)24 DateTypeAdapter (com.google.gson.internal.bind.DateTypeAdapter)24 DataMap (com.google.android.gms.wearable.DataMap)16 Sensor (com.eveningoutpost.dexdrip.Models.Sensor)6 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)4 Calibration (com.eveningoutpost.dexdrip.Models.Calibration)4 SharedPreferences (android.content.SharedPreferences)2 Select (com.activeandroid.query.Select)2 AlertType (com.eveningoutpost.dexdrip.Models.AlertType)2 BloodTest (com.eveningoutpost.dexdrip.Models.BloodTest)2 StepCounter (com.eveningoutpost.dexdrip.Models.StepCounter)2 TransmitterData (com.eveningoutpost.dexdrip.Models.TransmitterData)2 Treatments (com.eveningoutpost.dexdrip.Models.Treatments)2 UserError (com.eveningoutpost.dexdrip.Models.UserError)2