コード例 #1
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());
            }
        }
コード例 #2
0
        /// <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);
                }
            }
        }
コード例 #3
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Trolley trolley = db.Trolleys.Find(id);

            if (trolley == null)
            {
                return(HttpNotFound());
            }
            TrolleyCache.UpdateTrolley(trolley);
            return(View(trolley));
        }
コード例 #4
0
        public ActionResult Create([Bind(Include = "TrolleyName,Number,IconColorRGB,CurrentLat,CurrentLon")] Trolley trolley)
        {
            if (ModelState.IsValid)
            {
                db.Trolleys.Add(trolley);
                db.SaveChanges();

                logger.Info($"Created trolley # {trolley.Number} '{trolley.TrolleyName}'");

                TrolleyCache.UpdateTrolley(trolley);
                return(RedirectToAction("Index"));
            }

            return(View(trolley));
        }
コード例 #5
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);
        }
コード例 #6
0
        /// <summary>
        /// Check for the case of vehicle(s) running on a route other than the scedule.
        /// This might be in the case of being schedule for top-of-main + heart-of-main,
        /// however due to vehicle breakdown, those routes are combined as one of the
        /// combination routes.
        /// </summary>
        /// <param name="saveTrolleysToDB"></param>
        /// <returns></returns>
        private async Task CheckForAdditionalVehicles(bool saveTrolleysToDB)
        {
            var runningTrolleys = TrolleyCache.GetRunningTrolleys(false);

            if (runningTrolleys.Count >= activeRoutes.Count)
            {
                await TrackGhostVehicles(saveTrolleysToDB);

                return;  // All expected trolleys found
            }

            // Poll all remaining routes for any possible vehicles
            foreach (var syncroRoute in trolleyService.routes)
            {
                if (!localRouteIDToSyncromaticsRoute.Values.Contains(syncroRoute))
                {
                    await CheckForVehiclesOnRoute(syncroRoute, saveTrolleysToDB);
                }
            }
        }
コード例 #7
0
        /// <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);
                    }
                }
            }
        }
コード例 #8
0
 // Mapped as - GET: api/Trolleys/Running?debug=true
 public List <RunningTrolley> Get(bool debug)
 {
     return(TrolleyCache.GetRunningTrolleys(true));
 }
コード例 #9
0
 // Mapped as - GET: api/Trolleys/Running
 public List <RunningTrolley> Get()
 {
     return(TrolleyCache.GetRunningTrolleys(false));
 }