public static void UpdateTrolley(Trolley trolley)
        {
            lock (_lock)
            {
                if (!trolleyCache.ContainsKey(trolley.ID))
                {
                    var runningTrolley = new RunningTrolley(trolley);
                    trolleyCache.Add(trolley.ID, runningTrolley);
                }
                else
                {
                    var runningTrolley = trolleyCache[trolley.ID];
                    if (trolley.CurrentLat.HasValue)
                    {
                        runningTrolley.Lat = (double)trolley.CurrentLat;
                    }
                    if (trolley.CurrentLon.HasValue)
                    {
                        runningTrolley.Lon = (double)trolley.CurrentLon;
                    }
                    runningTrolley.LastUpdated = DateTime.Now;
                }

            }
        }
 public static void UpdateTrolley(Trolley trolley)
 {
     lock (_lock)
     {
         if (!trolleyCache.ContainsKey(trolley.ID))
         {
             var runningTrolley = new RunningTrolley(trolley);
             trolleyCache.Add(trolley.ID, runningTrolley);
         }
         else
         {
             var runningTrolley = trolleyCache[trolley.ID];
             if (trolley.CurrentLat.HasValue)
             {
                 runningTrolley.Lat = (double)trolley.CurrentLat;
             }
             if (trolley.CurrentLon.HasValue)
             {
                 runningTrolley.Lon = (double)trolley.CurrentLon;
             }
             runningTrolley.LastUpdated   = DateTime.Now;
             runningTrolley.PassengerLoad = trolley.PassengerLoad;
             runningTrolley.IconColorRGB  = trolley.IconColorRGB;
             runningTrolley.Capacity      = trolley.Capacity;
         }
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Reset trolley tracking info if color changes (assigned to new route)
 /// </summary>
 /// <param name="trolley"></param>
 public static void ResetTrolleyInfo(Trolley trolley)
 {
     lock (_lock)
     {
         if (trolleyTrackingInfo.ContainsKey(trolley.ID))
         {
             trolleyTrackingInfo.Remove(trolley.ID);
         }
     }
 }
Exemplo n.º 4
0
        private static void CreateTrackingInfo(Trolley trolley)
        {
            var newTrackingInfo = new TrolleyTrackingInfo(trolley);

            if (activeRoutes.Count == 1)
            {
                // For a single active route, all trolleys follow that route
                newTrackingInfo.CurrentRoute = activeRoutes.First().Value;
            }
            trolleyTrackingInfo.Add(trolley.ID, newTrackingInfo);
        }
        private static void UpdateStopTime(Trolley trolley)
        {
            UpdateStopList();

            // See if trolley has moved far enough to justify a full stop distance check
            var currentLocation = new Coordinate((double)trolley.CurrentLat, (double)trolley.CurrentLon);

            if (currentLocation.GreatCircleDistance(lastReportedLocation[trolley.Number]) > MinMoveDistance)
            {
                CheckForStopInRange(trolley, currentLocation);
                lastReportedLocation[trolley.Number] = currentLocation;
            }
        }
 public RunningTrolley(Trolley trolley)
 {
     ID = trolley.ID;
     // lat, lon = 0,0 if no values
     if (trolley.CurrentLat.HasValue)
     {
         Lat = (double)trolley.CurrentLat;
     }
     if (trolley.CurrentLon.HasValue)
     {
         Lon = (double)trolley.CurrentLon;
     }
     LastUpdated = DateTime.Now;
 }
        /// <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);
            }
        }
        /// <summary>
        /// Check and record time seen at each stop
        /// </summary>
        /// <param name="trolley"></param>
        public static void UpdateTrolleyStopArrivalTime(Trolley trolley)
        {
            if ((trolley.CurrentLat == null) || (trolley.CurrentLon == null))
            {
                return;
            }

            lock (_lock)
            {
                if (!lastReportedLocation.ContainsKey(trolley.Number))
                {
                    lastReportedLocation.Add(trolley.Number, new Coordinate((double)trolley.CurrentLat, (double)trolley.CurrentLon));
                }
                else
                {
                    UpdateStopTime(trolley);
                }
            }
        }
        /// <summary>
        /// Check and record time seen at each stop
        /// </summary>
        /// <param name="trolley"></param>
        public static void UpdateTrolleyStopArrivalTime(Trolley trolley)
        {
            if ((trolley.CurrentLat == null) || (trolley.CurrentLon == null))
            {
                return;
            }

            lock (_lock)
            {
                if (!lastReportedLocation.ContainsKey(trolley.Number))
                {
                        lastReportedLocation.Add(trolley.Number, new Coordinate((double)trolley.CurrentLat, (double)trolley.CurrentLon));

                }
                else
                {
                    UpdateStopTime(trolley);
                }

            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// Assign trollies to route, for now based on color match.
 /// If there is only 1 active route, assign all trollies to that route, regardless of color
 /// </summary>
 private static void MatchTrolleyToRoute(Trolley trolley)
 {
     if (activeRoutes.Count == 1)
     {
         var activeRoute  = activeRoutes.First().Value;
         var trackingInfo = trolleyTrackingInfo[trolley.ID];
         trackingInfo.CurrentRoute = activeRoute;
     }
     else
     {
         foreach (var route in activeRoutes.Values)
         {
             if (route.RouteColorRGB == trolley.IconColorRGB)
             {
                 var trackingInfo = trolleyTrackingInfo[trolley.ID];
                 trackingInfo.CurrentRoute = route;
                 break;
             }
         }
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// Check and record time seen at each stop
        /// </summary>
        /// <param name="trolley"></param>
        public static void UpdateTrolleyStopArrivalTime(Trolley trolley)
        {
            if ((trolley.CurrentLat == null) || (trolley.CurrentLon == null))
            {
                return;
            }

            lock (_lock)
            {
                RefreshStaticData();
                if (runningTrolleys.Count == 0)
                {
                    trolleyTrackingInfo.Clear();
                    return; // Nothing on the schedule
                }

                if (!trolleyTrackingInfo.ContainsKey(trolley.ID))
                {
                    CreateTrackingInfo(trolley);
                }
                if (trolleyTrackingInfo[trolley.ID].CurrentRoute == null)
                {
                    // Match will be repeatedly tried until there is a match
                    MatchTrolleyToRoute(trolley);
                }
                if (trolleyTrackingInfo[trolley.ID].CurrentRoute == null)
                {
                    // Unable to match trolley to any route
                    return;
                }
                var trackingInfo = trolleyTrackingInfo[trolley.ID];
                trackingInfo.CurrentTrolley = trolley;

                UpdateStopTime(trackingInfo);
            }
        }
        private static void UpdateStopTime(Trolley trolley)
        {
            UpdateStopList();

            // See if trolley has moved far enough to justify a full stop distance check
            var currentLocation = new Coordinate((double)trolley.CurrentLat, (double)trolley.CurrentLon);
            if (currentLocation.GreatCircleDistance(lastReportedLocation[trolley.Number]) > MinMoveDistance)
            {
                CheckForStopInRange(trolley, currentLocation);
                lastReportedLocation[trolley.Number] = currentLocation;
            }
        }
        /// <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));
                    }
                    lastStopIDByTrolley.Add(trolley.Number, stopSummary.ID);
                    return;
                }

            }

            // Not currently in any stop zone
            if (lastStopIDByTrolley.ContainsKey(trolley.Number))
            {
                lastStopIDByTrolley.Remove(trolley.Number);
            }
        }
Exemplo n.º 14
0
 public TrolleyTrackingInfo(Trolley trolley)
 {
     LastStopID           = -1;
     LastReportedLocation = new Coordinate((double)trolley.CurrentLat, (double)trolley.CurrentLon);
     CurrentTrolley       = trolley;
 }