Huawei: reset some fields for sleep summary on invalid data(like naps)

This commit is contained in:
Me7c7
2025-08-03 00:12:17 +03:00
parent 57d6398c50
commit f886efdf61
2 changed files with 28 additions and 4 deletions
@@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class HuaweiTrueSleepSequenceDataParser {
@@ -158,6 +159,12 @@ public class HuaweiTrueSleepSequenceDataParser {
if (data == null || data.length == 0 || data.length > 4) {
return def;
}
// NOTE: Looks like validData should be float but the watch returns it as integer.
// unsigned integer stored as 0xBF800000 = 10111111 10000000 00000000 00000000
// for float -1.
if(Arrays.equals(data,new byte[]{(byte) 0xBF, (byte) 0x80, 0x00, 0x00})) {
return def;
}
int res = 0;
for (int i = 0; i < data.length; i++) {
res |= (data[i] & 0xFF) << (((data.length - i) - 1) * 8);
@@ -309,7 +316,7 @@ public class HuaweiTrueSleepSequenceDataParser {
byte[] value = tv2.getBytes(0x5);
fillSleepSummary(details, dictId, dataType, value);
} catch (Exception e) {
throw new RuntimeException(e);
LOG.error("Sleep dict sync: tag is missing", e);
}
}
@@ -327,6 +334,19 @@ public class HuaweiTrueSleepSequenceDataParser {
return null;
}
public static void correctSummary(SleepSummary summary) {
if(summary.validData == -1) {
summary.setBedTime(0);
summary.setRisingTime(0);
summary.setSleepScore(-1);
summary.setSleepDataQuality(-1);
summary.setDeepPart(-1);
summary.setSnoreFreq(-1);
summary.setSleepEfficiency(-1);
summary.setSleepLatency(-1);
}
}
private static long adjustTimeToMinute(long time) {
if (time % 60 != 0) {
time = (time / 60) * 60;
@@ -2818,10 +2818,14 @@ public class HuaweiSupportProvider {
for (HuaweiSequenceDataParser.SequenceData sd : sequenceFileData.getSequenceDataList()) {
LOG.info("SLEEP SequenceData: {}", sd);
HuaweiTrueSleepSequenceDataParser.SleepSummary sleepDataSummary = HuaweiTrueSleepSequenceDataParser.parseSleepDataSummary(sequenceFileData, sd);
LOG.info("SLEEP DataSummary: {}", sleepDataSummary);
if(sleepDataSummary == null)
if(sleepDataSummary == null) {
LOG.warn("SLEEP DataSummary is null");
continue;
}
// NOTE: some watches return incorrect(random) data for some fields. We need to correct this data.
HuaweiTrueSleepSequenceDataParser.correctSummary(sleepDataSummary);
LOG.info("SLEEP DataSummary: {}", sleepDataSummary);
HuaweiSleepStatsSample sleepStat = new HuaweiSleepStatsSample();
sleepStat.setTimestamp(sleepDataSummary.getFallAsleepTime() * 1000L);