예제 #1
0
        // Assumed predictions are up to date and therefore overwrite schedule's data
        public void UpdateIfPrediction(DepartureResponse departureData,
                                       ScheduleResource schedule,
                                       HashSet <PredictionResource> predictions)
        {
            // In schedules and predictions it looked like the tripId was the only unique id in both (should only be 1 returned)
            var scheduleTripId = schedule.relationships.trip.data.id;
            var prediction     = predictions.FirstOrDefault(x => x.relationships.trip.data.id == scheduleTripId);

            if (prediction == null)
            {
                return;
            }

            // If train is not on time and has null departure time then departure time is set to "TBD" in the model
            if (!string.IsNullOrEmpty(prediction.attributes.status) && prediction.attributes.status != ON_TIME_STATUS && prediction.attributes.departure_time == null)
            {
                departureData.DepartureTime = "TBD";
            }
            else if (prediction.attributes.departure_time != null)
            {
                departureData.DepartureTime = prediction.attributes.departure_time;
            }

            if (!string.IsNullOrEmpty(prediction.attributes.status))
            {
                departureData.Status = prediction.attributes.status;
            }

            departureData.TrackNumber = ConvertStopIdToTrackNumber(prediction.relationships.stop.data.id);

            departureData.DirectionId = prediction.attributes.direction_id;
        }
예제 #2
0
        private static string GetWellingtonResponse(MetlinkResponse metlinkResponse)
        {
            var responseString = string.Empty;

            foreach (var key in Destinations.Keys)
            {
                var firstDeparture = metlinkResponse.Departures.FirstOrDefault(d => d.Destination.StopId == key);

                if (firstDeparture != null)
                {
                    var departureResponse = new DepartureResponse
                    {
                        Direction   = "Outbound",
                        Destination = Destinations[key],
                        Minutes     = firstDeparture.DepartureTime.GetSecondsFromNow() / 60,
                        Seconds     = firstDeparture.DepartureTime.GetSecondsFromNow() % 60
                    };

                    responseString += string.Format(DepartureFormat, "Wellington", Destinations[key], departureResponse.Seconds);
                }
                else if (key != "WELL")
                {
                    responseString += string.Format("No {0} departures listed. ", Destinations[key]);
                }
            }

            return(responseString);
        }
예제 #3
0
        public Departure(DepartureResponse departureResponseModel)
        {
            FullName  = departureResponseModel.Name;
            ShortName = departureResponseModel.SName;
            Track     = departureResponseModel.Track;
            switch (departureResponseModel.Type)
            {
            case "BUS":
                VehicleType = VehicleType.Bus;
                break;

            case "VAS":     // Västtåg
            case "LDT":     // Long Distance Train
            case "REG":     // Regional Train
                VehicleType = VehicleType.Train;
                break;

            case "BOAT":
                VehicleType = VehicleType.Boat;
                break;

            case "TRAM":
                VehicleType = VehicleType.Tram;
                break;

            case "TAXI":
                VehicleType = VehicleType.Taxi;
                break;

            default:
                VehicleType = VehicleType.Unknown;
                break;
            }
            JourneyNumber      = departureResponseModel.JourneyNumber;
            JourneyId          = departureResponseModel.JourneyId;
            StopName           = departureResponseModel.Stop;
            StopId             = departureResponseModel.StopId;
            ScheduledDeparture = DateTime.ParseExact(string.Format("{0} {1}", departureResponseModel.ScheduledDate, departureResponseModel.ScheduledTime), "yyyy-MM-dd HH:mm", System.Globalization.CultureInfo.InvariantCulture);
            string realDate = departureResponseModel.RealisticDate;
            string realTime = departureResponseModel.RealisticTime;

            if (!string.IsNullOrEmpty(realDate) || !string.IsNullOrEmpty(realTime))
            {
                RealisticDeparture = DateTime.ParseExact(string.Format("{0} {1}", realDate, realTime), "yyyy-MM-dd HH:mm", System.Globalization.CultureInfo.InvariantCulture);
            }
            else
            {
                RealisticDeparture = null;
            }
            Direction               = departureResponseModel.Direction;
            Accessibility           = departureResponseModel.Accessibility;
            LineLogoTextColor       = departureResponseModel.ForegroundColor;
            LineLogoBackgroundColor = departureResponseModel.BackgroundColor;
            LineLogoBorderStyle     = departureResponseModel.Stroke;
        }
예제 #4
0
        public async Task <List <DepartureResponse> > GetDepartureData()
        {
            var departureDataList = new List <DepartureResponse>();

            var schedules = await GetTrimmedSchedules();

            var predictions = await _mbtaApiRepository.GetNorthStationPredictions();

            var routes = await _mbtaApiRepository.GetAllCommuterRailRoutes();

            foreach (var schedule in schedules)
            {
                // If on schedule then status will be "On Time"
                var departureData =
                    new DepartureResponse(
                        schedule.attributes.departure_time,
                        ConvertStopIdToTrackNumber(schedule.relationships.stop.data.id),
                        ON_TIME_STATUS,
                        schedule.attributes.direction_id);

                UpdateIfPrediction(departureData, schedule, predictions.ToHashSet());

                departureData.Destination = GetDestination(schedule.relationships.route.data.id,
                                                           departureData.DirectionId, routes);

                departureDataList.Add(departureData);
            }

            // Remove TBD times so list can be ordered and then re-add them
            var tbdTimes = departureDataList.Where(x => x.DepartureTime == "TBD").ToList();

            departureDataList.RemoveAll(x => x.DepartureTime == "TBD");
            var orderedDepartureTimes = departureDataList.OrderBy(x => DateTime.Parse(x.DepartureTime)).ToList();

            orderedDepartureTimes.AddRange(tbdTimes);

            return(orderedDepartureTimes);
        }
예제 #5
0
        public void UpdateIfPredictionUpdates()
        {
            var schedules        = CreateSchedules();
            var scheduleToChange = schedules[2];

            var predictions = CreatePredictions();
            var prediction  = predictions[1];

            var departureData = new DepartureResponse(
                scheduleToChange.attributes.departure_time,
                scheduleToChange.trackNumber,
                ON_TIME_STATUS,
                scheduleToChange.attributes.direction_id);

            _mbtaAppManager.UpdateIfPrediction(departureData, scheduleToChange, predictions.ToHashSet());

            Assert.NotEqual(DateTime.Parse(prediction.attributes.departure_time), DateTime.Parse(scheduleToChange.attributes.departure_time));

            Assert.Equal(prediction.attributes.status.ToUpper(), departureData.Status);
            Assert.NotEqual(ON_TIME_STATUS, prediction.attributes.status);

            Assert.NotEqual(prediction.attributes.direction_id, scheduleToChange.attributes.direction_id);
        }
예제 #6
0
        private static async Task <string> BuildResponseString(string direction, List <Departure> orderedDepartures)
        {
            var stops = await Stops.Current();

            var stop = stops[orderedDepartures.First().StopId];

            var nextDeparture = orderedDepartures.FirstOrDefault(d => d.Direction == direction && d.ServiceId != "WRL");

            if (nextDeparture != null)
            {
                var departureResponse = new DepartureResponse
                {
                    Direction   = direction,
                    Destination = stops[nextDeparture.Destination.StopId],
                    Minutes     = nextDeparture.DepartureTime.GetSecondsFromNow() / 60,
                    Seconds     = nextDeparture.DepartureTime.GetSecondsFromNow() % 60
                };

                return(string.Format(DepartureFormat, stop, stops[nextDeparture.Destination.StopId], departureResponse.Minutes, departureResponse.Seconds));
            }

            return(string.Empty);
        }