Java Code Examples for io.realm.RealmResults#first()

The following examples show how to use io.realm.RealmResults#first() . 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: ReadingData.java    From OpenLibre with GNU General Public License v3.0 6 votes vote down vote up
private GlucoseData makeGlucoseData(Realm realmProcessedData, int glucoseLevelRaw, int ageInSensorMinutes, long dataDate) {
    // if this data point has been read from this sensor before, reuse the object form the database, instead of changing the old data
    RealmResults<GlucoseData> previousGlucoseData = realmProcessedData.where(GlucoseData.class)
            .equalTo(GlucoseData.ID, GlucoseData.generateId(sensor, ageInSensorMinutes, false, glucoseLevelRaw)).findAll();

    // check if a valid previous data point was found
    if (!previousGlucoseData.isEmpty()) {
        if (previousGlucoseData.first().getGlucoseLevelRaw() == glucoseLevelRaw) {
            return previousGlucoseData.first();
        }
        // if the old value does not equal the new one and the sensor has been running for more than three hours, there is an error in the data
        if (ageInSensorMinutes > 3 * minSensorAgeInMinutes) {
            Log.e("OpenLibre::ReadingData", "error in glucose level raw:" + previousGlucoseData.first().getGlucoseLevelRaw() + " != " + glucoseLevelRaw
                    + " for glucose data with id: " + previousGlucoseData.first().getId());
            history.clear();
            trend.clear();
            return null;
        }
    }
    return new GlucoseData(sensor, ageInSensorMinutes, timezoneOffsetInMinutes, glucoseLevelRaw, false, dataDate);
}
 
Example 2
Source File: APSResult.java    From HAPP with GNU General Public License v3.0 6 votes vote down vote up
public static APSResult last(Realm realm) {
    RealmResults<APSResult> results = realm.where(APSResult.class)
            .findAllSorted("timestamp", Sort.DESCENDING);

    if (results.isEmpty()) {
        return null;
    } else {
        return results.first();
    }
    //APSResult last = new Select()
    //        .from(APSResult.class)
    //        .orderBy("datetime desc")
    //        .executeSingle();

    //if (last != null){
    //    if (last.accepted == null) last.accepted = false;
    //}

    //return last;
}
 
Example 3
Source File: SensorStatusFragment.java    From OpenLibre with GNU General Public License v3.0 5 votes vote down vote up
public void setupUi(View view) {
    Realm realmProcessedData = Realm.getInstance(realmConfigProcessedData);
    RealmResults<SensorData> sensorDataResults = realmProcessedData.where(SensorData.class).
            findAllSorted(START_DATE, Sort.DESCENDING);

    TextView sensorId = (TextView) view.findViewById(R.id.tv_sensor_id_value);
    TextView sensorStartDate = (TextView) view.findViewById(R.id.tv_sensor_start_date_value);
    TextView sensorEndsIn = (TextView) view.findViewById(R.id.tv_sensor_ends_in_value);

    if (sensorDataResults.size() == 0) {
        sensorId.setText(getResources().getString(R.string.no_sensor_registered));
        sensorStartDate.setText("");
        sensorEndsIn.setText("");
    } else {
        SensorData sensorData = sensorDataResults.first();
        sensorId.setText(sensorData.getTagId());
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
        sensorStartDate.setText(dateFormat.format(new Date(sensorData.getStartDate())));
        long timeLeft = sensorData.getTimeLeft();
        if (timeLeft >= TimeUnit.MINUTES.toMillis(1L)) {
            sensorEndsIn.setText(getDurationBreakdown(getResources(), sensorData.getTimeLeft()));
        } else {
            sensorEndsIn.setText(getResources().getString(R.string.sensor_expired));
        }
    }
    realmProcessedData.close();
}
 
Example 4
Source File: RealmRoomMessage.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public static RealmRoomMessage findLastMessage(Realm realm, long roomId) {
    //TODO [Saeed Mozaffari] [2017-10-23 11:24 AM] - Can Write Better Code?
    RealmResults<RealmRoomMessage> realmRoomMessages = findDescending(realm, roomId);
    RealmRoomMessage realmRoomMessage = null;
    if (realmRoomMessages.size() > 0) {
        realmRoomMessage = realmRoomMessages.first();
    }
    return realmRoomMessage;
}
 
Example 5
Source File: Integration.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static Integration getIntegration(String type, String local_object, String happ_id, Realm realm){
    RealmResults<Integration> results = realm.where(Integration.class)
            .equalTo("type", type)
            .equalTo("local_object", local_object)
            .equalTo("local_object_id", happ_id)
            .findAllSorted("date_updated", Sort.DESCENDING);

    if (results.isEmpty()) {                                                                    //We dont have an Integration for this item
        return null;

    } else {                                                                                    //Found an Integration, return it
        return results.first();
    }
}
 
Example 6
Source File: Integration.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static Integration getIntegrationByID(String uuid, Realm realm) {
    RealmResults<Integration> results = realm.where(Integration.class)
            .equalTo("id", uuid)
            .findAllSorted("timestamp", Sort.DESCENDING);
    if (results.isEmpty()) {
        return null;
    } else {
        return results.first();
    }
}
 
Example 7
Source File: Bolus.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static Bolus getBolus(String uuid, Realm realm) {
    RealmResults<Bolus> results = realm.where(Bolus.class)
            .equalTo("id", uuid)
            .findAllSorted("timestamp", Sort.DESCENDING);

    if (results.isEmpty()) {
        return null;
    } else {
        return results.first();
    }
}
 
Example 8
Source File: Stat.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static Stat last(Realm realm) {
    RealmResults<Stat> results = realm.where(Stat.class)
            .findAllSorted("timestamp", Sort.DESCENDING);

    if (results.isEmpty()) {
        return null;
    } else {
        return results.first();
    }
    //return new Select()
    //        .from(Stats.class)
    //        .orderBy("datetime desc")
    //        .executeSingle();
}
 
Example 9
Source File: Carb.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static Carb getCarb(String uuid, Realm realm) {
    RealmResults<Carb> results = realm.where(Carb.class)
            .equalTo("id", uuid)
            .findAllSorted("timestamp", Sort.DESCENDING);

    if (results.isEmpty()) {
        return null;
    } else {
        return results.first();
    }
}
 
Example 10
Source File: TempBasal.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static TempBasal getTempBasalByID(String uuid, Realm realm) {
    RealmResults<TempBasal> results = realm.where(TempBasal.class)
            .equalTo("id", uuid)
            .findAllSorted("start_time", Sort.DESCENDING);

    if (results.isEmpty()) {
        return null;
    } else {
        return results.first();
    }
}
 
Example 11
Source File: TempBasal.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static TempBasal last(Realm realm) {
    RealmResults<TempBasal> results = realm.where(TempBasal.class)
            .findAllSorted("start_time", Sort.DESCENDING);

    if (results.isEmpty()) {
        return new TempBasal();     //returns an empty TempBasal, other than null
    } else {
        return results.first();
    }
}
 
Example 12
Source File: TempBasal.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static TempBasal getCurrentActive(Date atThisDate, Realm realm) {
    RealmResults<TempBasal> results = realm.where(TempBasal.class)
            .findAllSorted("start_time", Sort.DESCENDING);
    TempBasal last = null;
    if (!results.isEmpty()) last = results.first();
    if (last != null && last.isactive(atThisDate)){
        return last;
    } else {
        return new TempBasal();     //returns an empty TempBasal, other than null or inactive basal
    }
}
 
Example 13
Source File: Bg.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static Bg last(Realm realm) {
    RealmResults<Bg> results = realm.where(Bg.class)
            .findAllSorted("datetime", Sort.DESCENDING);

    if (results.isEmpty()) {
        return null;
    } else {
        return results.first();
    }
    //return new Select()
    //        .from(Bg.class)
    //        .orderBy("datetime desc")
    //        .executeSingle();
}
 
Example 14
Source File: PumpHistoryAlarm.java    From 600SeriesAndroidUploader with MIT License 4 votes vote down vote up
public static void alarm(
        PumpHistorySender pumpHistorySender, Realm realm, long pumpMAC,
        Date eventDate, int eventRTC, int eventOFFSET,
        int faultNumber,
        byte notificationMode,
        boolean alarmHistory,
        boolean extraData,
        byte[] alarmData) throws IntegrityException {

    PumpHistoryAlarm record;

    // check if a silenced alarm notification
    if (faultNumber == 110) {

        record = realm.where(PumpHistoryAlarm.class)
                .equalTo("pumpMAC", pumpMAC)
                .equalTo("alarmedRTC", eventRTC)
                .equalTo("alarmed", true)
                .equalTo("silenced", false)
                .findFirst();

        if (record != null) {
            Log.d(TAG, String.format("*update* alarm (silenced) #%s", record.faultNumber));
            pumpHistorySender.setSenderREQ(record);
        } else {
            Log.d(TAG, String.format("*new* alarm (silenced) #%s", faultNumber));
            record = realm.createObject(PumpHistoryAlarm.class);
            record.pumpMAC = pumpMAC;
            record.eventDate = eventDate;
            record.alarmedDate = eventDate;
            record.alarmedRTC = eventRTC;
            record.alarmedOFFSET = eventOFFSET;
            record.faultNumber = faultNumber;
        }

        record.silenced = true;
    }

    else {

        record = realm.where(PumpHistoryAlarm.class)
                .equalTo("pumpMAC", pumpMAC)
                .equalTo("alarmedRTC", eventRTC)
                .equalTo("alarmed", true)
                .equalTo("faultNumber", faultNumber)
                .findFirst();

        if (record == null) {
            // look for a cleared alarm
            RealmResults<PumpHistoryAlarm> results = realm.where(PumpHistoryAlarm.class)
                    .equalTo("pumpMAC", pumpMAC)
                    .equalTo("alarmed", false)
                    .equalTo("cleared", true)
                    .equalTo("faultNumber", faultNumber)
                    .greaterThanOrEqualTo("clearedRTC", eventRTC)
                    .lessThan("clearedRTC", HistoryUtils.offsetRTC(eventRTC, 12 * 60 * 60))
                    .sort("clearedDate", Sort.ASCENDING)
                    .findAll();
            if (results.size() == 0) {
                Log.d(TAG, String.format("*new* alarm #%s", faultNumber));
                record = realm.createObject(PumpHistoryAlarm.class);
                record.pumpMAC = pumpMAC;
            } else {
                Log.d(TAG, String.format("*update* alarm #%s", faultNumber));
                record = results.first();
            }

            record.eventDate = eventDate;

            record.alarmedDate = eventDate;
            record.alarmedRTC = eventRTC;
            record.alarmedOFFSET = eventOFFSET;
            record.alarmed = true;

            record.faultNumber = faultNumber;
            record.notificationMode = notificationMode;
            record.alarmHistory = alarmHistory;

            record.extraData = extraData;
            if (extraData && alarmData != null) record.alarmData = alarmData;

            // check if pump re-raised/escalated an alarm that is already recorded in the history
            if (!alarmHistory && notificationMode == 3) record.repeated = true;

            // key composed of 2 byte faultNumber and 4 byte eventRTC due to multiple alerts at the same time
            record.key = HistoryUtils.key("ALARM", (short) faultNumber, eventRTC);

            pumpHistorySender.setSenderREQ(record);
        }

        else HistoryUtils.integrity(record, eventDate);
    }
}
 
Example 15
Source File: PumpHistoryBasal.java    From 600SeriesAndroidUploader with MIT License 4 votes vote down vote up
public static void resume(
        PumpHistorySender pumpHistorySender, Realm realm, long pumpMAC,
        Date eventDate, int eventRTC, int eventOFFSET,
        byte reason) {

    PumpHistoryBasal resumeRecord = realm.where(PumpHistoryBasal.class)
            .equalTo("pumpMAC", pumpMAC)
            .equalTo("recordtype", RECORDTYPE.RESUME.value())
            .equalTo("eventRTC", eventRTC)
            .findFirst();
    if (resumeRecord == null) {
        Log.d(TAG, "*new* resume basal");
        resumeRecord = realm.createObject(PumpHistoryBasal.class);
        resumeRecord.pumpMAC = pumpMAC;
        resumeRecord.recordtype = RECORDTYPE.RESUME.value();
        resumeRecord.eventDate = eventDate;
        resumeRecord.eventRTC = eventRTC;
        resumeRecord.eventOFFSET = eventOFFSET;
        resumeRecord.resumeReason = reason;
        resumeRecord.programmedDuration = 0;
        resumeRecord.key = HistoryUtils.key("RESUME", eventRTC);
        pumpHistorySender.setSenderREQ(resumeRecord);

        // look for corresponding suspend and update it's duration
        RealmResults<PumpHistoryBasal> suspendRecords = realm.where(PumpHistoryBasal.class)
                .equalTo("pumpMAC", pumpMAC)
                .equalTo("recordtype", RECORDTYPE.SUSPEND.value())
                .equalTo("completed", false)
                .greaterThan("eventRTC", HistoryUtils.offsetRTC(eventRTC, - 24 * 60 * 60))
                .lessThan("eventRTC", eventRTC)
                .sort("eventDate", Sort.DESCENDING)
                .findAll();
        if (suspendRecords.size() > 0) {
            PumpHistoryBasal suspendRecord = suspendRecords.first();
            suspendRecord.completed = true;
            suspendRecord.completedDuration = (int) Math.round(((double) (eventRTC - suspendRecord.getEventRTC())) / 60);
            suspendRecord.resumeReason = reason;
            resumeRecord.suspendReason = suspendRecord.suspendReason;
        }

        // look for a temp that was in progress and resume temp with recalculated duration (for nightscout)
        RealmResults<PumpHistoryBasal> realmResults = realm.where(PumpHistoryBasal.class)
                .equalTo("pumpMAC", pumpMAC)
                .equalTo("recordtype", RECORDTYPE.PROGRAMMED.value())
                .equalTo("completed", false)
                .greaterThan("eventRTC", HistoryUtils.offsetRTC(eventRTC, - 24 * 60 * 60))
                .lessThan("eventRTC", eventRTC)
                .sort("eventDate", Sort.DESCENDING)
                .findAll();
        if (realmResults.size() == 1) {
            PumpHistoryBasal programedRecord = realmResults.first();
            int remaining = (int) Math.round(((double) programedRecord.programmedDuration - ((double) (eventRTC - programedRecord.eventRTC) / 60)));
            if (remaining > 0) {
                Log.d(TAG, "temp still in progress after resumed basal");
                resumeRecord.programmedDuration = remaining;
                resumeRecord.type = programedRecord.type;
                resumeRecord.rate = programedRecord.rate;
                resumeRecord.percentageOfRate = programedRecord.percentageOfRate;
            }
        }

    }
}