1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-09 03:37:03 +01:00

Huawei: Fix workout reparsing

Fixes #4138
This commit is contained in:
Martin.JM 2024-09-20 23:36:08 +02:00 committed by José Rebelo
parent bc814b31e7
commit 43262b3f29
8 changed files with 48 additions and 38 deletions

View File

@ -210,7 +210,7 @@ public abstract class HuaweiBRCoordinator extends AbstractBLClassicDeviceCoordin
@Override
public ActivitySummaryParser getActivitySummaryParser(final GBDevice device, final Context context) {
return new HuaweiWorkoutGbParser(device, context);
return new HuaweiWorkoutGbParser(device);
}
@Override

View File

@ -219,7 +219,7 @@ public abstract class HuaweiLECoordinator extends AbstractBLEDeviceCoordinator i
@Override
public ActivitySummaryParser getActivitySummaryParser(final GBDevice device, final Context context) {
return new HuaweiWorkoutGbParser(device, context);
return new HuaweiWorkoutGbParser(device);
}
@Override

View File

@ -70,7 +70,7 @@ public class HuaweiSettingsCustomizer implements DeviceSpecificSettingsCustomize
if (preference.getKey().equals("huawei_reparse_workout_data")) {
if (((SwitchPreferenceCompat) preference).isChecked()) {
GB.toast("Starting workout reparse", Toast.LENGTH_SHORT, 0);
new HuaweiWorkoutGbParser(handler.getDevice(), handler.getContext()).parseAllWorkouts();
new HuaweiWorkoutGbParser(handler.getDevice()).parseAllWorkouts();
GB.toast("Workout reparse is complete", Toast.LENGTH_SHORT, 0);
((SwitchPreferenceCompat) preference).setChecked(false);

View File

@ -45,17 +45,23 @@ public interface ActivitySummaryParser {
final GBDevice gbDevice,
final int timestampSeconds) {
final Device device = DBHelper.getDevice(gbDevice, session);
return findOrCreateBaseActivitySummary(session, device.getId(), timestampSeconds);
}
static BaseActivitySummary findOrCreateBaseActivitySummary(final DaoSession session,
final long deviceId,
final int timestampSeconds) {
final User user = DBHelper.getUser(session);
final BaseActivitySummaryDao summaryDao = session.getBaseActivitySummaryDao();
final QueryBuilder<BaseActivitySummary> qb = summaryDao.queryBuilder();
qb.where(BaseActivitySummaryDao.Properties.StartTime.eq(new Date(timestampSeconds * 1000L)));
qb.where(BaseActivitySummaryDao.Properties.DeviceId.eq(device.getId()));
qb.where(BaseActivitySummaryDao.Properties.DeviceId.eq(deviceId));
qb.where(BaseActivitySummaryDao.Properties.UserId.eq(user.getId()));
final List<BaseActivitySummary> summaries = qb.build().list();
if (summaries.isEmpty()) {
final BaseActivitySummary summary = new BaseActivitySummary();
summary.setStartTime(new Date(timestampSeconds * 1000L));
summary.setDevice(device);
summary.setDeviceId(deviceId);
summary.setUser(user);
// These will be set later, once we parse the summary

View File

@ -16,7 +16,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei;
import android.content.Context;
import android.widget.Toast;
import org.slf4j.Logger;
@ -64,11 +63,9 @@ public class HuaweiWorkoutGbParser implements ActivitySummaryParser {
// TODO: Might be nicer to propagate the exceptions, so they can be handled upstream
private final GBDevice gbDevice;
private final Context context;
public HuaweiWorkoutGbParser(final GBDevice gbDevice, final Context context) {
public HuaweiWorkoutGbParser(final GBDevice gbDevice) {
this.gbDevice = gbDevice;
this.context = context;
}
@Override
@ -93,7 +90,7 @@ public class HuaweiWorkoutGbParser implements ActivitySummaryParser {
LOG.warn("Failed to find huawei summary for {}", summary.getStartTime());
return summary;
}
updateBaseSummary(db, huaweiSummaries.get(0), summary);
updateBaseSummary(session, huaweiSummaries.get(0), summary);
} catch (Exception e) {
LOG.error("Failed to update summary");
}
@ -235,9 +232,10 @@ public class HuaweiWorkoutGbParser implements ActivitySummaryParser {
parseUnknownWorkoutData();
try (DBHandler db = GBApplication.acquireDB()) {
QueryBuilder<HuaweiWorkoutSummarySample> qb = db.getDaoSession().getHuaweiWorkoutSummarySampleDao().queryBuilder();
final DaoSession session = db.getDaoSession();
QueryBuilder<HuaweiWorkoutSummarySample> qb = session.getHuaweiWorkoutSummarySampleDao().queryBuilder();
for (HuaweiWorkoutSummarySample summary : qb.listLazy()) {
parseWorkout(summary.getWorkoutId());
parseWorkout(session, summary.getWorkoutId(), summary.getDeviceId());
}
} catch (Exception e) {
GB.toast("Exception parsing workouts", Toast.LENGTH_SHORT, GB.ERROR, e);
@ -306,43 +304,49 @@ public class HuaweiWorkoutGbParser implements ActivitySummaryParser {
}
public void parseWorkout(Long workoutId) {
if (workoutId == null)
return;
try (DBHandler db = GBApplication.acquireDB()) {
QueryBuilder<HuaweiWorkoutSummarySample> qbSummary = db.getDaoSession().getHuaweiWorkoutSummarySampleDao().queryBuilder().where(
HuaweiWorkoutSummarySampleDao.Properties.WorkoutId.eq(workoutId)
);
List<HuaweiWorkoutSummarySample> summarySamples = qbSummary.build().list();
if (summarySamples.size() != 1)
return;
HuaweiWorkoutSummarySample summary = summarySamples.get(0);
final BaseActivitySummary baseSummary = ActivitySummaryParser.findOrCreateBaseActivitySummary(
db.getDaoSession(),
gbDevice,
summary.getStartTimestamp()
);
updateBaseSummary(db, summary, baseSummary);
db.getDaoSession().getBaseActivitySummaryDao().insertOrReplace(baseSummary);
final DaoSession session = db.getDaoSession();
final Device device = DBHelper.getDevice(gbDevice, session);
parseWorkout(session, workoutId, device.getId());
} catch (Exception e) {
GB.toast("Exception parsing workout data", Toast.LENGTH_SHORT, GB.ERROR, e);
LOG.error("Exception parsing workout data", e);
}
}
public void updateBaseSummary(final DBHandler db,
public void parseWorkout(final DaoSession session, final Long workoutId, final long deviceId) {
if (workoutId == null)
return;
QueryBuilder<HuaweiWorkoutSummarySample> qbSummary = session.getHuaweiWorkoutSummarySampleDao().queryBuilder().where(
HuaweiWorkoutSummarySampleDao.Properties.WorkoutId.eq(workoutId)
);
List<HuaweiWorkoutSummarySample> summarySamples = qbSummary.build().list();
if (summarySamples.size() != 1)
return;
HuaweiWorkoutSummarySample summary = summarySamples.get(0);
final BaseActivitySummary baseSummary = ActivitySummaryParser.findOrCreateBaseActivitySummary(
session,
deviceId,
summary.getStartTimestamp()
);
updateBaseSummary(session, summary, baseSummary);
session.getBaseActivitySummaryDao().insertOrReplace(baseSummary);
}
public void updateBaseSummary(final DaoSession session,
final HuaweiWorkoutSummarySample summary,
final BaseActivitySummary baseSummary) {
try {
QueryBuilder<HuaweiWorkoutDataSample> qbData = db.getDaoSession().getHuaweiWorkoutDataSampleDao().queryBuilder().where(
QueryBuilder<HuaweiWorkoutDataSample> qbData = session.getHuaweiWorkoutDataSampleDao().queryBuilder().where(
HuaweiWorkoutDataSampleDao.Properties.WorkoutId.eq(summary.getWorkoutId())
);
List<HuaweiWorkoutDataSample> dataSamples = qbData.build().list();
QueryBuilder<HuaweiWorkoutPaceSample> qbPace = db.getDaoSession().getHuaweiWorkoutPaceSampleDao().queryBuilder().where(
QueryBuilder<HuaweiWorkoutPaceSample> qbPace = session.getHuaweiWorkoutPaceSampleDao().queryBuilder().where(
HuaweiWorkoutPaceSampleDao.Properties.WorkoutId.eq(summary.getWorkoutId())
);

View File

@ -112,7 +112,7 @@ public class GetWorkoutDataRequest extends Request {
nextRequest.setFinalizeReq(this.finalizeReq);
this.nextRequest(nextRequest);
} else {
new HuaweiWorkoutGbParser(getDevice(), getContext()).parseWorkout(this.databaseId);
new HuaweiWorkoutGbParser(getDevice()).parseWorkout(this.databaseId);
if (!remainder.isEmpty()) {
GetWorkoutTotalsRequest nextRequest = new GetWorkoutTotalsRequest(

View File

@ -89,7 +89,7 @@ public class GetWorkoutPaceRequest extends Request {
nextRequest.setFinalizeReq(this.finalizeReq);
this.nextRequest(nextRequest);
} else {
new HuaweiWorkoutGbParser(getDevice(), getContext()).parseWorkout(this.databaseId);
new HuaweiWorkoutGbParser(getDevice()).parseWorkout(this.databaseId);
if (!remainder.isEmpty()) {
GetWorkoutTotalsRequest nextRequest = new GetWorkoutTotalsRequest(

View File

@ -103,7 +103,7 @@ public class GetWorkoutTotalsRequest extends Request {
nextRequest.setFinalizeReq(this.finalizeReq);
this.nextRequest(nextRequest);
} else {
new HuaweiWorkoutGbParser(getDevice(), getContext()).parseWorkout(databaseId);
new HuaweiWorkoutGbParser(getDevice()).parseWorkout(databaseId);
if (!remainder.isEmpty()) {
GetWorkoutTotalsRequest nextRequest = new GetWorkoutTotalsRequest(