diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/postrunner/ActivitiesDB.rb | 65 | ||||
-rw-r--r-- | lib/postrunner/Activity.rb | 21 | ||||
-rw-r--r-- | lib/postrunner/ActivityReport.rb | 2 | ||||
-rw-r--r-- | lib/postrunner/Main.rb | 6 |
4 files changed, 69 insertions, 25 deletions
diff --git a/lib/postrunner/ActivitiesDB.rb b/lib/postrunner/ActivitiesDB.rb index e0c61e4..a5a4b8d 100644 --- a/lib/postrunner/ActivitiesDB.rb +++ b/lib/postrunner/ActivitiesDB.rb @@ -3,6 +3,7 @@ require 'yaml' require 'fit4ruby' require 'postrunner/Activity' +require 'postrunner/PersonalRecords' require 'postrunner/FlexiTable' module PostRunner @@ -19,18 +20,14 @@ module PostRunner @archive_file = File.join(@db_dir, 'archive.yml') create_directories - if Dir.exists?(@db_dir) - begin - if File.exists?(@archive_file) - @activities = YAML.load_file(@archive_file) - else - @activities = [] - end - rescue - Log.fatal "Cannot load archive file '#{@archive_file}': #{$!}" + begin + if File.exists?(@archive_file) + @activities = YAML.load_file(@archive_file) + else + @activities = [] end - else - @activities = [] + rescue StandardError + Log.fatal "Cannot load archive file '#{@archive_file}': #{$!}" end unless @activities.is_a?(Array) @@ -43,6 +40,8 @@ module PostRunner @activities.each do |a| a.db = self end + + @records = PersonalRecords.new(self) end def add(fit_file) @@ -66,14 +65,18 @@ module PostRunner begin FileUtils.cp(fit_file, @fit_dir) - rescue + rescue StandardError Log.fatal "Cannot copy #{fit_file} into #{@fit_dir}: #{$!}" end - @activities << Activity.new(self, base_fit_file, fit_activity) + @activities << (activity = Activity.new(self, base_fit_file, + fit_activity)) @activities.sort! do |a1, a2| - a2.start_time <=> a1.start_time + a2.timestamp <=> a1.timestamp end + + activity.register_records(@records) + sync Log.info "#{fit_file} successfully added to archive" @@ -94,6 +97,20 @@ module PostRunner @activities.each { |a| a.check } end + def ref_by_fit_file(fit_file) + i = 1 + @activities.each do |activity| + return i if activity.fit_file == fit_file + i += 1 + end + + nil + end + + def activity_by_fit_file(fit_file) + @activities.find { |a| a.fit_file == fit_file } + end + def find(query) case query when /\A-?\d+$\z/ @@ -172,18 +189,28 @@ module PostRunner t.row([ i += 1, a.name[0..19], - a.start_time.strftime("%a, %Y %b %d %H:%M"), - "%.2f" % (a.distance / 1000), - secsToHMS(a.duration), + a.timestamp.strftime("%a, %Y %b %d %H:%M"), + "%.2f" % (a.total_distance / 1000), + secsToHMS(a.total_timer_time), speedToPace(a.avg_speed) ]) end puts t.to_s end + def show_records + puts @records.to_s + end + private def sync - File.open(@archive_file, 'w') { |f| f.write(@activities.to_yaml) } + begin + File.open(@archive_file, 'w') { |f| f.write(@activities.to_yaml) } + rescue StandardError + Log.fatal "Cannot write archive file '#{@archive_file}': #{$!}" + end + + @records.sync end def create_directories @@ -197,7 +224,7 @@ module PostRunner Log.info "Creating #{name} directory #{dir}" begin Dir.mkdir(dir) - rescue + rescue StandardError Log.fatal "Cannot create #{name} directory #{dir}: #{$!}" end end diff --git a/lib/postrunner/Activity.rb b/lib/postrunner/Activity.rb index a37c2de..0ded82b 100644 --- a/lib/postrunner/Activity.rb +++ b/lib/postrunner/Activity.rb @@ -6,12 +6,13 @@ module PostRunner class Activity - attr_reader :fit_file, :name + attr_reader :fit_file, :name, :fit_activity attr_accessor :db # This is a list of variables that provide data from the fit file. To # speed up access to it, we cache the data in the activity database. - @@CachedVariables = %w( start_time distance duration avg_speed ) + @@CachedVariables = %w( timestamp total_distance total_timer_time + avg_speed ) def initialize(db, fit_file, fit_activity, name = nil) @db = db @@ -27,12 +28,12 @@ module PostRunner end def check - load_fit_file + @fit_activity = load_fit_file Log.info "FIT file #{@fit_file} is OK" end def dump(filter) - load_fit_file(filter) + @fit_activity = load_fit_file(filter) end def yaml_initialize(tag, value) @@ -67,6 +68,18 @@ module PostRunner @name = name end + def register_records(db) + @fit_activity.personal_records.each do |r| + if r.longest_distance == 1 + # In case longest_distance is 1 the distance is stored in the + # duration field in 10-th of meters. + db.register_result(r.duration * 10.0 , 0, r.start_time, @fit_file) + else + db.register_result(r.distance, r.duration, r.start_time, @fit_file) + end + end + end + private def load_fit_file(filter = nil) diff --git a/lib/postrunner/ActivityReport.rb b/lib/postrunner/ActivityReport.rb index 60ceef1..4c328c9 100644 --- a/lib/postrunner/ActivityReport.rb +++ b/lib/postrunner/ActivityReport.rb @@ -23,7 +23,7 @@ module PostRunner t = FlexiTable.new t.enable_frame(false) t.body - t.row([ 'Date:', session.start_time]) + t.row([ 'Date:', session.timestamp]) t.row([ 'Distance:', "#{'%.2f' % (session.total_distance / 1000.0)} km" ]) t.row([ 'Time:', secsToHMS(session.total_timer_time) ]) t.row([ 'Avg. Pace:', diff --git a/lib/postrunner/Main.rb b/lib/postrunner/Main.rb index 3ec3c70..fadb89a 100644 --- a/lib/postrunner/Main.rb +++ b/lib/postrunner/Main.rb @@ -3,7 +3,6 @@ require 'logger' require 'fit4ruby' require 'postrunner/version' -require 'postrunner/RuntimeConfig' require 'postrunner/ActivitiesDB' module PostRunner @@ -115,6 +114,9 @@ delete <ref> list List all FIT files stored in the data base. +records + List all personal records. + rename <ref> Replace the FIT file name with a more meaningful name that describes the activity. @@ -147,6 +149,8 @@ EOT process_files(args, :import) when 'list' @activities.list + when 'records' + @activities.show_records when 'rename' process_activities(args, :rename) when 'summary' |