com.eveningoutpost.dexdrip.Services.SyncService Java Examples

The following examples show how to use com.eveningoutpost.dexdrip.Services.SyncService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Treatments.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
private static void pushTreatmentSync(Treatments treatment, boolean is_new, String suggested_uuid) {

        if (Home.get_master_or_follower()) GcmActivity.pushTreatmentAsync(treatment);

        if (!(Pref.getBoolean("cloud_storage_api_enable", false) || Pref.getBoolean("cloud_storage_mongodb_enable", false))) {
            NSClientChat.pushTreatmentAsync(treatment);
        } else {
            Log.d(TAG, "Skipping NSClient treatment broadcast as nightscout direct sync is enabled");
        }

        if (suggested_uuid == null) {
            // only sync to nightscout if source of change was not from nightscout
            if (UploaderQueue.newEntry(is_new ? "insert" : "update", treatment) != null) {
                SyncService.startSyncService(3000); // sync in 3 seconds
            }
        }
    }
 
Example #2
Source File: Treatments.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
private static void pushTreatmentSync(Treatments treatment, boolean is_new, String suggested_uuid) {

        if (Home.get_master_or_follower()) GcmActivity.pushTreatmentAsync(treatment);

        if (!(Pref.getBoolean("cloud_storage_api_enable", false) || Pref.getBoolean("cloud_storage_mongodb_enable", false))) {
            NSClientChat.pushTreatmentAsync(treatment);
        } else {
            Log.d(TAG, "Skipping NSClient treatment broadcast as nightscout direct sync is enabled");
        }

        if (suggested_uuid == null) {
            // only sync to nightscout if source of change was not from nightscout
            if (UploaderQueue.newEntry(is_new ? "insert" : "update", treatment) != null) {
                SyncService.startSyncService(3000); // sync in 3 seconds
            }
        }
    }
 
Example #3
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void pushBgReadingSyncToWatch(BgReading bgReading, boolean is_new) {
    Log.d(TAG, "pushTreatmentSyncToWatch Add treatment to UploaderQueue.");
    if (Pref.getBooleanDefaultFalse("wear_sync")) {
        if (UploaderQueue.newEntryForWatch(is_new ? "insert" : "update", bgReading) != null) {
            SyncService.startSyncService(3000); // sync in 3 seconds
        }
    }
}
 
Example #4
Source File: BloodTest.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void pushBloodTestSyncToWatch(BloodTest bt, boolean is_new) {
    Log.d(TAG, "pushTreatmentSyncToWatch Add treatment to UploaderQueue.");
    if (Pref.getBooleanDefaultFalse("wear_sync")) {
        if (UploaderQueue.newEntryForWatch(is_new ? "insert" : "update", bt) != null) {
            SyncService.startSyncService(3000); // sync in 3 seconds
        }
    }
}
 
Example #5
Source File: BloodTest.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
private static void processFromMessage(BloodTestMessage btm) {
    if ((btm != null) && (btm.uuid != null) && (btm.uuid.length() == 36)) {
        boolean is_new = false;
        BloodTest bt = byUUID(btm.uuid);
        if (bt == null) {
            bt = getForPreciseTimestamp(Wire.get(btm.timestamp, BloodTestMessage.DEFAULT_TIMESTAMP), CLOSEST_READING_MS);
            if (bt != null) {
                UserError.Log.wtf(TAG, "Error matches a different uuid with the same timestamp: " + bt.uuid + " vs " + btm.uuid + " skipping!");
                return;
            }
            bt = new BloodTest();
            is_new = true;
        } else {
            if (bt.state != Wire.get(btm.state, BloodTestMessage.DEFAULT_STATE)) {
                is_new = true;
            }
        }
        bt.timestamp = Wire.get(btm.timestamp, BloodTestMessage.DEFAULT_TIMESTAMP);
        bt.mgdl = Wire.get(btm.mgdl, BloodTestMessage.DEFAULT_MGDL);
        bt.created_timestamp = Wire.get(btm.created_timestamp, BloodTestMessage.DEFAULT_CREATED_TIMESTAMP);
        bt.state = Wire.get(btm.state, BloodTestMessage.DEFAULT_STATE);
        bt.source = Wire.get(btm.source, BloodTestMessage.DEFAULT_SOURCE);
        bt.uuid = btm.uuid;
        bt.saveit(); // de-dupe by uuid
        if (is_new) { // cannot handle updates yet
            if (UploaderQueue.newEntry(is_new ? "insert" : "update", bt) != null) {
                if (JoH.quietratelimit("start-sync-service", 5)) {
                    SyncService.startSyncService(3000); // sync in 3 seconds
                }
            }
        }
    } else {
        UserError.Log.wtf(TAG, "processFromMessage uuid is null or invalid");
    }
}
 
Example #6
Source File: Treatments.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void pushTreatmentSyncToWatch(Treatments treatment, boolean is_new) {
    Log.d(TAG, "pushTreatmentSyncToWatch Add treatment to UploaderQueue.");
    if (Pref.getBooleanDefaultFalse("wear_sync")) {
        if (UploaderQueue.newEntryForWatch(is_new ? "insert" : "update", treatment) != null) {
            SyncService.startSyncService(3000); // sync in 3 seconds
        }
    }
}
 
Example #7
Source File: Treatments.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void delete_by_uuid(String uuid, boolean from_interactive) {
    Treatments thistreat = byuuid(uuid);
    if (thistreat != null) {

        UploaderQueue.newEntry("delete", thistreat);
        if (from_interactive) {
            GcmActivity.push_delete_treatment(thistreat);
            SyncService.startSyncService(3000); // sync in 3 seconds
        }

        thistreat.delete();
        Home.staticRefreshBGCharts();
    }
}
 
Example #8
Source File: BgReading.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void pushBgReadingSyncToWatch(BgReading bgReading, boolean is_new) {
    Log.d(TAG, "pushTreatmentSyncToWatch Add treatment to UploaderQueue.");
    if (Pref.getBooleanDefaultFalse("wear_sync")) {
        if (UploaderQueue.newEntryForWatch(is_new ? "insert" : "update", bgReading) != null) {
            SyncService.startSyncService(3000); // sync in 3 seconds
        }
    }
}
 
Example #9
Source File: Treatments.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void delete_by_uuid(String uuid, boolean from_interactive) {
    Treatments thistreat = byuuid(uuid);
    if (thistreat != null) {

        UploaderQueue.newEntry("delete", thistreat);
        if (from_interactive) {
            GcmActivity.push_delete_treatment(thistreat);
            SyncService.startSyncService(3000); // sync in 3 seconds
        }

        thistreat.delete();
        Home.staticRefreshBGCharts();
    }
}
 
Example #10
Source File: Treatments.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void pushTreatmentSyncToWatch(Treatments treatment, boolean is_new) {
    Log.d(TAG, "pushTreatmentSyncToWatch Add treatment to UploaderQueue.");
    if (Pref.getBooleanDefaultFalse("wear_sync")) {
        if (UploaderQueue.newEntryForWatch(is_new ? "insert" : "update", treatment) != null) {
            SyncService.startSyncService(3000); // sync in 3 seconds
        }
    }
}
 
Example #11
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void pushBgReadingSyncToWatch(BgReading bgReading, boolean is_new) {
    Log.d(TAG, "pushTreatmentSyncToWatch Add treatment to UploaderQueue.");
    if (Pref.getBooleanDefaultFalse("wear_sync")) {
        if (UploaderQueue.newEntryForWatch(is_new ? "insert" : "update", bgReading) != null) {
            SyncService.startSyncService(3000); // sync in 3 seconds
        }
    }
}
 
Example #12
Source File: BgReading.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void pushBgReadingSyncToWatch(BgReading bgReading, boolean is_new) {
    Log.d(TAG, "pushTreatmentSyncToWatch Add treatment to UploaderQueue.");
    if (Pref.getBooleanDefaultFalse("wear_sync")) {
        if (UploaderQueue.newEntryForWatch(is_new ? "insert" : "update", bgReading) != null) {
            SyncService.startSyncService(3000); // sync in 3 seconds
        }
    }
}
 
Example #13
Source File: BloodTest.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void pushBloodTestSyncToWatch(BloodTest bt, boolean is_new) {
    Log.d(TAG, "pushTreatmentSyncToWatch Add treatment to UploaderQueue.");
    if (Pref.getBooleanDefaultFalse("wear_sync")) {
        if (UploaderQueue.newEntryForWatch(is_new ? "insert" : "update", bt) != null) {
            SyncService.startSyncService(3000); // sync in 3 seconds
        }
    }
}
 
Example #14
Source File: BloodTest.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
private static void processFromMessage(BloodTestMessage btm) {
    if ((btm != null) && (btm.uuid != null) && (btm.uuid.length() == 36)) {
        boolean is_new = false;
        BloodTest bt = byUUID(btm.uuid);
        if (bt == null) {
            bt = getForPreciseTimestamp(Wire.get(btm.timestamp, BloodTestMessage.DEFAULT_TIMESTAMP), CLOSEST_READING_MS);
            if (bt != null) {
                UserError.Log.wtf(TAG, "Error matches a different uuid with the same timestamp: " + bt.uuid + " vs " + btm.uuid + " skipping!");
                return;
            }
            bt = new BloodTest();
            is_new = true;
        } else {
            if (bt.state != Wire.get(btm.state, BloodTestMessage.DEFAULT_STATE)) {
                is_new = true;
            }
        }
        bt.timestamp = Wire.get(btm.timestamp, BloodTestMessage.DEFAULT_TIMESTAMP);
        bt.mgdl = Wire.get(btm.mgdl, BloodTestMessage.DEFAULT_MGDL);
        bt.created_timestamp = Wire.get(btm.created_timestamp, BloodTestMessage.DEFAULT_CREATED_TIMESTAMP);
        bt.state = Wire.get(btm.state, BloodTestMessage.DEFAULT_STATE);
        bt.source = Wire.get(btm.source, BloodTestMessage.DEFAULT_SOURCE);
        bt.uuid = btm.uuid;
        bt.saveit(); // de-dupe by uuid
        if (is_new) { // cannot handle updates yet
            if (UploaderQueue.newEntry(is_new ? "insert" : "update", bt) != null) {
                if (JoH.quietratelimit("start-sync-service", 5)) {
                    SyncService.startSyncService(3000); // sync in 3 seconds
                }
            }
        }
    } else {
        UserError.Log.wtf(TAG, "processFromMessage uuid is null or invalid");
    }
}
 
Example #15
Source File: BgSendQueue.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public static void handleNewBgReading(final BgReading bgReading, String operation_type, Context context, boolean is_follower, boolean quick) {
    if (bgReading == null) {
        UserError.Log.wtf("BgSendQueue", "handleNewBgReading called with null bgReading!");
        return;
    }
    final PowerManager.WakeLock wakeLock = JoH.getWakeLock("sendQueue", 120000);
    try {

        // Add to upload queue
        //if (!is_follower) {
            UploaderQueue.newEntry(operation_type, bgReading);
        //}

        // all this other UI stuff probably shouldn't be here but in lieu of a better method we keep with it..
        if (!quick) {
            if (Home.activityVisible) {
                context.sendBroadcast(new Intent(Intents.ACTION_NEW_BG_ESTIMATE_NO_DATA));
            }

            if (AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, xDripWidget.class)).length > 0) {
                //context.startService(new Intent(context, WidgetUpdateService.class));
                JoH.startService(WidgetUpdateService.class);
            }
        }

        // emit local broadcast
        BroadcastGlucose.sendLocalBroadcast(bgReading);

            // TODO I don't really think this is needed anymore
            if (!quick && Pref.getBooleanDefaultFalse("excessive_wakelocks")) {
                // just keep it alive for 3 more seconds to allow the watch to be updated
                // dangling wakelock
                JoH.getWakeLock("broadcstNightWatch", 3000);
            }


        if (!quick) {
            NewDataObserver.newBgReading(bgReading, is_follower);
        }

        if ((!is_follower) && (Pref.getBoolean("plus_follow_master", false))) {
            if (Pref.getBoolean("display_glucose_from_plugin", false))
            {
                // TODO does this currently ignore noise or is noise properly calculated on the follower?
                // munge bgReading for follower TODO will probably want extra option for this in future
                // TODO we maybe don't need deep clone for this! Check how value will be used below
                //GcmActivity.syncBGReading(PluggableCalibration.mungeBgReading(new Cloner().deepClone(bgReading)));
                GcmActivity.syncBGReading(PluggableCalibration.mungeBgReading(BgReading.fromJSON(bgReading.toJSON(true))));
            } else {
                // send as is
                GcmActivity.syncBGReading(bgReading);
            }
        }

        // process the uploader queue
        if (JoH.ratelimit("start-sync-service", 30)) {
            JoH.startService(SyncService.class);
        }


    } finally {
        JoH.releaseWakeLock(wakeLock);
    }
}
 
Example #16
Source File: BloodTest.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public static BloodTest create(long timestamp_ms, double mgdl, String source, String suggested_uuid) {

        if ((timestamp_ms == 0) || (mgdl == 0)) {
            UserError.Log.e(TAG, "Either timestamp or mgdl is zero - cannot create reading");
            return null;
        }

        if (timestamp_ms < 1487759433000L) {
            UserError.Log.d(TAG, "Timestamp really too far in the past @ " + timestamp_ms);
            return null;
        }

        final long now = JoH.tsl();
        if (timestamp_ms > now) {
            if ((timestamp_ms - now) > 600000) {
                UserError.Log.wtf(TAG, "Timestamp is > 10 minutes in the future! Something is wrong: " + JoH.dateTimeText(timestamp_ms));
                return null;
            }
            timestamp_ms = now; // force to now if it showed up to 10 mins in the future
        }

        final BloodTest match = getForPreciseTimestamp(timestamp_ms, CLOSEST_READING_MS);
        if (match == null) {
            final BloodTest bt = new BloodTest();
            bt.timestamp = timestamp_ms;
            bt.mgdl = mgdl;
            bt.uuid = suggested_uuid == null ? UUID.randomUUID().toString() : suggested_uuid;
            bt.created_timestamp = JoH.tsl();
            bt.state = STATE_VALID;
            bt.source = source;
            bt.saveit();
            if (UploaderQueue.newEntry("insert", bt) != null) {
                SyncService.startSyncService(3000); // sync in 3 seconds
            }

            if (Pref.getBooleanDefaultFalse("bluetooth_meter_for_calibrations_auto")) {
                if ((JoH.msSince(bt.timestamp) < Constants.MINUTE_IN_MS * 5) && (JoH.msSince(bt.timestamp) > 0)) {
                    UserError.Log.d(TAG, "Blood test value recent enough to send to G5");
                    //Ob1G5StateMachine.addCalibration((int) bt.mgdl, timestamp_ms);
                    NativeCalibrationPipe.addCalibration((int) bt.mgdl, timestamp_ms);
                }
            }

            return bt;
        } else {
            UserError.Log.d(TAG, "Not creating new reading as timestamp is too close");
        }
        return null;
    }
 
Example #17
Source File: CollectionServiceStarter.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
private void startSyncService() {
    Log.d(TAG, "starting Sync service");
    mContext.startService(new Intent(mContext, SyncService.class));
}
 
Example #18
Source File: BgSendQueue.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
public static void handleNewBgReading(BgReading bgReading, String operation_type, Context context) {
    PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "sendQueue");
    wakeLock.acquire();
    try {
    	
   		addToQueue(bgReading, operation_type);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

        Intent updateIntent = new Intent(Intents.ACTION_NEW_BG_ESTIMATE_NO_DATA);
        context.sendBroadcast(updateIntent);

        if(AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, xDripWidget.class)).length > 0){
            context.startService(new Intent(context, WidgetUpdateService.class));
        }


        if (prefs.getBoolean("broadcast_data_through_intents", false)) {

            //prepare data
            double calculated_value = bgReading.calculated_value;
            boolean hide_slope = bgReading.hide_slope;
            String slopeName = hide_slope?null:bgReading.slopeName();
            int batteryLevel = getBatteryLevel(context);
            final long timestamp = bgReading.timestamp;
            Calibration cal = Calibration.last();
            double raw = NightscoutUploader.getNightscoutRaw(bgReading, cal);
            double slope = BgReading.currentSlope();

            //send broadcast
            BgEstimateBroadcaster.broadcastBgEstimate(calculated_value, raw, timestamp, slope, slopeName, batteryLevel, context);

            //just keep it alive for 3 more seconds to allow the watch to be updated
            // TODO: change NightWatch to not allow the system to sleep.
            powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "broadcastNightWatch").acquire(3000);

        }

        // send to wear
        if (prefs.getBoolean("wear_sync", false)) {

            /*By integrating the watch part of Nightwatch we inherited the same wakelock
                problems NW had - so adding the same quick fix for now.
                TODO: properly "wakelock" the wear (and probably pebble) services
             */
            context.startService(new Intent(context, WatchUpdaterService.class));
            powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "quickFix3").acquire(15000);
        }

        // send to pebble
        if(prefs.getBoolean("broadcast_to_pebble", false)) {
            context.startService(new Intent(context, PebbleSync.class));
        }



        if (prefs.getBoolean("share_upload", false)) {
            Log.d("ShareRest", "About to call ShareRest!!");
            String receiverSn = prefs.getString("share_key", "SM00000000").toUpperCase();
            BgUploader bgUploader = new BgUploader(context);
            bgUploader.upload(new ShareUploadPayload(receiverSn, bgReading));
        }
        context.startService(new Intent(context, SyncService.class));

        //Text to speech
        Log.d("BgToSpeech", "gonna call speak");
        BgToSpeech.speak(bgReading.calculated_value, bgReading.timestamp);


    } finally {
        wakeLock.release();
    }
}
 
Example #19
Source File: NightscoutBackfillActivity.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public synchronized void backfillRun(View v) {
    locked = JoH.tsl();
    doitButton.setVisibility(View.INVISIBLE);
    JoH.static_toast_long(gs(R.string.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);
                    locked = 0; // clear lock
                } else {
                    JoH.static_toast_long(gs(R.string.didnt_find_any_glucose_readings_in_that_time_period));
                }
            } finally {
                JoH.releaseWakeLock(wl);
            }
        }
    }).start();

    finish();
}
 
Example #20
Source File: BgSendQueue.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public static void handleNewBgReading(final BgReading bgReading, String operation_type, Context context, boolean is_follower, boolean quick) {
    if (bgReading == null) {
        UserError.Log.wtf("BgSendQueue", "handleNewBgReading called with null bgReading!");
        return;
    }
    final PowerManager.WakeLock wakeLock = JoH.getWakeLock("sendQueue", 120000);
    try {

        // Add to upload queue
        //if (!is_follower) {
            UploaderQueue.newEntry(operation_type, bgReading);
        //}

        // all this other UI stuff probably shouldn't be here but in lieu of a better method we keep with it..
        if (!quick) {
            if (Home.activityVisible) {
                context.sendBroadcast(new Intent(Intents.ACTION_NEW_BG_ESTIMATE_NO_DATA));
            }

            if (AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, xDripWidget.class)).length > 0) {
                //context.startService(new Intent(context, WidgetUpdateService.class));
                JoH.startService(WidgetUpdateService.class);
            }
        }

        // emit local broadcast
        BroadcastGlucose.sendLocalBroadcast(bgReading);

            // TODO I don't really think this is needed anymore
            if (!quick && Pref.getBooleanDefaultFalse("excessive_wakelocks")) {
                // just keep it alive for 3 more seconds to allow the watch to be updated
                // dangling wakelock
                JoH.getWakeLock("broadcstNightWatch", 3000);
            }


        if (!quick) {
            NewDataObserver.newBgReading(bgReading, is_follower);
        }

        if ((!is_follower) && (Pref.getBoolean("plus_follow_master", false))) {
            if (Pref.getBoolean("display_glucose_from_plugin", false))
            {
                // TODO does this currently ignore noise or is noise properly calculated on the follower?
                // munge bgReading for follower TODO will probably want extra option for this in future
                // TODO we maybe don't need deep clone for this! Check how value will be used below
                //GcmActivity.syncBGReading(PluggableCalibration.mungeBgReading(new Cloner().deepClone(bgReading)));
                GcmActivity.syncBGReading(PluggableCalibration.mungeBgReading(BgReading.fromJSON(bgReading.toJSON(true))));
            } else {
                // send as is
                GcmActivity.syncBGReading(bgReading);
            }
        }

        // process the uploader queue
        if (JoH.ratelimit("start-sync-service", 30)) {
            JoH.startService(SyncService.class);
        }


    } finally {
        JoH.releaseWakeLock(wakeLock);
    }
}
 
Example #21
Source File: NightscoutBackfillActivity.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public synchronized void backfillRun(View v) {
    locked = JoH.tsl();
    doitButton.setVisibility(View.INVISIBLE);
    JoH.static_toast_long(gs(R.string.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);
                    locked = 0; // clear lock
                } else {
                    JoH.static_toast_long(gs(R.string.didnt_find_any_glucose_readings_in_that_time_period));
                }
            } finally {
                JoH.releaseWakeLock(wl);
            }
        }
    }).start();

    finish();
}
 
Example #22
Source File: BloodTest.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public static BloodTest create(long timestamp_ms, double mgdl, String source, String suggested_uuid) {

        if ((timestamp_ms == 0) || (mgdl == 0)) {
            UserError.Log.e(TAG, "Either timestamp or mgdl is zero - cannot create reading");
            return null;
        }

        if (timestamp_ms < 1487759433000L) {
            UserError.Log.d(TAG, "Timestamp really too far in the past @ " + timestamp_ms);
            return null;
        }

        final long now = JoH.tsl();
        if (timestamp_ms > now) {
            if ((timestamp_ms - now) > 600000) {
                UserError.Log.wtf(TAG, "Timestamp is > 10 minutes in the future! Something is wrong: " + JoH.dateTimeText(timestamp_ms));
                return null;
            }
            timestamp_ms = now; // force to now if it showed up to 10 mins in the future
        }

        final BloodTest match = getForPreciseTimestamp(timestamp_ms, CLOSEST_READING_MS);
        if (match == null) {
            final BloodTest bt = new BloodTest();
            bt.timestamp = timestamp_ms;
            bt.mgdl = mgdl;
            bt.uuid = suggested_uuid == null ? UUID.randomUUID().toString() : suggested_uuid;
            bt.created_timestamp = JoH.tsl();
            bt.state = STATE_VALID;
            bt.source = source;
            bt.saveit();
            if (UploaderQueue.newEntry("insert", bt) != null) {
                SyncService.startSyncService(3000); // sync in 3 seconds
            }

            if (Pref.getBooleanDefaultFalse("bluetooth_meter_for_calibrations_auto")) {
                if ((JoH.msSince(bt.timestamp) < Constants.MINUTE_IN_MS * 5) && (JoH.msSince(bt.timestamp) > 0)) {
                    UserError.Log.d(TAG, "Blood test value recent enough to send to G5");
                    //Ob1G5StateMachine.addCalibration((int) bt.mgdl, timestamp_ms);
                    NativeCalibrationPipe.addCalibration((int) bt.mgdl, timestamp_ms);
                }
            }

            return bt;
        } else {
            UserError.Log.d(TAG, "Not creating new reading as timestamp is too close");
        }
        return null;
    }