Class: Fit4Ruby::FitDataRecord
Direct Known Subclasses
Activity, DataSources, DeveloperDataId, DeviceInfo, EPO_Data, Event, FieldDescription, FileCreator, FileId, HRV, HeartRateZones, Lap, Length, Metrics, Monitoring, MonitoringInfo, Monitoring_B, PersonalRecords, PhysiologicalMetrics, Record, SensorSettings, Session, Software, TrainingStatus, UserData, UserProfile, Workout, WorkoutStep
Constant Summary
collapse
- RecordOrder =
['user_data', 'user_profile', 'workout', 'workout_set',
'device_info', 'data_sources', 'event',
'record', 'lap', 'length', 'session', 'heart_rate_zones',
'personal_records']
BDFieldNameTranslator::BD_DICT
Constants included
from Converters
Converters::FACTORS, Converters::OFFSETS
Instance Attribute Summary collapse
Instance Method Summary
collapse
#to_bd_field_name
Methods included from Converters
#conversion_factor, #conversion_offset, #fit_time_to_time, #secsToDHMS, #secsToHM, #secsToHMS, #speedToPace, #time_to_fit_time
Constructor Details
Returns a new instance of FitDataRecord.
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/fit4ruby/FitDataRecord.rb', line 29
def initialize(record_id)
@message = GlobalFitMessages.find_by_name(record_id)
@message.fields_by_name.each do |name, _|
create_instance_variable(name)
end
@meta_field_units = {}
@timestamp = Time.now
end
|
Instance Attribute Details
#message ⇒ Object
Returns the value of attribute message.
27
28
29
|
# File 'lib/fit4ruby/FitDataRecord.rb', line 27
def message
@message
end
|
#timestamp ⇒ Object
Returns the value of attribute timestamp.
27
28
29
|
# File 'lib/fit4ruby/FitDataRecord.rb', line 27
def timestamp
@timestamp
end
|
Instance Method Details
#<=>(other) ⇒ Object
101
102
103
104
105
106
|
# File 'lib/fit4ruby/FitDataRecord.rb', line 101
def <=>(other)
@timestamp == other.timestamp ?
RecordOrder.index(@message.name) <=>
RecordOrder.index(other.message.name) :
@timestamp <=> other.timestamp
end
|
#==(other) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/fit4ruby/FitDataRecord.rb', line 86
def ==(other)
@message.each_field(field_values_as_hash) do |_, field|
ivar_name = "@#{field.name}"
v1 = field.native_to_fit(instance_variable_get(ivar_name))
v2 = field.native_to_fit(other.instance_variable_get(ivar_name))
return false unless v1 == v2
end
true
end
|
#export ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/fit4ruby/FitDataRecord.rb', line 175
def export
message = {
'message' => @message.name,
'number' => @message.number,
'fields' => {}
}
@message.each_field(field_values_as_hash) do |number, field|
ivar_name = "@#{field.name}"
fit_value = field.native_to_fit(instance_variable_get(ivar_name))
unless field.is_undefined?(fit_value)
fld = {
'number' => number,
'value' => field.fit_to_native(fit_value),
'type' => field.type
}
fld['unit'] = field.opts[:unit] if field.opts[:unit]
fld['scale'] = field.opts[:scale] if field.opts[:scale]
message['fields'][field.name] = fld
end
end
message
end
|
#get(name) ⇒ Object
61
62
63
64
65
66
67
|
# File 'lib/fit4ruby/FitDataRecord.rb', line 61
def get(name)
ivar_name = "@#{name}"
return nil unless instance_variable_defined?(ivar_name)
instance_variable_get(ivar_name)
end
|
#get_as(name, to_unit) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/fit4ruby/FitDataRecord.rb', line 69
def get_as(name, to_unit)
value = respond_to?(name) ? send(name) : get(name)
return nil if value.nil?
return value if to_unit.nil? || to_unit.empty?
if @meta_field_units.include?(name)
unit = @meta_field_units[name]
else
unless (unit = get_unit_by_name(name))
Log.fatal "Field #{name} has no unit"
end
end
conversion_offset(unit, to_unit) + value * conversion_factor(unit, to_unit)
end
|
#get_unit_by_name(name) ⇒ Object
202
203
204
205
|
# File 'lib/fit4ruby/FitDataRecord.rb', line 202
def get_unit_by_name(name)
field = @message.fields_by_name[name]
field.opts[:unit]
end
|
#set(name, value) ⇒ Object
51
52
53
54
55
56
57
58
59
|
# File 'lib/fit4ruby/FitDataRecord.rb', line 51
def set(name, value)
ivar_name = "@#{name}"
unless instance_variable_defined?(ivar_name)
Log.debug("Unknown FIT record field '#{name}' in global message " \
"#{@message.name} (#{@message.number}).")
return
end
instance_variable_set(ivar_name, value)
end
|
#set_field_values(field_values) ⇒ Object
45
46
47
48
49
|
# File 'lib/fit4ruby/FitDataRecord.rb', line 45
def set_field_values(field_values)
field_values.each do |field, value|
set(field.to_s, value)
end
end
|
#write(io, id_mapper) ⇒ Object
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/fit4ruby/FitDataRecord.rb', line 108
def write(io, id_mapper)
global_fit_message = @message.construct(field_values = field_values_as_hash)
unless (local_message_number = id_mapper.get_local(global_fit_message))
local_message_number = id_mapper.add_global(global_fit_message)
global_fit_message.write(io, local_message_number)
end
= FitRecordHeader.new
.normal = 0
.message_type = 0
.local_message_type = local_message_number
.write(io)
fields = []
values = {}
global_fit_message.each_field(field_values) do |_, field|
bin_data_type = FitDefinitionFieldBase.fit_type_to_bin_data(field.type)
field_name = to_bd_field_name(field.name)
field_def = [bin_data_type, field_name]
iv = "@#{field.name}"
if instance_variable_defined?(iv) &&
!(iv_value = instance_variable_get(iv)).nil?
values[field.name] = field.native_to_fit(iv_value)
else
value = FitDefinitionFieldBase.undefined_value(field.type)
values[field.name] = field.opts[:array] ? [value] :
field.type == 'string' ? '' : value
end
if field.type == 'string'
values[field.name] += "\0"
elsif field.opts[:array]
field_def = [:array, field_name,
{ type: bin_data_type,
initial_length: values[field.name].size }]
end
fields << field_def
end
bd = BinData::Struct.new(endian: :little, fields: fields)
global_fit_message.each_field(field_values) do |_, field|
bd[to_bd_field_name(field.name)] = values[field.name]
end
bd.write(io)
end
|