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.



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

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:



115
116
117
# File 'lib/postrunner/FFS_Device.rb', line 115

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:

  • (FFS_Activity or FFS_Monitoring)

    Corresponding entry in the FitFileStore or nil if file could not be added.



52
53
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
# File 'lib/postrunner/FFS_Device.rb', line 52

def add_fit_file(fit_file_name, fit_entity, overwrite)
  case fit_entity.class
  when Fit4Ruby::Activity.class
    entity = activity_by_file_name(File.basename(fit_file_name))
    entities = @activities
    type = 'activity'
    new_entity_class = FFS_Activity
  when Fit4Ruby::Monitoring.class
    entity = monitoring_by_file_name(File.basename(fit_file_name))
    entities = @monitorings
    type = 'monitoring'
    new_entity_class = FFS_Monitoring
  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!

  # 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:



108
109
110
# File 'lib/postrunner/FFS_Device.rb', line 108

def delete_activity(activity)
  @activities.delete(activity)
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:



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

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

#restoreObject

Handle initialization of persistent attributes.



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

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