public void AddRouteDetail(TrolleyTrackerContext db, Route route)
        {
            var stops = (from stop in db.Stops
                         from routeStop in db.RouteStops
                         orderby routeStop.StopSequence
                         where (routeStop.StopID == stop.ID) && (routeStop.RouteID == route.ID)
                         select stop).ToList();

            foreach (var stop in stops)
            {
                // Construct with route info so route shape segment index is included
                var stopSummary = new StopSummary(stop, route);

                // Use arrival times if available
                var stopWithArrivalTime = StopArrivalTime.GetStopSummaryWithArrivalTimes(stop.ID);
                if (stopWithArrivalTime != null)
                {
                    stopSummary.NextTrolleyArrivalTime = stopWithArrivalTime.NextTrolleyArrivalTime;
                }
                this.Stops.Add(stopSummary);
            }

            var shapes = from shape in db.Shapes
                         orderby shape.Sequence
                         where (shape.RouteID == route.ID)
                         select shape;

            foreach (var shape in shapes)
            {
                var coordinate = new Location();
                coordinate.Lat = shape.Lat;
                coordinate.Lon = shape.Lon;
                this.RoutePath.Add(coordinate);
            }
        }
        private void AddRouteDetail(RouteDetail routeDetail, Route route)
        {
            var stops = from stop in db.Stops
                        from routeStop in db.RouteStops
                        orderby routeStop.StopSequence
                        where (routeStop.StopID == stop.ID) && (routeStop.RouteID == route.ID)
                        select stop;

            foreach (var stop in stops)
            {
                // Use arrival times if available
                var stopWithArrivalTime = StopArrivalTime.GetStopSummaryWithArrivalTimes(stop.ID);
                if (stopWithArrivalTime != null)
                {
                    routeDetail.Stops.Add(stopWithArrivalTime);
                }
                else
                {
                    routeDetail.Stops.Add(new StopSummary(stop));
                }
            }

            var shapes = from shape in db.Shapes
                         orderby shape.Sequence
                         where (shape.RouteID == route.ID)
                         select shape;

            foreach (var shape in shapes)
            {
                var coordinate = new Location();
                coordinate.Lat = shape.Lat;
                coordinate.Lon = shape.Lon;
                routeDetail.RoutePath.Add(coordinate);
            }
        }
 private void CheckActiveRoutes()
 {
     if (activeRoutes == null ||
         (lastRouteUpdated - DateTime.Now).TotalSeconds > RouteUpdateInterval)
     {
         activeRoutes     = StopArrivalTime.GetActiveRoutes();
         lastRouteUpdated = DateTime.Now;
     }
 }
        /// <summary>
        /// Confirm that trolley color matches route color.   If not, change it and
        /// save to DB and reset arrival time logic for that trolley.
        /// </summary>
        /// <param name="trolley"></param>
        /// <param name="route"></param>
        private async Task CheckTrolleyColorMatch(Trolley trolley, Route route)
        {
            if (trolley.IconColorRGB.ToLower() == route.RouteColorRGB.ToLower())
            {
                return;
            }
            trolley.IconColorRGB = route.RouteColorRGB;
            await SaveTrolleyToDB(trolley);

            StopArrivalTime.ResetTrolleyInfo(trolley);
        }
Beispiel #5
0
        public IHttpActionResult GetStop(int id)
        {
            var stopSummary = StopArrivalTime.GetStopSummaryWithArrivalTimes(id);

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

            return(Ok(stopSummary));
        }
        /// <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);
                }
            }
        }
        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());
            }
        }
        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);
        }
        /// <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);
                    }
                }
            }
        }