1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-09-02 12:54:10 +02:00

merge last measurement to the Bluetooth measurement.

This commit is contained in:
OliE
2018-02-25 18:09:16 +01:00
parent e1d74bf94f
commit bdbbe9c779
3 changed files with 81 additions and 1 deletions

View File

@@ -21,10 +21,12 @@ import android.arch.persistence.room.Entity;
import android.arch.persistence.room.ForeignKey;
import android.arch.persistence.room.Index;
import android.arch.persistence.room.PrimaryKey;
import android.util.Log;
import com.health.openscale.core.utils.Converters;
import com.j256.simplecsv.common.CsvColumn;
import java.lang.reflect.Field;
import java.util.Date;
@Entity(tableName = "scaleMeasurements",
@@ -125,6 +127,25 @@ public class ScaleMeasurement implements Cloneable {
hip /= divisor;
}
public void merge(ScaleMeasurement measurements) {
try {
Field[] fields = getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(measurements);
if (Float.class.isAssignableFrom(value.getClass())) {
if ((float)field.get(this) == 0.0f) {
field.set(this, value);
}
}
field.setAccessible(false);
}
} catch (IllegalAccessException e) {
Log.e("ScaleMeasurement", "Error: " + e.getMessage());
}
}
public int getId() {
return id;
}

View File

@@ -67,6 +67,7 @@ import com.health.openscale.gui.utils.PermissionHelper;
import java.io.File;
import java.lang.reflect.Field;
import java.util.List;
import cat.ereza.customactivityoncrash.config.CaocConfig;
@@ -486,7 +487,16 @@ public class MainActivity extends AppCompatActivity
setBluetoothStatusIcon(R.drawable.ic_bluetooth_connection_success);
ScaleMeasurement scaleBtData = (ScaleMeasurement) msg.obj;
OpenScale.getInstance(getApplicationContext()).addScaleData(scaleBtData);
OpenScale openScale = OpenScale.getInstance(getApplicationContext());
List<ScaleMeasurement> scaleMeasurementList = openScale.getScaleMeasurementList();
if (!scaleMeasurementList.isEmpty()) {
ScaleMeasurement lastMeasurement = scaleMeasurementList.get(0);
scaleBtData.merge(lastMeasurement);
}
openScale.addScaleData(scaleBtData);
break;
case BT_INIT_PROCESS:
setBluetoothStatusIcon(R.drawable.ic_bluetooth_connection_success);

View File

@@ -0,0 +1,49 @@
/* Copyright (C) 2018 olie.xdev <olie.xdev@googlemail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package com.health.openscale;
import com.health.openscale.core.datatypes.ScaleMeasurement;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;
public class MeasurementTest {
private static final double DELTA = 1e-15;
@Test
public void mergeTest() throws InstantiationException, IllegalAccessException {
ScaleMeasurement measurementA = new ScaleMeasurement();
ScaleMeasurement measurementB = new ScaleMeasurement();
measurementA.setWeight(80.0f);
measurementA.setBone(3.0f);
measurementA.setMuscle(55.0f);
measurementB.setWeight(90.0f);
measurementB.setBone(10.0f);
measurementB.setHip(5.0f);
measurementB.setWater(12.0f);
measurementA.merge(measurementB);
assertEquals(80.0f, measurementA.getWeight(), DELTA);
assertEquals(3.0f, measurementA.getBone(), DELTA);
assertEquals( 5.0f, measurementA.getHip(), DELTA);
assertEquals( 12.0f, measurementA.getWater(), DELTA);
assertEquals( 55.0f, measurementA.getMuscle(), DELTA);
}
}