1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-26 10:35:50 +01:00

Mi Composition Scale: Persist weight samples

This commit is contained in:
José Rebelo 2024-08-28 17:59:15 +01:00 committed by José Rebelo
parent f746ef42f3
commit 7579ba11b1
4 changed files with 101 additions and 131 deletions

View File

@ -17,42 +17,36 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */ along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.miscale; package nodomain.freeyourgadget.gadgetbridge.devices.miscale;
import android.app.Activity;
import android.bluetooth.le.ScanFilter; import android.bluetooth.le.ScanFilter;
import android.content.Context;
import android.net.Uri;
import android.os.ParcelUuid; import android.os.ParcelUuid;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.slf4j.Logger; import androidx.annotation.NonNull;
import org.slf4j.LoggerFactory;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import de.greenrobot.dao.query.QueryBuilder;
import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractBLEDeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.AbstractBLEDeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler; import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.Device; import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.MiScaleWeightSampleDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.model.WeightSample;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService; import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miscale.MiCompositionScaleDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.devices.miscale.MiCompositionScaleDeviceSupport;
public class MiCompositionScaleCoordinator extends AbstractBLEDeviceCoordinator { public class MiCompositionScaleCoordinator extends AbstractBLEDeviceCoordinator {
private static final Logger LOG = LoggerFactory.getLogger(MiCompositionScaleCoordinator.class);
@Override @Override
protected void deleteDevice(@NonNull GBDevice gbDevice, @NonNull Device device, @NonNull DaoSession session) throws GBException { protected void deleteDevice(@NonNull final GBDevice gbDevice, @NonNull final Device device, @NonNull final DaoSession session) throws GBException {
final Long deviceId = device.getId();
final QueryBuilder<?> qb = session.getMiScaleWeightSampleDao().queryBuilder();
qb.where(MiScaleWeightSampleDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
} }
@Override @Override
@ -63,61 +57,25 @@ public class MiCompositionScaleCoordinator extends AbstractBLEDeviceCoordinator
@NonNull @NonNull
@Override @Override
public Collection<? extends ScanFilter> createBLEScanFilters() { public Collection<? extends ScanFilter> createBLEScanFilters() {
ParcelUuid bodyCompositionService = new ParcelUuid(GattService.UUID_SERVICE_BODY_COMPOSITION); final ParcelUuid bodyCompositionService = new ParcelUuid(GattService.UUID_SERVICE_BODY_COMPOSITION);
ScanFilter.Builder builder = new ScanFilter.Builder(); final ScanFilter.Builder builder = new ScanFilter.Builder();
builder.setServiceUuid(bodyCompositionService); builder.setServiceUuid(bodyCompositionService);
int manufacturerId = 0x0157; // Huami final int manufacturerId = 0x0157; // Huami
builder.setManufacturerData(manufacturerId, new byte[6], new byte[6]); builder.setManufacturerData(manufacturerId, new byte[6], new byte[6]);
return Collections.singletonList(builder.build()); return Collections.singletonList(builder.build());
} }
@Override @Override
public int getBondingStyle() { public int getBatteryCount() {
return super.BONDING_STYLE_NONE;
}
@Nullable
@Override
public Class<? extends Activity> getPairingActivity() {
return null;
}
@Override
public boolean supportsActivityDataFetching() {
return false;
}
@Override
public boolean supportsActivityTracking() {
return false;
}
@Override
public SampleProvider<? extends ActivitySample> getSampleProvider(GBDevice device, DaoSession session) {
return null;
}
@Override
public InstallHandler findInstallHandler(Uri uri, Context context) {
return null;
}
@Override
public boolean supportsScreenshots(final GBDevice device) {
return false;
}
@Override
public int getAlarmSlotCount(GBDevice device) {
return 0; return 0;
} }
@Override @Override
public boolean supportsHeartRateMeasurement(GBDevice device) { public int getBondingStyle() {
return false; return super.BONDING_STYLE_NONE;
} }
@Override @Override
@ -126,32 +84,37 @@ public class MiCompositionScaleCoordinator extends AbstractBLEDeviceCoordinator
} }
@Override @Override
public boolean supportsAppsManagement(final GBDevice device) { public TimeSampleProvider<? extends WeightSample> getWeightSampleProvider(final GBDevice device, final DaoSession session) {
return new MiScaleSampleProvider(device, session);
}
@Override
public boolean supportsWeightMeasurement() {
return true;
}
@Override
public boolean supportsActivityTracking() {
return true;
}
@Override
public boolean supportsActivityTabs() {
return false; return false;
} }
@Override @Override
public Class<? extends Activity> getAppsManagementActivity() { public boolean supportsSleepMeasurement() {
return null;
}
@Override
public boolean supportsCalendarEvents() {
return false; return false;
} }
@Override @Override
public boolean supportsRealtimeData() { public boolean supportsStepCounter() {
return false; return false;
} }
@Override @Override
public boolean supportsWeather() { public boolean supportsSpeedzones() {
return false;
}
@Override
public boolean supportsFindDevice() {
return false; return false;
} }
@ -161,13 +124,11 @@ public class MiCompositionScaleCoordinator extends AbstractBLEDeviceCoordinator
return MiCompositionScaleDeviceSupport.class; return MiCompositionScaleDeviceSupport.class;
} }
@Override @Override
public int getDeviceNameResource() { public int getDeviceNameResource() {
return R.string.devicetype_micompositionscale; return R.string.devicetype_micompositionscale;
} }
@Override @Override
public int getDefaultIconResource() { public int getDefaultIconResource() {
return R.drawable.ic_device_miscale; return R.drawable.ic_device_miscale;

View File

@ -71,9 +71,9 @@ public class MiSmartScaleCoordinator extends AbstractBLEDeviceCoordinator {
} }
@Override @Override
protected void deleteDevice(@NonNull GBDevice gbDevice, @NonNull Device device, @NonNull DaoSession session) throws GBException { protected void deleteDevice(@NonNull final GBDevice gbDevice, @NonNull final Device device, @NonNull final DaoSession session) throws GBException {
Long deviceId = device.getId(); final Long deviceId = device.getId();
QueryBuilder<?> qb = session.getMiScaleWeightSampleDao().queryBuilder(); final QueryBuilder<?> qb = session.getMiScaleWeightSampleDao().queryBuilder();
qb.where(MiScaleWeightSampleDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities(); qb.where(MiScaleWeightSampleDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
} }
@ -88,7 +88,7 @@ public class MiSmartScaleCoordinator extends AbstractBLEDeviceCoordinator {
} }
@Override @Override
public TimeSampleProvider<? extends WeightSample> getWeightSampleProvider(GBDevice device, DaoSession session) { public TimeSampleProvider<? extends WeightSample> getWeightSampleProvider(final GBDevice device, final DaoSession session) {
return new MiScaleSampleProvider(device, session); return new MiScaleSampleProvider(device, session);
} }

View File

@ -20,17 +20,26 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.miscale;
import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Intent; import android.content.Intent;
import android.os.Parcelable;
import android.widget.Toast; import android.widget.Toast;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
import nodomain.freeyourgadget.gadgetbridge.devices.miscale.MiScaleSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.MiScaleWeightSample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.btle.GattCharacteristic; import nodomain.freeyourgadget.gadgetbridge.service.btle.GattCharacteristic;
@ -38,26 +47,19 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.IntentListener; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.IntentListener;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfo;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfoProfile; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfoProfile;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class MiCompositionScaleDeviceSupport extends AbstractBTLEDeviceSupport { public class MiCompositionScaleDeviceSupport extends AbstractBTLEDeviceSupport {
private static final Logger LOG = LoggerFactory.getLogger(MiCompositionScaleDeviceSupport.class); private static final Logger LOG = LoggerFactory.getLogger(MiCompositionScaleDeviceSupport.class);
private static final String UNIT_KG = "kg"; private static final String UNIT_KG = "kg";
private static final String UNIT_LBS = "lb"; private static final String UNIT_LBS = "lb";
private static final String UNIT_JIN = "jīn"; private static final String UNIT_JIN = "jīn";
private final DeviceInfoProfile<MiCompositionScaleDeviceSupport> deviceInfoProfile; private final DeviceInfoProfile<MiCompositionScaleDeviceSupport> deviceInfoProfile;
private final GBDeviceEventVersionInfo versionCmd = new GBDeviceEventVersionInfo(); private final GBDeviceEventVersionInfo versionCmd = new GBDeviceEventVersionInfo();
private final IntentListener mListener = new IntentListener() {
@Override
public void notify(Intent intent) {
String s = intent.getAction();
if (s.equals(DeviceInfoProfile.ACTION_DEVICE_INFO)) {
handleDeviceInfo((nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfo) intent.getParcelableExtra(DeviceInfoProfile.EXTRA_DEVICE_INFO));
}
}
};
public MiCompositionScaleDeviceSupport() { public MiCompositionScaleDeviceSupport() {
super(LOG); super(LOG);
@ -68,12 +70,20 @@ public class MiCompositionScaleDeviceSupport extends AbstractBTLEDeviceSupport {
addSupportedService(UUID.fromString("00001530-0000-3512-2118-0009af100700")); addSupportedService(UUID.fromString("00001530-0000-3512-2118-0009af100700"));
deviceInfoProfile = new DeviceInfoProfile<>(this); deviceInfoProfile = new DeviceInfoProfile<>(this);
final IntentListener mListener = intent -> {
if (DeviceInfoProfile.ACTION_DEVICE_INFO.equals(intent.getAction())) {
final Parcelable deviceInfo = intent.getParcelableExtra(DeviceInfoProfile.EXTRA_DEVICE_INFO);
if (deviceInfo != null) {
handleDeviceInfo((DeviceInfo) deviceInfo);
}
}
};
deviceInfoProfile.addListener(mListener); deviceInfoProfile.addListener(mListener);
addSupportedProfile(deviceInfoProfile); addSupportedProfile(deviceInfoProfile);
} }
@Override @Override
protected TransactionBuilder initializeDevice(TransactionBuilder builder) { protected TransactionBuilder initializeDevice(final TransactionBuilder builder) {
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext())); builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
LOG.debug("Requesting Device Info!"); LOG.debug("Requesting Device Info!");
@ -88,38 +98,34 @@ public class MiCompositionScaleDeviceSupport extends AbstractBTLEDeviceSupport {
} }
@Override @Override
public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { public boolean onCharacteristicChanged(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic); if (super.onCharacteristicChanged(gatt, characteristic)) {
return true;
}
UUID characteristicUUID = characteristic.getUuid(); final UUID characteristicUUID = characteristic.getUuid();
if (characteristicUUID.equals(GattCharacteristic.UUID_CHARACTERISTIC_BODY_COMPOSITION_MEASUREMENT)) { if (characteristicUUID.equals(GattCharacteristic.UUID_CHARACTERISTIC_BODY_COMPOSITION_MEASUREMENT)) {
final byte[] data = characteristic.getValue(); final byte[] data = characteristic.getValue();
boolean stabilized = testBit(data[1], 5) && !testBit(data[1], 7); final byte flags = data[1];
boolean isLbs = testBit(data[1], 0); final boolean stabilized = testBit(flags, 5) && !testBit(flags, 7);
boolean isJin = testBit(data[1], 4);
boolean isKg = !(isLbs && isJin);
String unit = "";
if (isKg) {
unit = UNIT_KG;
} else if (isLbs) {
unit = UNIT_LBS;
} else if (isJin) {
unit = UNIT_JIN;
}
if (stabilized) { if (stabilized) {
int year = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 2); final int year = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 2);
int month = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 4); final int month = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 4);
int day = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 5); final int day = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 5);
int hour = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 6); final int hour = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 6);
int minute = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 7); final int minute = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 7);
int second = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 8); final int second = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 8);
Calendar c = GregorianCalendar.getInstance(); final Calendar c = GregorianCalendar.getInstance();
c.set(year, month - 1, day, hour, minute, second); c.set(year, month - 1, day, hour, minute, second);
Date date = c.getTime(); final Date date = c.getTime();
float weight = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 11) / (isKg ? 200.0f : 100.0f);
handleWeightInfo(date, weight, unit); float weightKg = WeightMeasurement.weightToKg(
characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 11),
flags
);
handleWeightInfo(date, weightKg);
} }
return true; return true;
@ -128,35 +134,38 @@ public class MiCompositionScaleDeviceSupport extends AbstractBTLEDeviceSupport {
return false; return false;
} }
private boolean testBit(byte value, int offset) { private boolean testBit(final byte value, final int offset) {
return ((value >> offset) & 1) == 1; return ((value >> offset) & 1) == 1;
} }
private void handleDeviceInfo(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfo info) { private void handleDeviceInfo(final DeviceInfo info) {
LOG.warn("Device info: " + info); LOG.debug("Device info: {}", info);
versionCmd.hwVersion = info.getHardwareRevision(); versionCmd.hwVersion = info.getHardwareRevision();
versionCmd.fwVersion = info.getSoftwareRevision(); versionCmd.fwVersion = info.getSoftwareRevision();
handleGBDeviceEvent(versionCmd); handleGBDeviceEvent(versionCmd);
} }
private void handleWeightInfo(Date date, float weight, String unit) { private void handleWeightInfo(final Date date, final float weightKg) {
// TODO GB.toast(getContext().getString(R.string.weight_kg, weightKg), Toast.LENGTH_SHORT, GB.INFO);
LOG.warn("Weight info: " + weight + unit);
GB.toast(weight + unit, Toast.LENGTH_SHORT, GB.INFO); try (DBHandler db = GBApplication.acquireDB()) {
final MiScaleSampleProvider provider = new MiScaleSampleProvider(getDevice(), db.getDaoSession());
final Long userId = DBHelper.getUser(db.getDaoSession()).getId();
final Long deviceId = DBHelper.getDevice(getDevice(), db.getDaoSession()).getId();
provider.addSample(new MiScaleWeightSample(
date.getTime(),
deviceId,
userId,
weightKg
));
} catch (final Exception e) {
LOG.error("Error saving weight sample", e);
}
} }
@Override @Override
public boolean useAutoConnect() { public boolean useAutoConnect() {
return false; return false;
} }
@Override
public boolean getImplicitCallbackModify() {
return true;
}
@Override
public boolean getSendWriteRequestResponse() {
return false;
}
} }

View File

@ -69,7 +69,7 @@ public class WeightMeasurement {
return new WeightMeasurement(calendar.getTime(), weightKg); return new WeightMeasurement(calendar.getTime(), weightKg);
} }
private static float weightToKg(float weight, byte flags) { public static float weightToKg(float weight, byte flags) {
boolean isLbs = testBit(flags, 0); boolean isLbs = testBit(flags, 0);
boolean isJin = testBit(flags, 4); boolean isJin = testBit(flags, 4);