Method: PostRunner::FitFileStore#add_fit_file

Defined in:
lib/postrunner/FitFileStore.rb

#add_fit_file(fit_file_name, fit_entity = nil, overwrite = false) ⇒ FFS_Activity or FFS_Monitoring

Add a file to the store.

Parameters:

  • fit_file_name (String)

    Name of the FIT file

  • overwrite (TrueClass, FalseClass) (defaults to: false)

    If true, an existing file will be replaced.

Returns:



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/postrunner/FitFileStore.rb', line 82

def add_fit_file(fit_file_name, fit_entity = nil, overwrite = false)
  md5sum = FitFileStore.calc_md5_sum(fit_file_name)
  if @store['fit_file_md5sums'].include?(md5sum)
    # The FIT file is already stored in the DB.
    return nil unless overwrite
  end

  # If we the file hasn't been read yet, read it in as a
  # Fit4Ruby::Activity or Fit4Ruby::Monitoring entity.
  unless fit_entity
    return nil unless (fit_entity = read_fit_file(fit_file_name))
  end

  unless [ Fit4Ruby::Activity,
           Fit4Ruby::Monitoring_B ].include?(fit_entity.class)
    Log.fatal "Unsupported FIT file type #{fit_entity.class}"
  end

  # Generate a String that uniquely identifies the device that generated
  # the FIT file.
  id = extract_fit_file_id(fit_entity)
  long_uid = "#{id[:manufacturer]}-#{id[:product]}-#{id[:serial_number]}"

  # Make sure the device that created the FIT file is properly registered.
  device = register_device(long_uid)
  # Store the FIT entity with the device.
  entity = device.add_fit_file(fit_file_name, fit_entity, overwrite)

  # The FIT file might be already stored or invalid. In that case we
  # abort this method.
  return nil unless entity

  if fit_entity.is_a?(Fit4Ruby::Activity)
    @store['records'].scan_activity_for_records(entity)

    # Generate HTML file for this activity.
    entity.generate_html_report

    # The HTML activity views contain links to their predecessors and
    # successors. After inserting a new activity, we need to re-generate
    # these views as well.
    if (pred = predecessor(entity))
      pred.generate_html_report
    end
    if (succ = successor(entity))
      succ.generate_html_report
    end
    # And update the index pages
    generate_html_index_pages
  end

  Log.info "#{File.basename(fit_file_name)} " +
           'has been successfully added to archive'

  entity
end