Class: Fit4Ruby::Activity
- Inherits:
-
FitDataRecord
- Object
- FitDataRecord
- Fit4Ruby::Activity
- 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.
Constant Summary
Constants inherited from FitDataRecord
Instance Attribute Summary collapse
-
#data_sources ⇒ Object
Returns the value of attribute data_sources.
-
#device_infos ⇒ Object
Returns the value of attribute device_infos.
-
#epo_data ⇒ Object
Returns the value of attribute epo_data.
-
#events ⇒ Object
Returns the value of attribute events.
-
#file_creator ⇒ Object
Returns the value of attribute file_creator.
-
#file_id ⇒ Object
Returns the value of attribute file_id.
-
#hrv ⇒ Object
Returns the value of attribute hrv.
-
#laps ⇒ Object
Returns the value of attribute laps.
-
#personal_records ⇒ Object
Returns the value of attribute personal_records.
-
#records ⇒ Object
Returns the value of attribute records.
-
#sessions ⇒ Object
Returns the value of attribute sessions.
-
#user_profiles ⇒ Object
Returns the value of attribute user_profiles.
Attributes inherited from FitDataRecord
Instance Method Summary collapse
-
#==(a) ⇒ TrueClass/FalseClass
Check if the current Activity is equal to the passed Activity.
-
#aggregate ⇒ Object
Call this method to update the aggregated data fields stored in Lap and Session objects.
-
#avg_speed ⇒ Object
Convenience method that averages the speed over all sessions.
-
#check ⇒ Object
Perform some basic logical checks on the object and all references sub objects.
-
#ending_hr ⇒ Object
Return the heart rate when the activity recording was last stopped.
-
#initialize(field_values = {}) ⇒ Activity
constructor
Create a new Activity object.
-
#new_data_sources(field_values = {}) ⇒ SourceData
Add a new SourceData to the Activity.
-
#new_device_info(field_values = {}) ⇒ DeviceInfo
Add a new DeviceInfo to the Activity.
-
#new_event(field_values = {}) ⇒ Event
Add a new Event to the Activity.
-
#new_file_creator(field_values = {}) ⇒ FileCreator
Add a new FileCreator to the Activity.
-
#new_file_id(field_values = {}) ⇒ FileId
Add a new FileId to the Activity.
-
#new_fit_data_record(record_type, field_values = {}) ⇒ Object
Create a new FitDataRecord.
-
#new_lap(field_values = {}) ⇒ Lap
Add a new Lap to the Activity.
-
#new_personal_record(field_values = {}) ⇒ PersonalRecord
Add a new PersonalRecord to the Activity.
-
#new_record(field_values = {}) ⇒ Record
Add a new Record to the Activity.
-
#new_session(field_values = {}) ⇒ Session
Add a new Session to the Activity.
-
#new_user_profile(field_values = {}) ⇒ UserProfile
Add a new UserProfile to the Activity.
-
#recovery_hr ⇒ Object
Return the measured recovery heart rate.
-
#recovery_info ⇒ Object
Returns the remaining recovery time at the start of the activity.
-
#recovery_time ⇒ Object
Returns the predicted recovery time needed after this activity.
-
#sport ⇒ Object
Returns the sport type of this activity.
-
#sub_sport ⇒ Object
Returns the sport subtype of this activity.
-
#total_distance ⇒ Object
Convenience method that aggregates all the distances from the included sessions.
-
#total_gps_distance ⇒ Object
Total distance convered by this activity purely computed by the GPS coordinates.
-
#vo2max ⇒ Object
Returns the computed VO2max value.
-
#write(io, id_mapper) ⇒ Object
Write the Activity data to a file.
Methods inherited from FitDataRecord
#<=>, #get, #get_as, #inspect, #set, #set_field_values
Methods included from Converters
#conversion_factor, #fit_time_to_time, #secsToDHMS, #secsToHM, #secsToHMS, #speedToPace, #time_to_fit_time
Constructor Details
#initialize(field_values = {}) ⇒ Activity
Create a new Activity object.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fit4ruby/Activity.rb', line 42 def initialize(field_values = {}) super('activity') @meta_field_units['total_gps_distance'] = 'm' @num_sessions = 0 @file_id = FileId.new @epo_data = nil @file_creator = FileCreator.new @device_infos = [] @data_sources = [] @user_profiles = [] @events = [] @sessions = [] @laps = [] @records = [] @hrv = [] @personal_records = [] @cur_session_laps = [] @cur_lap_records = [] @lap_counter = 1 set_field_values(field_values) end |
Instance Attribute Details
#data_sources ⇒ Object
Returns the value of attribute data_sources.
34 35 36 |
# File 'lib/fit4ruby/Activity.rb', line 34 def data_sources @data_sources end |
#device_infos ⇒ Object
Returns the value of attribute device_infos.
34 35 36 |
# File 'lib/fit4ruby/Activity.rb', line 34 def device_infos @device_infos end |
#epo_data ⇒ Object
Returns the value of attribute epo_data.
34 35 36 |
# File 'lib/fit4ruby/Activity.rb', line 34 def epo_data @epo_data end |
#events ⇒ Object
Returns the value of attribute events.
34 35 36 |
# File 'lib/fit4ruby/Activity.rb', line 34 def events @events end |
#file_creator ⇒ Object
Returns the value of attribute file_creator.
34 35 36 |
# File 'lib/fit4ruby/Activity.rb', line 34 def file_creator @file_creator end |
#file_id ⇒ Object
Returns the value of attribute file_id.
34 35 36 |
# File 'lib/fit4ruby/Activity.rb', line 34 def file_id @file_id end |
#hrv ⇒ Object
Returns the value of attribute hrv.
34 35 36 |
# File 'lib/fit4ruby/Activity.rb', line 34 def hrv @hrv end |
#laps ⇒ Object
Returns the value of attribute laps.
34 35 36 |
# File 'lib/fit4ruby/Activity.rb', line 34 def laps @laps end |
#personal_records ⇒ Object
Returns the value of attribute personal_records.
34 35 36 |
# File 'lib/fit4ruby/Activity.rb', line 34 def personal_records @personal_records end |
#records ⇒ Object
Returns the value of attribute records.
34 35 36 |
# File 'lib/fit4ruby/Activity.rb', line 34 def records @records end |
#sessions ⇒ Object
Returns the value of attribute sessions.
34 35 36 |
# File 'lib/fit4ruby/Activity.rb', line 34 def sessions @sessions end |
#user_profiles ⇒ Object
Returns the value of attribute user_profiles.
34 35 36 |
# File 'lib/fit4ruby/Activity.rb', line 34 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.
351 352 353 354 355 356 357 358 359 |
# File 'lib/fit4ruby/Activity.rb', line 351 def ==(a) super(a) && @file_id == a.file_id && @file_creator == a.file_creator && @device_infos == a.device_infos && @data_sources == a.data_sources && @user_profiles == a.user_profiles && @events == a.events && @sessions == a.sessions && personal_records == a.personal_records end |
#aggregate ⇒ Object
Call this method to update the aggregated data fields stored in Lap and Session objects.
171 172 173 174 |
# File 'lib/fit4ruby/Activity.rb', line 171 def aggregate @laps.each { |l| l.aggregate } @sessions.each { |s| s.aggregate } end |
#avg_speed ⇒ Object
Convenience method that averages the speed over all sessions.
177 178 179 180 181 |
# File 'lib/fit4ruby/Activity.rb', line 177 def avg_speed speed = 0.0 @sessions.each { |s| speed += s.avg_speed } speed / @sessions.length end |
#check ⇒ Object
Perform some basic logical checks on the object and all references sub objects. Any errors will be reported via the Log object.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/fit4ruby/Activity.rb', line 70 def check unless @timestamp && @timestamp >= Time.parse('1990-01-01T00:00:00+00:00') Log.fatal "Activity has no valid timestamp" end unless @total_timer_time Log.fatal "Activity has no valid total_timer_time" end unless @device_infos.length > 0 Log.fatal "Activity must have at least one device_info section" end @device_infos.each.with_index { |d, index| d.check(index) } unless @num_sessions == @sessions.count Log.fatal "Activity record requires #{@num_sessions}, but " "#{@sessions.length} session records were found in the " "FIT file." end # Records must have consecutively growing timestamps and distances. ts = Time.parse('1989-12-31') distance = nil @records.each do |r| Log.fatal "Record has no timestamp" unless r. if r. < ts Log.fatal "Record has earlier timestamp than previous record" end if r.distance if distance && r.distance < distance Log.fatal "Record #{r.} has smaller distance " + "(#{r.distance}) than an earlier record (#{distance})" end distance = r.distance end ts = r. end # Laps must have a consecutively growing message index. @laps.each.with_index do |lap, index| lap.check(index) end @sessions.each { |s| s.check(self) } end |
#ending_hr ⇒ Object
Return the heart rate when the activity recording was last stopped.
184 185 186 |
# File 'lib/fit4ruby/Activity.rb', line 184 def ending_hr @records.empty? ? nil : @records[-1].heart_rate end |
#new_data_sources(field_values = {}) ⇒ SourceData
Add a new SourceData to the Activity.
288 289 290 |
# File 'lib/fit4ruby/Activity.rb', line 288 def new_data_sources(field_values = {}) new_fit_data_record('data_sources', field_values) end |
#new_device_info(field_values = {}) ⇒ DeviceInfo
Add a new DeviceInfo to the Activity.
280 281 282 |
# File 'lib/fit4ruby/Activity.rb', line 280 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.
304 305 306 |
# File 'lib/fit4ruby/Activity.rb', line 304 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.
272 273 274 |
# File 'lib/fit4ruby/Activity.rb', line 272 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.
263 264 265 |
# File 'lib/fit4ruby/Activity.rb', line 263 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.
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/fit4ruby/Activity.rb', line 367 def new_fit_data_record(record_type, field_values = {}) case record_type when 'file_id' @file_id = (record = FileId.new(field_values)) when 'epo_data' @epo_data = (record = EPO_Data.new(field_values)) when 'file_creator' @file_creator = (record = FileCreator.new(field_values)) when 'device_info' @device_infos << (record = DeviceInfo.new(field_values)) when 'data_sources' @data_sources << (record = DataSources.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? # Copy selected fields from section to lap. lap_field_values = {} [ :timestamp, :sport ].each do |f| lap_field_values[f] = field_values[f] if field_values.include?(f) end # Ensure that all previous records have been assigned to a lap. record = create_new_lap(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 'hrv' @hrv << (record = HRV.new(field_values)) 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.
327 328 329 |
# File 'lib/fit4ruby/Activity.rb', line 327 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.
335 336 337 |
# File 'lib/fit4ruby/Activity.rb', line 335 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.
343 344 345 |
# File 'lib/fit4ruby/Activity.rb', line 343 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.
317 318 319 |
# File 'lib/fit4ruby/Activity.rb', line 317 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.
296 297 298 |
# File 'lib/fit4ruby/Activity.rb', line 296 def new_user_profile(field_values = {}) new_fit_data_record('user_profile', field_values) end |
#recovery_hr ⇒ Object
Return the measured recovery heart rate.
189 190 191 192 193 194 195 |
# File 'lib/fit4ruby/Activity.rb', line 189 def recovery_hr @events.each do |e| return e.recovery_hr if e.event == 'recovery_hr' end nil end |
#recovery_info ⇒ Object
Returns the remaining recovery time at the start of the activity.
199 200 201 202 203 204 205 |
# File 'lib/fit4ruby/Activity.rb', line 199 def recovery_info @events.each do |e| return e.recovery_info if e.event == 'recovery_info' end nil end |
#recovery_time ⇒ Object
Returns the predicted recovery time needed after this activity.
209 210 211 212 213 214 215 |
# File 'lib/fit4ruby/Activity.rb', line 209 def recovery_time @events.each do |e| return e.recovery_time if e.event == 'recovery_time' end nil end |
#sport ⇒ Object
Returns the sport type of this activity.
234 235 236 |
# File 'lib/fit4ruby/Activity.rb', line 234 def sport @sessions[0].sport end |
#sub_sport ⇒ Object
Returns the sport subtype of this activity.
239 240 241 |
# File 'lib/fit4ruby/Activity.rb', line 239 def sub_sport @sessions[0].sub_sport end |
#total_distance ⇒ Object
Convenience method that aggregates all the distances from the included sessions.
114 115 116 117 118 |
# File 'lib/fit4ruby/Activity.rb', line 114 def total_distance d = 0.0 @sessions.each { |s| d += s.total_distance } d end |
#total_gps_distance ⇒ Object
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.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/fit4ruby/Activity.rb', line 123 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. 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 = 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 distance = Fit4Ruby::GeoMath.distance(last_lat, last_long, lat, long) d += distance end if speed = distance / (r. - ) end if timer_stops[0] == r. # 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 = nil timer_stops.shift else last_lat = lat last_long = long = r. end end end d end |
#vo2max ⇒ Object
Returns the computed VO2max value. This value is computed by the device based on multiple previous activities.
219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/fit4ruby/Activity.rb', line 219 def vo2max # First check the event log for a vo2max reporting event. @events.each do |e| return e.vo2max if e.event == 'vo2max' end # Then check the user_profile entries for a metmax entry. METmax * 3.5 # is same value as VO2max. @user_profiles.each do |u| return u.metmax * 3.5 if u.metmax end nil end |
#write(io, id_mapper) ⇒ Object
Write the Activity data to a file.
247 248 249 250 251 252 253 254 255 256 |
# File 'lib/fit4ruby/Activity.rb', line 247 def write(io, id_mapper) @file_id.write(io, id_mapper) @file_creator.write(io, id_mapper) (@device_infos + @data_sources + @user_profiles + @events + @sessions + @laps + @records + @personal_records).sort.each do |s| s.write(io, id_mapper) end super end |