public DepartureBoard GetStation(string stationId, string date, string time) { VasttrafikSharp.Objects.DepartureBoard departureBoard = new DepartureBoard() { Departures = new List <Departure>() }; try { Dictionary <string, string> p = new Dictionary <string, string>(); p.Add("authKey", this.ApiKey); p.Add("date", date); p.Add("time", time); p.Add("id", stationId); System.Net.WebResponse response = GetREST("http://api.vasttrafik.se/bin/rest.exe/v1/departureBoard", this.ApiKey, p); XmlReader reader = XmlReader.Create(response.GetResponseStream()); XmlSerializer serializer = new XmlSerializer(typeof(VasttrafikSharp.Objects.DepartureBoard)); departureBoard = (VasttrafikSharp.Objects.DepartureBoard)serializer.Deserialize(reader); reader.Close(); } catch (Exception e) { departureBoard = new DepartureBoard() { Departures = new List <Departure>() }; } return(departureBoard); }
static async Task Main(string[] args) { HttpClient httpClient = new HttpClient(new SocketsHttpHandler { // Potentially add a proxy like this // Proxy = new WebProxy("http://127.0.0.1:8888") }); RejseplanenClient client = new RejseplanenClient(httpClient); // Perform queries MBW.Clients.Rejseplanen.Schema.RestLocation.LocationList locationLookup = await client.GetLocationAsync("Vestergade"); Console.WriteLine("Locations search: " + string.Join(", ", locationLookup.CoordLocation.Select(s => s.Name).Concat(locationLookup.StopLocation.Select(p => p.Name)))); WGS84Coordinate loc = new WGS84Coordinate { X = 55.650000, Y = 12.560000 }; LocationList locationsNearby = await client.GetStopsNearbyAsync(loc); Console.WriteLine("Stops nearby: " + locationsNearby.StopLocation.Count); DepartureBoard departureBoard = await client.GetDepartureBoardAsync(8600626); Console.WriteLine("Coming departures: " + departureBoard.Departure.Count); }
private static DateTime ExtractServerDateTime(DepartureBoard board) { return(new DateTime( int.Parse(board.ServerDate.Substring(0, 4)), int.Parse(board.ServerDate.Substring(5, 2)), int.Parse(board.ServerDate.Substring(8, 2)), int.Parse(board.ServerTime.Substring(0, 2)), int.Parse(board.ServerTime.Substring(3, 2)), 0)); }
/// <summary> /// Stops the server. /// </summary> public static void Stop() { Router?.StopServer(); DepartureBoard?.StopServer(); BasicData?.StopServer(); IsStopped = true; Logging.Log("The server has stopped."); }
public Dictionary <string, List <Departure> > GetStation(string stationId, DateTime date) { Dictionary <string, List <Departure> > departureBins = new Dictionary <string, List <Departure> >(); int attempts = 0; string lastDate = date.ToString("yyyy-MM-dd"); string lastTime = date.ToString("HH:mm"); while (!AllBinsHaveAtleastTwoItems(departureBins) && attempts < 5) { attempts++; DepartureBoard result = GetStation(stationId, lastDate, lastTime); if (result.Departures.Count > 0) { lastDate = result.Departures.Last().Date; lastTime = GetDepartureTime(result.Departures.Last()); } PlaceDeparturesInBins(result.Departures, departureBins); } return(departureBins); }
/// <summary> /// Starts the server. /// </summary> /// <param name="address">IP address.</param> /// <param name="routerPort">Router server port.</param> /// <param name="dbPort">Departure board server port.</param> /// <param name="dfPort">Data feed server port.</param> public static void Start(IPAddress address, int routerPort, int dbPort, int dfPort) { Logging.Log("The server has started."); if (routerPort >= 0) { Router = new RouterServer(address, routerPort); } if (dbPort >= 0) { DepartureBoard = new DepartureBoardServer(address, dbPort); } if (dfPort >= 0) { BasicData = new BasicDataFeedServer(address, dfPort); } Router?.Start(); DepartureBoard?.Start(); BasicData?.Start(); IsStopped = false; }
/// <summary> /// Returns a list of departues and times, from this stopId, including Grouped stops /// </summary> /// <param name="stopId">Requested stop</param> /// <param name="time">First departure</param> /// <param name="takeOnly">No of departuers to include (default = 5)</param> /// <param name="intervalShown">Interval to look for (default = 2 hours)</param> /// <returns></returns> public DepartureBoard GetBoard(int stopId, long time, int takeOnly = 5, int intervalShown = 7200) { var departureBoard = new DepartureBoard(); // get information of the stop in question departureBoard.Stop = ts.NetworkNodes.Where(n => n.StopId == stopId) .Select(n => new StopInformation { StopId = n.StopId, StopName = n.ShortStopName, Time = time, Lat = n.Lat, Long = n.Long }).First(); // is the stop member of a group of stops? var stopGroupId = ts.NetworkNodes.Where(s => s.StopId == stopId).Select(s => s.GroupId).FirstOrDefault(); // if no group found, set to -1 as stops not in groups has id = 0 if (stopGroupId == 0) { stopGroupId = -1; } var allTrips = GetAllTripsFromStop(); if (allTrips.Count == 0) { return(null); // no trips found! } var distinctTrips = allTrips .Select(e => new { TransportTypeId = e.TranportTypeId, RouteId = e.RouteId, HeadsignId = e.HeadsignId }) .Distinct(); // first add all distinct trips to the list of possible trips foreach (var item in distinctTrips) { var twdt = new TripWithDeparturetimes(); // get the trip information for this trip twdt.Trip = new TripInformation { TransportTypeId = item.TransportTypeId, TransportTypeName = ts.TransportTypes.Where(t => t.TransportTypeId == item.TransportTypeId) .Select(t => t.TransportTypeName).FirstOrDefault(), RouteName = ts.Routes.Where(r => r.RouteId == item.RouteId) .Select(r => r.RouteName).FirstOrDefault(), HeadsignName = ts.Headsigns.Where(h => h.HeadsignId == item.HeadsignId) .Select(h => h.HeadsignName).FirstOrDefault() }; // select all trips belonging to that distict trip, take = takeOnly twdt.DepartureTimes = allTrips .Where(t => t.TranportTypeId == item.TransportTypeId & t.RouteId == item.RouteId & t.HeadsignId == item.HeadsignId) .Select(t => t.DepartureTime) .Take(takeOnly) .ToList(); if (twdt.DepartureTimes.Count > 0) { departureBoard.TripsList.Add(twdt); } } return((departureBoard.TripsList.Count == 0) ? null : departureBoard); List <InternalTripInfo> GetAllTripsFromStop() { return((from nodes in ts.NetworkNodes join edges in ts.Edges on nodes.StopId equals edges.FromStop join timetables in ts.TimeTables on edges.EdgeId equals timetables.EdgeId join trips in ts.Trips on timetables.TripId equals trips.TripId where (nodes.StopId == stopId | nodes.GroupId == stopGroupId) // find either stop or group & edges.DistanceWalk == 0 // exclude walking trips & timetables.DepartureTime >= time // start of interval & timetables.DepartureTime <= time + intervalShown // end of interval orderby new { trips.TransportTypeId, trips.RouteId } select new InternalTripInfo { TranportTypeId = trips.TransportTypeId, RouteId = trips.RouteId, HeadsignId = trips.HeadsignId, DepartureTime = timetables.DepartureTime }) .ToList()); } }