diff options
-rw-r--r-- | Rakefile | 10 | ||||
-rw-r--r-- | lib/postrunner/ActivitiesDB.rb | 8 | ||||
-rw-r--r-- | lib/postrunner/Activity.rb | 25 | ||||
-rw-r--r-- | lib/postrunner/ActivityView.rb | 13 | ||||
-rw-r--r-- | spec/PostRunner_spec.rb | 55 |
5 files changed, 80 insertions, 31 deletions
@@ -1,6 +1,16 @@ require "bundler/gem_tasks" require "rspec/core/rake_task" +# Add the include path for the fit4ruby library. We assume it is located in +# the same directory as the postrunner directory. +fit4ruby = File.realpath(File.join(File.dirname(__FILE__), '..', + 'fit4ruby', 'lib')) +if ENV['RUBYLIB'] + ENV['RUBYLIB'] += ":#{fit4ruby}" +else + ENV['RUBYLIB'] = fit4ruby +end + RSpec::Core::RakeTask.new task :default => :spec diff --git a/lib/postrunner/ActivitiesDB.rb b/lib/postrunner/ActivitiesDB.rb index e4f8b6b..759bec6 100644 --- a/lib/postrunner/ActivitiesDB.rb +++ b/lib/postrunner/ActivitiesDB.rb @@ -46,11 +46,11 @@ module PostRunner Log.fatal "The archive file '#{@archive_file}' is corrupted" end - # The reference to this object is needed inside Activity object but is - # not stored in the archive file. We have to retrofit the Activity - # instances with this data. + # Not all instance variables of Activity are stored in the file. The + # normal constructor is not run during YAML::load_file. We have to + # initialize those instance variables in a secondary step. @activities.each do |a| - a.db = self + a.late_init(self) end @records = PersonalRecords.new(self) diff --git a/lib/postrunner/Activity.rb b/lib/postrunner/Activity.rb index 5a7607f..3fe0bfd 100644 --- a/lib/postrunner/Activity.rb +++ b/lib/postrunner/Activity.rb @@ -19,7 +19,7 @@ module PostRunner class Activity - attr_reader :fit_file, :name, :fit_activity + attr_reader :fit_file, :name, :fit_activity, :html_dir, :html_file attr_accessor :db # This is a list of variables that provide data from the fit file. To @@ -28,10 +28,10 @@ module PostRunner avg_speed ) def initialize(db, fit_file, fit_activity, name = nil) - @db = db @fit_file = fit_file @fit_activity = fit_activity @name = name || fit_file + late_init(db) @@CachedVariables.each do |v| v_str = "@#{v}" @@ -40,9 +40,17 @@ module PostRunner end end + def late_init(db) + @db = db + @html_dir = File.join(@db.db_dir, 'html') + @html_file = File.join(@html_dir, "#{@fit_file[0..-5]}.html") + end + def check @fit_activity = load_fit_file Log.info "FIT file #{@fit_file} is OK" + # Re-generate the HTML file for this activity + ActivityView.new(self) end def dump(filter) @@ -74,11 +82,14 @@ module PostRunner def show @fit_activity = load_fit_file unless @fit_activity - view = ActivityView.new(self, File.join(@db.db_dir, 'html')) - #view = TrackView.new(self, '../../html') - #view.generate_html - #chart = ChartView.new(self, '../../html') - #chart.generate_html + + ActivityView.new(self) unless File.exists?(@html_file) + + cmd = "#{ENV['BROWSER'] || 'firefox'} \"#{@html_file}\" &" + unless system(cmd) + Log.fatal "Failed to execute the following shell command: #{$cmd}\n" + + "#{$!}" + end end def summary diff --git a/lib/postrunner/ActivityView.rb b/lib/postrunner/ActivityView.rb index 4abed85..43e8461 100644 --- a/lib/postrunner/ActivityView.rb +++ b/lib/postrunner/ActivityView.rb @@ -24,9 +24,9 @@ module PostRunner include ViewWidgets - def initialize(activity, output_dir) + def initialize(activity) @activity = activity - @output_dir = output_dir + @output_dir = activity.html_dir @output_file = nil ensure_output_dir @@ -34,7 +34,6 @@ module PostRunner @doc = HTMLBuilder.new generate_html(@doc) write_file - show_in_browser end private @@ -143,14 +142,6 @@ EOT end end - def show_in_browser - cmd = "#{ENV['BROWSER'] || 'firefox'} \"#{@output_file}\" &" - unless system(cmd) - Log.fatal "Failed to execute the following shell command: #{$cmd}\n" + - "#{$!}" - end - end - end end diff --git a/spec/PostRunner_spec.rb b/spec/PostRunner_spec.rb index ebacc5e..d3ab129 100644 --- a/spec/PostRunner_spec.rb +++ b/spec/PostRunner_spec.rb @@ -26,20 +26,57 @@ describe PostRunner::Main do end def create_fit_file(name, date) - a = Fit4Ruby::Activity.new - a.timestamp = Time.parse(date) + ts = Time.parse(date) + a = Fit4Ruby::Activity.new({ :timestamp => ts }) a.total_timer_time = 30 * 60 - 0.upto(30) do |mins| - r = a.new_record('record') - r.timestamp = a.timestamp + mins * 60 - r.distance = 200.0 * mins - r.cadence = 75 + a.new_user_profile({ :timestamp => ts, + :age => 33, :height => 1.78, :weight => 73.0, + :gender => 'male', :activity_class => 4.0, + :max_hr => 178 }) + + a.new_event({ :timestamp => ts, :event => 'timer', + :event_type => 'start_time' }) + a.new_device_info({ :timestamp => ts, :device_index => 0 }) + a.new_device_info({ :timestamp => ts, :device_index => 1, + :battery_status => 'ok' }) + 0.upto(a.total_timer_time / 60) do |mins| + ts += 60 + a.new_record({ + :timestamp => ts, + :position_lat => 51.5512 - mins * 0.0008, + :position_long => 11.647 + mins * 0.002, + :distance => 200.0 * mins, + :altitude => 100 + mins * 0.5, + :speed => 3.1, + :vertical_oscillation => 9 + mins * 0.02, + :stance_time => 235.0 * mins * 0.01, + :stance_time_percent => 32.0, + :heart_rate => 140 + mins, + :cadence => 75, + :activity_type => 'running', + :fractional_cadence => (mins % 2) / 2.0 + }) if mins > 0 && mins % 5 == 0 - s = a.new_record('laps') + a.new_lap({ :timestamp => ts }) end end - a.new_record('session') + a.new_session({ :timestamp => ts }) + a.new_event({ :timestamp => ts, :event => 'recovery_time', + :event_type => 'marker', + :data => 2160 }) + a.new_event({ :timestamp => ts, :event => 'vo2max', + :event_type => 'marker', :data => 52 }) + a.new_event({ :timestamp => ts, :event => 'timer', + :event_type => 'stop_all' }) + a.new_device_info({ :timestamp => ts, :device_index => 0 }) + ts += 1 + a.new_device_info({ :timestamp => ts, :device_index => 1, + :battery_status => 'low' }) + ts += 120 + a.new_event({ :timestamp => ts, :event => 'recovery_hr', + :event_type => 'marker', :data => 132 }) + a.aggregate Fit4Ruby.write(name, a) end |