diff options
author | Chris Schlaeger <chris@linux.com> | 2015-09-12 19:02:12 +0200 |
---|---|---|
committer | Chris Schlaeger <chris@linux.com> | 2015-09-12 19:02:12 +0200 |
commit | ccc542fb3d2e339debef5ed5195217ab1f7232a4 (patch) | |
tree | f638639664717139780cddbe1cc00c938f8afb8e /lib | |
parent | d01d0ee8539f952ba74588ff88ee80da0f058c4e (diff) | |
download | postrunner-ccc542fb3d2e339debef5ed5195217ab1f7232a4.zip |
Refactor FIT file reader to prepare for other FIT formats
Diffstat (limited to 'lib')
-rw-r--r-- | lib/postrunner/ActivitiesDB.rb | 43 | ||||
-rw-r--r-- | lib/postrunner/Main.rb | 29 |
2 files changed, 49 insertions, 23 deletions
diff --git a/lib/postrunner/ActivitiesDB.rb b/lib/postrunner/ActivitiesDB.rb index 929f8bd..67e98c0 100644 --- a/lib/postrunner/ActivitiesDB.rb +++ b/lib/postrunner/ActivitiesDB.rb @@ -85,40 +85,39 @@ module PostRunner end end - # Add a new FIT file to the database. + # Check if the fit file can be added. If it's already there or was deleted + # before, it's not welcome. # @param fit_file [String] Name of the FIT file. - # @return [TrueClass or FalseClass] True if the file could be added. False + # @return [TrueClass or FalseClass] True if the file can be added. False # otherwise. - def add(fit_file) - base_fit_file = File.basename(fit_file) - if @activities.find { |a| a.fit_file == base_fit_file } - Log.debug "Activity #{fit_file} is already included in the archive" + def fit_file_welcome?(fit_file_name) + base_fit_file_name = File.basename(fit_file_name) + if @activities.find { |a| a.fit_file == base_fit_file_name } + Log.debug "Activity #{fit_file_name} is already included in the archive" return false end - if File.exists?(File.join(@fit_dir, base_fit_file)) - Log.debug "Activity #{fit_file} has been deleted before" + if File.exists?(File.join(@fit_dir, base_fit_file_name)) + Log.debug "Activity #{fit_file_name} has been deleted before" return false end - begin - fit_activity = Fit4Ruby.read(fit_file) - rescue Fit4Ruby::Error - Log.error $! - return false - end - unless fit_activity - Log.error "#{fit_file} does not contain any activity records" - return false - end + true + end + # Add a new FIT file to the database. + # @param fit_file [String] Name of the FIT file. + # @return [TrueClass or FalseClass] True if the file could be added. False + # otherwise. + def add(fit_file_name, fit_activity) begin - FileUtils.cp(fit_file, @fit_dir) + FileUtils.cp(fit_file_name, @fit_dir) rescue StandardError - Log.fatal "Cannot copy #{fit_file} into #{@fit_dir}: #{$!}" + Log.fatal "Cannot copy #{fit_file_name} into #{@fit_dir}: #{$!}" end - @activities << (activity = Activity.new(self, base_fit_file, + @activities << (activity = Activity.new(self, + File.basename(fit_file_name), fit_activity)) @activities.sort! do |a1, a2| a2.timestamp <=> a1.timestamp @@ -140,7 +139,7 @@ module PostRunner end sync - Log.info "#{fit_file} successfully added to archive" + Log.info "#{fit_file_name} successfully added to archive" true end diff --git a/lib/postrunner/Main.rb b/lib/postrunner/Main.rb index 617f278..f72d3f5 100644 --- a/lib/postrunner/Main.rb +++ b/lib/postrunner/Main.rb @@ -301,17 +301,44 @@ EOT end end + # Process a single FIT file according to the given command. + # @param file [String] File name of a FIT file + # @param command [Symbol] Processing instruction + # @return [TrueClass, FalseClass] true if command was successful, false + # otherwise def process_file(file, command) case command when :check, :dump read_fit_file(file) when :import - @activities.add(file) + import_fit_file(file) else Log.fatal("Unknown file command #{command}") end end + # Import the given FIT file. + # @param fit_file_name [String] File name of the FIT file + # @return [TrueClass, FalseClass] true if file was successfully imported, + # false otherwise + def import_fit_file(fit_file_name) + begin + fit_entity = Fit4Ruby.read(fit_file_name) + rescue Fit4Ruby::Error + Log.error $! + return false + end + + if fit_entity.is_a?(Fit4Ruby::Activity) + return @activities.fit_file_welcome?(fit_file_name) && + @activities.add(fit_file_name, fit_entity) + elsif fit_entity.is_a?(Fit4Ruby::Monitoring_B) + else + Log.error "#{fit_file_name} is not a recognized FIT file" + return false + end + end + def process_activity(activity, command) case command when :check |