예제 #1
0
        /// <summary>
        /// Obtain effective schedules, and format for display into RouteScheduleSummary
        /// NOTES:  Schedules that result from an override do not have a real database ID, and are not stored in the database
        /// There may be multiple ID 0 values from created schedules
        /// </summary>
        /// <param name="dateLabels"></param>
        /// <param name="routeSchedules"></param>
        /// <param name="routeScheduleOverrides"></param>
        /// <returns></returns>
        public static List <RouteScheduleSummary> BuildEffectiveRouteSchedule(IEnumerable <RouteSchedule> routeSchedules,
                                                                              IEnumerable <RouteScheduleOverride> routeScheduleOverrides
                                                                              )
        {
            var effectiveScheduleSummaries = new List <RouteScheduleSummary>();

            try
            {
                var localNow                = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow);
                var scheduleToDate          = new Dictionary <RouteSchedule, DateTime>();
                var effectiveRouteSchedules = BuildEffectiveRouteSchedule(localNow, 14, routeSchedules, scheduleToDate, routeScheduleOverrides);

                foreach (var routeSchedule in effectiveRouteSchedules)
                {
                    var scheduleSummary = new RouteScheduleSummary(routeSchedule);
                    var scheduleDate    = scheduleToDate[routeSchedule];
                    scheduleSummary.DayOfWeek = scheduleDate.ToShortDateString() + " " + scheduleSummary.DayOfWeek;
                    effectiveScheduleSummaries.Add(scheduleSummary);
                }

                // Sorting is needed to avoid some unusual MVCSchedule bugs - order may have been changed
                // after some route start times were delayed.
                effectiveScheduleSummaries.Sort(CompareRouteStartTimes);
            }
            catch (Exception ex)
            {
                var message = String.Format($"Exception building schedule: {ex.GetType()}; with message: {ex.Message}");
                logger.Error(ex, message);
            }

            return(effectiveScheduleSummaries);
        }
        // GET: RouteSchedules
        public ActionResult Index()
        {
            ViewBag.DaysOfWeek = BuildScheduleView.daysOfWeek;

            ViewBag.ServerTime = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow).ToString("MM-dd-yyyy HH:mm:ss");

            ViewBag.CssFile = Url.Content("~/Content/RouteScheduleSummary.css");
            return(View(BuildScheduleView.ConfigureScheduleView(false)));
        }
        /// <summary>
        /// Get all vehicles on this route - normally just a single trolley
        /// </summary>
        /// <param name="route"></param>
        /// <param name="saveTrolleysToDB">True if time to save trolley positions</param>
        private async Task GetVehiclesOnRoute(Route route, bool saveTrolleysToDB)
        {
            var syncromaticsRoute = FindMatchingRoute(route);

            if (syncromaticsRoute == null)
            {
                //Trace.WriteLine("No route match found to " + syncromaticsRoute.name);
                return;
            }
            var vehicles = await syncromatics.GetVehiclesOnRoute(syncromaticsRoute.id);

            foreach (var vehicle in vehicles)
            {
                if (lastVehicleUpdateTime.ContainsKey(vehicle.id))
                {
                    // Check for stall (no update from Syncromatics)
                    if (lastVehicleUpdateTime[vehicle.id] == vehicle.lastUpdated)
                    {
                        //Trace.WriteLine("Stalled vehicle, syncromatics # " + vehicle.name);
                        continue;
                    }
                    lastVehicleUpdateTime[vehicle.id] = vehicle.lastUpdated;
                }
                else
                {
                    lastVehicleUpdateTime.Add(vehicle.id, vehicle.lastUpdated);
                }

                var trolley = FindMatchingTrolley(vehicle);
                if (trolley != null)
                {
                    //Trace.WriteLine("Tracking trolley " + trolley.Number);

                    trolley.CurrentLat     = vehicle.lat;
                    trolley.CurrentLon     = vehicle.lon;
                    trolley.Capacity       = vehicle.capacity;
                    trolley.PassengerLoad  = vehicle.passengerLoad;
                    trolley.LastBeaconTime = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow);

                    if (saveTrolleysToDB)
                    {
                        await SaveTrolleyToDB(trolley);
                    }

                    await CheckTrolleyColorMatch(trolley, route);

                    TrolleyCache.UpdateTrolley(trolley);
                    StopArrivalTime.UpdateTrolleyStopArrivalTime(trolley);
                }
            }
        }
예제 #4
0
        // GET: RouteSchedules
        public ActionResult Index()
        {
            //var routeSchedules = db.RouteSchedules.Include(r => r.Route);

            ViewBag.DaysOfWeek = BuildScheduleView.daysOfWeek;

            ViewBag.ServerTime = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow).ToString("MM-dd-yyyy HH:mm:ss");

            ViewBag.CssFile = Url.Content("~/Content/RouteScheduleSummary.css");
            return(View(BuildScheduleView.ConfigureScheduleView(db, false)));



            //return View(routeSchedules.ToList());
        }
예제 #5
0
        public static List <RouteSummary> GetActiveRoutes()
        {
            // Azure server instances run with DateTime.Now set to UTC
            var currentDateTime = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow);

            var activeRoutes = new List <RouteSummary>();
            var weekday      = (int)currentDateTime.DayOfWeek;


            List <RouteSchedule>         todaysFixedRouteSchedules = null;
            List <RouteScheduleOverride> routeScheduleOverrideList = null;

            using (var db = new TrolleyTrackerContext())
            {
                // Note: ToList() to avoid "There is already an open DataReader associated with this Command which must be closed first." exception,
                // even though MultipleActiveResultSets is already true in the connection string.
                todaysFixedRouteSchedules = (from rs in db.RouteSchedules.Include(r => r.Route)
                                             orderby rs.StartTime
                                             where rs.DayOfWeek == weekday
                                             select rs).ToList();

                var today = currentDateTime.Date;
                routeScheduleOverrideList = (from rso in db.RouteScheduleOverrides.Include(rso => rso.NewRoute).Include(rso => rso.OverriddenRoute)
                                             orderby rso.OverrideDate, rso.StartTime, rso.NewRoute.ShortName
                                             where rso.OverrideDate == today
                                             select rso).ToList();
            }

            var scheduleToDate       = new Dictionary <RouteSchedule, DateTime>();
            var todaysRouteSchedules = BuildScheduleView.BuildEffectiveRouteSchedule(currentDateTime, 1, todaysFixedRouteSchedules, scheduleToDate, routeScheduleOverrideList);

            // Get today's effective routes
            // Return active routes 5 minutes early so that progress from garage to starting point
            // is shown, also if trolley is a few minutes early.

            var startTimeRef = currentDateTime.Add(new TimeSpan(0, 5, 0)).TimeOfDay;
            var endTimeRef   = currentDateTime.TimeOfDay;

            foreach (var routeSchedule in todaysRouteSchedules)
            {
                if ((startTimeRef > routeSchedule.StartTime.TimeOfDay) && (endTimeRef < routeSchedule.EndTime.TimeOfDay))
                {
                    activeRoutes.Add(new RouteSummary(routeSchedule.Route));
                }
            }
            return(activeRoutes);
        }
예제 #6
0
        /// <summary>
        /// Obtain effective schedules, and format for display into RouteScheduleSummary
        /// NOTES:  Schedules that result from an override do not have a real database ID, and are not stored in the database
        /// There may be multiple ID 0 values from created schedules
        /// </summary>
        /// <param name="dateLabels"></param>
        /// <param name="routeSchedules"></param>
        /// <param name="routeScheduleOverrides"></param>
        /// <returns></returns>
        public static List <RouteScheduleSummary> BuildEffectiveRouteSchedule(IEnumerable <RouteSchedule> routeSchedules,
                                                                              IEnumerable <RouteScheduleOverride> routeScheduleOverrides
                                                                              )
        {
            var localNow                = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow);
            var scheduleToDate          = new Dictionary <RouteSchedule, DateTime>();
            var effectiveRouteSchedules = BuildEffectiveRouteSchedule(localNow, 14, routeSchedules, scheduleToDate, routeScheduleOverrides);

            var effectiveScheduleSummaries = new List <RouteScheduleSummary>();

            foreach (var routeSchedule in effectiveRouteSchedules)
            {
                var scheduleSummary = new RouteScheduleSummary(routeSchedule);
                var scheduleDate    = scheduleToDate[routeSchedule];
                scheduleSummary.DayOfWeek = scheduleDate.ToShortDateString() + " " + scheduleSummary.DayOfWeek;
                effectiveScheduleSummaries.Add(scheduleSummary);
            }

            return(effectiveScheduleSummaries);
        }
        /// <summary>
        /// Copy of GetVehiclesOnRoute - kludge to handle 'ghost vehicles' not
        /// on route.
        /// </summary>
        /// <param name="route"></param>
        /// <param name="saveTrolleysToDB">True if time to save trolley positions</param>
        private async Task CheckForVehiclesOnRoute(Syncromatics.Route syncromaticsRoute, bool saveTrolleysToDB)
        {
            var vehicles = await syncromatics.GetVehiclesOnRoute(syncromaticsRoute.id);

            if (vehicles.Count == 0)
            {
                if (ghostRoutes.Contains(syncromaticsRoute))
                {
                    // Drive logged out of this route
                    ghostRoutes.Remove(syncromaticsRoute);
                }
                return;
            }

            foreach (var vehicle in vehicles)
            {
                if (lastVehicleUpdateTime.ContainsKey(vehicle.id))
                {
                    // Check for stall (no update from Syncromatics)
                    if (lastVehicleUpdateTime[vehicle.id] == vehicle.lastUpdated)
                    {
                        //Trace.WriteLine("Stalled vehicle, syncromatics # " + vehicle.name);
                        continue;
                    }
                    lastVehicleUpdateTime[vehicle.id] = vehicle.lastUpdated;
                }
                else
                {
                    lastVehicleUpdateTime.Add(vehicle.id, vehicle.lastUpdated);
                }

                var trolley = FindMatchingTrolley(vehicle);
                if (trolley != null)
                {
                    Trace.WriteLine("Tracking Ghost trolley " + trolley.Number);

                    trolley.CurrentLat    = vehicle.lat;
                    trolley.CurrentLon    = vehicle.lon;
                    trolley.Capacity      = vehicle.capacity;
                    trolley.PassengerLoad = vehicle.passengerLoad;
                    var colorBlack = "#000000";
                    if (trolley.IconColorRGB != colorBlack)
                    {
                        trolley.IconColorRGB = colorBlack;
                        saveTrolleysToDB     = true;
                    }
                    trolley.LastBeaconTime = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow);

                    if (saveTrolleysToDB)
                    {
                        await SaveTrolleyToDB(trolley);
                    }

                    TrolleyCache.UpdateTrolley(trolley);
                    StopArrivalTime.UpdateTrolleyStopArrivalTime(trolley);
                    if (!ghostRoutes.Contains(syncromaticsRoute))
                    {
                        ghostRoutes.Add(syncromaticsRoute);
                    }
                }
            }
        }