Class: PostRunner::FFS_Activity
- Inherits:
-
PEROBS::Object
- Object
- PEROBS::Object
- PostRunner::FFS_Activity
- Includes:
- DirUtils
- Defined in:
- lib/postrunner/FFS_Activity.rb
Overview
The FFS_Activity objects can store a reference to the FIT file data and caches some frequently used values. In some cases the cached values can be used to overwrite the data from the FIT file.
Constant Summary collapse
- ActivityTypes =
{ 'generic' => 'Generic', 'running' => 'Running', 'cycling' => 'Cycling', 'transition' => 'Transition', 'fitness_equipment' => 'Fitness Equipment', 'swimming' => 'Swimming', 'basketball' => 'Basketball', 'soccer' => 'Soccer', 'tennis' => 'Tennis', 'american_football' => 'American Football', 'walking' => 'Walking', 'cross_country_skiing' => 'Cross Country Skiing', 'alpine_skiing' => 'Alpine Skiing', 'snowboarding' => 'Snowboarding', 'rowing' => 'Rowing', 'mountaineering' => 'Mountaneering', 'hiking' => 'Hiking', 'multisport' => 'Multisport', 'paddling' => 'Paddling', 'commuting' => 'Commuting', 'all' => 'All' }
- ActivitySubTypes =
{ 'generic' => 'Generic', 'treadmill' => 'Treadmill', 'street' => 'Street', 'trail' => 'Trail', 'track' => 'Track', 'spin' => 'Spin', 'indoor_cycling' => 'Indoor Cycling', 'road' => 'Road', 'mountain' => 'Mountain', 'downhill' => 'Downhill', 'recumbent' => 'Recumbent', 'cyclocross' => 'Cyclocross', 'hand_cycling' => 'Hand Cycling', 'track_cycling' => 'Track Cycling', 'indoor_rowing' => 'Indoor Rowing', 'elliptical' => 'Elliptical', 'stair_climbing' => 'Stair Climbing', 'lap_swimming' => 'Lap Swimming', 'open_water' => 'Open Water', 'flexibility_training' => 'Flexibility Training', 'strength_training' => 'Strength Training', 'warm_up' => 'Warm up', 'match' => 'Match', 'exercise' => 'Excersize', 'challenge' => 'Challenge', 'indoor_skiing' => 'Indoor Skiing', 'cardio_training' => 'Cardio Training', 'virtual_activity' => 'Virtual Activity', 'all' => 'All' }
- @@Schemata =
{ 'long_date' => Schema.new('long_date', 'Date', { :func => 'timestamp', :column_alignment => :left, :format => 'date_with_weekday' }), 'sub_type' => Schema.new('sub_type', 'Subtype', { :func => 'activity_sub_type', :column_alignment => :left }), 'type' => Schema.new('type', 'Type', { :func => 'activity_type', :column_alignment => :left }) }
Instance Attribute Summary collapse
-
#fit_activity ⇒ Object
readonly
Returns the value of attribute fit_activity.
Instance Method Summary collapse
-
#<=>(a) ⇒ Object
FFS_Activity objects are sorted by their timestamp values and then by their device long_uids.
- #activity_sub_type ⇒ Object
- #activity_type ⇒ Object
- #check ⇒ Object
- #distance(timestamp, unit_system) ⇒ Object
- #dump(filter) ⇒ Object
- #events ⇒ Object
- #generate_html_report ⇒ Object
-
#has_records? ⇒ Boolean
Return true if this activity generated any personal records.
- #html_file_name(full_path = true) ⇒ Object
-
#initialize(p, device, fit_file_name, fit_entity) ⇒ FFS_Activity
constructor
Create a new FFS_Activity object.
- #load_fit_file(filter = nil) ⇒ Object
- #purge_fit_file ⇒ Object
- #query(key) ⇒ Object
- #set(attribute, value) ⇒ Object
- #show ⇒ Object
- #sources ⇒ Object
-
#store_fit_file(fit_file_name) ⇒ Object
Store a copy of the given FIT file in the corresponding directory.
- #summary ⇒ Object
Methods included from DirUtils
Constructor Details
#initialize(p, device, fit_file_name, fit_entity) ⇒ FFS_Activity
Create a new FFS_Activity object.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/postrunner/FFS_Activity.rb', line 111 def initialize(p, device, fit_file_name, fit_entity) super(p) self.device = device self.fit_file_name = fit_file_name ? File.basename(fit_file_name) : nil self.name = fit_file_name ? File.basename(fit_file_name) : nil self.norecord = false if (@fit_activity = fit_entity) self. = fit_entity. self.total_timer_time = fit_entity.total_timer_time self.sport = fit_entity.sport self.sub_sport = fit_entity.sub_sport self.total_distance = fit_entity.total_distance self.avg_speed = fit_entity.avg_speed end end |
Instance Attribute Details
#fit_activity ⇒ Object (readonly)
Returns the value of attribute fit_activity.
103 104 105 |
# File 'lib/postrunner/FFS_Activity.rb', line 103 def fit_activity @fit_activity end |
Instance Method Details
#<=>(a) ⇒ Object
FFS_Activity objects are sorted by their timestamp values and then by their device long_uids.
147 148 149 150 |
# File 'lib/postrunner/FFS_Activity.rb', line 147 def <=>(a) == a. ? a.device.long_uid <=> self.device.long_uid : a. <=> end |
#activity_sub_type ⇒ Object
256 257 258 |
# File 'lib/postrunner/FFS_Activity.rb', line 256 def activity_sub_type ActivitySubTypes[@sub_sport] || "Undefined #{@sub_sport}" end |
#activity_type ⇒ Object
252 253 254 |
# File 'lib/postrunner/FFS_Activity.rb', line 252 def activity_type ActivityTypes[@sport] || 'Undefined' end |
#check ⇒ Object
152 153 154 155 |
# File 'lib/postrunner/FFS_Activity.rb', line 152 def check generate_html_report Log.info "FIT file #{@fit_file_name} is OK" end |
#distance(timestamp, unit_system) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/postrunner/FFS_Activity.rb', line 260 def distance(, unit_system) load_fit_file @fit_activity.records.each do |record| if record. >= unit = { :metric => 'km', :statute => 'mi'}[unit_system] value = record.get_as('distance', unit) return '-' unless value return "#{'%.2f %s' % [value, unit]}" end end '-' end |
#dump(filter) ⇒ Object
157 158 159 |
# File 'lib/postrunner/FFS_Activity.rb', line 157 def dump(filter) load_fit_file(filter) end |
#events ⇒ Object
180 181 182 183 |
# File 'lib/postrunner/FFS_Activity.rb', line 180 def events load_fit_file puts EventList.new(self, @store['config']['unit_system'].to_sym).to_s end |
#generate_html_report ⇒ Object
247 248 249 250 |
# File 'lib/postrunner/FFS_Activity.rb', line 247 def generate_html_report load_fit_file ActivityView.new(self, @store['config']['unit_system'].to_sym) end |
#has_records? ⇒ Boolean
Return true if this activity generated any personal records.
238 239 240 |
# File 'lib/postrunner/FFS_Activity.rb', line 238 def has_records? !@store['records'].activity_records(self).empty? end |
#html_file_name(full_path = true) ⇒ Object
242 243 244 245 |
# File 'lib/postrunner/FFS_Activity.rb', line 242 def html_file_name(full_path = true) fn = "#{@device.short_uid}_#{@fit_file_name[0..-5]}.html" full_path ? File.join(@store['config']['html_dir'], fn) : fn end |
#load_fit_file(filter = nil) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/postrunner/FFS_Activity.rb', line 275 def load_fit_file(filter = nil) return if @fit_activity dir = @store['file_store'].fit_file_dir(@fit_file_name, @device.long_uid, 'activity') fit_file = File.join(dir, @fit_file_name) begin @fit_activity = Fit4Ruby.read(fit_file, filter) rescue Fit4Ruby::Error Log.fatal "#{@fit_file_name} corrupted: #{$!}" end unless @fit_activity Log.fatal "#{fit_file} does not contain any activity records" end end |
#purge_fit_file ⇒ Object
292 293 294 |
# File 'lib/postrunner/FFS_Activity.rb', line 292 def purge_fit_file @fit_activity = nil end |
#query(key) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/postrunner/FFS_Activity.rb', line 161 def query(key) unless @@Schemata.include?(key) raise ArgumentError, "Unknown key '#{key}' requested in query" end schema = @@Schemata[key] if schema.func value = send(schema.func) else unless instance_variable_defined?(key) raise ArgumentError, "Don't know how to query '#{key}'" end value = instance_variable_get(key) end QueryResult.new(value, schema) end |
#set(attribute, value) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/postrunner/FFS_Activity.rb', line 206 def set(attribute, value) case attribute when 'name' self.name = value when 'note' self.note = value when 'type' load_fit_file unless ActivityTypes.values.include?(value) Log.fatal "Unknown activity type '#{value}'. Must be one of " + ActivityTypes.values.join(', ') end self.sport = ActivityTypes.invert[value] when 'subtype' unless ActivitySubTypes.values.include?(value) Log.fatal "Unknown activity subtype '#{value}'. Must be one of " + ActivitySubTypes.values.join(', ') end self.sub_sport = ActivitySubTypes.invert[value] when 'norecord' unless %w( true false).include?(value) Log.fatal "norecord must either be 'true' or 'false'" end self.norecord = value == 'true' else Log.fatal "Unknown activity attribute '#{attribute}'. Must be one of " + 'name, type or subtype' end generate_html_report end |
#show ⇒ Object
185 186 187 188 189 190 191 |
# File 'lib/postrunner/FFS_Activity.rb', line 185 def show html_file = html_file_name generate_html_report #unless File.exists?(html_file) @store['file_store'].show_in_browser(html_file) end |
#sources ⇒ Object
193 194 195 196 |
# File 'lib/postrunner/FFS_Activity.rb', line 193 def sources load_fit_file puts DataSources.new(self, @store['config']['unit_system'].to_sym).to_s end |
#store_fit_file(fit_file_name) ⇒ Object
Store a copy of the given FIT file in the corresponding directory.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/postrunner/FFS_Activity.rb', line 130 def store_fit_file(fit_file_name) # Get the right target directory for this particular FIT file. dir = @store['file_store'].fit_file_dir(File.basename(fit_file_name), @device.long_uid, 'activity') # Create the necessary directories if they don't exist yet. create_directory(dir, 'Device activity diretory') # Copy the file into the target directory. begin FileUtils.cp(fit_file_name, dir) rescue StandardError Log.fatal "Cannot copy #{fit_file_name} into #{dir}: #{$!}" end end |
#summary ⇒ Object
198 199 200 201 202 203 204 |
# File 'lib/postrunner/FFS_Activity.rb', line 198 def summary load_fit_file puts ActivitySummary.new(self, @store['config']['unit_system'].to_sym, { :name => @name, :type => activity_type, :sub_type => activity_sub_type }).to_s end |