Esempio n. 1
0
        private void UpdateInLoop(
            IBus13RouteDataService dataService,
            IEnumerable <Route> routes,
            TimeSpan sleepInterval,
            CancellationToken token)
        {
            _updateRoutineState = new UpdateRoutineState(routes);
            while (!token.IsCancellationRequested)
            {
                var nextSleepInterval = sleepInterval;
                lock (_lockObject)
                {
                    var now = DateTime.UtcNow;
                    try
                    {
                        if ((now - _updateRoutineState.LastUpdate) >= sleepInterval)
                        {
                            _updateRoutineState = this.RunUpdateVehicleLocationsRoutine(dataService, _updateRoutineState);
                        }
                        else
                        {
                            nextSleepInterval = sleepInterval - (now - _updateRoutineState.LastUpdate) + TimeSpan.FromSeconds(1);
                        }
                    }
                    catch (Exception e)
                    {
                        this.RaiseVehicleLocationsUpdateFailedEvent();
                        Insights.Report(e, Xamarin.Insights.Severity.Warning);
                    }
                }

                Task.Delay(nextSleepInterval).Wait(token);
            }
        }
Esempio n. 2
0
        public Bus13LiveDataProvider(Area area, TimeSpan updateInterval)
        {
            this.Area       = area;
            _endpoint       = area.Endpoint;
            _location       = area.DataServiceCode;
            _updateInterval = updateInterval;

            _dataService = new Bus13RouteDataService(_endpoint, _location, area.DataServiceInfoParam);
        }
Esempio n. 3
0
        private static void ListVehicles(IBus13RouteDataService service)
        {
            var routes   = service.GetRoutesAsync().Result;
            var vehicles = service.GetVehicleLocationsAsync(routes, GeoRect.EarthWide, 0).Result;

            foreach (var update in vehicles.Updates)
            {
                Console.WriteLine(
                    "id:{0} type:{1} route:{2}",
                    update.Vehicle.Id,
                    update.Vehicle.Type,
                    update.Vehicle.RouteInfo.RouteNumber);
            }
        }
Esempio n. 4
0
        private static void Trace(IBus13RouteDataService service, string vehicleId, IVehicleTraceOutputWriter outputWriter)
        {
            Console.WriteLine("Retrieving routes...");
            var routes = service.GetRoutesAsync().Result;

            Console.WriteLine("{0} routes found...", routes.Count());

            Console.WriteLine("Tracing {0}...", vehicleId);
            _traceTask = Task.Factory.StartNew(() =>
            {
                var lastUpdate = DateTime.MinValue;
                var timestamp  = 0;
                while (true)
                {
                    ClearLine();
                    Console.Write("retrieving vehicle locations...");
                    var response = service.GetVehicleLocationsAsync(routes, GeoRect.EarthWide, timestamp).Result;

                    var update = response.Updates.FirstOrDefault(x => x.Vehicle.Id.Equals(vehicleId));
                    if (update != null)
                    {
                        lastUpdate = DateTime.Now;

                        ClearLine();
                        Console.WriteLine(
                            "id:{0}, lat:{1}, lng:{2}, upd: {3}, rcvd:{4}",
                            vehicleId,
                            update.Vehicle.Location.Position.Latitude,
                            update.Vehicle.Location.Position.Longitude,
                            update.LastUpdate.ToString("u"),
                            lastUpdate.ToString("u"));

                        outputWriter.Write(update);
                    }

                    timestamp = response.Timestamp;

                    ClearLine();
                    Console.Write("waiting 10s...");
                    Task.Delay(System.TimeSpan.FromSeconds(10)).Wait();
                }
            }).ConfigureAwait(false);
        }
Esempio n. 5
0
        private UpdateRoutineState RunUpdateVehicleLocationsRoutine(
            IBus13RouteDataService dataService,
            UpdateRoutineState state)
        {
            this.RaiseVehicleLocationsUpdateStartedEvent();

            var response = dataService.GetVehicleLocationsAsync(
                state.Routes,
                GeoRect.EarthWide,
                state.Timestamp)
                           .ConfigureAwait(false)
                           .GetAwaiter()
                           .GetResult();

            var vehicleLocationUpdates = this.UpdateVehicleLocations(response.Updates);

            this.RaiseVehicleLocationsUpdatedEvent(vehicleLocationUpdates);

            return(UpdateRoutineState.CreateFrom(state, response.Timestamp));
        }