From db4184c0dd0e565cc06591ff50eb38000b584254 Mon Sep 17 00:00:00 2001 From: Me7c7 Date: Wed, 3 Sep 2025 18:07:12 +0300 Subject: [PATCH] Huawei: fix temperature sample provider, refactoring --- ...HuaweiCompatTemperatureSampleProvider.java | 86 ++++++++++++++--- .../devices/huawei/HuaweiUtil.java | 4 + .../devices/huawei/HuaweiSupportProvider.java | 93 +------------------ 3 files changed, 84 insertions(+), 99 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiCompatTemperatureSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiCompatTemperatureSampleProvider.java index 188097c796..7588036310 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiCompatTemperatureSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiCompatTemperatureSampleProvider.java @@ -3,11 +3,17 @@ package nodomain.freeyourgadget.gadgetbridge.devices.huawei; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import java.nio.ByteBuffer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; +import de.greenrobot.dao.query.DeleteQuery; import de.greenrobot.dao.query.QueryBuilder; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; @@ -20,6 +26,7 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.TemperatureSample; public class HuaweiCompatTemperatureSampleProvider implements TimeSampleProvider { + protected static final Logger LOG = LoggerFactory.getLogger(HuaweiCompatTemperatureSampleProvider.class); private final HuaweiTemperatureSampleProvider sp; @@ -32,23 +39,18 @@ public class HuaweiCompatTemperatureSampleProvider implements TimeSampleProvider sp = new HuaweiTemperatureSampleProvider(device, session); } - private double conv2Double(byte[] b) { - return ByteBuffer.wrap(b).getDouble(); - } - @NonNull @Override public List getAllSamples(long timestampFrom, long timestampTo) { TemperatureSample newFirst = sp.getFirstSample(); - if (newFirst != null && newFirst.getTimestamp() < timestampFrom) { return sp.getAllSamples(timestampFrom, timestampTo); } List ret = sp.getAllSamples(timestampFrom, timestampTo); - long oldTo = timestampFrom; + long oldTo = timestampTo; if (!ret.isEmpty()) { oldTo = ret.get(0).getTimestamp(); } @@ -82,7 +84,7 @@ public class HuaweiCompatTemperatureSampleProvider implements TimeSampleProvider int idx = 0; for (HuaweiDictDataValues vl : valuesData) { - double skinTemperature = conv2Double(vl.getValue()); + double skinTemperature = HuaweiUtil.convBytes2Double(vl.getValue()); if (skinTemperature >= 20 && skinTemperature <= 42) { HuaweiTemperatureSample sample = new HuaweiTemperatureSample(); sample.setTimestamp(vl.getHuaweiDictData().getStartTimestamp()); @@ -146,7 +148,7 @@ public class HuaweiCompatTemperatureSampleProvider implements TimeSampleProvider HuaweiTemperatureSample sample = new HuaweiTemperatureSample(); sample.setTimestamp(valuesData.get(0).getHuaweiDictData().getStartTimestamp()); - sample.setTemperature((float) conv2Double(valuesData.get(0).getValue())); + sample.setTemperature((float) HuaweiUtil.convBytes2Double(valuesData.get(0).getValue())); sample.setTemperatureType(0); return sample; } @@ -186,7 +188,7 @@ public class HuaweiCompatTemperatureSampleProvider implements TimeSampleProvider HuaweiTemperatureSample sample = new HuaweiTemperatureSample(); sample.setTimestamp(valuesData.get(0).getHuaweiDictData().getStartTimestamp()); - sample.setTemperature((float) conv2Double(valuesData.get(0).getValue())); + sample.setTemperature((float) HuaweiUtil.convBytes2Double(valuesData.get(0).getValue())); sample.setTemperatureType(0); return sample; } @@ -219,8 +221,70 @@ public class HuaweiCompatTemperatureSampleProvider implements TimeSampleProvider HuaweiTemperatureSample sample = new HuaweiTemperatureSample(); sample.setTimestamp(valuesData.get(0).getHuaweiDictData().getStartTimestamp()); - sample.setTemperature((float) conv2Double(valuesData.get(0).getValue())); + sample.setTemperature((float) HuaweiUtil.convBytes2Double(valuesData.get(0).getValue())); sample.setTemperatureType(0); return sample; } + + public static boolean migrateOldData() { + long count; + try (DBHandler db = GBApplication.acquireDB()) { + DaoSession daoSession = db.getDaoSession(); + QueryBuilder qbv1 = daoSession.getHuaweiDictDataValuesDao().queryBuilder(); + count = qbv1.count(); + } catch (Exception e) { + LOG.error("Error calculate data to migrate", e); + return false; + } + + int limit = 10000; + int offset = 0; + while (true) { + try (DBHandler db = GBApplication.acquireDB()) { + DaoSession daoSession = db.getDaoSession(); + + QueryBuilder qbv = daoSession.getHuaweiDictDataValuesDao().queryBuilder(); + + qbv.where(HuaweiDictDataValuesDao.Properties.DictType.eq(HuaweiDictTypes.SKIN_TEMPERATURE_VALUE)).where(HuaweiDictDataValuesDao.Properties.Tag.eq(10)).limit(limit).offset(offset); + + final List valuesData = qbv.build().list(); + + if (valuesData.isEmpty()) + break; + + List res = new ArrayList<>(); + for (HuaweiDictDataValues vl : valuesData) { + double skinTemperature = HuaweiUtil.convBytes2Double(vl.getValue()); + if (skinTemperature >= 20 && skinTemperature <= 42) { + res.add(new HuaweiTemperatureSample(vl.getHuaweiDictData().getStartTimestamp(), vl.getHuaweiDictData().getDeviceId(), vl.getHuaweiDictData().getUserId(), Math.max(vl.getHuaweiDictData().getModifyTimestamp(), vl.getHuaweiDictData().getEndTimestamp()), (float) skinTemperature, 0)); + } + } + daoSession.getHuaweiTemperatureSampleDao().insertInTx(res); + offset += limit; + LOG.info("Migrating: {}/{}", offset, count); + } catch (Exception e) { + LOG.error("Error migrate data", e); + return false; + } + } + + try (DBHandler db = GBApplication.acquireDB()) { + DaoSession daoSession = db.getDaoSession(); + + final DeleteQuery tableValuesDeleteQuery = daoSession.getHuaweiDictDataValuesDao().queryBuilder() + .where(HuaweiDictDataValuesDao.Properties.DictType.eq(HuaweiDictTypes.SKIN_TEMPERATURE_VALUE)) + .buildDelete(); + tableValuesDeleteQuery.executeDeleteWithoutDetachingEntities(); + + final DeleteQuery tableDataDeleteQuery = daoSession.getHuaweiDictDataDao().queryBuilder() + .where(HuaweiDictDataDao.Properties.DictClass.eq(HuaweiDictTypes.SKIN_TEMPERATURE_CLASS)) + .buildDelete(); + tableDataDeleteQuery.executeDeleteWithoutDetachingEntities(); + + } catch (Exception e) { + LOG.error("Error delete data", e); + return false; + } + return true; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiUtil.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiUtil.java index dc1be2c263..7d2acfa961 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiUtil.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiUtil.java @@ -56,4 +56,8 @@ public class HuaweiUtil { .put(id) .array(); } + + public static double convBytes2Double(byte[] b) { + return ByteBuffer.wrap(b).getDouble(); + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiSupportProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiSupportProvider.java index fe91cdda58..5f61be0e75 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiSupportProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiSupportProvider.java @@ -40,7 +40,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.io.IOException; import java.util.ArrayList; @@ -55,7 +54,6 @@ import de.greenrobot.dao.query.DeleteQuery; import de.greenrobot.dao.query.QueryBuilder; import kotlin.Triple; import nodomain.freeyourgadget.gadgetbridge.GBApplication; -import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity; import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst; @@ -66,6 +64,7 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCameraRemote; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventDisplayMessage; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; +import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiCompatTemperatureSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiConstants; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiCoordinatorSupplier; @@ -83,6 +82,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiStressSamplePro import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiTemperatureSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiTruSleepParser; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiTrueSleepSequenceDataParser; +import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiUtil; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.CameraRemote; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.GpsAndTime; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Weather; @@ -92,10 +92,6 @@ import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst; import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary; import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummaryDao; import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiActivitySample; -import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiDictData; -import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiDictDataDao; -import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiDictDataValues; -import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiDictDataValuesDao; import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiSleepStageSample; import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiSleepStatsSample; import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiStressSample; @@ -1540,15 +1536,13 @@ public class HuaweiSupportProvider { public void onData(int dictClass, List dictData) { LOG.info("DictionarySyncCallback onData: {}", dictClass); if (dictClass == HuaweiDictTypes.SKIN_TEMPERATURE_CLASS) { - List temperatureSamples = new ArrayList<>(); - for (HuaweiP2PDataDictionarySyncService.DictData dt : dictData) { long timestamp = dt.getStartTimestamp(); long lastTime = Math.max(dt.getEndTimestamp(), dt.getModifyTimestamp()); for (HuaweiP2PDataDictionarySyncService.DictData.DictDataValue val : dt.getData()) { if (val.getTag() == 10 && val.getDataType() == HuaweiDictTypes.SKIN_TEMPERATURE_VALUE) { - double skinTemperature = conv2Double(val.getValue()); + double skinTemperature = HuaweiUtil.convBytes2Double(val.getValue()); if (skinTemperature >= 20 && skinTemperature <= 42) { HuaweiTemperatureSample sample = new HuaweiTemperatureSample(); sample.setTimestamp(timestamp); @@ -3121,91 +3115,14 @@ public class HuaweiSupportProvider { syncState.updateState(needSync); } - private double conv2Double(byte[] b) { - return ByteBuffer.wrap(b).getDouble(); - } - public void onTestNewFunction() { AsyncTask.execute(new Runnable() { @Override public void run() { long startTime = System.nanoTime(); - long count = 0; - try (DBHandler db = GBApplication.acquireDB()) { - DaoSession daoSession = db.getDaoSession(); - QueryBuilder qbv1 = daoSession.getHuaweiDictDataValuesDao().queryBuilder(); - count = qbv1.count(); - - } catch ( - GBException e) { - LOG.error("EX", e); - } catch ( - Exception e) { - LOG.error("EX2", e); - } - - int limit = 10000; - int offset = 0; - while (true) { - try (DBHandler db = GBApplication.acquireDB()) { - DaoSession daoSession = db.getDaoSession(); - - QueryBuilder qbv = daoSession.getHuaweiDictDataValuesDao().queryBuilder(); - - qbv.where(HuaweiDictDataValuesDao.Properties.DictType.eq(HuaweiDictTypes.SKIN_TEMPERATURE_VALUE)).where(HuaweiDictDataValuesDao.Properties.Tag.eq(10)).limit(limit).offset(offset); - - final List valuesData = qbv.build().list(); - - if (valuesData.isEmpty()) - break; - - - List res = new ArrayList<>(); - for (HuaweiDictDataValues vl : valuesData) { - double skinTemperature = conv2Double(vl.getValue()); - if (skinTemperature >= 20 && skinTemperature <= 42) { - res.add(new HuaweiTemperatureSample(vl.getHuaweiDictData().getStartTimestamp(), vl.getHuaweiDictData().getDeviceId(), vl.getHuaweiDictData().getUserId(), Math.max(vl.getHuaweiDictData().getModifyTimestamp(), vl.getHuaweiDictData().getEndTimestamp()), (float) skinTemperature, 0)); - } - } - daoSession.getHuaweiTemperatureSampleDao().insertInTx(res); - offset += limit; - LOG.info("Migrating: {}/{}", offset, count); - } catch (GBException e) { - LOG.error("EX", e); - return; - } catch (Exception e) { - LOG.error("EX2", e); - return; - } - try { - Thread.sleep(100); - } catch (InterruptedException e) { - LOG.error("Sleep error", e); - } - } - - try (DBHandler db = GBApplication.acquireDB()) { - DaoSession daoSession = db.getDaoSession(); - - final DeleteQuery tableValuesDeleteQuery = daoSession.getHuaweiDictDataValuesDao().queryBuilder() - .where(HuaweiDictDataValuesDao.Properties.DictType.eq(HuaweiDictTypes.SKIN_TEMPERATURE_VALUE)) - .buildDelete(); - tableValuesDeleteQuery.executeDeleteWithoutDetachingEntities(); - - final DeleteQuery tableDataDeleteQuery = daoSession.getHuaweiDictDataDao().queryBuilder() - .where(HuaweiDictDataDao.Properties.DictClass.eq(HuaweiDictTypes.SKIN_TEMPERATURE_CLASS)) - .buildDelete(); - tableDataDeleteQuery.executeDeleteWithoutDetachingEntities(); - - } catch ( - GBException e) { - LOG.error("EX", e); - } catch ( - Exception e) { - LOG.error("EX2", e); - } - LOG.info("Migrating took : {} ms", ((System.nanoTime() - startTime) / 1000000.0)); + boolean ret = HuaweiCompatTemperatureSampleProvider.migrateOldData(); + LOG.info("Migrating: {} Took : {} ms", ret ? "OK" : "Error", ((System.nanoTime() - startTime) / 1000000.0)); } }); }