syncFromLastTime static method
Implementation
static Future<void> syncFromLastTime(ACLDatabase aclDatabase) async {
print('Start: SyncDataHelper.syncFromLastTime!');
final appConfig = await aclDatabase.appConfigDao.findLatest();
if (appConfig == null || appConfig.shouldSyncData != true) {
print(
'SyncDataHelper.syncFromLastTime: No config found or shouldSyncData = false!');
return;
}
final httpService = HttpService(
apiEndpoint: appConfig.apiEndpoint,
apiKey: appConfig.apiKey,
apiSecret: appConfig.apiSecret,
appBundle: appConfig.appBundle,
);
final now = DateTime.now();
final yesterday = now.subtract(const Duration(days: 1));
final lastSync = appConfig.lastSync != null
? DateTime.parse(appConfig.lastSync.toString())
: null;
if (lastSync != null &&
lastSync.day == yesterday.day &&
lastSync.month == yesterday.month &&
lastSync.year == yesterday.year) {
print('SyncDataHelper.syncFromLastTime: Already sync!');
return;
}
try {
final deviceId = await AclSdkPlatform.instance.generateDeviceId();
await AppUsageAuditHelper.syncAppUsage(
appUsageAuditDao: aclDatabase.appUsageAuditDao,
lastSync: lastSync,
deviceId: deviceId,
httpService: httpService,
);
await BatteryAuditHelper.syncBatteryAudit(
batteryAuditDao: aclDatabase.batteryAuditDao,
lastSync: lastSync,
deviceId: deviceId,
httpService: httpService,
);
await DeviceResourceHelper.syncDeviceResourceAudit(
deviceResourceAuditDao: aclDatabase.deviceResourceAuditDao,
lastSync: lastSync,
deviceId: deviceId,
httpService: httpService,
);
await LocationAuditHelper.syncLocationAudit(
locationAuditDao: aclDatabase.locationAuditDao,
lastSync: lastSync,
deviceId: deviceId,
httpService: httpService,
);
await NetworkAuditHelper.syncNetworkAudit(
networkAuditDao: aclDatabase.networkAuditDao,
lastSync: lastSync,
deviceId: deviceId,
httpService: httpService,
);
await NotificationAuditHelper.syncLocationAudit(
notificationAuditDao: aclDatabase.notificationAuditDao,
lastSync: lastSync,
deviceId: deviceId,
httpService: httpService,
);
await aclDatabase.appConfigDao.updateLastSync(
DateTime(yesterday.year, yesterday.month, yesterday.day, 23, 59, 59));
} catch (e) {
print('Error: SyncDataHelper.syncFromLastTime when sync data');
print(e);
}
try {
print('Cleanup data out of 60 days');
await aclDatabase.appUsageAuditDao.cleanupBefore60Days();
await aclDatabase.batteryAuditDao.cleanupBefore60Days();
await aclDatabase.deviceResourceAuditDao.cleanupBefore60Days();
await aclDatabase.locationAuditDao.cleanupBefore60Days();
await aclDatabase.networkAuditDao.cleanupBefore60Days();
await aclDatabase.notificationAuditDao.cleanupBefore60Days();
await aclDatabase.featureDao.cleanupBefore60Days();
} catch (e) {
print('Error: SyncDataHelper.syncFromLastTime when cleanup data');
print(e);
}
}