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.



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.



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.



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 53

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
  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.



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.



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

#monitorings(from_time, to_time) ⇒ Array

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



131
132
133
134
135
136
137
138
139
140
# File 'lib/postrunner/FFS_Device.rb', line 131

def monitorings(from_time, to_time)
  list = []
  @monitorings.each do |m|
    if (from_time <= m.period_start && m.period_start < to_time) ||
       (from_time <= m.period_end && m.period_end < to_time)
      list << m
    end
  end
  list
end

#restoreObject

Handle initialization of persistent attributes.



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

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