src/classes/Train.js
// next stationand expected arrival time.
next stationand expected arrival time.
next stationand expected arrival time .
locate(combinedIntervals, stations) {
    try {
      if (!this.id || !this.mostRecentTripEntity) {
        throw "Incomplete train data."
      }
      let te = this.mostRecentTripEntity
      let nextStopUpdate;
      let index = 0;
      let arrivalEstimate = 0;
      while (arrivalEstimate < te.timestamp) {
        nextStopUpdate = te.stopTimeUpdates[index];
        arrivalEstimate = parseInt(nextStopUpdate.arrival);
        index += 1;
        if (index === te.stopTimeUpdates.length) {
          console.log("All arrival estimates are in the past.")
          arrivalEstimate = te.timestamp;
          break;
        }
      }
      this.routeId = te.trip.routeId;
      this.direction = te.trip.direction;
      const nextStopId = nextStopUpdate.GtfsStopId;
      const waitTimeEstimate = arrivalEstimate - te.timestamp;
      if (waitTimeEstimate < 0) {
        debugger;
      }
      if (!(this.direction === 'N' || this.direction === 'S')) {
        throw 'Invalid train direction: ' + this.direction;
      }
      this.nextStopId = nextStopId;
      const trainPos = this.findPosition(waitTimeEstimate, combinedIntervals, stations);
      if (!trainPos.latitude || !trainPos.longitude) {
        throw "Error finding lat/long."
      }
      if (this.marker && this.latitude != trainPos.latitude || this.longitude != trainPos.longitude) {
        this.move = true;
      }
      this.latitude = trainPos.latitude;
      this.longitude = trainPos.longitude;
      this.intermediateDestinations = trainPos.intermediateDestinations
    } catch (error) {
      console.log("Error locating train:", error);
    }
  }
locate ( combinedIntervals , stations ) { try { if ( ! this . id || ! this . mostRecentTripEntity ) { throw "Incomplete train data." } let te = this . mostRecentTripEntity let nextStopUpdate ; let index = 0 ; let arrivalEstimate = 0 ; while ( arrivalEstimate < te . timestamp ) { nextStopUpdate = te . stopTimeUpdates [ index ] ; arrivalEstimate = parseInt ( nextStopUpdate . arrival ) ; index += 1 ; if ( index === te . stopTimeUpdates . length ) { console . log ( "All arrival estimates are in the past." ) arrivalEstimate = te . timestamp ; break ; } } this . routeId = te . trip . routeId ; this . direction = te . trip . direction ; const nextStopId = nextStopUpdate . GtfsStopId ; const waitTimeEstimate = arrivalEstimate - te . timestamp ; if ( waitTimeEstimate < 0 ) { debugger ; } if ( ! ( this . direction === 'N' || this . direction === 'S' ) ) { throw 'Invalid train direction: ' + this . direction ; } this . nextStopId = nextStopId ; const trainPos = this . findPosition ( waitTimeEstimate , combinedIntervals , stations ) ; if ( ! trainPos . latitude || ! trainPos . longitude ) { throw "Error finding lat/long." } if ( this . marker && this . latitude != trainPos . latitude || this . longitude != trainPos . longitude ) { this . move = true ; } this . latitude = trainPos . latitude ; this . longitude = trainPos . longitude ; this . intermediateDestinations = trainPos . intermediateDestinations } catch ( error ) { console . log ( "Error locating train:" , error ) ; } }
next stationand expected arrival time.
next stationand expected arrival time .
// In case arrivalEstimate is negative loop through // 🚸 When does this happen? // 🚸 These shouldn't change, but what if they do? // All trains are either N or S (uptown/downtown) // Update .nextStopId for next update to read.