src/train-announcement.js
function getDepartures (fromStationId, toStationId, fromTime, toTime) {
  fromTime = fromTime || '-00:30:00'
  toTime = toTime || '03:00:00'

  let optionalFilters = ''
  if (toStationId) {
    optionalFilters += '<OR>' +
      `<EQ name="ViaToLocation.LocationName" value="${toStationId}"/>` +
      `<EQ name="ToLocation.LocationName" value="${toStationId}"/>` +
      '</OR>'
  }
  let xmlRequest = fs.readFileSync(xmlRequestFile)
    .toString()
    .replace('{apikey}', env.apiKey)
    .replace('{fromStationId}', fromStationId)
    .replace('{optionalFilters}', optionalFilters)
    .replace('{fromTime}', fromTime)
    .replace('{toTime}', toTime)

  return new Promise(function (resolve, reject) {
    request(
      {method: 'POST', url: env.url, body: xmlRequest},
      function (err, response, body) {
        if (err) {
          return reject(err)
        }
        let bodyObj = JSON.parse(body)
        let departures = handleDeparturesResponse(bodyObj)
        resolve(deduplicateDepartures(departures))
      }
    )
  }).then(function (departures) {
    let stationIds = getStationIdsFromDepartures(departures)
    return trainStationService.getTrainStationsInfo(stationIds)
      .then(function (stationsInfo) {
        return replaceStationIdsForNamesInDepartures(departures, stationsInfo)
      })
  })
}
function getDepartures (fromStationId, toStationId, fromTime, toTime) {
  fromTime = fromTime || '-00:30:00'
  toTime = toTime || '03:00:00'

  let optionalFilters = ''
  if (toStationId) {
    optionalFilters += '<OR>' +
      `<EQ name="ViaToLocation.LocationName" value="${toStationId}"/>` +
      `<EQ name="ToLocation.LocationName" value="${toStationId}"/>` +
      '</OR>'
  }
  let xmlRequest = fs.readFileSync(xmlRequestFile)
    .toString()
    .replace('{apikey}', env.apiKey)
    .replace('{fromStationId}', fromStationId)
    .replace('{optionalFilters}', optionalFilters)
    .replace('{fromTime}', fromTime)
    .replace('{toTime}', toTime)

  return new Promise(function (resolve, reject) {
    request(
      {method: 'POST', url: env.url, body: xmlRequest},
      function (err, response, body) {
        if (err) {
          return reject(err)
        }
        let bodyObj = JSON.parse(body)
        let departures = handleDeparturesResponse(bodyObj)
        resolve(deduplicateDepartures(departures))
      }
    )
  }).then(function (departures) {
    let stationIds = getStationIdsFromDepartures(departures)
    return trainStationService.getTrainStationsInfo(stationIds)
      .then(function (stationsInfo) {
        return replaceStationIdsForNamesInDepartures(departures, stationsInfo)
      })
  })
}
function getDepartures ( fromStationId , toStationId , fromTime , toTime ) { fromTime = fromTime || '-00:30:00' toTime = toTime || '03:00:00' let optionalFilters = '' if ( toStationId ) { optionalFilters += '<OR>' + ` ${ toStationId } ` + ` ${ toStationId } ` + '</OR>' } let xmlRequest = fs . readFileSync ( xmlRequestFile ) . toString ( ) . replace ( '{apikey}' , env . apiKey ) . replace ( '{fromStationId}' , fromStationId ) . replace ( '{optionalFilters}' , optionalFilters ) . replace ( '{fromTime}' , fromTime ) . replace ( '{toTime}' , toTime ) return new Promise ( function ( resolve , reject ) { request ( { method : 'POST' , url : env . url , body : xmlRequest } , function ( err , response , body ) { if ( err ) { return reject ( err ) } let bodyObj = JSON . parse ( body ) let departures = handleDeparturesResponse ( bodyObj ) resolve ( deduplicateDepartures ( departures ) ) } ) } ) . then ( function ( departures ) { let stationIds = getStationIdsFromDepartures ( departures ) return trainStationService . getTrainStationsInfo ( stationIds ) . then ( function ( stationsInfo ) { return replaceStationIdsForNamesInDepartures ( departures , stationsInfo ) } ) } ) }
Query the departures from a station
@param  {string} fromStationId Id of the station of departure
@param  {string} toStationId   Id of a station on the route of the train (optional)
@param  {string} fromTime      HH:mm:ss Includes trains leaving how long AFTER the current time? (default: -00:30:00)
- If the value is negative, includes trains leaving before the current time
@param  {string} toTime        HH:mm:ss Excludes trains leaving how long AFTER the current time? (default: 03:00:00)
@return {array}                Array of departure objects containing the following keys:
- train {string}: Train id
- track {string}: Track nunber at departing station
- cancelled {boolean}: Departure cancelled?
- delayed {boolean}: Departure delayed?
- deviation {string[]}: Deiations, for example: "Bus Replacement"
- date {string}: Date of departure (DD/MM/YYYY)
- time {string}: Time of departure (HH:mm:ss)
- estimatedDate {string}: Estimated date of departure (DD/MM/YYYY)
- estimatedTime {string}: Estimated time of departure (HH:mm:ss)
- plannedEstimatedDate {string}: Planned delayed departure date (DD/MM/YYYY)
- plannedEstimatedTime {string}: Planned delayed departure time (HH:mm:ss)
- scheduledDepartureDate {string}: The train's announced departure date (DD/MM/YYYY)
- scheduledDepartureTime {string}: The train's announced departure time (HH:mm:ss)
- destination {string}: Name of the final destination station
- via {string[]}: Name of the stations where the train stops
Query the departures from a station