Search in sources :

Example 6 with DateTypeAdapter

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

the class WatchUpdaterService method syncLogData.

private synchronized void syncLogData(DataMap dataMap, boolean bBenchmark) {
    // KS
    Log.d(TAG, "syncLogData");
    long watch_syncLogsRequested = dataMap.getLong("syncLogsRequested", -1);
    ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
    long timeOfLastEntry = 0;
    int saved = 0;
    if (entries != null) {
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new DateTypeAdapter()).serializeSpecialFloatingPointValues().create();
        Log.d(TAG, "syncLogData add Table entries count=" + entries.size() + " watch_syncLogsRequested=" + watch_syncLogsRequested);
        for (DataMap entry : entries) {
            if (entry != null) {
                String record = entry.getString("entry");
                if (record != null) {
                    UserError data = gson.fromJson(record, UserError.class);
                    if (data != null) {
                        timeOfLastEntry = (long) data.timestamp + 1;
                        if (data.shortError != null && !data.shortError.isEmpty()) {
                            // add wear prefix
                            if (!data.shortError.startsWith("wear")) {
                                data.shortError = mPrefs.getString("wear_logs_prefix", "wear") + data.shortError;
                            }
                        }
                        UserError exists = UserError.getForTimestamp(data);
                        if (exists == null && !bBenchmark) {
                            data.save();
                            saved++;
                        } else {
                        // Log.d(TAG, "syncLogData Log entry already exists with shortError=" + data.shortError + " timestamp=" + JoH.dateTimeText((long)data.timestamp));
                        }
                    }
                }
            }
        }
        if (saved > 0) {
            Log.d(TAG, "syncLogData Saved timeOfLastEntry=" + JoH.dateTimeText(timeOfLastEntry) + " saved=" + saved);
        } else {
            Log.d(TAG, "syncLogData No records saved due to being duplicates! timeOfLastEntry=" + JoH.dateTimeText(timeOfLastEntry) + " count=" + entries.size());
        }
        sendDataReceived(DATA_ITEM_RECEIVED_PATH, "DATA_RECEIVED_LOGS count=" + entries.size(), timeOfLastEntry, bBenchmark ? "BM" : "LOG", watch_syncLogsRequested);
    }
}
Also used : UserError(com.eveningoutpost.dexdrip.Models.UserError) DateTypeAdapter(com.google.gson.internal.bind.DateTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) DataMap(com.google.android.gms.wearable.DataMap)

Example 7 with DateTypeAdapter

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

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)

Example 8 with DateTypeAdapter

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

the class NewCalibration method newCalibrationToJson.

private static String newCalibrationToJson(double bgValue, String uuid, long offset) {
    NewCalibration[] newCalibrationArray = new NewCalibration[1];
    NewCalibration newCalibration = new NewCalibration();
    newCalibration.bgValue = bgValue;
    newCalibration.uuid = uuid;
    newCalibration.timestamp = JoH.tsl();
    newCalibration.offset = offset;
    newCalibrationArray[0] = newCalibration;
    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new DateTypeAdapter()).serializeSpecialFloatingPointValues().create();
    String output = gson.toJson(newCalibrationArray);
    Log.d(TAG, "newCalibrationToJson Created the string " + output);
    return output;
}
Also used : DateTypeAdapter(com.google.gson.internal.bind.DateTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson)

Example 9 with DateTypeAdapter

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

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

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