Search in sources :

Example 16 with ScaleUser

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

the class DatabaseTest method measurementOperations.

@Test
public void measurementOperations() throws Exception {
    final ScaleUser scaleUser1 = new ScaleUser();
    final ScaleUser scaleUser2 = new ScaleUser();
    scaleUser1.setId((int) userDao.insert(scaleUser1));
    scaleUser2.setId((int) userDao.insert(scaleUser2));
    // User 1 data initialization
    final int user1 = scaleUser1.getId();
    ScaleMeasurement measurement11 = new ScaleMeasurement();
    ScaleMeasurement measurement12 = new ScaleMeasurement();
    ScaleMeasurement measurement13 = new ScaleMeasurement();
    measurement11.setUserId(user1);
    measurement12.setUserId(user1);
    measurement13.setUserId(user1);
    measurement11.setWeight(10.0f);
    measurement12.setWeight(20.0f);
    measurement13.setWeight(30.0f);
    measurement11.setDateTime(new Date(100));
    measurement12.setDateTime(new Date(200));
    measurement13.setDateTime(new Date(300));
    // User 2 data initialization
    final int user2 = scaleUser2.getId();
    ScaleMeasurement measurement21 = new ScaleMeasurement();
    ScaleMeasurement measurement22 = new ScaleMeasurement();
    measurement21.setUserId(user2);
    measurement22.setUserId(user2);
    measurement21.setWeight(15.0f);
    measurement22.setWeight(25.0f);
    measurement21.setDateTime(new Date(150));
    measurement22.setDateTime(new Date(250));
    // check if database is empty
    assertTrue(measurementDAO.getAll(user1).isEmpty());
    assertTrue(measurementDAO.getAll(user2).isEmpty());
    // insert measurement as list and single insertion
    List<ScaleMeasurement> scaleMeasurementList = new ArrayList<>();
    scaleMeasurementList.add(measurement11);
    scaleMeasurementList.add(measurement13);
    scaleMeasurementList.add(measurement12);
    measurementDAO.insertAll(scaleMeasurementList);
    assertEquals(3, measurementDAO.getAll(user1).size());
    measurementDAO.insert(measurement22);
    measurementDAO.insert(measurement21);
    assertEquals(2, measurementDAO.getAll(user2).size());
    // check if sorted DESC by date correctly
    assertEquals(30.0f, measurementDAO.getAll(user1).get(0).getWeight(), DELTA);
    assertEquals(25.0f, measurementDAO.getAll(user2).get(0).getWeight(), DELTA);
    // don't allow insertion with the same date
    long id = measurementDAO.insert(measurement11);
    assertEquals(-1, id);
    assertEquals(3, measurementDAO.getAll(user1).size());
    // test get(datetime) method
    assertEquals(20.0f, measurementDAO.get(new Date(200), user1).getWeight(), DELTA);
    // test get(id) method
    scaleMeasurementList = measurementDAO.getAll(user1);
    assertEquals(scaleMeasurementList.get(2).getWeight(), measurementDAO.get(scaleMeasurementList.get(2).getId()).getWeight(), DELTA);
    // test getPrevious(id) method
    assertNull(measurementDAO.getPrevious(scaleMeasurementList.get(2).getId(), user1));
    assertEquals(scaleMeasurementList.get(2).getWeight(), measurementDAO.getPrevious(scaleMeasurementList.get(1).getId(), user1).getWeight(), DELTA);
    assertEquals(scaleMeasurementList.get(1).getWeight(), measurementDAO.getPrevious(scaleMeasurementList.get(0).getId(), user1).getWeight(), DELTA);
    // test getNext(id) method
    assertNull(measurementDAO.getNext(scaleMeasurementList.get(0).getId(), user1));
    assertEquals(scaleMeasurementList.get(0).getWeight(), measurementDAO.getNext(scaleMeasurementList.get(1).getId(), user1).getWeight(), DELTA);
    assertEquals(scaleMeasurementList.get(1).getWeight(), measurementDAO.getNext(scaleMeasurementList.get(2).getId(), user1).getWeight(), DELTA);
    // test getAllInRange method
    assertEquals(1, measurementDAO.getAllInRange(new Date(0), new Date(200), user1).size());
    assertEquals(0, measurementDAO.getAllInRange(new Date(0), new Date(50), user1).size());
    assertEquals(2, measurementDAO.getAllInRange(new Date(100), new Date(201), user1).size());
    assertEquals(1, measurementDAO.getAllInRange(new Date(0), new Date(200), user1).size());
    assertEquals(3, measurementDAO.getAllInRange(new Date(0), new Date(1000), user1).size());
    assertEquals(2, measurementDAO.getAllInRange(new Date(150), new Date(400), user1).size());
    assertEquals(0, measurementDAO.getAllInRange(new Date(10), new Date(20), user2).size());
    assertEquals(1, measurementDAO.getAllInRange(new Date(70), new Date(200), user2).size());
    assertEquals(2, measurementDAO.getAllInRange(new Date(0), new Date(1000), user2).size());
    // test update method
    assertEquals(30.0f, measurementDAO.get(scaleMeasurementList.get(0).getId()).getWeight(), DELTA);
    scaleMeasurementList.get(0).setWeight(42.0f);
    measurementDAO.update(scaleMeasurementList.get(0));
    assertEquals(42.0f, measurementDAO.get(scaleMeasurementList.get(0).getId()).getWeight(), DELTA);
    // test delete method
    assertEquals(3, measurementDAO.getAll(user1).size());
    measurementDAO.delete(scaleMeasurementList.get(0).getId());
    assertEquals(2, measurementDAO.getAll(user1).size());
    // test delete all method
    assertEquals(2, measurementDAO.getAll(user1).size());
    assertEquals(2, measurementDAO.getAll(user2).size());
    measurementDAO.deleteAll(user1);
    measurementDAO.deleteAll(user2);
    assertEquals(0, measurementDAO.getAll(user1).size());
    assertEquals(0, measurementDAO.getAll(user2).size());
    assertTrue(measurementDAO.getAll(user1).isEmpty());
    assertTrue(measurementDAO.getAll(user2).isEmpty());
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) ArrayList(java.util.ArrayList) Date(java.util.Date) Test(org.junit.Test)

Example 17 with ScaleUser

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

the class BluetoothBeurerSanitas method processUserInfo.

private void processUserInfo(byte[] data) {
    final int count = data[2] & 0xFF;
    final int current = data[3] & 0xFF;
    if (remoteUsers.size() == current - 1) {
        String name = decodeString(data, 12, 3);
        int year = 1900 + (data[15] & 0xFF);
        remoteUsers.add(new RemoteUser(decodeUserId(data, 4), name, year));
        Timber.d("Received user %d/%d: %s (%d)", current, count, name, year);
    }
    Timber.d("Sending ack for CMD_USER_INFO");
    sendAck(data);
    if (current != count) {
        Timber.d("Not all users received, waiting for more...");
        // More data should be incoming, so make sure we wait
        stopMachineState();
        return;
    }
    Calendar cal = Calendar.getInstance();
    for (ScaleUser scaleUser : OpenScale.getInstance().getScaleUserList()) {
        final String localName = convertUserNameToScale(scaleUser);
        cal.setTime(scaleUser.getBirthday());
        final int year = cal.get(Calendar.YEAR);
        for (RemoteUser remoteUser : remoteUsers) {
            if (localName.startsWith(remoteUser.name) && year == remoteUser.year) {
                remoteUser.localUserId = scaleUser.getId();
                Timber.d("Remote user %s (0x%x) is local user %s (%d)", remoteUser.name, remoteUser.remoteUserId, scaleUser.getUserName(), remoteUser.localUserId);
                break;
            }
        }
    }
    if (waitForDataInStep != 4) {
        Timber.w("Received final CMD_USER_INFO in wrong state...");
        if (waitForDataInStep >= 0) {
            Timber.w("...while waiting for other data. Retrying last step.");
            // We are in the wrong state.
            // This may happen, so let's just retry whatever we did before.
            jumpBackOneStep();
        } else {
            Timber.w("...ignored, no data expected.");
        }
    }
    // All data received, no more waiting.
    waitForDataInStep = -1;
    // All users received
    resumeMachineState();
}
Also used : ScaleUser(com.health.openscale.core.datatypes.ScaleUser) Calendar(java.util.Calendar)

Example 18 with ScaleUser

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

the class BluetoothExingtechY1 method onNextStep.

@Override
protected boolean onNextStep(int stepNr) {
    switch(stepNr) {
        case 0:
            setNotificationOn(WEIGHT_MEASUREMENT_SERVICE, WEIGHT_MEASUREMENT_CHARACTERISTIC);
            break;
        case 1:
            final ScaleUser selectedUser = OpenScale.getInstance().getSelectedScaleUser();
            // 00 - male; 01 - female
            byte gender = selectedUser.getGender().isMale() ? (byte) 0x00 : (byte) 0x01;
            // cm
            byte height = (byte) (((int) selectedUser.getBodyHeight()) & 0xff);
            byte age = (byte) (selectedUser.getAge() & 0xff);
            int userId = selectedUser.getId();
            byte[] cmdByte = { (byte) 0x10, (byte) userId, gender, age, height };
            writeBytes(WEIGHT_MEASUREMENT_SERVICE, CMD_MEASUREMENT_CHARACTERISTIC, cmdByte);
            break;
        case 2:
            sendMessage(R.string.info_step_on_scale, 0);
            break;
        default:
            return false;
    }
    return true;
}
Also used : ScaleUser(com.health.openscale.core.datatypes.ScaleUser)

Example 19 with ScaleUser

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

the class BluetoothExingtechY1 method parseBytes.

private void parseBytes(byte[] weightBytes) {
    int userId = weightBytes[0] & 0xFF;
    // 0x00 male; 0x01 female
    int gender = weightBytes[1] & 0xFF;
    // 10 ~ 99
    int age = weightBytes[2] & 0xFF;
    // 0 ~ 255
    int height = weightBytes[3] & 0xFF;
    // kg
    float weight = Converters.fromUnsignedInt16Be(weightBytes, 4) / 10.0f;
    // %
    float fat = Converters.fromUnsignedInt16Be(weightBytes, 6) / 10.0f;
    // %
    float water = Converters.fromUnsignedInt16Be(weightBytes, 8) / 10.0f;
    // kg
    float bone = Converters.fromUnsignedInt16Be(weightBytes, 10) / 10.0f;
    // %
    float muscle = Converters.fromUnsignedInt16Be(weightBytes, 12) / 10.0f;
    // index
    float visc_fat = weightBytes[14] & 0xFF;
    float calorie = Converters.fromUnsignedInt16Be(weightBytes, 15);
    float bmi = Converters.fromUnsignedInt16Be(weightBytes, 17) / 10.0f;
    ScaleMeasurement scaleBtData = new ScaleMeasurement();
    final ScaleUser selectedUser = OpenScale.getInstance().getSelectedScaleUser();
    scaleBtData.setWeight(weight);
    scaleBtData.setFat(fat);
    scaleBtData.setMuscle(muscle);
    scaleBtData.setWater(water);
    scaleBtData.setBone(bone);
    scaleBtData.setVisceralFat(visc_fat);
    scaleBtData.setDateTime(new Date());
    addScaleMeasurement(scaleBtData);
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) Date(java.util.Date)

Example 20 with ScaleUser

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

the class BluetoothYunmaiSE_Mini method onNextStep.

@Override
protected boolean onNextStep(int stepNr) {
    switch(stepNr) {
        case 0:
            byte[] userId = Converters.toInt16Be(getUniqueNumber());
            final ScaleUser selectedUser = OpenScale.getInstance().getSelectedScaleUser();
            byte sex = selectedUser.getGender().isMale() ? (byte) 0x01 : (byte) 0x02;
            byte display_unit = selectedUser.getScaleUnit() == Converters.WeightUnit.KG ? (byte) 0x01 : (byte) 0x02;
            byte body_type = (byte) YunmaiLib.toYunmaiActivityLevel(selectedUser.getActivityLevel());
            byte[] user_add_or_query = new byte[] { (byte) 0x0d, (byte) 0x12, (byte) 0x10, (byte) 0x01, (byte) 0x00, (byte) 0x00, userId[0], userId[1], (byte) selectedUser.getBodyHeight(), sex, (byte) selectedUser.getAge(), (byte) 0x55, (byte) 0x5a, (byte) 0x00, (byte) 0x00, display_unit, body_type, (byte) 0x00 };
            user_add_or_query[user_add_or_query.length - 1] = xorChecksum(user_add_or_query, 1, user_add_or_query.length - 1);
            writeBytes(WEIGHT_CMD_SERVICE, WEIGHT_CMD_CHARACTERISTIC, user_add_or_query);
            break;
        case 1:
            byte[] unixTime = Converters.toInt32Be(new Date().getTime() / 1000);
            byte[] set_time = new byte[] { (byte) 0x0d, (byte) 0x0d, (byte) 0x11, unixTime[0], unixTime[1], unixTime[2], unixTime[3], (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
            set_time[set_time.length - 1] = xorChecksum(set_time, 1, set_time.length - 1);
            writeBytes(WEIGHT_CMD_SERVICE, WEIGHT_CMD_CHARACTERISTIC, set_time);
            break;
        case 2:
            setNotificationOn(WEIGHT_MEASUREMENT_SERVICE, WEIGHT_MEASUREMENT_CHARACTERISTIC);
            break;
        case 3:
            byte[] magic_bytes = new byte[] { (byte) 0x0d, (byte) 0x05, (byte) 0x13, (byte) 0x00, (byte) 0x16 };
            writeBytes(WEIGHT_CMD_SERVICE, WEIGHT_CMD_CHARACTERISTIC, magic_bytes);
            sendMessage(R.string.info_step_on_scale, 0);
            break;
        default:
            return false;
    }
    return true;
}
Also used : ScaleUser(com.health.openscale.core.datatypes.ScaleUser) Date(java.util.Date)

Aggregations

ScaleUser (com.health.openscale.core.datatypes.ScaleUser)69 ScaleMeasurement (com.health.openscale.core.datatypes.ScaleMeasurement)23 Date (java.util.Date)21 Calendar (java.util.Calendar)9 OpenScale (com.health.openscale.core.OpenScale)8 ParseException (java.text.ParseException)6 Converters (com.health.openscale.core.utils.Converters)5 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)4 Intent (android.content.Intent)3 SharedPreferences (android.content.SharedPreferences)3 SimpleDateFormat (java.text.SimpleDateFormat)3 AlertDialog (android.app.AlertDialog)2 Context (android.content.Context)2 DialogInterface (android.content.DialogInterface)2 Uri (android.net.Uri)2 ArrayAdapter (android.widget.ArrayAdapter)2 Spinner (android.widget.Spinner)2 TableRow (android.widget.TableRow)2 TextView (android.widget.TextView)2