Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add vehicle class for tracking single vehicles.

  • Loading branch information...
commit fa0f287ac5e718137f05ba2a259190eb288c04a9 1 parent cb7ba04
@namxam namxam authored
Showing with 75 additions and 0 deletions.
  1. +75 −0 app/models/vehicle.rb
View
75 app/models/vehicle.rb
@@ -0,0 +1,75 @@
+require 'arrivals'
+
+class Vehicle
+
+ attr_accessor :line, :destination, :direction, :position, :speed
+
+ def self.at_station(station)
+ arrivals = Arrivals.new(station.kvb_id).import.trains
+
+ arrivals.map do |arrival|
+ line = Line.find_by_number(arrival[:line])
+ Rails.logger.debug { "Line: #{line.number} => #{arrival[:destination]}" }
+ if destination = Station.with_name_or_alias(arrival[:destination])
+ direction = station.direction(line, destination) # :up, :down, nil
+ Vehicle.new(line, direction, destination, station, arrival[:arrival])
+ end
+ end.compact
+ end
+
+ def kind
+ line.kind
+ end
+
+ def initialize(line, direction, destination, station, travel_time_to_station)
+ self.line = line
+ self.direction = direction
+ self.destination = destination
+
+ @station = station
+ @travel_time_to_station = travel_time_to_station
+ end
+
+ def id
+ @id ||= nil
+ end
+
+ def arrival_time_at_destination
+ @arrival_time_at_destination ||= begin
+ route = Line.cached_routes[line.number]
+
+ state = :delete
+ remaining_route = route.delete_if do |station_kvb_id, data|
+ if state == :delete and station_kvb_id == @station.kvb_id
+ state = :keep
+ end
+
+ if state == :keep
+ if station_kvb_id == destination.kvb_id
+ state == :delete
+ end
+ end
+
+ state == :delete
+ end
+
+ #raise remaining_route.inspect
+
+ remaining_route.reduce(@travel_time_to_station) { |memo, row| memo += row.last[:travel_time_up] }
+ end
+ end
+
+ def to_hash
+ {
+ line: self.line.number,
+ id: self.id,
+ kind: self.kind,
+ position: self.position,
+ destination: self.destination,
+ direction: self.direction,
+ speed: self.speed,
+ arrival_time_at_destination: self.arrival_time_at_destination
+ }
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.