Search in sources :

Example 6 with ScaleMeasurement

use of com.health.openscale.core.datatypes.ScaleMeasurement in project openScale by oliexdev.

the class OpenScale method getSmartUserAssignment.

private int getSmartUserAssignment(float weight, float range) {
    List<ScaleUser> scaleUsers = getScaleUserList();
    Map<Float, Integer> inRangeWeights = new TreeMap<>();
    for (int i = 0; i < scaleUsers.size(); i++) {
        List<ScaleMeasurement> scaleUserData = measurementDAO.getAll(scaleUsers.get(i).getId());
        float lastWeight = 0;
        if (scaleUserData.size() > 0) {
            lastWeight = scaleUserData.get(0).getWeight();
        } else {
            lastWeight = scaleUsers.get(i).getInitialWeight();
        }
        if ((lastWeight - range) <= weight && (lastWeight + range) >= weight) {
            inRangeWeights.put(Math.abs(lastWeight - weight), scaleUsers.get(i).getId());
        }
    }
    if (inRangeWeights.size() > 0) {
        // return the user id which is nearest to the weight (first element of the tree map)
        return inRangeWeights.entrySet().iterator().next().getValue();
    }
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    // if ignore out of range preference is true don't add this data
    if (prefs.getBoolean("ignoreOutOfRange", false)) {
        return -1;
    }
    // return selected scale user id if not out of range preference is checked and weight is out of range of any user
    return getSelectedScaleUser().getId();
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) SharedPreferences(android.content.SharedPreferences) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) TreeMap(java.util.TreeMap)

Example 7 with ScaleMeasurement

use of com.health.openscale.core.datatypes.ScaleMeasurement in project openScale by oliexdev.

the class BluetoothMGB method onBluetoothDataChange.

@Override
public void onBluetoothDataChange(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic gattCharacteristic) {
    packet_buf = gattCharacteristic.getValue();
    packet_pos = 0;
    if (packet_buf == null || packet_buf.length <= 0) {
        return;
    }
    if (packet_buf.length != 20) {
        return;
    }
    int hdr_1 = popInt();
    int hdr_2 = popInt();
    int hdr_3 = popInt();
    if (hdr_1 == 0xAC && hdr_2 == 0x02 && hdr_3 == 0xFF) {
        measurement = new ScaleMeasurement();
        // unknown =00
        popInt();
        // unknown =02
        popInt();
        // unknown =21
        popInt();
        // Year
        popInt();
        // Month
        popInt();
        // Day
        popInt();
        // Hour
        popInt();
        // Minute
        popInt();
        // Second
        popInt();
        measurement.setDateTime(new Date());
        measurement.setWeight(popFloat());
        // BMI
        popFloat();
        measurement.setFat(popFloat());
        // unknown =00
        popInt();
        // unknown =00
        popInt();
    } else if (hdr_1 == 0x01 && hdr_2 == 0x00) {
        measurement.setMuscle(popFloat());
        // BMR
        popFloat();
        measurement.setBone(popFloat());
        measurement.setWater(popFloat());
        // Age
        popInt();
        // protein rate
        popFloat();
        // unknown =00
        popInt();
        // unknown =01
        popInt();
        // unknown =1b
        popInt();
        // unknown =a5
        popInt();
        // unknown =02
        popInt();
        // unknown =47;48;4e;4b;42
        popInt();
        addScaleData(measurement);
    // Visceral fat?
    // Standart weight?
    // WeightControl?
    // Body fat?
    // Muscle weight?
    }
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) Date(java.util.Date)

Example 8 with ScaleMeasurement

use of com.health.openscale.core.datatypes.ScaleMeasurement in project openScale by oliexdev.

the class BluetoothMiScale method parseBytes.

private void parseBytes(byte[] weightBytes) {
    try {
        float weight = 0.0f;
        final byte ctrlByte = weightBytes[0];
        final boolean isWeightRemoved = isBitSet(ctrlByte, 7);
        final boolean isStabilized = isBitSet(ctrlByte, 5);
        final boolean isLBSUnit = isBitSet(ctrlByte, 0);
        final boolean isCattyUnit = isBitSet(ctrlByte, 4);
        // Only if the value is stabilized and the weight is *not* removed, the date is valid
        if (isStabilized && !isWeightRemoved) {
            final int year = ((weightBytes[4] & 0xFF) << 8) | (weightBytes[3] & 0xFF);
            final int month = (int) weightBytes[5];
            final int day = (int) weightBytes[6];
            final int hours = (int) weightBytes[7];
            final int min = (int) weightBytes[8];
            final int sec = (int) weightBytes[9];
            if (isLBSUnit || isCattyUnit) {
                weight = (float) (((weightBytes[2] & 0xFF) << 8) | (weightBytes[1] & 0xFF)) / 100.0f;
            } else {
                weight = (float) (((weightBytes[2] & 0xFF) << 8) | (weightBytes[1] & 0xFF)) / 200.0f;
            }
            String date_string = year + "/" + month + "/" + day + "/" + hours + "/" + min;
            Date date_time = new SimpleDateFormat("yyyy/MM/dd/HH/mm").parse(date_string);
            // Is the year plausible? Check if the year is in the range of 20 years...
            if (validateDate(date_time, 20)) {
                final ScaleUser selectedUser = OpenScale.getInstance(context).getSelectedScaleUser();
                ScaleMeasurement scaleBtData = new ScaleMeasurement();
                scaleBtData.setConvertedWeight(weight, selectedUser.getScaleUnit());
                scaleBtData.setDateTime(date_time);
                addScaleData(scaleBtData);
            } else {
                Log.e("BluetoothMiScale", "Invalid Mi scale weight year " + year);
            }
        }
    } catch (ParseException e) {
        setBtStatus(BT_UNEXPECTED_ERROR, "Error while decoding bluetooth date string (" + e.getMessage() + ")");
    }
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 9 with ScaleMeasurement

use of com.health.openscale.core.datatypes.ScaleMeasurement in project openScale by oliexdev.

the class BluetoothMiScale2 method parseBytes.

private void parseBytes(byte[] weightBytes) {
    try {
        float weight = 0.0f;
        final byte ctrlByte0 = weightBytes[0];
        final byte ctrlByte1 = weightBytes[1];
        final boolean isWeightRemoved = isBitSet(ctrlByte1, 7);
        final boolean isDateInvalid = isBitSet(ctrlByte1, 6);
        final boolean isStabilized = isBitSet(ctrlByte1, 5);
        final boolean isLBSUnit = isBitSet(ctrlByte0, 0);
        final boolean isCattyUnit = isBitSet(ctrlByte1, 6);
        if (isStabilized && !isWeightRemoved && !isDateInvalid) {
            final int year = ((weightBytes[3] & 0xFF) << 8) | (weightBytes[2] & 0xFF);
            final int month = (int) weightBytes[4];
            final int day = (int) weightBytes[5];
            final int hours = (int) weightBytes[6];
            final int min = (int) weightBytes[7];
            final int sec = (int) weightBytes[8];
            if (isLBSUnit || isCattyUnit) {
                weight = (float) (((weightBytes[12] & 0xFF) << 8) | (weightBytes[11] & 0xFF)) / 100.0f;
            } else {
                weight = (float) (((weightBytes[12] & 0xFF) << 8) | (weightBytes[11] & 0xFF)) / 200.0f;
            }
            String date_string = year + "/" + month + "/" + day + "/" + hours + "/" + min;
            Date date_time = new SimpleDateFormat("yyyy/MM/dd/HH/mm").parse(date_string);
            // Is the year plausible? Check if the year is in the range of 20 years...
            if (validateDate(date_time, 20)) {
                final ScaleUser selectedUser = OpenScale.getInstance(context).getSelectedScaleUser();
                ScaleMeasurement scaleBtData = new ScaleMeasurement();
                scaleBtData.setConvertedWeight(weight, selectedUser.getScaleUnit());
                scaleBtData.setDateTime(date_time);
                addScaleData(scaleBtData);
            } else {
                Log.e("BluetoothMiScale", "Invalid Mi scale weight year " + year);
            }
        }
    } catch (ParseException e) {
        setBtStatus(BT_UNEXPECTED_ERROR, "Error while decoding bluetooth date string (" + e.getMessage() + ")");
    }
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 10 with ScaleMeasurement

use of com.health.openscale.core.datatypes.ScaleMeasurement in project openScale by oliexdev.

the class BluetoothOneByone method parseBytes.

private void parseBytes(byte[] weightBytes) {
    // kg
    float weight = (float) (((weightBytes[4] & 0xFF) << 8) | (weightBytes[5] & 0xFF)) / 10.0f;
    // %
    float fat = (float) (((weightBytes[6] & 0xFF) << 8) | (weightBytes[7] & 0xFF)) / 10.0f;
    // %
    float bone = (float) (((weightBytes[8] & 0xFF) & 0xFF)) / 10.0f;
    // %
    float muscle = (float) (((weightBytes[9] & 0xFF) << 8) | (weightBytes[10] & 0xFF)) / 10.0f;
    // %
    float visfat = (float) (((weightBytes[11] & 0xFF) & 0xFF)) / 10.0f;
    // %
    float water = (float) (((weightBytes[12] & 0xFF) << 8) | (weightBytes[13] & 0xFF)) / 10.0f;
    // kcal
    float bmr = (float) (((weightBytes[14] & 0xFF) << 8) | (weightBytes[15] & 0xFF));
    ScaleMeasurement scaleBtData = new ScaleMeasurement();
    scaleBtData.setWeight(weight);
    scaleBtData.setFat(fat);
    scaleBtData.setMuscle(muscle);
    scaleBtData.setWater(water);
    scaleBtData.setBone(bone);
    scaleBtData.setDateTime(new Date());
    addScaleData(scaleBtData);
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) Date(java.util.Date)

Aggregations

ScaleMeasurement (com.health.openscale.core.datatypes.ScaleMeasurement)39 Date (java.util.Date)13 ScaleUser (com.health.openscale.core.datatypes.ScaleUser)12 BufferedReader (java.io.BufferedReader)9 ParseException (java.text.ParseException)9 StringReader (java.io.StringReader)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 Calendar (java.util.Calendar)7 MeasurementView (com.health.openscale.gui.views.MeasurementView)6 SimpleDateFormat (java.text.SimpleDateFormat)5 BMRMeasurementView (com.health.openscale.gui.views.BMRMeasurementView)4 FloatMeasurementView (com.health.openscale.gui.views.FloatMeasurementView)4 WeightMeasurementView (com.health.openscale.gui.views.WeightMeasurementView)4 IOException (java.io.IOException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Stack (java.util.Stack)2 SimpleLineChartValueFormatter (lecho.lib.hellocharts.formatter.SimpleLineChartValueFormatter)2 Axis (lecho.lib.hellocharts.model.Axis)2 AxisValue (lecho.lib.hellocharts.model.AxisValue)2