Class: Fit4Ruby::Activity

Inherits:
FitDataRecord show all
Defined in:
lib/fit4ruby/Activity.rb

Overview

This is the most important class of this library. It holds references to all other data structures. Each of the objects it references are direct equivalents of the message record structures used in the FIT file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FitDataRecord

#<=>, #get, #get_as, #inspect, #set, #set_field_values

Methods included from Converters

#conversion_factor, #fit_time_to_time, #secsToDHMS, #secsToHMS, #speedToPace, #time_to_fit_time

Constructor Details

#initialize(field_values = {}) ⇒ Activity

Create a new Activity object.

Parameters:

  • field_values (Hash) (defaults to: {})

    A Hash that provides initial values for certain fields of the FitDataRecord.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fit4ruby/Activity.rb', line 37

def initialize(field_values = {})
  super('activity')
  @meta_field_units['total_gps_distance'] = 'm'
  @num_sessions = 0

  @file_id = FileId.new
  @file_creator = FileCreator.new
  @device_infos = []
  @user_profiles = []
  @events = []
  @sessions = []
  @laps = []
  @records = []
  @personal_records = []

  @cur_session_laps = []
  @cur_lap_records = []

  @lap_counter = 1

  set_field_values(field_values)
end

Instance Attribute Details

#device_infosObject

Returns the value of attribute device_infos.



31
32
33
# File 'lib/fit4ruby/Activity.rb', line 31

def device_infos
  @device_infos
end

#eventsObject

Returns the value of attribute events.



31
32
33
# File 'lib/fit4ruby/Activity.rb', line 31

def events
  @events
end

#file_creatorObject

Returns the value of attribute file_creator.



31
32
33
# File 'lib/fit4ruby/Activity.rb', line 31

def file_creator
  @file_creator
end

#file_idObject

Returns the value of attribute file_id.



31
32
33
# File 'lib/fit4ruby/Activity.rb', line 31

def file_id
  @file_id
end

#lapsObject

Returns the value of attribute laps.



31
32
33
# File 'lib/fit4ruby/Activity.rb', line 31

def laps
  @laps
end

#personal_recordsObject

Returns the value of attribute personal_records.



31
32
33
# File 'lib/fit4ruby/Activity.rb', line 31

def personal_records
  @personal_records
end

#recordsObject

Returns the value of attribute records.



31
32
33
# File 'lib/fit4ruby/Activity.rb', line 31

def records
  @records
end

#sessionsObject

Returns the value of attribute sessions.



31
32
33
# File 'lib/fit4ruby/Activity.rb', line 31

def sessions
  @sessions
end

#user_profilesObject

Returns the value of attribute user_profiles.



31
32
33
# File 'lib/fit4ruby/Activity.rb', line 31

def user_profiles
  @user_profiles
end

Instance Method Details

#==(a) ⇒ TrueClass/FalseClass

Check if the current Activity is equal to the passed Activity. otherwise false.

Parameters:

  • a (Activity)

    Activity to compare this Activity with.

Returns:

  • (TrueClass/FalseClass)

    true if both Activities are equal,



291
292
293
294
295
296
297
# File 'lib/fit4ruby/Activity.rb', line 291

def ==(a)
  super(a) && @file_id == a.file_id &&
    @file_creator == a.file_creator &&
    @device_infos == a.device_infos && @user_profiles == a.user_profiles &&
    @events == a.events &&
    @sessions == a.sessions && personal_records == a.personal_records
end

#aggregateObject

Call this method to update the aggregated data fields stored in Lap and Session objects.



135
136
137
138
# File 'lib/fit4ruby/Activity.rb', line 135

def aggregate
  @laps.each { |l| l.aggregate }
  @sessions.each { |s| s.aggregate }
end

#avg_speedObject

Convenience method that averages the speed over all sessions.



141
142
143
144
145
# File 'lib/fit4ruby/Activity.rb', line 141

def avg_speed
  speed = 0.0
  @sessions.each { |s| speed += s.avg_speed }
  speed / @sessions.length
end

#checkObject

Perform some basic logical checks on the object and all references sub objects. Any errors will be reported via the Log object.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fit4ruby/Activity.rb', line 62

def check
  unless @timestamp && @timestamp >= Time.parse('1990-01-01T00:00:00+00:00')
    Log.error "Activity has no valid timestamp"
  end
  unless @total_timer_time
    Log.error "Activity has no valid total_timer_time"
  end
  unless @num_sessions == @sessions.count
    Log.error "Activity record requires #{@num_sessions}, but "
              "#{@sessions.length} session records were found in the "
              "FIT file."
  end
  @sessions.each { |s| s.check(self) }
  # Laps must have a consecutively growing message index.
  @laps.each.with_index do |lap, index|
    unless lap.message_index == index
      Log.error "Lap #{index} has wrong message_index #{lap.message_index}"
    end
  end
end

#ending_hrObject

Return the heart rate when the activity recording was last stopped.



148
149
150
# File 'lib/fit4ruby/Activity.rb', line 148

def ending_hr
  @records.empty? ? nil : @records[-1].heart_rate
end

#new_device_info(field_values = {}) ⇒ DeviceInfo

Add a new DeviceInfo to the Activity.

Parameters:

  • field_values (Hash) (defaults to: {})

    A Hash that provides initial values for certain fields of the FitDataRecord.

Returns:



228
229
230
# File 'lib/fit4ruby/Activity.rb', line 228

def new_device_info(field_values = {})
  new_fit_data_record('device_info', field_values)
end

#new_event(field_values = {}) ⇒ Event

Add a new Event to the Activity.

Parameters:

  • field_values (Hash) (defaults to: {})

    A Hash that provides initial values for certain fields of the FitDataRecord.

Returns:



244
245
246
# File 'lib/fit4ruby/Activity.rb', line 244

def new_event(field_values = {})
  new_fit_data_record('event', field_values)
end

#new_file_creator(field_values = {}) ⇒ FileCreator

Add a new FileCreator to the Activity. It will replace any previously added FileCreator object.

Parameters:

  • field_values (Hash) (defaults to: {})

    A Hash that provides initial values for certain fields of the FitDataRecord.

Returns:



220
221
222
# File 'lib/fit4ruby/Activity.rb', line 220

def new_file_creator(field_values = {})
  new_fit_data_record('file_creator', field_values)
end

#new_file_id(field_values = {}) ⇒ FileId

Add a new FileId to the Activity. It will replace any previously added FileId object.

Parameters:

  • field_values (Hash) (defaults to: {})

    A Hash that provides initial values for certain fields of the FitDataRecord.

Returns:



211
212
213
# File 'lib/fit4ruby/Activity.rb', line 211

def new_file_id(field_values = {})
  new_fit_data_record('file_id', field_values)
end

#new_fit_data_record(record_type, field_values = {}) ⇒ Object

Create a new FitDataRecord.

Parameters:

  • record_type (String)

    Type that identifies the FitDataRecord derived class to create.

  • field_values (Hash) (defaults to: {})

    A Hash that provides initial values for certain fields of the FitDataRecord.

Returns:

  • FitDataRecord



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/fit4ruby/Activity.rb', line 305

def new_fit_data_record(record_type, field_values = {})
  case record_type
  when 'file_id'
    @file_id = (record = FileId.new(field_values))
  when 'file_creator'
    @file_creator = (record = FileCreator.new(field_values))
  when 'device_info'
    @device_infos << (record = DeviceInfo.new(field_values))
  when 'user_profile'
    @user_profiles << (record = UserProfile.new(field_values))
  when 'event'
    @events << (record = Event.new(field_values))
  when 'session'
    unless @cur_lap_records.empty?
      # Ensure that all previous records have been assigned to a lap.
      record = create_new_lap(field_values)
    end
    @num_sessions += 1
    @sessions << (record = Session.new(@cur_session_laps, @lap_counter,
                                       field_values))
    @cur_session_laps = []
  when 'lap'
    record = create_new_lap(field_values)
  when 'record'
    @cur_lap_records << (record = Record.new(field_values))
    @records << record
  when 'personal_records'
    @personal_records << (record = PersonalRecords.new(field_values))
  else
    record = nil
  end

  record
end

#new_lap(field_values = {}) ⇒ Lap

Add a new Lap to the Activity. All previoulsy added Record objects are associated with this Lap unless they have been associated with another Lap before.

Parameters:

  • field_values (Hash) (defaults to: {})

    A Hash that provides initial values for certain fields of the FitDataRecord.

Returns:



267
268
269
# File 'lib/fit4ruby/Activity.rb', line 267

def new_lap(field_values = {})
  new_fit_data_record('lap', field_values)
end

#new_personal_record(field_values = {}) ⇒ PersonalRecord

Add a new PersonalRecord to the Activity.

Parameters:

  • field_values (Hash) (defaults to: {})

    A Hash that provides initial values for certain fields of the FitDataRecord.

Returns:

  • (PersonalRecord)


275
276
277
# File 'lib/fit4ruby/Activity.rb', line 275

def new_personal_record(field_values = {})
  new_fit_data_record('personal_record', field_values)
end

#new_record(field_values = {}) ⇒ Record

Add a new Record to the Activity.

Parameters:

  • field_values (Hash) (defaults to: {})

    A Hash that provides initial values for certain fields of the FitDataRecord.

Returns:



283
284
285
# File 'lib/fit4ruby/Activity.rb', line 283

def new_record(field_values = {})
  new_fit_data_record('record', field_values)
end

#new_session(field_values = {}) ⇒ Session

Add a new Session to the Activity. All previously added Lap objects are associated with this Session unless they have been associated with another Session before. If there are any Record objects that have not yet been associated with a Lap, a new lap will be created and the Record objects will be associated with this Lap. The Lap will be associated with the newly created Session.

Parameters:

  • field_values (Hash) (defaults to: {})

    A Hash that provides initial values for certain fields of the FitDataRecord.

Returns:



257
258
259
# File 'lib/fit4ruby/Activity.rb', line 257

def new_session(field_values = {})
  new_fit_data_record('session', field_values)
end

#new_user_profile(field_values = {}) ⇒ UserProfile

Add a new UserProfile to the Activity.

Parameters:

  • field_values (Hash) (defaults to: {})

    A Hash that provides initial values for certain fields of the FitDataRecord.

Returns:



236
237
238
# File 'lib/fit4ruby/Activity.rb', line 236

def (field_values = {})
  new_fit_data_record('user_profile', field_values)
end

#recovery_hrObject

Return the measured recovery heart rate.



153
154
155
156
157
158
159
# File 'lib/fit4ruby/Activity.rb', line 153

def recovery_hr
  @events.each do |e|
    return e.recovery_hr if e.event == 'recovery_hr'
  end

  nil
end

#recovery_timeObject

Returns the predicted recovery time needed after this activity.

Returns:

  • recovery time in seconds.



163
164
165
166
167
168
169
# File 'lib/fit4ruby/Activity.rb', line 163

def recovery_time
  @events.each do |e|
    return e.recovery_time if e.event == 'recovery_time'
  end

  nil
end

#sportObject

Returns the sport type of this activity.



182
183
184
# File 'lib/fit4ruby/Activity.rb', line 182

def sport
  @sessions[0].sport
end

#sub_sportObject

Returns the sport subtype of this activity.



187
188
189
# File 'lib/fit4ruby/Activity.rb', line 187

def sub_sport
  @sessions[0].sub_sport
end

#total_distanceObject

Convenience method that aggregates all the distances from the included sessions.



85
86
87
88
89
# File 'lib/fit4ruby/Activity.rb', line 85

def total_distance
  d = 0.0
  @sessions.each { |s| d += s.total_distance }
  d
end

#total_gps_distanceObject

Total distance convered by this activity purely computed by the GPS coordinates. This may differ from the distance computed by the device as it can be based on a purely calibrated footpod.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/fit4ruby/Activity.rb', line 94

def total_gps_distance
  timer_stops = []
  # Generate a list of all timestamps where the timer was stopped.
  @events.each do |e|
    if e.event == 'timer' && e.event_type == 'stop_all'
      timer_stops << e.timestamp
    end
  end

  # The first record of a FIT file can already have a distance associated
  # with it. The GPS location of the first record is not where the start
  # button was pressed. This introduces a slight inaccurcy when computing
  # the total distance purely on the GPS coordinates found in the records.
  d = 0.0
  last_lat = last_long = nil

  # Iterate over all the records and accumlate the distances between the
  # neiboring coordinates.
  @records.each do |r|
    if (lat = r.position_lat) && (long = r.position_long)
      if last_lat && last_long
        d += Fit4Ruby::GeoMath.distance(last_lat, last_long,
                                        lat, long)
      end
      if timer_stops[0] == r.timestamp
        # If a stop event was found for this record timestamp we clear the
        # last_* values so that the distance covered while being stopped
        # is not added to the total.
        last_lat = last_long = nil
        timer_stops.shift
      else
        last_lat = lat
        last_long = long
      end
    end
  end
  d
end

#vo2maxObject

Returns the computed VO2max value. This value is computed by the device based on multiple previous activities.



173
174
175
176
177
178
179
# File 'lib/fit4ruby/Activity.rb', line 173

def vo2max
  @events.each do |e|
    return e.vo2max if e.event == 'vo2max'
  end

  nil
end

#write(io, id_mapper) ⇒ Object

Write the Activity data to a file.

Parameters:

  • io (IO)

    File reference

  • id_mapper (FitMessageIdMapper)

    Maps global FIT record types to local ones.



195
196
197
198
199
200
201
202
203
204
# File 'lib/fit4ruby/Activity.rb', line 195

def write(io, id_mapper)
  @file_id.write(io, id_mapper)
  @file_creator.write(io, id_mapper)

  (@device_infos + @user_profiles + @events + @sessions + @laps +
   @records + @personal_records).sort.each do |s|
    s.write(io, id_mapper)
  end
  super
end