Search in sources :

Example 1 with InfluxDBUploader

use of com.eveningoutpost.dexdrip.InfluxDB.InfluxDBUploader in project xDrip by NightscoutFoundation.

the class UploaderTask method doInBackground.

public Void doInBackground(String... urls) {
    try {
        final List<Long> circuits = new ArrayList<>();
        final List<String> types = new ArrayList<>();
        types.add(BgReading.class.getSimpleName());
        types.add(Calibration.class.getSimpleName());
        types.add(BloodTest.class.getSimpleName());
        types.add(Treatments.class.getSimpleName());
        if (Pref.getBooleanDefaultFalse("wear_sync")) {
            circuits.add(UploaderQueue.WATCH_WEARAPI);
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_mongodb_enable")) {
            circuits.add(UploaderQueue.MONGO_DIRECT);
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_api_enable")) {
            if ((Pref.getBoolean("cloud_storage_api_use_mobile", true) || (JoH.isLANConnected()))) {
                circuits.add(UploaderQueue.NIGHTSCOUT_RESTAPI);
            } else {
                Log.e(TAG, "Skipping Nightscout upload due to mobile data only");
            }
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_influxdb_enable")) {
            circuits.add(UploaderQueue.INFLUXDB_RESTAPI);
        }
        for (long THIS_QUEUE : circuits) {
            final List<BgReading> bgReadings = new ArrayList<>();
            final List<Calibration> calibrations = new ArrayList<>();
            final List<BloodTest> bloodtests = new ArrayList<>();
            final List<Treatments> treatmentsAdd = new ArrayList<>();
            final List<String> treatmentsDel = new ArrayList<>();
            final List<UploaderQueue> items = new ArrayList<>();
            for (String type : types) {
                final List<UploaderQueue> bgups = UploaderQueue.getPendingbyType(type, THIS_QUEUE);
                if (bgups != null) {
                    for (UploaderQueue up : bgups) {
                        switch(up.action) {
                            case "insert":
                            case "update":
                            case "create":
                                items.add(up);
                                if (type.equals(BgReading.class.getSimpleName())) {
                                    final BgReading this_bg = BgReading.byid(up.reference_id);
                                    if (this_bg != null) {
                                        bgReadings.add(this_bg);
                                    } else {
                                        Log.wtf(TAG, "BgReading with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(Calibration.class.getSimpleName())) {
                                    final Calibration this_cal = Calibration.byid(up.reference_id);
                                    if ((this_cal != null) && (this_cal.isValid())) {
                                        calibrations.add(this_cal);
                                    } else {
                                        Log.wtf(TAG, "Calibration with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(BloodTest.class.getSimpleName())) {
                                    final BloodTest this_bt = BloodTest.byid(up.reference_id);
                                    if (this_bt != null) {
                                        bloodtests.add(this_bt);
                                    } else {
                                        Log.wtf(TAG, "Bloodtest with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(Treatments.class.getSimpleName())) {
                                    final Treatments this_treat = Treatments.byid(up.reference_id);
                                    if (this_treat != null) {
                                        treatmentsAdd.add(this_treat);
                                    } else {
                                        Log.wtf(TAG, "Treatments with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                }
                                break;
                            case "delete":
                                if ((THIS_QUEUE == UploaderQueue.WATCH_WEARAPI || THIS_QUEUE == UploaderQueue.NIGHTSCOUT_RESTAPI) && type.equals(Treatments.class.getSimpleName())) {
                                    items.add(up);
                                    Log.wtf(TAG, "Delete Treatments with ID: " + up.reference_uuid);
                                    treatmentsDel.add(up.reference_uuid);
                                } else if (up.reference_uuid != null) {
                                    Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " delete not yet implemented: " + up.reference_uuid);
                                    // mark as completed so as not to tie up the queue for now
                                    up.completed(THIS_QUEUE);
                                }
                                break;
                            default:
                                Log.e(TAG, "Unsupported operation type for " + type + " " + up.action);
                                break;
                        }
                    }
                }
            }
            if ((bgReadings.size() > 0) || (calibrations.size() > 0) || (bloodtests.size() > 0) || (treatmentsAdd.size() > 0 || treatmentsDel.size() > 0) || (UploaderQueue.getPendingbyType(Treatments.class.getSimpleName(), THIS_QUEUE, 1).size() > 0)) {
                Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " Processing: " + bgReadings.size() + " BgReadings and " + calibrations.size() + " Calibrations " + bloodtests.size() + " bloodtests " + treatmentsAdd.size() + " treatmentsAdd " + treatmentsDel.size() + " treatmentsDel");
                boolean uploadStatus = false;
                if (THIS_QUEUE == UploaderQueue.MONGO_DIRECT) {
                    final NightscoutUploader uploader = new NightscoutUploader(xdrip.getAppContext());
                    uploadStatus = uploader.uploadMongo(bgReadings, calibrations, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.NIGHTSCOUT_RESTAPI) {
                    final NightscoutUploader uploader = new NightscoutUploader(xdrip.getAppContext());
                    uploadStatus = uploader.uploadRest(bgReadings, bloodtests, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.INFLUXDB_RESTAPI) {
                    final InfluxDBUploader influxDBUploader = new InfluxDBUploader(xdrip.getAppContext());
                    uploadStatus = influxDBUploader.upload(bgReadings, calibrations, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.WATCH_WEARAPI) {
                    uploadStatus = WatchUpdaterService.sendWearUpload(bgReadings, calibrations, bloodtests, treatmentsAdd, treatmentsDel);
                }
                // TODO some kind of fail counter?
                if (uploadStatus) {
                    for (UploaderQueue up : items) {
                        // approve all types for this queue
                        up.completed(THIS_QUEUE);
                    }
                    Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " Marking: " + items.size() + " Items as successful");
                    if (PersistentStore.getBoolean(BACKFILLING_BOOSTER)) {
                        Log.d(TAG, "Scheduling boosted repeat query");
                        SyncService.startSyncService(2000);
                    }
                }
            } else {
                Log.d(TAG, "Nothing to upload for: " + UploaderQueue.getCircuitName(THIS_QUEUE));
                if (PersistentStore.getBoolean(BACKFILLING_BOOSTER)) {
                    PersistentStore.setBoolean(BACKFILLING_BOOSTER, false);
                    Log.d(TAG, "Switched off backfilling booster");
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "caught exception", e);
        exception = e;
        return null;
    }
    return null;
}
Also used : BloodTest(com.eveningoutpost.dexdrip.Models.BloodTest) ArrayList(java.util.ArrayList) Calibration(com.eveningoutpost.dexdrip.Models.Calibration) InfluxDBUploader(com.eveningoutpost.dexdrip.InfluxDB.InfluxDBUploader) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Treatments(com.eveningoutpost.dexdrip.Models.Treatments)

Example 2 with InfluxDBUploader

use of com.eveningoutpost.dexdrip.InfluxDB.InfluxDBUploader in project xDrip-plus by jamorham.

the class UploaderTask method doInBackground.

public Void doInBackground(String... urls) {
    try {
        final List<Long> circuits = new ArrayList<>();
        final List<String> types = new ArrayList<>();
        types.add(BgReading.class.getSimpleName());
        types.add(Calibration.class.getSimpleName());
        types.add(BloodTest.class.getSimpleName());
        types.add(Treatments.class.getSimpleName());
        if (Pref.getBooleanDefaultFalse("wear_sync")) {
            circuits.add(UploaderQueue.WATCH_WEARAPI);
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_mongodb_enable")) {
            circuits.add(UploaderQueue.MONGO_DIRECT);
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_api_enable")) {
            if ((Pref.getBoolean("cloud_storage_api_use_mobile", true) || (JoH.isLANConnected()))) {
                circuits.add(UploaderQueue.NIGHTSCOUT_RESTAPI);
            } else {
                Log.e(TAG, "Skipping Nightscout upload due to mobile data only");
            }
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_influxdb_enable")) {
            circuits.add(UploaderQueue.INFLUXDB_RESTAPI);
        }
        for (long THIS_QUEUE : circuits) {
            final List<BgReading> bgReadings = new ArrayList<>();
            final List<Calibration> calibrations = new ArrayList<>();
            final List<BloodTest> bloodtests = new ArrayList<>();
            final List<Treatments> treatmentsAdd = new ArrayList<>();
            final List<String> treatmentsDel = new ArrayList<>();
            final List<UploaderQueue> items = new ArrayList<>();
            for (String type : types) {
                final List<UploaderQueue> bgups = UploaderQueue.getPendingbyType(type, THIS_QUEUE);
                if (bgups != null) {
                    for (UploaderQueue up : bgups) {
                        switch(up.action) {
                            case "insert":
                            case "update":
                            case "create":
                                items.add(up);
                                if (type.equals(BgReading.class.getSimpleName())) {
                                    final BgReading this_bg = BgReading.byid(up.reference_id);
                                    if (this_bg != null) {
                                        bgReadings.add(this_bg);
                                    } else {
                                        Log.wtf(TAG, "BgReading with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(Calibration.class.getSimpleName())) {
                                    final Calibration this_cal = Calibration.byid(up.reference_id);
                                    if ((this_cal != null) && (this_cal.isValid())) {
                                        calibrations.add(this_cal);
                                    } else {
                                        Log.wtf(TAG, "Calibration with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(BloodTest.class.getSimpleName())) {
                                    final BloodTest this_bt = BloodTest.byid(up.reference_id);
                                    if (this_bt != null) {
                                        bloodtests.add(this_bt);
                                    } else {
                                        Log.wtf(TAG, "Bloodtest with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(Treatments.class.getSimpleName())) {
                                    final Treatments this_treat = Treatments.byid(up.reference_id);
                                    if (this_treat != null) {
                                        treatmentsAdd.add(this_treat);
                                    } else {
                                        Log.wtf(TAG, "Treatments with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                }
                                break;
                            case "delete":
                                if ((THIS_QUEUE == UploaderQueue.WATCH_WEARAPI || THIS_QUEUE == UploaderQueue.NIGHTSCOUT_RESTAPI) && type.equals(Treatments.class.getSimpleName())) {
                                    items.add(up);
                                    Log.wtf(TAG, "Delete Treatments with ID: " + up.reference_uuid);
                                    treatmentsDel.add(up.reference_uuid);
                                } else if (up.reference_uuid != null) {
                                    Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " delete not yet implemented: " + up.reference_uuid);
                                    // mark as completed so as not to tie up the queue for now
                                    up.completed(THIS_QUEUE);
                                }
                                break;
                            default:
                                Log.e(TAG, "Unsupported operation type for " + type + " " + up.action);
                                break;
                        }
                    }
                }
            }
            if ((bgReadings.size() > 0) || (calibrations.size() > 0) || (bloodtests.size() > 0) || (treatmentsAdd.size() > 0 || treatmentsDel.size() > 0) || (UploaderQueue.getPendingbyType(Treatments.class.getSimpleName(), THIS_QUEUE, 1).size() > 0)) {
                Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " Processing: " + bgReadings.size() + " BgReadings and " + calibrations.size() + " Calibrations " + bloodtests.size() + " bloodtests " + treatmentsAdd.size() + " treatmentsAdd " + treatmentsDel.size() + " treatmentsDel");
                boolean uploadStatus = false;
                if (THIS_QUEUE == UploaderQueue.MONGO_DIRECT) {
                    final NightscoutUploader uploader = new NightscoutUploader(xdrip.getAppContext());
                    uploadStatus = uploader.uploadMongo(bgReadings, calibrations, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.NIGHTSCOUT_RESTAPI) {
                    final NightscoutUploader uploader = new NightscoutUploader(xdrip.getAppContext());
                    uploadStatus = uploader.uploadRest(bgReadings, bloodtests, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.INFLUXDB_RESTAPI) {
                    final InfluxDBUploader influxDBUploader = new InfluxDBUploader(xdrip.getAppContext());
                    uploadStatus = influxDBUploader.upload(bgReadings, calibrations, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.WATCH_WEARAPI) {
                    uploadStatus = WatchUpdaterService.sendWearUpload(bgReadings, calibrations, bloodtests, treatmentsAdd, treatmentsDel);
                }
                // TODO some kind of fail counter?
                if (uploadStatus) {
                    for (UploaderQueue up : items) {
                        // approve all types for this queue
                        up.completed(THIS_QUEUE);
                    }
                    Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " Marking: " + items.size() + " Items as successful");
                    if (PersistentStore.getBoolean(BACKFILLING_BOOSTER)) {
                        Log.d(TAG, "Scheduling boosted repeat query");
                        SyncService.startSyncService(2000);
                    }
                }
            } else {
                Log.d(TAG, "Nothing to upload for: " + UploaderQueue.getCircuitName(THIS_QUEUE));
                if (PersistentStore.getBoolean(BACKFILLING_BOOSTER)) {
                    PersistentStore.setBoolean(BACKFILLING_BOOSTER, false);
                    Log.d(TAG, "Switched off backfilling booster");
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "caught exception", e);
        exception = e;
        return null;
    }
    return null;
}
Also used : BloodTest(com.eveningoutpost.dexdrip.Models.BloodTest) ArrayList(java.util.ArrayList) Calibration(com.eveningoutpost.dexdrip.Models.Calibration) InfluxDBUploader(com.eveningoutpost.dexdrip.InfluxDB.InfluxDBUploader) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Treatments(com.eveningoutpost.dexdrip.Models.Treatments)

Aggregations

InfluxDBUploader (com.eveningoutpost.dexdrip.InfluxDB.InfluxDBUploader)2 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)2 BloodTest (com.eveningoutpost.dexdrip.Models.BloodTest)2 Calibration (com.eveningoutpost.dexdrip.Models.Calibration)2 Treatments (com.eveningoutpost.dexdrip.Models.Treatments)2 ArrayList (java.util.ArrayList)2