Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/src/huami2021_fetch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ class Huami2021FetchStart {
final int expected;
final DateTime roundStart;

bool get ok => status == 0x01;
/// False for a bad status or an implausible date (roundStart then is the
/// 1970 placeholder and must not stamp any samples).
bool get ok => status == 0x01 && roundStart.year >= 2000 && roundStart.year <= 2100;
bool get hasData => ok && expected > 0;
}

Expand Down Expand Up @@ -105,8 +107,10 @@ Huami2021FetchStart? parseHuami2021FetchStart(Uint8List b) {
second <= 59 &&
year >= 2000 &&
year <= 2100;
final dt = DateTime(year, month, day, hour, minute, second);
// Feb 31 etc. pass the field checks but normalise into the next month.
final roundStart =
plausible ? DateTime(year, month, day, hour, minute, second) : DateTime(1970);
plausible && dt.month == month && dt.day == day ? dt : DateTime(1970);
return Huami2021FetchStart(status: status, expected: expected, roundStart: roundStart);
}

Expand Down
14 changes: 13 additions & 1 deletion test/huami2021_fetch_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,19 @@ void main() {
() {
final b = Uint8List.fromList(
[0x10, 0x01, 0x01, 1, 0, 0, 0, 0xEA, 0x07, 13, 32, 24, 60, 60]);
expect(parseHuami2021FetchStart(b)!.roundStart, DateTime(1970));
final r = parseHuami2021FetchStart(b)!;
expect(r.roundStart, DateTime(1970));
// a 1970 round start must never read as a usable round
expect(r.ok, isFalse);
expect(r.hasData, isFalse);
});

test('feb 31 is rejected, not normalised into march', () {
final b = Uint8List.fromList(
[0x10, 0x01, 0x01, 1, 0, 0, 0, 0xEA, 0x07, 2, 31, 1, 1, 1]);
final r = parseHuami2021FetchStart(b)!;
expect(r.ok, isFalse);
expect(r.roundStart, DateTime(1970));
});

test('a non-success status is not hasData', () {
Expand Down
Loading