Class: Session

Inherits:
Object
  • Object
show all
Includes:
BinData
Defined in:
lib/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSession

Returns a new instance of Session.



29
30
31
32
33
34
# File 'lib/session.rb', line 29

def initialize
  @laps = []
  @handles = []
  @straight_line_distance = 0
  LOGGER.debug "Reading new session"
end

Instance Attribute Details

#handlesObject (readonly)

Returns the value of attribute handles.



4
5
6
# File 'lib/session.rb', line 4

def handles
  @handles
end

#lapsObject (readonly)

Returns the value of attribute laps.



4
5
6
# File 'lib/session.rb', line 4

def laps
  @laps
end

#projection_originObject (readonly)

Returns the value of attribute projection_origin.



4
5
6
# File 'lib/session.rb', line 4

def projection_origin
  @projection_origin
end

#routeObject

Returns the value of attribute route.



3
4
5
# File 'lib/session.rb', line 3

def route
  @route
end

#session_infoObject (readonly)

Returns the value of attribute session_info.



4
5
6
# File 'lib/session.rb', line 4

def session_info
  @session_info
end

#straight_line_distanceObject (readonly)

Returns the value of attribute straight_line_distance.



4
5
6
# File 'lib/session.rb', line 4

def straight_line_distance
  @straight_line_distance
end

Class Method Details

.read_session(data, tag_data_length) ⇒ Object



25
26
27
# File 'lib/session.rb', line 25

def self.read_session(data, tag_data_length)
  new.parse_data(data, tag_data_length)
end

.read_sessions(data) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/session.rb', line 8

def self.read_sessions(data)
  sessions = []
  session_count = BinData::Uint32le.read(data)
  LOGGER.debug "reading #{session_count} sessions"

  session_count.times do |i|
    tag = TagDataExtractor.read(data)

    LOGGER.debug "in session #{i}, tag is #{tag} and is #{tag.data_length} bytes long"

    if TAGS[tag.tag] == :session
      sessions << read_session(data, tag.data_length)
    end
  end
  sessions
end

Instance Method Details

#calculateObject



36
37
38
39
40
# File 'lib/session.rb', line 36

def calculate
  LOGGER.debug("starting to calculate shit for #{self.inspect}")
  route.calculate_parameters
  calculate_laps
end

#calculate_lapsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/session.rb', line 42

def calculate_laps
  last_distance, last_lap = 0, nil

  @laps.each do |lap|
    pl = route.parameterized_location_from_time(lap.time)
    lap.position = route.position_from_parameterized_location(pl)

    distance = route.distance_from_parameterized_location(pl)
    if lap.is_of_type?(:lap, :stop)
      lap.distance = distance - last_distance

      if last_lap
        lap.straight_line_distance = lap.position.distance_to(last_lap.position)
      else
        lap.straight_line_distance = 0
      end
      @straight_line_distance += lap.straight_line_distance
    end
    last_distance = distance
    last_lap = lap
  end

  LOGGER.debug("Laps after calculation: #{@laps.inspect}")
end

#parse_data(data, tag_data_length) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/session.rb', line 67

def parse_data(data, tag_data_length)
  start_pos = data.pos
  while data.pos < (start_pos + tag_data_length)
    tag = TagDataExtractor.read(data)

    LOGGER.debug "tag is #{tag}, #{tag.data_length} bytes"

    send("set_#{TAGS[tag.tag]}", data)
  end
  self
end