public static List <RouteScheduleSummary> GetSchedules()
        {
            using (var db = new TrolleyTracker.Models.TrolleyTrackerContext())
            {
                var currentDateTime = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow);

                var fixedRouteSchedules = (from route in db.Routes
                                           from routeSchedule in db.RouteSchedules
                                           orderby routeSchedule.StartTime, routeSchedule.Route.ShortName
                                           where (routeSchedule.RouteID == route.ID)
                                           select routeSchedule).ToList <RouteSchedule>();

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

                var scheduleToDate = new Dictionary <RouteSchedule, DateTime>();
                var routeSchedules = BuildScheduleView.BuildEffectiveRouteSchedule(currentDateTime, 7, fixedRouteSchedules, scheduleToDate, routeScheduleOverrideList);

                var schedules = new List <RouteScheduleSummary>();
                foreach (var routeSchedule in routeSchedules)
                {
                    schedules.Add(new RouteScheduleSummary(routeSchedule));
                }
                return(schedules);
            }
        }
コード例 #2
0
        public IHttpActionResult PostLocation(int id, LocationUpdate locationUpdate)
        {
            if (AppSettingsInterface.UseSyncromatics)
            {
                return(Ok());                                       // Disregard beacon if using Syncromatics
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            using (var db = new TrolleyTracker.Models.TrolleyTrackerContext())
            {
                var trolley = (from Trolley t in db.Trolleys
                               where t.Number == id
                               select t).FirstOrDefault <Trolley>();
                if (trolley == null)
                {
                    return(NotFound());
                }

                if ((locationUpdate.Lat < -90.0) || (locationUpdate.Lat > 90))
                {
                    return(BadRequest("Invalid latitude"));
                }
                if ((locationUpdate.Lon < -180.0) || (locationUpdate.Lon > 180))
                {
                    return(BadRequest("Invalid longitude"));
                }

                trolley.CurrentLat     = locationUpdate.Lat;
                trolley.CurrentLon     = locationUpdate.Lon;
                trolley.LastBeaconTime = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow);
                if (lastTrolleyWriteTime.ContainsKey(trolley.Number))
                {
                    if ((DateTime.Now - lastTrolleyWriteTime[trolley.Number]).TotalSeconds > 30.0)
                    {
                        db.SaveChanges();
                        lastTrolleyWriteTime[trolley.Number] = DateTime.Now;
                    }
                }
                else
                {
                    lastTrolleyWriteTime.Add(trolley.Number, DateTime.Now);
                }
                TrolleyCache.UpdateTrolley(trolley);
                StopArrivalTime.UpdateTrolleyStopArrivalTime(trolley);

                return(Ok());
            }
        }
コード例 #3
0
        /// <summary>
        /// Check for stop proximity - try to minimize number of polled stops
        ///   - First preference is the previous stop
        ///    Then loop through all stops
        /// </summary>
        /// <param name="trolley"></param>
        /// <param name="currentLocation"></param>
        private static void CheckForStopInRange(Trolley trolley, Coordinate currentLocation)
        {
            //var lastStopID = -1;
            if (lastStopIDByTrolley.ContainsKey(trolley.Number))
            {
                var stopID      = lastStopIDByTrolley[trolley.Number];
                var stopSummary = stopSummaries[stopID];
                if (currentLocation.GreatCircleDistance(new Coordinate(stopSummary.Lat, stopSummary.Lon)) < StopRadiusCheck)
                {
                    // Still within stop zone
                    return;
                }
            }

            foreach (var stopID in stopSummaries.Keys)
            {
                var stopSummary = stopSummaries[stopID];
                if (currentLocation.GreatCircleDistance(new Coordinate(stopSummary.Lat, stopSummary.Lon)) < StopRadiusCheck)
                {
                    // Found new stop zone
                    if (stopSummary.LastTrolleyArrivalTime.ContainsKey(trolley.Number))
                    {
                        stopSummary.LastTrolleyArrivalTime[trolley.Number] = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow);
                    }
                    else
                    {
                        stopSummary.LastTrolleyArrivalTime.Add(trolley.Number, UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow));
                    }
                    if (lastStopIDByTrolley.ContainsKey(trolley.Number))
                    {
                        lastStopIDByTrolley[trolley.Number] = stopSummary.ID;
                    }
                    else
                    {
                        lastStopIDByTrolley.Add(trolley.Number, stopSummary.ID);
                    }
                    return;
                }
            }

            // Not currently in any stop zone
            if (lastStopIDByTrolley.ContainsKey(trolley.Number))
            {
                lastStopIDByTrolley.Remove(trolley.Number);
            }
        }
コード例 #4
0
        public IHttpActionResult PostLocation(int id, LocationUpdate locationUpdate)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var trolley = (from Trolley t in db.Trolleys
                           where t.Number == id
                           select t).FirstOrDefault <Trolley>();

            if (trolley == null)
            {
                return(NotFound());
            }

            if ((locationUpdate.Lat < -90.0) || (locationUpdate.Lat > 90))
            {
                return(BadRequest("Invalid latitude"));
            }
            if ((locationUpdate.Lon < -180.0) || (locationUpdate.Lon > 180))
            {
                return(BadRequest("Invalid longitude"));
            }

            trolley.CurrentLat     = locationUpdate.Lat;
            trolley.CurrentLon     = locationUpdate.Lon;
            trolley.LastBeaconTime = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow);
            if (lastTrolleyWriteTime.ContainsKey(trolley.Number))
            {
                if ((DateTime.Now - lastTrolleyWriteTime[trolley.Number]).TotalSeconds > 30.0)
                {
                    db.SaveChanges();
                    lastTrolleyWriteTime[trolley.Number] = DateTime.Now;
                }
            }
            else
            {
                lastTrolleyWriteTime.Add(trolley.Number, DateTime.Now);
            }
            TrolleyCache.UpdateTrolley(trolley);
            StopArrivalTime.UpdateTrolleyStopArrivalTime(trolley);

            return(Ok()); // CreatedAtRoute("DefaultApi", new { id = trolley.ID }, trolley);
        }
コード例 #5
0
        private static void UpdateStopArrivalTime(RouteStop currentRouteStop)
        {
            if (currentRouteStop == null)
            {
                return;
            }
            currentRouteStop.LastTimeAtStop = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow);

            var dbRouteStop = (from rs in db.RouteStops
                               where rs.ID == currentRouteStop.ID
                               select rs).FirstOrDefault();

            if (dbRouteStop == null)
            {
                return;
            }
            dbRouteStop.LastTimeAtStop = currentRouteStop.LastTimeAtStop;
            db.SaveChanges();
        }
コード例 #6
0
        // Mapped as - GET: api/Routes/Active
        public List <RouteSummary> Get()
        {
            // 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;

            // 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.
            var todaysFixedRouteSchedules = (from route in db.Routes
                                             from routeSchedule in db.RouteSchedules
                                             orderby routeSchedule.StartTime
                                             where (routeSchedule.RouteID == route.ID) && (routeSchedule.DayOfWeek == weekday)
                                             select routeSchedule).ToList <RouteSchedule>();

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

            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);
        }
コード例 #7
0
        /// <summary>
        /// Set or refresh stop arrival times for all stops on this route based on just arriving at current stop
        /// </summary>
        /// <param name="trackingInfo"></param>
        /// <param name="currentLocation"></param>
        /// <param name="stopSummary"></param>
        private static void PredictStopArrivalTimes(TrolleyTrackingInfo trackingInfo)
        {
            var trolleyNumber = trackingInfo.CurrentTrolley.Number;

            var now           = UTCToLocalTime.LocalTimeFromUTC(DateTime.UtcNow);
            var stopsCount    = trackingInfo.CurrentRoute.RouteStops.Count;
            int travelSeconds = 0;  // Total travel time from current stop

            for (var i = 0; i <= stopsCount; i++)
            {
                var stopIndex = (trackingInfo.CurrentRouteStopIndex + i) % stopsCount;
                var routeStop = trackingInfo.CurrentRoute.RouteStops[stopIndex];
                if (routeStop.LastTimeAtStop.HasValue)
                {
                    var timeAtStop        = now.AddSeconds(travelSeconds);
                    var previousRouteStop = trackingInfo.CurrentRoute.RouteStops[(stopIndex + stopsCount - 1) % stopsCount];
                    var previousSummary   = stopSummaries[previousRouteStop.StopID];
                    SetArrivalTime(trolleyNumber, previousSummary, timeAtStop);
                    travelSeconds += routeStop.AverageTravelTimeToNextStop;
                }
            }
        }