Class: PostRunner::FFS_Device

Inherits:
PEROBS::Object
  • Object
show all
Defined in:
lib/postrunner/FFS_Device.rb

Overview

Objects of this class can store the activities and monitoring data of a specific device. The device gets a random number assigned as a unique but anonymous ID. It also gets a long ID assigned that is a String of the manufacturer, the product name and the serial number concatenated by dashes. All objects are transparently stored in the PEROBS::Store.

Instance Method Summary collapse

Constructor Details

#initialize(p, short_uid, long_uid) ⇒ FFS_Device

Create a new FFS_Device object.

Parameters:

  • p (PEROBS::Handle)

    p

  • short_uid (Fixnum)

    A random number used a unique ID

  • long_uid (String)

    A string consisting of the manufacturer and product name and the serial number.



33
34
35
36
37
38
# File 'lib/postrunner/FFS_Device.rb', line 33

def initialize(p, short_uid, long_uid)
  super(p)
  self.short_uid = short_uid
  self.long_uid = long_uid
  restore
end

Instance Method Details

#activity_by_file_name(file_name) ⇒ FFS_Activity

Return the activity with the given file name.

Parameters:

  • file_name (String)

    Base name of the fit file.

Returns:



128
129
130
# File 'lib/postrunner/FFS_Device.rb', line 128

def activity_by_file_name(file_name)
  @activities.find { |a| a.fit_file_name == file_name }
end

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

Add a new FIT file for this device.

Parameters:

  • fit_file_name (String)

    The full path to the FIT file

  • fit_entity (Fit4Ruby::FitEntity)

    The content of the FIT file

  • overwrite (Boolean)

    A flag to indicate if an existing file should be replaced with the new one.

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
# File 'lib/postrunner/FFS_Device.rb', line 54

def add_fit_file(fit_file_name, fit_entity, overwrite)
  if fit_entity.is_a?(Fit4Ruby::Activity)
    entity = activity_by_file_name(File.basename(fit_file_name))
    entities = @activities
    type = 'activity'
    new_entity_class = FFS_Activity
  elsif fit_entity.is_a?(Fit4Ruby::Monitoring_B)
    entity = monitoring_by_file_name(File.basename(fit_file_name))
    entities = @monitorings
    type = 'monitoring'
    new_entity_class = FFS_Monitoring
  elsif fit_entity.is_a?(Fit4Ruby::Metrics)
    entity = metrics_by_file_name(File.basename(fit_file_name))
    entities = @metrics
    type = 'metrics'
    new_entity_class = FFS_Metrics
  else
    Log.fatal "Unsupported FIT entity #{fit_entity.class}"
  end

  if entity
    if overwrite
      # Replace the old file. All meta-information will be lost.
      entities.delete_if { |e| e.fit_file_name == fit_file_name }
      entity = @store.new(new_entity_class, myself, fit_file_name,
                          fit_entity)
    else
      Log.debug "FIT file #{fit_file_name} has already been imported"
      # Refuse to replace the file.
      return nil
    end
  else
    # Don't add the entity if has deleted before and overwrite isn't true.
    path = @store['file_store'].fit_file_dir(File.basename(fit_file_name),
                                             long_uid, type)
    fq_fit_file_name = File.join(path, File.basename(fit_file_name))
    if File.exists?(fq_fit_file_name) && !overwrite
      Log.debug "FIT file #{fq_fit_file_name} has already been imported " +
                "and deleted"
      return nil
    end
    # Add the new file to the list.
    entity = @store.new(new_entity_class, myself, fit_file_name, fit_entity)
  end
  entity.store_fit_file(fit_file_name)
  entities << entity
  entities.sort!

  md5sums = @store['fit_file_md5sums']
  md5sums << FitFileStore.calc_md5_sum(fit_file_name)
  # We only store the 512 most recently added FIT files. This should be
  # more than a device can store. This will allow us to skip the already
  # imported FIT files quickly instead of having to parse them each time.
  md5sums.shift if md5sums.length > 512

  # Scan the activity for any potential new personal records and register
  # them.
  if entity.is_a?(FFS_Activity)
    records = @store['records']
    records.scan_activity_for_records(entity, true)
  end

  entity
end

#delete_activity(activity) ⇒ Object

Delete the given activity from the activity list.

Parameters:



121
122
123
# File 'lib/postrunner/FFS_Device.rb', line 121

def delete_activity(activity)
  @activities.delete(activity)
end

#metrics_by_file_name(file_name) ⇒ FFS_Activity

Return the metrics with the given file name.

Parameters:

  • file_name (String)

    Base name of the fit file.

Returns:



142
143
144
# File 'lib/postrunner/FFS_Device.rb', line 142

def metrics_by_file_name(file_name)
  @metrics.find { |a| a.fit_file_name == file_name }
end

#monitoring_by_file_name(file_name) ⇒ FFS_Activity

Return the monitoring with the given file name.

Parameters:

  • file_name (String)

    Base name of the fit file.

Returns:



135
136
137
# File 'lib/postrunner/FFS_Device.rb', line 135

def monitoring_by_file_name(file_name)
  @monitorings.find { |a| a.fit_file_name == file_name }
end

#monitorings(from_time, to_time) ⇒ Array

Return all monitorings that overlap with the time interval given by from_time and to_time.

Parameters:

  • from_time (Time)

    start time of the interval

  • to_time (Time)

    end time of the interval (not included)

Returns:

  • (Array)

    list of overlapping FFS_Monitoring objects.



151
152
153
154
155
156
# File 'lib/postrunner/FFS_Device.rb', line 151

def monitorings(from_time, to_time)
  @monitorings.select do |m|
    (from_time <= m.period_start && m.period_start < to_time) ||
      (from_time <= m.period_end && m.period_end < to_time)
  end
end

#restoreObject

Handle initialization of persistent attributes.



41
42
43
44
45
# File 'lib/postrunner/FFS_Device.rb', line 41

def restore
  attr_init(:activities) { @store.new(PEROBS::Array) }
  attr_init(:monitorings) { @store.new(PEROBS::Array) }
  attr_init(:metrics) { @store.new(PEROBS::Array) }
end