1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 04:46:51 +01:00

Use ActivitySummaryData on CMF, Huami, Xiaomi, Zepp OS

This commit is contained in:
José Rebelo 2024-07-26 15:55:16 +01:00
parent 42de2fadd4
commit 7712ea773a
4 changed files with 96 additions and 173 deletions

View File

@ -19,9 +19,6 @@ package nodomain.freeyourgadget.gadgetbridge.devices.cmfwatchpro.workout;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryEntries.ACTIVE_SECONDS;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryEntries.UNIT_SECONDS;
import org.json.JSONException;
import org.json.JSONObject;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;
@ -29,6 +26,7 @@ import java.util.Date;
import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryData;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryParser;
import nodomain.freeyourgadget.gadgetbridge.service.devices.cmfwatchpro.CmfActivityType;
@ -41,7 +39,7 @@ public class CmfWorkoutSummaryParser implements ActivitySummaryParser {
@Override
public BaseActivitySummary parseBinaryData(final BaseActivitySummary summary) {
final JSONObject summaryData = new JSONObject();
final ActivitySummaryData summaryData = new ActivitySummaryData();
final ByteBuffer buf = ByteBuffer.wrap(summary.getRawSummaryData()).order(ByteOrder.LITTLE_ENDIAN);
@ -64,32 +62,8 @@ public class CmfWorkoutSummaryParser implements ActivitySummaryParser {
summary.setActivityKind(ActivityKind.TYPE_UNKNOWN);
}
addSummaryData(summaryData, ACTIVE_SECONDS, duration, UNIT_SECONDS);
summaryData.add(ACTIVE_SECONDS, duration, UNIT_SECONDS);
return summary;
}
protected void addSummaryData(final JSONObject summaryData, final String key, final float value, final String unit) {
if (value > 0) {
try {
final JSONObject innerData = new JSONObject();
innerData.put("value", value);
innerData.put("unit", unit);
summaryData.put(key, innerData);
} catch (final JSONException ignore) {
}
}
}
protected void addSummaryData(final JSONObject summaryData, final String key, final String value) {
if (key != null && !key.equals("") && value != null && !value.equals("")) {
try {
final JSONObject innerData = new JSONObject();
innerData.put("value", value);
innerData.put("unit", "string");
summaryData.put(key, innerData);
} catch (final JSONException ignore) {
}
}
}
}

View File

@ -19,8 +19,6 @@ package nodomain.freeyourgadget.gadgetbridge.devices.huami;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryEntries.*;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -30,6 +28,7 @@ import java.util.Date;
import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryData;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryParser;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuamiActivityDetailsParser;
@ -39,8 +38,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiSportsAct
public class HuamiActivitySummaryParser implements ActivitySummaryParser {
private static final Logger LOG = LoggerFactory.getLogger(HuamiActivityDetailsParser.class);
private JSONObject summaryData = new JSONObject();
protected ActivitySummaryData summaryData = new ActivitySummaryData();
public BaseActivitySummary parseBinaryData(BaseActivitySummary summary) {
Date startTime = summary.getStartTime();
@ -48,7 +46,7 @@ public class HuamiActivitySummaryParser implements ActivitySummaryParser {
LOG.error("Due to a bug, we can only parse the summary when startTime is already set");
return null;
}
summaryData = new JSONObject();
summaryData = new ActivitySummaryData();
parseBinaryData(summary, startTime);
summary.setSummaryData(summaryData.toString());
return summary;
@ -70,7 +68,7 @@ public class HuamiActivitySummaryParser implements ActivitySummaryParser {
activityKind = activityType.toActivityKind();
} catch (Exception ex) {
LOG.error("Error mapping activity kind: " + ex.getMessage(), ex);
addSummaryData("Raw Activity Kind", rawKind, UNIT_NONE);
summaryData.add("Raw Activity Kind", rawKind, UNIT_NONE);
}
summary.setActivityKind(activityKind);
@ -276,9 +274,9 @@ public class HuamiActivitySummaryParser implements ActivitySummaryParser {
buffer.getInt(); // unknown probably flatDistance = buffer.getFloat();
flatSeconds = buffer.getInt() / 1000; // ms?
addSummaryData(ASCENT_SECONDS, ascentSeconds, UNIT_SECONDS);
addSummaryData(DESCENT_SECONDS, descentSeconds, UNIT_SECONDS);
addSummaryData(FLAT_SECONDS, flatSeconds, UNIT_SECONDS);
summaryData.add(ASCENT_SECONDS, ascentSeconds, UNIT_SECONDS);
summaryData.add(DESCENT_SECONDS, descentSeconds, UNIT_SECONDS);
summaryData.add(FLAT_SECONDS, flatSeconds, UNIT_SECONDS);
}
averageHR = buffer.getShort();
@ -311,62 +309,62 @@ public class HuamiActivitySummaryParser implements ActivitySummaryParser {
// summary.setAveragePace(BLETypeConversions.toUnsigned(averagePace);
// summary.setAverageStride(BLETypeConversions.toUnsigned(averageStride);
addSummaryData(ASCENT_SECONDS, ascentSeconds, UNIT_SECONDS);
addSummaryData(DESCENT_SECONDS, descentSeconds, UNIT_SECONDS);
addSummaryData(FLAT_SECONDS, flatSeconds, UNIT_SECONDS);
addSummaryData(ASCENT_DISTANCE, ascentDistance, UNIT_METERS);
addSummaryData(DESCENT_DISTANCE, descentDistance, UNIT_METERS);
addSummaryData(FLAT_DISTANCE, flatDistance, UNIT_METERS);
summaryData.add(ASCENT_SECONDS, ascentSeconds, UNIT_SECONDS);
summaryData.add(DESCENT_SECONDS, descentSeconds, UNIT_SECONDS);
summaryData.add(FLAT_SECONDS, flatSeconds, UNIT_SECONDS);
summaryData.add(ASCENT_DISTANCE, ascentDistance, UNIT_METERS);
summaryData.add(DESCENT_DISTANCE, descentDistance, UNIT_METERS);
summaryData.add(FLAT_DISTANCE, flatDistance, UNIT_METERS);
addSummaryData(DISTANCE_METERS, distanceMeters, UNIT_METERS);
// addSummaryData("distanceMeters2", distanceMeters2, UNIT_METERS);
addSummaryData(ASCENT_METERS, ascentMeters, UNIT_METERS);
addSummaryData(DESCENT_METERS, descentMeters, UNIT_METERS);
summaryData.add(DISTANCE_METERS, distanceMeters, UNIT_METERS);
// summaryData.add("distanceMeters2", distanceMeters2, UNIT_METERS);
summaryData.add(ASCENT_METERS, ascentMeters, UNIT_METERS);
summaryData.add(DESCENT_METERS, descentMeters, UNIT_METERS);
if (maxAltitude != -100000) {
addSummaryData(ALTITUDE_MAX, maxAltitude, UNIT_METERS);
summaryData.add(ALTITUDE_MAX, maxAltitude, UNIT_METERS);
}
if (minAltitude != 100000) {
addSummaryData(ALTITUDE_MIN, minAltitude, UNIT_METERS);
summaryData.add(ALTITUDE_MIN, minAltitude, UNIT_METERS);
}
if (minAltitude != 100000) {
addSummaryData(ALTITUDE_AVG, averageAltitude, UNIT_METERS);
summaryData.add(ALTITUDE_AVG, averageAltitude, UNIT_METERS);
}
addSummaryData(STEPS, steps, UNIT_STEPS);
addSummaryData(ACTIVE_SECONDS, activeSeconds, UNIT_SECONDS);
addSummaryData(CALORIES_BURNT, caloriesBurnt, UNIT_KCAL);
addSummaryData(SPEED_MAX, maxSpeed, UNIT_METERS_PER_SECOND);
addSummaryData(SPEED_MIN, minSpeed, UNIT_METERS_PER_SECOND);
addSummaryData(SPEED_AVG, averageSpeed, UNIT_METERS_PER_SECOND);
addSummaryData(CADENCE_MAX, maxCadence, UNIT_SPM);
addSummaryData(CADENCE_MIN, minCadence, UNIT_SPM);
addSummaryData(CADENCE_AVG, averageCadence, UNIT_SPM);
summaryData.add(STEPS, steps, UNIT_STEPS);
summaryData.add(ACTIVE_SECONDS, activeSeconds, UNIT_SECONDS);
summaryData.add(CALORIES_BURNT, caloriesBurnt, UNIT_KCAL);
summaryData.add(SPEED_MAX, maxSpeed, UNIT_METERS_PER_SECOND);
summaryData.add(SPEED_MIN, minSpeed, UNIT_METERS_PER_SECOND);
summaryData.add(SPEED_AVG, averageSpeed, UNIT_METERS_PER_SECOND);
summaryData.add(CADENCE_MAX, maxCadence, UNIT_SPM);
summaryData.add(CADENCE_MIN, minCadence, UNIT_SPM);
summaryData.add(CADENCE_AVG, averageCadence, UNIT_SPM);
if (!(activityKind == ActivityKind.TYPE_ELLIPTICAL_TRAINER ||
activityKind == ActivityKind.TYPE_JUMP_ROPING ||
activityKind == ActivityKind.TYPE_EXERCISE ||
activityKind == ActivityKind.TYPE_YOGA ||
activityKind == ActivityKind.TYPE_INDOOR_CYCLING)) {
addSummaryData(PACE_MIN, minPace, UNIT_SECONDS_PER_M);
addSummaryData(PACE_MAX, maxPace, UNIT_SECONDS_PER_M);
// addSummaryData("averagePace", averagePace, UNIT_SECONDS_PER_M);
summaryData.add(PACE_MIN, minPace, UNIT_SECONDS_PER_M);
summaryData.add(PACE_MAX, maxPace, UNIT_SECONDS_PER_M);
// summaryData.add("averagePace", averagePace, UNIT_SECONDS_PER_M);
}
addSummaryData(STRIDE_TOTAL, totalStride, UNIT_METERS);
addSummaryData(HR_AVG, averageHR, UNIT_BPM);
addSummaryData(HR_MAX, maxHR, UNIT_BPM);
addSummaryData(HR_MIN, minHR, UNIT_BPM);
addSummaryData(PACE_AVG_SECONDS_KM, averageKMPaceSeconds, UNIT_SECONDS_PER_KM);
addSummaryData(STRIDE_AVG, averageStride, UNIT_CM);
addSummaryData(STRIDE_MAX, maxStride, UNIT_CM);
addSummaryData(STRIDE_MIN, minStride, UNIT_CM);
// addSummaryData("averageStride2", averageStride2, UNIT_CM);
summaryData.add(STRIDE_TOTAL, totalStride, UNIT_METERS);
summaryData.add(HR_AVG, averageHR, UNIT_BPM);
summaryData.add(HR_MAX, maxHR, UNIT_BPM);
summaryData.add(HR_MIN, minHR, UNIT_BPM);
summaryData.add(PACE_AVG_SECONDS_KM, averageKMPaceSeconds, UNIT_SECONDS_PER_KM);
summaryData.add(STRIDE_AVG, averageStride, UNIT_CM);
summaryData.add(STRIDE_MAX, maxStride, UNIT_CM);
summaryData.add(STRIDE_MIN, minStride, UNIT_CM);
// summaryData.add("averageStride2", averageStride2, UNIT_CM);
if (activityKind == ActivityKind.TYPE_SWIMMING || activityKind == ActivityKind.TYPE_SWIMMING_OPENWATER) {
addSummaryData(STROKE_DISTANCE_AVG, averageStrokeDistance, UNIT_METERS);
addSummaryData(STROKE_AVG_PER_SECOND, averageStrokesPerSecond, UNIT_STROKES_PER_SECOND);
addSummaryData(LAP_PACE_AVERAGE, averageLapPace, "second");
addSummaryData(STROKES, strokes, "strokes");
addSummaryData(SWOLF_INDEX, swolfIndex, "swolf_index");
summaryData.add(STROKE_DISTANCE_AVG, averageStrokeDistance, UNIT_METERS);
summaryData.add(STROKE_AVG_PER_SECOND, averageStrokesPerSecond, UNIT_STROKES_PER_SECOND);
summaryData.add(LAP_PACE_AVERAGE, averageLapPace, "second");
summaryData.add(STROKES, strokes, "strokes");
summaryData.add(SWOLF_INDEX, swolfIndex, "swolf_index");
String swimStyleName = "unknown"; // TODO: translate here or keep as string identifier here?
switch (swimStyle) {
case 1:
@ -382,32 +380,8 @@ public class HuamiActivitySummaryParser implements ActivitySummaryParser {
swimStyleName = "medley";
break;
}
addSummaryData(SWIM_STYLE, swimStyleName);
addSummaryData(LAPS, laps, "laps");
}
}
protected void addSummaryData(String key, float value, String unit) {
if (value > 0) {
try {
JSONObject innerData = new JSONObject();
innerData.put("value", value);
innerData.put("unit", unit);
summaryData.put(key, innerData);
} catch (JSONException ignore) {
}
}
}
protected void addSummaryData(String key, String value) {
if (key != null && !key.equals("") && value != null && !value.equals("")) {
try {
JSONObject innerData = new JSONObject();
innerData.put("value", value);
innerData.put("unit", "string");
summaryData.put(key, innerData);
} catch (JSONException ignore) {
}
summaryData.add(SWIM_STYLE, swimStyleName);
summaryData.add(LAPS, laps, "laps");
}
}
}

View File

@ -76,7 +76,7 @@ public class ZeppOsActivitySummaryParser extends HuamiActivitySummaryParser {
if (summaryProto.hasTime()) {
int totalDuration = summaryProto.getTime().getTotalDuration();
summary.setEndTime(new Date(startTime.getTime() + totalDuration * 1000L));
addSummaryData(ACTIVE_SECONDS, summaryProto.getTime().getWorkoutDuration(), UNIT_SECONDS);
summaryData.add(ACTIVE_SECONDS, summaryProto.getTime().getWorkoutDuration(), UNIT_SECONDS);
// TODO pause durations
}
@ -85,94 +85,94 @@ public class ZeppOsActivitySummaryParser extends HuamiActivitySummaryParser {
summary.setBaseLatitude(summaryProto.getLocation().getBaseLatitude());
summary.setBaseAltitude(summaryProto.getLocation().getBaseAltitude() / 2);
// TODO: Min/Max Latitude/Longitude
addSummaryData(ALTITUDE_BASE, summaryProto.getLocation().getBaseAltitude() / 2, UNIT_METERS);
summaryData.add(ALTITUDE_BASE, summaryProto.getLocation().getBaseAltitude() / 2, UNIT_METERS);
}
if (summaryProto.hasHeartRate()) {
addSummaryData(HR_AVG, summaryProto.getHeartRate().getAvg(), UNIT_BPM);
addSummaryData(HR_MAX, summaryProto.getHeartRate().getMax(), UNIT_BPM);
addSummaryData(HR_MIN, summaryProto.getHeartRate().getMin(), UNIT_BPM);
summaryData.add(HR_AVG, summaryProto.getHeartRate().getAvg(), UNIT_BPM);
summaryData.add(HR_MAX, summaryProto.getHeartRate().getMax(), UNIT_BPM);
summaryData.add(HR_MIN, summaryProto.getHeartRate().getMin(), UNIT_BPM);
}
if (summaryProto.hasSteps()) {
addSummaryData(CADENCE_MAX, summaryProto.getSteps().getMaxCadence() * 60, UNIT_SPM);
addSummaryData(CADENCE_AVG, summaryProto.getSteps().getAvgCadence() * 60, UNIT_SPM);
addSummaryData(STRIDE_AVG, summaryProto.getSteps().getAvgStride(), UNIT_CM);
addSummaryData(STEPS, summaryProto.getSteps().getSteps(), UNIT_STEPS);
summaryData.add(CADENCE_MAX, summaryProto.getSteps().getMaxCadence() * 60, UNIT_SPM);
summaryData.add(CADENCE_AVG, summaryProto.getSteps().getAvgCadence() * 60, UNIT_SPM);
summaryData.add(STRIDE_AVG, summaryProto.getSteps().getAvgStride(), UNIT_CM);
summaryData.add(STEPS, summaryProto.getSteps().getSteps(), UNIT_STEPS);
}
if (summaryProto.hasDistance()) {
addSummaryData(DISTANCE_METERS, summaryProto.getDistance().getDistance(), UNIT_METERS);
summaryData.add(DISTANCE_METERS, summaryProto.getDistance().getDistance(), UNIT_METERS);
}
if (summaryProto.hasPace()) {
addSummaryData(PACE_MAX, summaryProto.getPace().getBest(), UNIT_SECONDS_PER_M);
addSummaryData(PACE_AVG_SECONDS_KM, summaryProto.getPace().getAvg() * 1000, UNIT_SECONDS_PER_KM);
summaryData.add(PACE_MAX, summaryProto.getPace().getBest(), UNIT_SECONDS_PER_M);
summaryData.add(PACE_AVG_SECONDS_KM, summaryProto.getPace().getAvg() * 1000, UNIT_SECONDS_PER_KM);
}
if (summaryProto.hasCalories()) {
addSummaryData(CALORIES_BURNT, summaryProto.getCalories().getCalories(), UNIT_KCAL);
summaryData.add(CALORIES_BURNT, summaryProto.getCalories().getCalories(), UNIT_KCAL);
}
if (summaryProto.hasHeartRateZones()) {
// TODO hr zones bpm?
if (summaryProto.getHeartRateZones().getZoneTimeCount() == 6) {
addSummaryData(HR_ZONE_NA, summaryProto.getHeartRateZones().getZoneTime(0), UNIT_SECONDS);
addSummaryData(HR_ZONE_WARM_UP, summaryProto.getHeartRateZones().getZoneTime(1), UNIT_SECONDS);
addSummaryData(HR_ZONE_FAT_BURN, summaryProto.getHeartRateZones().getZoneTime(2), UNIT_SECONDS);
addSummaryData(HR_ZONE_AEROBIC, summaryProto.getHeartRateZones().getZoneTime(3), UNIT_SECONDS);
addSummaryData(HR_ZONE_ANAEROBIC, summaryProto.getHeartRateZones().getZoneTime(4), UNIT_SECONDS);
addSummaryData(HR_ZONE_EXTREME, summaryProto.getHeartRateZones().getZoneTime(5), UNIT_SECONDS);
summaryData.add(HR_ZONE_NA, summaryProto.getHeartRateZones().getZoneTime(0), UNIT_SECONDS);
summaryData.add(HR_ZONE_WARM_UP, summaryProto.getHeartRateZones().getZoneTime(1), UNIT_SECONDS);
summaryData.add(HR_ZONE_FAT_BURN, summaryProto.getHeartRateZones().getZoneTime(2), UNIT_SECONDS);
summaryData.add(HR_ZONE_AEROBIC, summaryProto.getHeartRateZones().getZoneTime(3), UNIT_SECONDS);
summaryData.add(HR_ZONE_ANAEROBIC, summaryProto.getHeartRateZones().getZoneTime(4), UNIT_SECONDS);
summaryData.add(HR_ZONE_EXTREME, summaryProto.getHeartRateZones().getZoneTime(5), UNIT_SECONDS);
} else {
LOG.warn("Unexpected number of HR zones {}", summaryProto.getHeartRateZones().getZoneTimeCount());
}
}
if (summaryProto.hasTrainingEffect()) {
addSummaryData(TRAINING_EFFECT_AEROBIC, summaryProto.getTrainingEffect().getAerobicTrainingEffect(), UNIT_NONE);
addSummaryData(TRAINING_EFFECT_ANAEROBIC, summaryProto.getTrainingEffect().getAnaerobicTrainingEffect(), UNIT_NONE);
addSummaryData(WORKOUT_LOAD, summaryProto.getTrainingEffect().getCurrentWorkoutLoad(), UNIT_NONE);
addSummaryData(MAXIMUM_OXYGEN_UPTAKE, summaryProto.getTrainingEffect().getMaximumOxygenUptake(), UNIT_ML_KG_MIN);
summaryData.add(TRAINING_EFFECT_AEROBIC, summaryProto.getTrainingEffect().getAerobicTrainingEffect(), UNIT_NONE);
summaryData.add(TRAINING_EFFECT_ANAEROBIC, summaryProto.getTrainingEffect().getAnaerobicTrainingEffect(), UNIT_NONE);
summaryData.add(WORKOUT_LOAD, summaryProto.getTrainingEffect().getCurrentWorkoutLoad(), UNIT_NONE);
summaryData.add(MAXIMUM_OXYGEN_UPTAKE, summaryProto.getTrainingEffect().getMaximumOxygenUptake(), UNIT_ML_KG_MIN);
}
if (summaryProto.hasAltitude()) {
addSummaryData(ALTITUDE_MAX, summaryProto.getAltitude().getMaxAltitude() / 200, UNIT_METERS);
addSummaryData(ALTITUDE_MIN, summaryProto.getAltitude().getMinAltitude() / 200, UNIT_METERS);
addSummaryData(ALTITUDE_AVG, summaryProto.getAltitude().getAvgAltitude() / 200, UNIT_METERS);
summaryData.add(ALTITUDE_MAX, summaryProto.getAltitude().getMaxAltitude() / 200, UNIT_METERS);
summaryData.add(ALTITUDE_MIN, summaryProto.getAltitude().getMinAltitude() / 200, UNIT_METERS);
summaryData.add(ALTITUDE_AVG, summaryProto.getAltitude().getAvgAltitude() / 200, UNIT_METERS);
// TODO totalClimbing
addSummaryData(ELEVATION_GAIN, summaryProto.getAltitude().getElevationGain() / 100, UNIT_METERS);
addSummaryData(ELEVATION_LOSS, summaryProto.getAltitude().getElevationLoss() / 100, UNIT_METERS);
summaryData.add(ELEVATION_GAIN, summaryProto.getAltitude().getElevationGain() / 100, UNIT_METERS);
summaryData.add(ELEVATION_LOSS, summaryProto.getAltitude().getElevationLoss() / 100, UNIT_METERS);
}
if (summaryProto.hasElevation()) {
addSummaryData(ASCENT_SECONDS, summaryProto.getElevation().getUphillTime(), UNIT_SECONDS);
addSummaryData(DESCENT_SECONDS, summaryProto.getElevation().getDownhillTime(), UNIT_SECONDS);
summaryData.add(ASCENT_SECONDS, summaryProto.getElevation().getUphillTime(), UNIT_SECONDS);
summaryData.add(DESCENT_SECONDS, summaryProto.getElevation().getDownhillTime(), UNIT_SECONDS);
}
if (summaryProto.hasSwimmingData()) {
addSummaryData(LAPS, summaryProto.getSwimmingData().getLaps(), UNIT_LAPS);
summaryData.add(LAPS, summaryProto.getSwimmingData().getLaps(), UNIT_LAPS);
switch (summaryProto.getSwimmingData().getLaneLengthUnit()) {
case 0:
addSummaryData(LANE_LENGTH, summaryProto.getSwimmingData().getLaneLength(), UNIT_METERS);
summaryData.add(LANE_LENGTH, summaryProto.getSwimmingData().getLaneLength(), UNIT_METERS);
break;
case 1:
addSummaryData(LANE_LENGTH, summaryProto.getSwimmingData().getLaneLength(), UNIT_YARD);
summaryData.add(LANE_LENGTH, summaryProto.getSwimmingData().getLaneLength(), UNIT_YARD);
break;
}
switch (summaryProto.getSwimmingData().getStyle()) {
// TODO i18n these
case 1:
addSummaryData(SWIM_STYLE, "breaststroke");
summaryData.add(SWIM_STYLE, "breaststroke");
break;
case 2:
addSummaryData(SWIM_STYLE, "freestyle");
summaryData.add(SWIM_STYLE, "freestyle");
break;
}
addSummaryData(STROKES, summaryProto.getSwimmingData().getStrokes(), UNIT_STROKES);
addSummaryData(STROKE_RATE_AVG, summaryProto.getSwimmingData().getAvgStrokeRate(), UNIT_STROKES_PER_MINUTE);
addSummaryData(STROKE_RATE_MAX, summaryProto.getSwimmingData().getMaxStrokeRate(), UNIT_STROKES_PER_MINUTE);
addSummaryData(STROKE_DISTANCE_AVG, summaryProto.getSwimmingData().getAvgDps(), UNIT_CM);
addSummaryData(SWOLF_INDEX, summaryProto.getSwimmingData().getSwolf(), UNIT_NONE);
summaryData.add(STROKES, summaryProto.getSwimmingData().getStrokes(), UNIT_STROKES);
summaryData.add(STROKE_RATE_AVG, summaryProto.getSwimmingData().getAvgStrokeRate(), UNIT_STROKES_PER_MINUTE);
summaryData.add(STROKE_RATE_MAX, summaryProto.getSwimmingData().getMaxStrokeRate(), UNIT_STROKES_PER_MINUTE);
summaryData.add(STROKE_DISTANCE_AVG, summaryProto.getSwimmingData().getAvgDps(), UNIT_CM);
summaryData.add(SWOLF_INDEX, summaryProto.getSwimmingData().getSwolf(), UNIT_NONE);
}
}
}

View File

@ -21,8 +21,6 @@ import static nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryEntries.
import static nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryEntries.TIME_START;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryEntries.UNIT_UNIX_EPOCH_SECONDS;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -33,6 +31,7 @@ import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryData;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class XiaomiSimpleActivityParser {
@ -49,7 +48,7 @@ public class XiaomiSimpleActivityParser {
}
public void parse(final BaseActivitySummary summary, final ByteBuffer buf) {
final JSONObject summaryData = new JSONObject();
final ActivitySummaryData summaryData = new ActivitySummaryData();
final byte[] header = new byte[headerSize];
buf.get(header);
@ -84,7 +83,7 @@ public class XiaomiSimpleActivityParser {
// ignored
} else if (dataEntry.getKey().equals(SWIM_STYLE)) {
String swimStyleName = "unknown";
Float swimStyle = value.floatValue();
final float swimStyle = value.floatValue();
if (swimStyle == 0) {
swimStyleName = "medley";
@ -98,7 +97,7 @@ public class XiaomiSimpleActivityParser {
swimStyleName = "butterfly";
}
addSummaryData(summaryData, dataEntry.getKey(), swimStyleName);
summaryData.add(dataEntry.getKey(), swimStyleName);
} else if (dataEntry.getKey().equals(XIAOMI_WORKOUT_TYPE)) {
// TODO use XiaomiWorkoutType
switch (value.intValue()) {
@ -112,37 +111,13 @@ public class XiaomiSimpleActivityParser {
summary.setActivityKind(ActivityKind.TYPE_UNKNOWN);
}
} else {
addSummaryData(summaryData, dataEntry.getKey(), value.floatValue(), dataEntry.getUnit());
summaryData.add(dataEntry.getKey(), value.floatValue(), dataEntry.getUnit());
}
}
summary.setSummaryData(summaryData.toString());
}
protected void addSummaryData(final JSONObject summaryData, final String key, final float value, final String unit) {
if (value > 0) {
try {
final JSONObject innerData = new JSONObject();
innerData.put("value", value);
innerData.put("unit", unit);
summaryData.put(key, innerData);
} catch (final JSONException ignore) {
}
}
}
protected void addSummaryData(final JSONObject summaryData, final String key, final String value) {
if (key != null && !key.equals("") && value != null && !value.equals("")) {
try {
final JSONObject innerData = new JSONObject();
innerData.put("value", value);
innerData.put("unit", "string");
summaryData.put(key, innerData);
} catch (final JSONException ignore) {
}
}
}
public static class Builder {
private int headerSize;
private List<XiaomiSimpleDataEntry> dataEntries = new ArrayList<>();