ParkSquare.RealTimeTrains
3.0.17
See the version list below for details.
dotnet add package ParkSquare.RealTimeTrains --version 3.0.17
NuGet\Install-Package ParkSquare.RealTimeTrains -Version 3.0.17
<PackageReference Include="ParkSquare.RealTimeTrains" Version="3.0.17" />
paket add ParkSquare.RealTimeTrains --version 3.0.17
#r "nuget: ParkSquare.RealTimeTrains, 3.0.17"
// Install ParkSquare.RealTimeTrains as a Cake Addin #addin nuget:?package=ParkSquare.RealTimeTrains&version=3.0.17 // Install ParkSquare.RealTimeTrains as a Cake Tool #tool nuget:?package=ParkSquare.RealTimeTrains&version=3.0.17
C# National Rail Realtime Train Departures, Arrivals & Live Running Information
Background
C# library for retrieving real time train information from Network Rail, National Rail other data sources via the Realtime Trains API. It also provides accurate data regarding timetables, services, bus replacements and platform information.
Getting Started
The source data feed is free for personal, academic and educational use. To connect to the Realtime Trains API you will need access credentials, which can be obtained by going to the Realtime Trains Developer portal. These credentials are in the form of a username and password, which this library will automatically add to your calls.
The main class you will need is RealTimeTrainsClient
. To create one of these, you will need to pass in an HttpClient
and an implementation of IClientConfig
. It's up to you how you implement IClientConfig
, for example you may
bind it to entries in your appsettings.config
or to secrets in an Azure Key Vault. Here is a simple example with hardcoded values:
public class ClientConfig : IClientConfig
{
public string BaseUrl => "https://api.rtt.io/api/v1/";
public string Username => "rttapi_yourusername";
public string Password => "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
}
The recommended way to instantiate your RealTimeTrainsClient
is to use Dependency Injection. Read about HttpClient dependency injection with .Net Core
and then come back. Here is an example of how to register the required dependencies in your project:
services.AddSingleton<IRealTimeTrainsClient, RealTimeTrainsClient>();
services.AddSingleton<IClientConfig, ClientConfig>();
services.AddHttpClient();
You can now access the RealTimeTrainsClient
by simply adding an IRealTimeTrainsClient
to the constructor of the class where you want to use it.
This allows you to search for arrivals, departures, or get detailed information on a specific service. There are several overloaded methods that allow your search to be more, or less, precise. Here is an example that retrieves current departures from London Kings Cross, and then lists the realtime station data for the last service it finds.
public class RealTimeTrainsDemo
{
private readonly IRealTimeTrainsClient _client;
public RealTimeTrainsDemo(IRealTimeTrainsClient client)
{
_client = client;
}
public async Task RunAsync()
{
var services = await _client.GetDeparturesAsync("KGX");
Console.WriteLine("Services departing from London King's Cross:");
foreach (var service in services.Services)
{
var origin = service.LocationDetail.Origin.First();
var destination = service.LocationDetail.Destination.Last();
Console.WriteLine($"\t{service.RunningIdentity}\t{service.ServiceUid}\t" +
$"{origin.PublicTime} {origin.Description} --> " +
$"{destination.PublicTime} {destination.Description} " +
$"({service.LocationDetail.Description})");
}
var lastService = services.Services.Last();
var now = DateTime.Now;
var serviceDetail = await _client.GetServiceAsync(lastService.ServiceUid, new DateFilter(now.Year, now.Month, now.Day));
foreach (var callingPoint in serviceDetail.Locations)
{
Console.WriteLine($"\t\t\tRealtime Data: ARR {callingPoint.RealtimeArrival} DEP {callingPoint.RealtimeDeparture} AT {callingPoint.Description} PLATFORM {callingPoint.Platform}");
}
}
}
All methods on the RealTimeTrainsClient object have equivalent non-async methods for backward compatibility.
For more information, see the Realtime Trains Location Listing documentation.
Note that 'Service Unique IDs' are only unique for a particular day, so you must also pass in the date you're interested in to this call. For more information, see the Realtime Trains Service Information documentation.
Example Usage
A live demo of this library can be found at thetrain.rocks.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Newtonsoft.Json (>= 13.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
8.0.1 | 111 | 6/22/2024 |
8.0.0 | 92 | 6/22/2024 |
3.0.17 | 398 | 2/9/2023 |
3.0.14 | 446 | 8/19/2022 |
3.0.13 | 557 | 5/30/2022 |
3.0.11 | 361 | 11/22/2021 |
3.0.9 | 487 | 5/16/2021 |
3.0.8 | 430 | 4/19/2021 |
2.1.3 | 680 | 7/2/2019 |
2.1.2 | 611 | 6/5/2019 |
2.1.1 | 631 | 6/4/2019 |
2.1.0 | 630 | 6/4/2019 |
2.0.0 | 667 | 4/4/2019 |
1.5.2 | 794 | 10/21/2018 |
1.5.1 | 795 | 10/21/2018 |
1.5.0 | 774 | 10/21/2018 |
1.4.1 | 974 | 7/4/2018 |
1.4.0 | 972 | 9/24/2017 |
1.3.0 | 961 | 9/24/2017 |
1.2.0 | 934 | 9/24/2017 |
1.1.0 | 1,042 | 1/7/2017 |
1.0.5 | 1,121 | 8/23/2016 |
1.0.4 | 1,451 | 8/15/2015 |
1.0.3 | 1,017 | 8/15/2015 |
1.0.2 | 1,054 | 8/15/2015 |
1.0.1 | 979 | 8/15/2015 |
1.0.0 | 1,031 | 8/15/2015 |