public async Task <DeviceData> GetDeviceData(string userId, string deviceId) { var owner = await _deviceSyncStore.DeviceClaimedByAsync(deviceId); if (owner != userId) { throw new DeviceAccessException(deviceId, userId, owner); } var activeItem = await _focusStore.GetActiveItem(userId); if (null == activeItem) { return(new DeviceData() { Event = null, Directions = null }); } if (null == activeItem.CalendarEventId && null == activeItem.CalendarEventFeedId) { return(null); //other items are not supported yet } var evt = await _calendarServiceClient.Users[userId].Feeds[activeItem.CalendarEventFeedId].Events.Get(activeItem.CalendarEventId); if (null == evt) { return(null); } Route route = null; if (null != activeItem.DirectionsKey) { route = DirectionUtils.SelectRoute(await _travelServiceClient.Directions[activeItem.DirectionsKey].GetAsync()); } return(new DeviceData() { Event = new EventData() { Start = evt.Start, Subject = evt.Subject }, Directions = null != route ? new DirectionsData { ArrivalTime = route.ArrivalTime, DepartureTime = route.DepatureTime, Legs = route.Steps.Select(v => new LegData() { ArrivalStop = v.ArrivalStop.Name, DepartureStop = v.DepartureStop.Name, DepartureTime = v.DepartureTime, Direction = new Regex("^Wien\\s").Replace(v.Headsign ?? "", ""), Line = v.Line.ShortName }).ToArray() } : null }); }
public async Task <FocusManageResult> Update(string userId, FocusUpdateRequest focusUpdateRequest) { if (null != focusUpdateRequest.Location) { await _focusGeofenceService.UpdateFocusItems(userId, focusUpdateRequest.Location); } var res = new FocusManageResult(); var activeItems = await focusStore.GetActiveAsync(userId); var updatedItemIds = new HashSet <string>(focusUpdateRequest.ItemSyncResult != null ? focusUpdateRequest.ItemSyncResult.ChangedItems.Select(v => v.Id) : new string[0]); foreach (var item in activeItems) { var evt = await calendarServiceClient.Users[userId].Feeds[item.CalendarEventFeedId].Events.Get(item.CalendarEventId); TransitDirections directions; if (null != focusUpdateRequest.ItemSyncResult && ( focusUpdateRequest.ItemSyncResult.AddedItems.Any(v => v.Id == item.Id) || focusUpdateRequest.ItemSyncResult.ChangedItems.Any(v => v.Id == item.Id)) || null == item.DirectionsMetadata?.Key) { directions = await GetFreshDirections(userId, evt, item, focusUpdateRequest.Location); } else { var directionsRes = await GetCachedDirectionsOrNew(userId, evt, item, focusUpdateRequest.Location); directions = directionsRes.Directions; if (directionsRes.IsNew) { updatedItemIds.Add(item.Id); } if (null != directions && null != focusUpdateRequest.ChangedDirections && focusUpdateRequest.ChangedDirections.Contains( item.DirectionsMetadata.Key)) { await logger.LogForFocusItem(userId, item.Id, $"Received direction updates for {evt.Subject}", DigitTraceAction.DirectionsUpdate); updatedItemIds.Add(item.Id); } } DateTimeOffset indicateTime; if (null == directions) { await logger.LogForFocusItem(userId, item.Id, $"No departure time found, using {FocusConstants.DefaultTravelTime.TotalMinutes:0} minutes for {evt.Subject}"); indicateTime = evt.Start - FocusConstants.DefaultTravelTime; } else { indicateTime = directions.Routes[item.DirectionsMetadata.PeferredRoute].DepatureTime; } item.IndicateTime = indicateTime; await focusStore.UpdateIndicateTime(item.Id, indicateTime); res.ActiveItems.Add(new FocusItemWithExternalData() { Start = item.Start, IndicateTime = item.IndicateTime, CalendarEvent = evt, Directions = directions, DirectionsMetadata = item.DirectionsMetadata, End = item.End, Id = item.Id }); } await _focusGeofenceService.RefreshGeofencesForActiveNavigations(userId, res, DateTimeOffset.Now); var active = await focusStore.GetActiveItem(userId); var activeItemChanged = await focusStore.UpdateActiveItem(userId, active?.Id); if (activeItemChanged || (null != active && updatedItemIds.Contains(active.Id))) { if (null != active) { await Task.WhenAll(focusSubscribers.Select(v => v.ActiveItemChanged(userId, res.ActiveItems.Where(d => d.Id == active.Id).Single()))); } else { await Task.WhenAll(focusSubscribers.Select(v => v.ActiveItemChanged(userId, null))); } } if (updatedItemIds.Count > 0) { await Task.WhenAll(focusSubscribers.Select(v => v.ActiveItemsChanged(userId, res))); } return(res); }