Search in sources :

Example 46 with DataMap

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

the class WatchUpdaterService method sendWearCalibrationData.

private static boolean sendWearCalibrationData(Integer count, long startTime, List<Calibration> list) {
    try {
        if (googleApiClient != null && !googleApiClient.isConnected() && !googleApiClient.isConnecting()) {
            googleApiClient.connect();
        }
        // if ((googleApiClient != null) && (googleApiClient.isConnected())) {
        if (googleApiClient != null) {
            Log.d(TAG, "sendWearCalibrationData");
            final Sensor sensor = Sensor.currentSensor();
            final Calibration last = list != null && list.size() > 0 ? list.get(0) : Calibration.last();
            List<Calibration> latest;
            BgReading lastBgReading = BgReading.last();
            // From BgReading:     lastBgReading.calibration.rawValueOverride()
            if (list != null)
                latest = list;
            else if (startTime != 0)
                latest = Calibration.latestForGraphSensor(count, startTime, Long.MAX_VALUE);
            else if (lastBgReading != null && lastBgReading.calibration != null && lastBgReading.calibration_flag == true) {
                Log.d(TAG, "sendWearCalibrationData lastBgReading.calibration_flag=" + lastBgReading.calibration_flag + " lastBgReading.timestamp: " + lastBgReading.timestamp + " lastBgReading.calibration.timestamp: " + lastBgReading.calibration.timestamp);
                latest = Calibration.allForSensor();
            } else {
                latest = Calibration.latest(count);
            }
            if ((sensor != null) && (last != null) && (latest != null && !latest.isEmpty())) {
                Log.d(TAG, "sendWearCalibrationData latest count = " + latest.size());
                final DataMap entries = dataMap(last);
                final ArrayList<DataMap> dataMaps = new ArrayList<>(latest.size());
                if (sensor.uuid != null) {
                    for (Calibration bg : latest) {
                        if ((bg != null) && (bg.sensor_uuid != null) && (bg.sensor_uuid.equals(sensor.uuid))) {
                            dataMaps.add(dataMap(bg));
                        }
                    }
                }
                // MOST IMPORTANT LINE FOR TIMESTAMP
                entries.putLong("time", new Date().getTime());
                entries.putDataMapArrayList("entries", dataMaps);
                new SendToDataLayerThread(WEARABLE_CALIBRATION_DATA_PATH, googleApiClient).executeOnExecutor(xdrip.executor, entries);
            } else
                Log.d(TAG, "sendWearCalibrationData latest count = 0");
        } else {
            Log.e(TAG, "sendWearCalibrationData No connection to wearable available for send treatment!");
            return false;
        }
    } catch (NullPointerException e) {
        Log.e(TAG, "Nullpointer exception in sendWearCalibrationData: " + e);
        return false;
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) Calibration(com.eveningoutpost.dexdrip.Models.Calibration) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Date(java.util.Date) Sensor(com.eveningoutpost.dexdrip.Models.Sensor) DataMap(com.google.android.gms.wearable.DataMap)

Example 47 with DataMap

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

the class WatchUpdaterService method dataMap.

private static DataMap dataMap(Calibration bg) {
    // KS
    DataMap dataMap = new DataMap();
    String json = bg.toS();
    Log.d(TAG, "dataMap BG GSON: " + json);
    dataMap.putString("bgs", json);
    return dataMap;
}
Also used : DataMap(com.google.android.gms.wearable.DataMap)

Example 48 with DataMap

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

the class WatchUpdaterService method sendActiveBtDeviceData.

private void sendActiveBtDeviceData() {
    // KS
    if (is_using_bt) {
        // only required for Collector running on watch
        forceGoogleApiConnect();
        ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
        if (btDevice != null) {
            if (wear_integration) {
                DataMap dataMap = new DataMap();
                Log.d(TAG, "sendActiveBtDeviceData name=" + btDevice.name + " address=" + btDevice.address + " connected=" + btDevice.connected);
                // MOST IMPORTANT LINE FOR TIMESTAMP
                dataMap.putLong("time", new Date().getTime());
                dataMap.putString("name", btDevice.name);
                dataMap.putString("address", btDevice.address);
                dataMap.putBoolean("connected", btDevice.connected);
                new SendToDataLayerThread(WEARABLE_ACTIVEBTDEVICE_DATA_PATH, googleApiClient).executeOnExecutor(xdrip.executor, dataMap);
            }
        }
    } else {
        Log.d(TAG, "Not sending activebluetoothdevice data as we are not using bt");
    }
}
Also used : ActiveBluetoothDevice(com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice) Date(java.util.Date) DataMap(com.google.android.gms.wearable.DataMap)

Example 49 with DataMap

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

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

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