Commit a209a9f5bce2a75dd7456740fe383b22f89f36a0

Authored by 权海
1 parent 0f66e87b

feat(ui):ohos计算和入库

1 import 'dart:async'; 1 import 'dart:async';
2 -import 'dart:io';  
3 2
4 import 'package:doublefeel_flutter/app/modules/friends/controllers/friend_trend_controller.dart'; 3 import 'package:doublefeel_flutter/app/modules/friends/controllers/friend_trend_controller.dart';
5 import 'package:doublefeel_flutter/app/modules/home/controllers/home_controller.dart'; 4 import 'package:doublefeel_flutter/app/modules/home/controllers/home_controller.dart';
@@ -922,9 +921,11 @@ class TodayController extends GetxController with WidgetsBindingObserver { @@ -922,9 +921,11 @@ class TodayController extends GetxController with WidgetsBindingObserver {
922 // changeDate(today); 921 // changeDate(today);
923 checkAddFriendVisible(); 922 checkAddFriendVisible();
924 checkHrvAdBannerVisible(); 923 checkHrvAdBannerVisible();
925 - checkHealthDataAuthCardVisible(isPullToRefresh: isPullToRefresh); 924 + checkHealthDataAuthCardVisible(
  925 + isPullToRefresh: isPullToRefresh,
  926 + shouldCaculateAndUpload: isOhos(),
  927 + );
926 checkNotificationAuthorizationBannerVisible(); 928 checkNotificationAuthorizationBannerVisible();
927 - _performHealthDataUpload();  
928 // await loadDataForDate(selectedDate.value); 929 // await loadDataForDate(selectedDate.value);
929 } 930 }
930 931
@@ -2462,7 +2462,7 @@ CREATE TABLE IF NOT EXISTS $sleepResultsTable ( @@ -2462,7 +2462,7 @@ CREATE TABLE IF NOT EXISTS $sleepResultsTable (
2462 await db.insert(table, row); 2462 await db.insert(table, row);
2463 return null; 2463 return null;
2464 } on DatabaseException catch (error) { 2464 } on DatabaseException catch (error) {
2465 - if (!error.isUniqueConstraintError()) rethrow; 2465 + if (!_isUniqueConstraintError(error)) rethrow;
2466 } 2466 }
2467 final rows = await db.query( 2467 final rows = await db.query(
2468 table, 2468 table,
@@ -2608,7 +2608,7 @@ CREATE TABLE IF NOT EXISTS $sleepResultsTable ( @@ -2608,7 +2608,7 @@ CREATE TABLE IF NOT EXISTS $sleepResultsTable (
2608 await db.insert(table, row); 2608 await db.insert(table, row);
2609 return null; 2609 return null;
2610 } on DatabaseException catch (error) { 2610 } on DatabaseException catch (error) {
2611 - if (!error.isUniqueConstraintError()) rethrow; 2611 + if (!_isUniqueConstraintError(error)) rethrow;
2612 } 2612 }
2613 final rows = await db.query( 2613 final rows = await db.query(
2614 table, 2614 table,
@@ -2619,6 +2619,13 @@ CREATE TABLE IF NOT EXISTS $sleepResultsTable ( @@ -2619,6 +2619,13 @@ CREATE TABLE IF NOT EXISTS $sleepResultsTable (
2619 return rows.isEmpty ? null : rows.first; 2619 return rows.isEmpty ? null : rows.first;
2620 } 2620 }
2621 2621
  2622 + bool _isUniqueConstraintError(DatabaseException error) {
  2623 + if (error.isUniqueConstraintError()) return true;
  2624 + final message = error.toString().toLowerCase();
  2625 + return message.contains('unique constraint failed') ||
  2626 + message.contains('code 2067');
  2627 + }
  2628 +
2622 int _currentUnixSeconds() { 2629 int _currentUnixSeconds() {
2623 return DateTime.now().millisecondsSinceEpoch ~/ 1000; 2630 return DateTime.now().millisecondsSinceEpoch ~/ 1000;
2624 } 2631 }
@@ -19,6 +19,7 @@ import '../health_raw_models.dart'; @@ -19,6 +19,7 @@ import '../health_raw_models.dart';
19 import '../health_raw_stress_calculator.dart'; 19 import '../health_raw_stress_calculator.dart';
20 import '../health_sleep_calculator.dart'; 20 import '../health_sleep_calculator.dart';
21 import '../platform_ios/apple_health_raw_data_core_service.dart'; 21 import '../platform_ios/apple_health_raw_data_core_service.dart';
  22 +import 'ohos_health_raw_data_sync_service.dart';
22 23
23 class OHOSHealthRawDataCoreService { 24 class OHOSHealthRawDataCoreService {
24 static const int defaultLookbackDays = 183; 25 static const int defaultLookbackDays = 183;
@@ -226,10 +227,14 @@ class OHOSHealthRawDataCoreService { @@ -226,10 +227,14 @@ class OHOSHealthRawDataCoreService {
226 if (effectiveEndTime < requestedStartTime) { 227 if (effectiveEndTime < requestedStartTime) {
227 throw ArgumentError.value(endTime, 'endTime'); 228 throw ArgumentError.value(endTime, 'endTime');
228 } 229 }
229 - await _syncCalculationRawDataIfNeeded( 230 + final syncResults = await _syncCalculationRawDataIfNeeded(
230 startTime: requestedStartTime, 231 startTime: requestedStartTime,
231 endTime: effectiveEndTime, 232 endTime: effectiveEndTime,
232 ); 233 );
  234 + _logInfo(
  235 + 'calculate_sync_finish startTime=$requestedStartTime '
  236 + 'endTime=$effectiveEndTime syncResults=$syncResults',
  237 + );
233 238
234 final hrvContextStart = await _localStore.latestHrvSourceStartTime(userId); 239 final hrvContextStart = await _localStore.latestHrvSourceStartTime(userId);
235 final realtimeContextStart = 240 final realtimeContextStart =
@@ -258,6 +263,17 @@ class OHOSHealthRawDataCoreService { @@ -258,6 +263,17 @@ class OHOSHealthRawDataCoreService {
258 : latestSleepResultTime - Duration.secondsPerDay, 263 : latestSleepResultTime - Duration.secondsPerDay,
259 earliestStartTime, 264 earliestStartTime,
260 ); 265 );
  266 + _logInfo(
  267 + 'calculate_context userId=$userId '
  268 + 'requestedStartTime=$requestedStartTime endTime=$effectiveEndTime '
  269 + 'hrvContextStart=$hrvContextStart '
  270 + 'realtimeContextStart=$realtimeContextStart '
  271 + 'latestHrvRawEndTime=$latestHrvRawEndTime '
  272 + 'latestRealtimeRawEndTime=$latestRealtimeRawEndTime '
  273 + 'latestSleepResultTime=$latestSleepResultTime '
  274 + 'hrvStartTime=$hrvStartTime heartRateStartTime=$heartRateStartTime '
  275 + 'sleepStartTime=$sleepStartTime',
  276 + );
261 277
262 final hrvPoints = await _fetchRawDataInChunks( 278 final hrvPoints = await _fetchRawDataInChunks(
263 HealthDataUploadType.hrv.type, 279 HealthDataUploadType.hrv.type,
@@ -287,6 +303,13 @@ class OHOSHealthRawDataCoreService { @@ -287,6 +303,13 @@ class OHOSHealthRawDataCoreService {
287 effectiveEndTime, 303 effectiveEndTime,
288 readChunkDays: readChunkDays, 304 readChunkDays: readChunkDays,
289 ); 305 );
  306 + _logInfo(
  307 + 'calculate_raw_counts userId=$userId '
  308 + 'hrv=${hrvPoints.length} heartRate=${heartRatePoints.length} '
  309 + 'restingHeartRate=${restingHeartRatePoints.length} '
  310 + 'sleepIntervals=${sleepIntervals.length} '
  311 + 'workoutIntervals=${workoutIntervals.length}',
  312 + );
290 313
291 final result = await Isolate.run( 314 final result = await Isolate.run(
292 () => HealthRawStressCalculator(userId: userId).calculate( 315 () => HealthRawStressCalculator(userId: userId).calculate(
@@ -300,6 +323,11 @@ class OHOSHealthRawDataCoreService { @@ -300,6 +323,11 @@ class OHOSHealthRawDataCoreService {
300 ), 323 ),
301 debugName: 'OHOSHealthRawStressCalculator', 324 debugName: 'OHOSHealthRawStressCalculator',
302 ); 325 );
  326 + _logInfo(
  327 + 'calculate_result_counts userId=$userId '
  328 + 'hrv=${result.hrvStressPoints.length} '
  329 + 'realtime=${result.realtimeStressPoints.length}',
  330 + );
303 final newResult = result.copyWith( 331 final newResult = result.copyWith(
304 hrvStressPoints: _filterNewHrvStressPoints( 332 hrvStressPoints: _filterNewHrvStressPoints(
305 result.hrvStressPoints, 333 result.hrvStressPoints,
@@ -310,7 +338,17 @@ class OHOSHealthRawDataCoreService { @@ -310,7 +338,17 @@ class OHOSHealthRawDataCoreService {
310 latestRealtimeRawEndTime, 338 latestRealtimeRawEndTime,
311 ), 339 ),
312 ); 340 );
  341 + _logInfo(
  342 + 'calculate_new_result_counts userId=$userId '
  343 + 'hrv=${newResult.hrvStressPoints.length} '
  344 + 'realtime=${newResult.realtimeStressPoints.length}',
  345 + );
313 await _localStore.upsertResult(newResult); 346 await _localStore.upsertResult(newResult);
  347 + _logInfo(
  348 + 'calculate_result_stored userId=$userId '
  349 + 'hrv=${newResult.hrvStressPoints.length} '
  350 + 'realtime=${newResult.realtimeStressPoints.length}',
  351 + );
314 352
315 final dailyStressPoints = await _calculateAndStoreDailyStressPoints( 353 final dailyStressPoints = await _calculateAndStoreDailyStressPoints(
316 userId: userId, 354 userId: userId,
@@ -322,6 +360,10 @@ class OHOSHealthRawDataCoreService { @@ -322,6 +360,10 @@ class OHOSHealthRawDataCoreService {
322 sleepIntervals: sleepIntervals, 360 sleepIntervals: sleepIntervals,
323 latestSleepResultTime: latestSleepResultTime, 361 latestSleepResultTime: latestSleepResultTime,
324 ); 362 );
  363 + _logInfo(
  364 + 'calculate_daily_sleep_stored userId=$userId '
  365 + 'daily=${dailyStressPoints.length} sleep=${sleepResults.length}',
  366 + );
325 if (_uploadResultsAfterCalculation) { 367 if (_uploadResultsAfterCalculation) {
326 await _uploadResults(); 368 await _uploadResults();
327 } 369 }
@@ -511,13 +553,15 @@ class OHOSHealthRawDataCoreService { @@ -511,13 +553,15 @@ class OHOSHealthRawDataCoreService {
511 if (_serverHealthApi == null) return; 553 if (_serverHealthApi == null) return;
512 } 554 }
513 555
514 - Future<void> _syncCalculationRawDataIfNeeded({ 556 + Future<List<OhosHealthRawDataSyncResult>> _syncCalculationRawDataIfNeeded({
515 required int startTime, 557 required int startTime,
516 required int endTime, 558 required int endTime,
517 }) async { 559 }) async {
518 final rawDataSource = _rawDataSource; 560 final rawDataSource = _rawDataSource;
519 - if (rawDataSource is! OhosHealthRawDataSource) return;  
520 - await rawDataSource.syncCalculationRawData( 561 + if (rawDataSource is! OhosHealthRawDataSource) {
  562 + return const <OhosHealthRawDataSyncResult>[];
  563 + }
  564 + return rawDataSource.syncCalculationRawData(
521 startTime: startTime, 565 startTime: startTime,
522 endTime: endTime, 566 endTime: endTime,
523 ); 567 );
@@ -599,6 +599,18 @@ class OhosHealthRawDataSyncResult { @@ -599,6 +599,18 @@ class OhosHealthRawDataSyncResult {
599 final int segmentCount; 599 final int segmentCount;
600 final int pageCount; 600 final int pageCount;
601 final int storedCount; 601 final int storedCount;
  602 +
  603 + @override
  604 + String toString() {
  605 + return 'OhosHealthRawDataSyncResult('
  606 + 'dataType=$dataType, '
  607 + 'startTime=$startTime, '
  608 + 'endTime=$endTime, '
  609 + 'segmentCount=$segmentCount, '
  610 + 'pageCount=$pageCount, '
  611 + 'storedCount=$storedCount'
  612 + ')';
  613 + }
602 } 614 }
603 615
604 int _dateKeyFromUnixSeconds(int seconds) { 616 int _dateKeyFromUnixSeconds(int seconds) {