コード例 #1
0
        private async Task <TransitDirections> GetFreshDirections(string userId, Event evt, FocusItem item, Location location)
        {
            string directionsKey = null;
            var    address       = await ResolveAddress(userId, evt);

            if (null != address && null != location)
            {
                try
                {
                    var start = new Coordinate()
                    {
                        Lat = location.Latitude,
                        Lng = location.Longitude
                    };
                    DateTimeOffset directionRequestTime = DateTimeOffset.Now;
                    if (item.DirectionsMetadata?.TravelStatus == TravelStatus.OnJourney)
                    {
                        directionRequestTime = location.Timestamp;
                    }
                    bool             requestWithNow   = evt.Start <= directionRequestTime;
                    DirectionsResult directionsResult = null;
                    const int        preferredRoute   = 0;
                    if (!requestWithNow)
                    {
                        directionsResult = await travelServiceClient.Users[userId].Directions.Transit.Get(start, address, evt.Start);
                        if (null != directionsResult.NotFound ||
                            directionsResult.TransitDirections.Routes[preferredRoute].DepatureTime < directionRequestTime)
                        {
                            requestWithNow = true;
                        }
                    }
                    if (requestWithNow)
                    {
                        directionsResult = await travelServiceClient.Users[userId].Directions.Transit.Get(start, address, null, directionRequestTime);
                    }
                    directionsKey = directionsResult?.CacheKey;
                    await focusStore.UpdateDirections(item.Id, directionsResult,
                                                      null != directionsResult.NotFound?null : (int?)preferredRoute);

                    item.DirectionsMetadata = new DirectionsMetadata()
                    {
                        Error         = directionsResult.NotFound?.Reason,
                        Key           = directionsResult.CacheKey,
                        PeferredRoute = preferredRoute
                    };
                    if (null == directionsResult.NotFound)
                    {
                        await travelServiceClient.Directions[directionsResult.CacheKey]
                        .Subscribe(new Uri(_options.DirectionsCallbackUri));
                    }
                    return(directionsResult.TransitDirections);
                }
                catch (TravelServiceException ex)
                {
                    await logger.LogForFocusItem(userId, item.Id, $"Error while retrieving directions for {evt.Subject}: {ex.Message}",
                                                 logLevel : LogLevel.Error);
                }
            }
            // TODO maybe clear directions with focusStore.UpdateDirections
            return(null);
        }
コード例 #2
0
        public async Task<Plan> GetPlan(string userId, DateTimeOffset from, DateTimeOffset to)
        {

            string start = "#home";

            var syncResult = await _focusCalendarSyncService.SyncAsync(userId, from, to);
            var items = (await _focusStore.GetTimeRangeAsync(userId, from, to)).OrderBy(d => d.Start);
            var directionsList = new List<DirectionsResult>();
            var focusItemsList = new List<FocusItem>();
            DateTimeOffset? getup = null;
            bool first = true;
            foreach (var e in items)
            {
                var calendarItem = syncResult.Events.Where(c => c.Id == e.CalendarEventId && c.FeedId == e.CalendarEventFeedId).SingleOrDefault();
                if (null != calendarItem)
                {
                    if (first)
                    {

                    }
                    focusItemsList.Add(e);
                    var address = calendarItem.GetFormattedAddress();
                    if (!string.IsNullOrEmpty(address))
                    {
                        var directions = await _travelServiceClient.Users[userId]
                            .Directions.Transit.Get(start, address, calendarItem.Start);
                        var route = DirectionUtils.SelectRoute(directions);
                        if (null == directions.NotFound)
                        {
                            if (first)
                            {
                                getup = route.DepatureTime - TimeSpan.FromHours(1);
                            }
                            directionsList.Add(directions);
                            await _focusStore.UpdateDirections(e.Id, directions, 0);
                            await _focusStore.UpdateIndicateTime(e.Id, route.DepatureTime);
                            e.IndicateTime = route.DepatureTime;
                            e.DirectionsMetadata = new DirectionsMetadata()
                            {
                                Error = directions.NotFound?.Reason,
                                Key = directions.CacheKey,
                                PeferredRoute = 0,
                                TravelStatus = TravelStatus.UnStarted
                            };
                        }
                        else
                        {
                            await _focusStore.UpdateDirections(e.Id, directions, null);
                            e.DirectionsMetadata = new DirectionsMetadata()
                            {
                                Error = directions.NotFound?.Reason,
                                Key = directions.CacheKey,
                                PeferredRoute = 0,
                                TravelStatus = TravelStatus.UnStarted
                            };
                        }
                        start = address;
                    }
                    first = false;
                }
            }
            var plan = new Plan()
            {
                Events = syncResult.Events,
                Directions = directionsList.ToArray(),
                FocusItems = items.ToArray(),
                GetUp = getup
            };
            return plan;
        }