1: public static void OfflineSyncUser(ProfileModel profile)
2: {
3: DateTime timeStamp = DateTime.Now;
4:
5: // Retrieve the latest info from HealthVault
6: if (profile.UserCtx.hv_personid != null)
7: {
8: HealthRecordItemCollection items = GetHVItemsOffline(profile.UserCtx.hv_personid,
9: profile.UserCtx.hv_recordid,profile.UserCtx.hv_last_sync_time);
10: if (items != null && items.Count > 0)
11: {
12: foreach (HealthRecordItem item in items)
13: {
14: // Do the distinct per item work
15: ProcessStepsHealthItem(item, profile);
16: }
17: WlkMiTracer.Instance.Log("HVSync.cs:OfflineSyncUser", WlkMiEvent.AppSync,
18: WlkMiCat.Info, string.Format("Number of items retrieved from HV: {0}",
19: items.Count));
20: }
21: //only update the last sync time if we are able to download items
22: if (items != null)
23: {
24: //set last sync time
25: profile.UserCtx.hv_last_sync_time = timeStamp;
26: profile.Save();
27: }
28: }
29:
30: // Clear the WlkMi data cache if the last sync is null
31: // In other words, you can also set the time stamp for user's last sync to null
32: // to trigger a full offline sync.
33: // TODO: Consider not doing this for production
34: if (!profile.UserCtx.hv_last_sync_time.HasValue)
35: {
36: WlkMiTracer.Instance.Log("HVSync.cs:OfflineSyncUser", WlkMiEvent.AppSync,
37: WlkMiCat.Info, string.Format("Deleting information for user {0}",
38: profile.UserCtx.user_id));
39:
40: WalkLogModel.ClearUserCache(profile);
41: }
42:
43: // Processtotals for this user
44: WalkLogModel.ProcessTotals(profile.UserCtx.user_id);
45:
46: WlkMiTracer.Instance.Log("HVSync.cs:OfflineSyncUser", WlkMiEvent.AppSync,
47: WlkMiCat.Info, string.Format("Completed Offline Sync of User: {0}",
48: profile.UserCtx.user_id.ToString()));
49: }
50:
51: public static HealthRecordItemCollection GetHVItemsOffline(Guid personId,
52: Guid recordGuid, DateTime? lastSync)
53: {
54: // Do the offline connection
55: OfflineWebApplicationConnection offlineConn =
56: new OfflineWebApplicationConnection(personId);
57: offlineConn.RequestTimeoutSeconds = 180; //extending time to prevent time outs for accounts with large number of items
58: offlineConn.Authenticate();
59: HealthRecordAccessor accessor =
60: new HealthRecordAccessor(offlineConn, recordGuid);
61: HealthRecordSearcher searcher = accessor.CreateSearcher();
62:
63: HealthRecordFilter filter = new HealthRecordFilter(Exercise.TypeId,
64: AerobicSession.TypeId);
65:
66: if (lastSync.HasValue)
67: filter.UpdatedDateMin = (DateTime)lastSync;
68:
69: searcher.Filters.Add(filter);
70:
71: HealthRecordItemCollection items = null;
72: try
73: {
74: items = searcher.GetMatchingItems()[0];
75: }
76: catch (Exception err)
77: {
78: WlkMiTracer.Instance.Log("HVSync.cs:GetHVItemsOffline", WlkMiEvent.AppSync, WlkMiCat.Error,
79: string.Format("Error for user {0} : {1} ",
80: recordGuid.ToString(),err.ToString()));
81: }
82: return items;
83: }