private static FavoriteStopViewModel ToViewModel(FavoriteStop favorite, BusStaticData staticData,
                                                         ClientBusSchedule schedule, DateTimeOffset currentTime)
        {
            var routeSchedules = schedule[favorite.Id]
                                 .Where(rs => rs.Value.Any())
                                 .OrderBy(rs => rs.Value.Aggregate(ARRIVAL_TIME_SEED, BusArrivalTime.Min))
                                 .Take(2)
                                 .ToList();

            var firstRoute  = routeSchedules.Count > 0 ? staticData.Routes[routeSchedules[0].Key] : null;
            var secondRoute = routeSchedules.Count > 1 ? staticData.Routes[routeSchedules[1].Key] : null;

            return(new FavoriteStopViewModel
            {
                StopId = favorite.Id,
                StopName = favorite.Name,

                FirstRouteName = firstRoute != null ? firstRoute.RouteNo : string.Empty,
                FirstRouteColor = firstRoute != null ? firstRoute.Color : string.Empty,
                FirstRouteArrivals = routeSchedules.Count > 0 ? RouteArrivalsSummary.ToEstimateSummary(routeSchedules[0].Value, currentTime) : "No arrivals!",

                SecondRouteName = secondRoute != null ? secondRoute.RouteNo : string.Empty,
                SecondRouteColor = secondRoute != null ? secondRoute.Color : string.Empty,
                SecondRouteArrivals = routeSchedules.Count > 1 ? RouteArrivalsSummary.ToEstimateSummary(routeSchedules[1].Value, currentTime) : string.Empty,

                DistanceFromUser = double.IsNaN(favorite.DistanceFromUser) ? "" : $"{favorite.DistanceFromUser:F1} miles",
                IsNearestStop = favorite.IsNearestStop
            });
        }
        public void SetStaticData(BusStaticData staticData)
        {
            var staticDataJson = JsonConvert.SerializeObject(staticData);

            _storageManager.SetStaticData(staticDataJson);
            _cacheManager.SetStaticData(staticDataJson);
        }
        private static List <FavoriteStop> GetFavoriteStops(BusStaticData staticData, IEnumerable <int> stopIds, LatLong?optionalUserLocation)
        {
            var favoriteStops = stopIds.Where(staticData.Stops.ContainsKey).Select(id =>
            {
                var stop             = staticData.Stops[id];
                var distanceFromUser = optionalUserLocation != null
                        ? DistanceTo(optionalUserLocation.Value.Lat, optionalUserLocation.Value.Lon, stop.Lat, stop.Long, 'M')
                        : double.NaN;

                return(new FavoriteStop(stop.ID, stop.Name, stop.RouteNames, distanceFromUser, isNearestStop: false));
            })
                                .ToList();

            var nearestStop = optionalUserLocation != null
                ? staticData.Stops.Values
                              .Aggregate((s1, s2) =>
                                         DistanceTo(optionalUserLocation.Value.Lat, optionalUserLocation.Value.Lon, s1.Lat, s1.Long, 'M') <
                                         DistanceTo(optionalUserLocation.Value.Lat, optionalUserLocation.Value.Lon, s2.Lat, s2.Long, 'M')?s1 : s2)
                : null;

            if (nearestStop != null && !favoriteStops.Any(f => f.Id == nearestStop.ID))
            {
                var distanceFromUser = optionalUserLocation != null
                    ? DistanceTo(optionalUserLocation.Value.Lat, optionalUserLocation.Value.Lon, nearestStop.Lat, nearestStop.Long, 'M')
                    : double.NaN;

                favoriteStops.Add(new FavoriteStop(nearestStop.ID, nearestStop.Name, nearestStop.RouteNames,
                                                   distanceFromUser, isNearestStop: true));
            }

            favoriteStops.Sort((f1, f2) => f1.DistanceFromUser.CompareTo(f2.DistanceFromUser));

            return(favoriteStops);
        }
Example #4
0
        public (BusSystemData data, List <string> errors) LoadTransitData()
        {
            var connexionzPlatforms = ConnexionzClient.LoadPlatforms();
            var connexionzRoutes    = ConnexionzClient.LoadRoutes();
            var googleData          = GoogleTransitClient.LoadData();

            var routes = CreateRoutes(googleData.Routes, connexionzRoutes);
            var stops  = CreateStops(connexionzPlatforms, connexionzRoutes);

            var staticData = new BusStaticData(
                routes: routes.ToDictionary(r => r.RouteNo),
                stops: stops.ToDictionary(s => s.Id)
                );

            var platformTagsLookup = connexionzPlatforms.ToDictionary(p => p.PlatformNo, p => p.PlatformTag);
            var schedule           = CreateSchedule(googleData.Schedules, connexionzRoutes, connexionzPlatforms);

            var transitData = new BusSystemData(
                staticData,
                schedule,
                platformTagsLookup);

            var errors = ValidateTransitData(transitData);

            return(transitData, errors);
        }
Example #5
0
 public async Task <BusStaticData> GetStaticDataAsync()
 {
     if (s_staticData == null)
     {
         s_staticData = JsonConvert.DeserializeObject <BusStaticData>(await GetSerializedStaticDataAsync());
     }
     return(s_staticData);
 }
Example #6
0
 public BusSystemData(
     BusStaticData staticData,
     ServerBusSchedule schedule,
     Dictionary <int, int> platformIdToPlatformTag)
 {
     StaticData = staticData;
     Schedule   = schedule;
     PlatformIdToPlatformTag = platformIdToPlatformTag;
 }
Example #7
0
        public BusSystemData LoadTransitData()
        {
            var connexionzPlatforms = ConnexionzClient.LoadPlatforms();
            var connexionzRoutes    = ConnexionzClient.LoadRoutes();
            var googleData          = GoogleTransitClient.LoadData();

            var routes = CreateRoutes(googleData.Routes, connexionzRoutes);
            var stops  = CreateStops(connexionzPlatforms, connexionzRoutes);

            var staticData = new BusStaticData
            {
                Routes = routes.ToDictionary(r => r.RouteNo),
                Stops  = stops.ToDictionary(s => s.ID)
            };

            var platformTagsLookup = connexionzPlatforms.ToDictionary(p => p.PlatformNo, p => p.PlatformTag);
            var schedule           = CreateSchedule(googleData.Schedules, connexionzRoutes, connexionzPlatforms);

            return(new BusSystemData(
                       staticData,
                       schedule,
                       platformTagsLookup));
        }
Example #8
0
 public void SetStaticData(BusStaticData staticData)
 {
     s_staticData           = staticData;
     s_serializedStaticData = JsonConvert.SerializeObject(staticData);
     File.WriteAllText(_staticDataPath, JsonConvert.SerializeObject(staticData));
 }
Example #9
0
        private static List <RouteArrivalsSummary> ToRouteArrivalsSummaries(List <string> routeNames,
                                                                            Dictionary <string, List <BusArrivalTime> > stopArrivals, DateTimeOffset currentTime, BusStaticData staticData)
        {
            var arrivalsSummaries =
                routeNames.Select(routeName =>
                                  new KeyValuePair <string, List <BusArrivalTime> >(routeName,
                                                                                    stopArrivals.ContainsKey(routeName) ? stopArrivals[routeName] : new List <BusArrivalTime>()))
                .OrderBy(kvp => kvp.Value.DefaultIfEmpty(ARRIVAL_TIME_SEED).Min())
                .Select(kvp => new RouteArrivalsSummary(routeName: kvp.Key,
                                                        routeArrivalTimes: kvp.Value, currentTime: currentTime))
                .Where(ras => staticData.Routes.ContainsKey(ras.RouteName))
                .ToList();

            return(arrivalsSummaries);
        }