diff --git a/.gitignore b/.gitignore index 13dfa36..c26d4f2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .env -node_modules/ \ No newline at end of file +node_modules/* \ No newline at end of file diff --git a/README.md b/README.md index cde0fad..b724310 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,15 @@ # trainspy -Get departures at any UK train station & recieve updates on trains throughout their journey via the RTT API. +Get departures at any UK train station & recieve updates on trains throughout their journey via a custom made RTT API. ## Install trainspy ```js npm i trainspy ``` # Get departures -Departures can be retrived by ```findTrains(stationCode)``` or ```findTrains(stationName)```: +Departures can be retrieved by ```stationCode``` or ```stationName```: ```js -findTrains("WLF") || findTrains("Whittlesford Parkway") +const x = findTrains("WLF") +const y = findTrains("Whittlesford Parkway") //same result as x ``` which returns in the following format: ```js @@ -22,35 +23,74 @@ which returns in the following format: ``` # Tracking a train -You first need the ```serviceID```. -Can be retrived by ```findTrains(stationCode)``` as shown above. ```js -trackTrain(serviceID, refreshRate) +trackTrain(serviceID, timeTillRefresh?) //minimum timeTillRefresh of 5 seconds ``` +You first need the ```serviceID```. +Can be retrived by ```findTrains(stationCode)``` as shown above. + ```js -trackTrain("L14125").then((emitter) => { - emitter.on("UPDATE", (update) => { - console.log(update) //replace with your logic in terms of the update +trackTrain("P70052").then((emitter) => { + emitter.on("journeyUpdate", (data) => { + //your code for journey updates here }); + emitter.on("errorUpdate", (data) => console.log(data)); }); ``` -```trackTrain()``` returns a promise - ```emitter```. Subscribe to this emitter as shown above. +```trackTrain()``` returns a promise - ```emitter```. Subscribe as shown above. This emits live updates (as JSON) on the train until the journey is complete. ```js -{ status: 'Approaching', station: 'Hackney Downs [HAC]' } +{ + status: 'Approaching', + station: { + name: 'Rugeley Town', + code: [ 'RGT' ], + arrival: { actual: '0615½' }, + departure: { actual: null }, + stopsHere: true + }, + destination: 'Rugeley Trent Valley', + delay: '+3' +} ``` +| Status | Explanation | +| ------------- | ------------- | +| Passed | Train just passed this non-stopping station | +| Approaching | Train is now approaching this station | +| Arriving | Train is now arriving at this stopping station | +| At platform | Train is now at a platform of this stopping station | +| Departed | Train just departed this stopping station | + +## More examples +### Track the next service from London to Manchester +(Provided this service departs in the next hour) ```js -{ status: 'Departed', station: 'Hackney Downs [HAC]' } +import {trackTrain, findTrains} from "trainspy"; + +const services = await findTrains("EUS"); +services.forEach((service) => { + if (service.destination == "Manchester Piccadilly") { + trackTrain(service.serviceID).then((emitter) => { + emitter.on("journeyUpdate", (update) => console.log(update)); + emitter.on("errorUpdate", (data) => console.log(data)); + }); + } +}); ``` -## More examples + + +### Change html content ```js -import trackTrain from "trainspy"; +import {trackTrain} from "trainspy"; trackTrain("P71733").then((myTrainTracker) => - myTrainTracker.on("UPDATE", (currentState) => { + myTrainTracker.on("journeyUpdate", (currentState) => { document.getElementByID("status").innerHTML = currentState.status; - document.getElementByID("station").innerHTML = currentState.station; - }) + document.getElementByID("station").innerHTML = currentState.station.name; + }); + myTrainTracker.on("errorUpdate",(error)=>{ + console.log(error) + } ); ```