Xiaomi: Update stress ranges to match watch

This commit is contained in:
José Rebelo 2023-12-12 20:46:59 +00:00
parent 372cf563ea
commit cb3e10f07a
5 changed files with 46 additions and 22 deletions

View File

@ -59,7 +59,6 @@ import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.StressSample;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class StressChartFragment extends AbstractChartFragment<StressChartFragment.StressChartsData> {
@ -97,7 +96,7 @@ public class StressChartFragment extends AbstractChartFragment<StressChartFragme
ensureStartAndEndSamples((List<StressSample>) samples);
return new StressChartsDataBuilder(samples).build();
return new StressChartsDataBuilder(samples, device.getDeviceCoordinator().getStressRanges()).build();
}
protected LineDataSet createDataSet(final StressType stressType, final List<Entry> values) {
@ -281,6 +280,7 @@ public class StressChartFragment extends AbstractChartFragment<StressChartFragme
private static final int UNKNOWN_VAL = 2;
private final List<? extends StressSample> samples;
private final int[] stressRanges;
private final TimestampTranslation tsTranslation = new TimestampTranslation();
@ -293,8 +293,9 @@ public class StressChartFragment extends AbstractChartFragment<StressChartFragme
long averageSum;
long averageNumSamples;
public StressChartsDataBuilder(final List<? extends StressSample> samples) {
public StressChartsDataBuilder(final List<? extends StressSample> samples, final int[] stressRanges) {
this.samples = samples;
this.stressRanges = stressRanges;
}
private void reset() {
@ -326,7 +327,7 @@ public class StressChartFragment extends AbstractChartFragment<StressChartFragme
private void processSample(final StressSample sample) {
//LOG.debug("Processing sample {} {}", sdf.format(new Date(sample.getTimestamp())), sample.getStress());
final StressType stressType = StressType.fromStress(sample.getStress());
final StressType stressType = StressType.fromStress(sample.getStress(), stressRanges);
final int ts = tsTranslation.shorten((int) (sample.getTimestamp() / 1000L));
if (ts == 0) {
@ -460,14 +461,14 @@ public class StressChartFragment extends AbstractChartFragment<StressChartFragme
return ContextCompat.getColor(context, colorId);
}
public static StressType fromStress(final int stress) {
if (stress < 0) {
public static StressType fromStress(final int stress, final int[] stressRanges) {
if (stress < stressRanges[0]) {
return StressType.UNKNOWN;
} else if (stress < 40) {
} else if (stress < stressRanges[1]) {
return StressType.RELAXED;
} else if (stress < 60) {
} else if (stress < stressRanges[2]) {
return StressType.MILD;
} else if (stress < 80) {
} else if (stress < stressRanges[3]) {
return StressType.MODERATE;
} else {
return StressType.HIGH;

View File

@ -80,6 +80,7 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviceCoordinator.class);
private Pattern supportedDeviceName = null;
/**
* This method should return a ReGexp pattern that will matched against a found device
* to check whether this coordinator supports that device.
@ -87,17 +88,17 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
* should be overridden.
*
* @return Pattern
* */
protected Pattern getSupportedDeviceName(){
*/
protected Pattern getSupportedDeviceName() {
return null;
}
@Override
public boolean supports(GBDeviceCandidate candidate) {
if(supportedDeviceName == null){
if (supportedDeviceName == null) {
supportedDeviceName = getSupportedDeviceName();
}
if(supportedDeviceName == null){
if (supportedDeviceName == null) {
throw new RuntimeException(getClass() + " should either override getSupportedDeviceName or supports(GBDeviceCandidate)");
}
@ -192,6 +193,15 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
return null;
}
@Override
public int[] getStressRanges() {
// 0-39 = relaxed
// 40-59 = mild
// 60-79 = moderate
// 80-100 = high
return new int[]{0, 40, 60, 80};
}
@Override
public TimeSampleProvider<? extends TemperatureSample> getTemperatureSampleProvider(GBDevice device, DaoSession session) {
return null;
@ -282,7 +292,9 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
}
@Override
public boolean supportsFlashing() { return false; }
public boolean supportsFlashing() {
return false;
}
@Nullable
@Override
@ -519,15 +531,16 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
int[] settings = new int[0];
ConnectionType connectionType = getConnectionType();
if(connectionType.usesBluetoothLE()){
if (connectionType.usesBluetoothLE()) {
settings = ArrayUtils.insert(0, settings, R.xml.devicesettings_reconnect_ble);
}
if(connectionType.usesBluetoothClassic()){
if (connectionType.usesBluetoothClassic()) {
settings = ArrayUtils.insert(0, settings, R.xml.devicesettings_reconnect_bl_classic);
}
return settings;
}
@Override
public int[] getSupportedDeviceSpecificApplicationSettings() {
return new int[0];
@ -613,7 +626,7 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
}
@Override
public int getOrderPriority(){
public int getOrderPriority() {
return 0;
}

View File

@ -249,6 +249,11 @@ public interface DeviceCoordinator {
*/
TimeSampleProvider<? extends StressSample> getStressSampleProvider(GBDevice device, DaoSession session);
/**
* Returns the stress ranges (relaxed, mild, moderate, high), so that stress can be categorized.
*/
int[] getStressRanges();
/**
* Returns the sample provider for temperature data, for the device being supported.
*/

View File

@ -115,6 +115,15 @@ public abstract class XiaomiCoordinator extends AbstractBLEDeviceCoordinator {
return new XiaomiStressSampleProvider(device, session);
}
@Override
public int[] getStressRanges() {
// 1-25 = relaxed
// 26-50 = mild
// 51-80 = moderate
// 81-100 = high
return new int[]{1, 26, 51, 81};
}
@Override
public TimeSampleProvider<? extends Spo2Sample> getSpo2SampleProvider(final GBDevice device, final DaoSession session) {
return new XiaomiSpo2SampleProvider(device, session);

View File

@ -50,11 +50,7 @@ public interface StressSample extends TimeSample {
Type getType();
/**
* Returns the normalized stress value between 0 and 100:
* - 0-39 = relaxed
* - 40-59 = mild
* - 60-79 = moderate
* - 80-100 = high
* Returns the stress value between 0 and 100.
*/
int getStress();
}