Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add in memory persistence of vehicles

  • Loading branch information...
commit d2201b1a53765fc91c7dc0b8db69dfecde4020b0 1 parent c45f8f0
@namxam namxam authored
Showing with 84 additions and 20 deletions.
  1. +30 −0 app/models/station_updater.rb
  2. +54 −20 app/models/vehicle.rb
View
30 app/models/station_updater.rb
@@ -0,0 +1,30 @@
+class StationUpdater
+
+ def self.update_station(station)
+ vehicles = Vehicle.at_station(station)
+
+ vehicles = vehicles.delete_if do |vehicle|
+ #binding.pry
+ regular_travel_time = Line.cached_routes[vehicle.line.number][station.kvb_id][:"travel_time_#{vehicle.direction}"]
+ vehicle.travel_time_to_station > (regular_travel_time + 1)
+ end
+
+ vehicles.compact.each do |vehicle|
+ Vehicle.vehicles[vehicle.grouping_id] ||= {}
+ data = Vehicle.vehicles[vehicle.grouping_id]
+
+ match = data.find do |arrival_time, value|
+ (arrival_time - vehicle.arrival_time_at_destination).abs < 3.minutes
+ end
+
+ if match
+ Vehicle.vehicles[vehicle.grouping_id][match.first] = nil
+ vehicle.id = match.last.id
+ Vehicle.vehicles[vehicle.grouping_id][vehicle.arrival_time_at_destination] = vehicle
+ else
+ Vehicle.vehicles[vehicle.grouping_id][vehicle.arrival_time_at_destination] = vehicle
+ end
+ end
+ end
+
+end
View
74 app/models/vehicle.rb
@@ -2,17 +2,24 @@
class Vehicle
- attr_accessor :line, :destination, :direction, :position, :speed
+ cattr_accessor :vehicles
+
+ @@vehicles = {}
+
+ attr_accessor :line, :destination, :direction, :position, :speed, :travel_time_to_station
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])
+ if line = Line.find_by_number(arrival[:line])
+ 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
+ else
+ Rails.logger.debug { "Could not find #{arrival.inspect}" }
+ nil
end
end.compact
end
@@ -31,31 +38,56 @@ def initialize(line, direction, destination, station, travel_time_to_station)
end
def id
- @id ||= nil
+ @id ||= begin
+ timestamp = arrival_time_at_destination.to_s(:db).gsub(/:/, '-').gsub(/ /, '-')
+ "#{self.line.number}-#{self.direction}-#{self.destination.kvb_id}-#{timestamp}"
+ end
+ end
+
+ def grouping_id
+ @id ||= begin
+ "#{self.line.number}-#{self.direction}-#{self.destination.kvb_id}"
+ end
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 self.direction == :up
+ 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
+ if state == :keep
+ if station_kvb_id == destination.kvb_id
+ state == :delete
+ end
end
+
+ state == :delete
end
+ else
+ state = :delete
+ remaining_route = route.delete_if do |station_kvb_id, data|
+ if state == :delete and station_kvb_id == destination.kvb_id
+ state = :keep
+ end
- state == :delete
- end
+ if state == :keep
+ if station_kvb_id == @station.kvb_id
+ state == :delete
+ end
+ end
- #raise remaining_route.inspect
+ state == :delete
+ end
+ end
- remaining_route.reduce(@travel_time_to_station) { |memo, row| memo += row.last[:travel_time_up] }
+ travel_tile_to_destination = remaining_route.reduce(@travel_time_to_station) { |memo, row| memo += row.last[:travel_time_up] }
+ travel_tile_to_destination.minutes.from_now
end
end
@@ -63,12 +95,14 @@ def to_hash
{
line: self.line.number,
id: self.id,
+ grouping_id: self.grouping_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
+ arrival_time_at_destination: self.arrival_time_at_destination,
+ travel_time_to_station: @travel_time_to_station
}
end
Please sign in to comment.
Something went wrong with that request. Please try again.