tcxkit 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +14 -0
- data/LICENSE +21 -0
- data/README.md +100 -0
- data/SPECIFICATION.md +323 -0
- data/lib/tcxkit/activity.rb +311 -0
- data/lib/tcxkit/error.rb +40 -0
- data/lib/tcxkit/field.rb +51 -0
- data/lib/tcxkit/limits.rb +63 -0
- data/lib/tcxkit/message.rb +63 -0
- data/lib/tcxkit/message_builder.rb +253 -0
- data/lib/tcxkit/reader.rb +108 -0
- data/lib/tcxkit/schema.rb +162 -0
- data/lib/tcxkit/types.rb +270 -0
- data/lib/tcxkit/version.rb +5 -0
- data/lib/tcxkit/xml/clock.rb +41 -0
- data/lib/tcxkit/xml/element.rb +60 -0
- data/lib/tcxkit/xml/parser.rb +193 -0
- data/lib/tcxkit/xml/source.rb +265 -0
- data/lib/tcxkit.rb +26 -0
- metadata +110 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TcxKit
|
|
4
|
+
# Turns the XML layer's element events into Messages (SPECIFICATION.md
|
|
5
|
+
# 6.2). A message is emitted when its element closes; every leaf element
|
|
6
|
+
# and attribute inside it, outside nested messages, becomes one of its
|
|
7
|
+
# Fields. One builder per pass.
|
|
8
|
+
class MessageBuilder
|
|
9
|
+
XSI_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance"
|
|
10
|
+
|
|
11
|
+
# Elements that are neither messages nor fields, by parent.
|
|
12
|
+
ROOT_CONTAINERS = %w[Activities Courses].freeze
|
|
13
|
+
SESSION_CONTAINERS = %w[FirstSport NextSport].freeze
|
|
14
|
+
|
|
15
|
+
# Schema keys of the Activity Extension v2 elements.
|
|
16
|
+
AX2_SEGMENTS = %w[TPX LX Speed RunCadence Watts AvgSpeed MaxBikeCadence AvgRunCadence MaxRunCadence Steps
|
|
17
|
+
AvgWatts MaxWatts Extensions].to_h { |name| [name, "ax2:#{name}".freeze] }.freeze
|
|
18
|
+
|
|
19
|
+
# Indexes counted across the document; the others count within their parent.
|
|
20
|
+
DOCUMENT_COUNTERS = %i[activity multi_sport_session course author element].freeze
|
|
21
|
+
|
|
22
|
+
# What a node in the element stack is: the root, a container, a message
|
|
23
|
+
# element (with its Frame) or a field element (with its Shape).
|
|
24
|
+
Node = Struct.new(:kind, :element, :frame, :shape)
|
|
25
|
+
|
|
26
|
+
# Where an element or attribute sits inside a message: its path, schema
|
|
27
|
+
# key (nil once a segment is outside the schema's namespaces), field name
|
|
28
|
+
# and type. Every trackpoint has the same few shapes, so they are made
|
|
29
|
+
# once per message type and reused; a builder makes at most SHAPE_LIMIT,
|
|
30
|
+
# because unknown names come from the file.
|
|
31
|
+
class Shape
|
|
32
|
+
attr_reader :path, :key, :name, :spec, :local_name
|
|
33
|
+
|
|
34
|
+
def initialize(path:, key:, name:, spec:, local_name:)
|
|
35
|
+
@path = path
|
|
36
|
+
@key = key
|
|
37
|
+
@name = name
|
|
38
|
+
@spec = spec
|
|
39
|
+
@local_name = local_name
|
|
40
|
+
@children = {}
|
|
41
|
+
@attributes = {}
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def child(namespace, name) = @children[namespace]&.[](name)
|
|
45
|
+
def attribute(namespace, name) = @attributes[namespace]&.[](name)
|
|
46
|
+
def add_child(namespace, name, shape) = (@children[namespace] ||= {})[name] = shape
|
|
47
|
+
def add_attribute(namespace, name, shape) = (@attributes[namespace] ||= {})[name] = shape
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
SHAPE_LIMIT = 10_000
|
|
51
|
+
|
|
52
|
+
# A message being collected.
|
|
53
|
+
class Frame
|
|
54
|
+
attr_reader :kind, :element, :context, :fields, :table, :shape
|
|
55
|
+
|
|
56
|
+
def initialize(kind, element, context, table, shape)
|
|
57
|
+
@kind = kind
|
|
58
|
+
@element = element
|
|
59
|
+
@context = context.freeze
|
|
60
|
+
@table = table
|
|
61
|
+
@shape = shape
|
|
62
|
+
@fields = []
|
|
63
|
+
@counters = Hash.new(0)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def next_index(kind)
|
|
67
|
+
index = @counters[kind]
|
|
68
|
+
@counters[kind] += 1
|
|
69
|
+
index
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def to_message
|
|
73
|
+
Message.new(name: kind, element_name: element.name, namespace: element.namespace, context: context,
|
|
74
|
+
line: element.line, column: element.column, fields: fields)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def initialize(&emit)
|
|
79
|
+
@emit = emit
|
|
80
|
+
@nodes = []
|
|
81
|
+
@counters = Hash.new(0)
|
|
82
|
+
@roots = {}.compare_by_identity
|
|
83
|
+
@shapes = 0
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def handle(type, element)
|
|
87
|
+
type == :start ? start(element) : finish(element)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
def start(element)
|
|
93
|
+
parent = @nodes.last
|
|
94
|
+
return @nodes << Node.new(:root, element, nil, nil) unless parent
|
|
95
|
+
|
|
96
|
+
kind = message_kind(element, parent)
|
|
97
|
+
if kind
|
|
98
|
+
@nodes << open_message(kind, element, parent.frame)
|
|
99
|
+
elsif container?(element, parent)
|
|
100
|
+
@nodes << Node.new(:container, element, parent.frame, nil)
|
|
101
|
+
elsif parent.frame
|
|
102
|
+
frame = parent.frame
|
|
103
|
+
node = Node.new(:field, element, frame, child_shape(parent.shape || frame.shape, element, frame.table))
|
|
104
|
+
add_attributes(node)
|
|
105
|
+
@nodes << node
|
|
106
|
+
else
|
|
107
|
+
@nodes << open_message(:element, element, nil)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def finish(element)
|
|
112
|
+
node = @nodes.pop
|
|
113
|
+
case node.kind
|
|
114
|
+
when :message then @emit.call(node.frame.to_message)
|
|
115
|
+
when :field then add_text(node, element) unless element.children?
|
|
116
|
+
when :root then @emit.call(document_message(element))
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def open_message(kind, element, parent_frame)
|
|
121
|
+
index = DOCUMENT_COUNTERS.include?(kind) ? next_document_index(kind) : parent_frame.next_index(kind)
|
|
122
|
+
context = (parent_frame ? parent_frame.context : {}).merge(kind => index)
|
|
123
|
+
frame = new_frame(kind, element, context, Schema.table(kind, context))
|
|
124
|
+
node = Node.new(:message, element, frame, frame.shape)
|
|
125
|
+
add_attributes(node)
|
|
126
|
+
node
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def new_frame(kind, element, context, table)
|
|
130
|
+
root = @roots[table] ||= Shape.new(path: "", key: "", name: nil, spec: nil, local_name: nil)
|
|
131
|
+
Frame.new(kind, element, context, table, root)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def next_document_index(kind)
|
|
135
|
+
index = @counters[kind]
|
|
136
|
+
@counters[kind] += 1
|
|
137
|
+
index
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# The shape of a child element: the parent's path and key joined with
|
|
141
|
+
# this element; a Value element takes its parent's name
|
|
142
|
+
# ("HeartRateBpm/Value" is heart_rate_bpm).
|
|
143
|
+
def child_shape(parent, element, table)
|
|
144
|
+
cached = parent.child(element.namespace, element.name)
|
|
145
|
+
return cached if cached
|
|
146
|
+
|
|
147
|
+
local = element.name
|
|
148
|
+
key = join_key(parent.key, segment(element))
|
|
149
|
+
named = local == "Value" && parent.local_name ? parent.local_name : local
|
|
150
|
+
shape = Shape.new(path: join(parent.path, local), key: key, name: Naming.snake_case(named).to_sym,
|
|
151
|
+
spec: (key && table[key]) || Types::UNKNOWN, local_name: local)
|
|
152
|
+
parent.add_child(element.namespace, local, shape) if remember?
|
|
153
|
+
shape
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def attribute_shape(parent, attribute, table)
|
|
157
|
+
cached = parent.attribute(attribute.namespace, attribute.name)
|
|
158
|
+
return cached if cached
|
|
159
|
+
|
|
160
|
+
key = join_key(parent.key, attribute_segment(attribute))
|
|
161
|
+
shape = Shape.new(path: join(parent.path, "@#{attribute.name}"), key: key,
|
|
162
|
+
name: Naming.snake_case(attribute.name).to_sym, spec: (key && table[key]) || Types::UNKNOWN,
|
|
163
|
+
local_name: nil)
|
|
164
|
+
parent.add_attribute(attribute.namespace, attribute.name, shape) if remember?
|
|
165
|
+
shape
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def remember?
|
|
169
|
+
(@shapes += 1) <= SHAPE_LIMIT
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def join(path, segment)
|
|
173
|
+
path.empty? ? segment.dup.freeze : "#{path}/#{segment}".freeze
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def join_key(key, segment)
|
|
177
|
+
key && segment && join(key, segment)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Which message an element starts, if any (only TCX v2 elements, where
|
|
181
|
+
# the schema puts them).
|
|
182
|
+
def message_kind(element, parent)
|
|
183
|
+
return nil unless element.namespace == NAMESPACE
|
|
184
|
+
|
|
185
|
+
parent_kind = parent.kind == :message ? parent.frame.kind : nil
|
|
186
|
+
parent_container = parent.kind == :container ? parent.element.name : nil
|
|
187
|
+
|
|
188
|
+
case element.name
|
|
189
|
+
when "Activity" then :activity if %w[Activities FirstSport NextSport].include?(parent_container)
|
|
190
|
+
when "MultiSportSession" then :multi_sport_session if parent_container == "Activities"
|
|
191
|
+
when "Transition" then :transition if parent_container == "NextSport"
|
|
192
|
+
when "Lap" then :lap if %i[activity course].include?(parent_kind)
|
|
193
|
+
when "Track" then :track if %i[lap transition course].include?(parent_kind)
|
|
194
|
+
when "Trackpoint" then :trackpoint if parent_kind == :track
|
|
195
|
+
when "Creator" then :creator if %i[activity course].include?(parent_kind)
|
|
196
|
+
when "Author" then :author if parent.kind == :root
|
|
197
|
+
when "Course" then :course if parent_container == "Courses"
|
|
198
|
+
when "CoursePoint" then :course_point if parent_kind == :course
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def container?(element, parent)
|
|
203
|
+
return false unless element.namespace == NAMESPACE
|
|
204
|
+
|
|
205
|
+
case parent.kind
|
|
206
|
+
when :root then ROOT_CONTAINERS.include?(element.name)
|
|
207
|
+
when :message then parent.frame.kind == :multi_sport_session && SESSION_CONTAINERS.include?(element.name)
|
|
208
|
+
else false
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def add_attributes(node)
|
|
213
|
+
node.element.attributes.each do |attribute|
|
|
214
|
+
shape = attribute_shape(node.shape, attribute, node.frame.table)
|
|
215
|
+
node.frame.fields << Field.new(name: shape.name, path: shape.path, namespace: attribute.namespace,
|
|
216
|
+
text: attribute.value, spec: shape.spec, line: node.element.line)
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# An empty Extensions is not a field, and neither is the missing text of
|
|
221
|
+
# an element that only carries attributes (such as <TPX CadenceSensor="Bike"/>).
|
|
222
|
+
def add_text(node, element)
|
|
223
|
+
return if element.name == "Extensions" && element.text.strip.empty?
|
|
224
|
+
return if element.text.empty? && !element.attributes.empty?
|
|
225
|
+
|
|
226
|
+
shape = node.shape
|
|
227
|
+
node.frame.fields << Field.new(name: shape.name, path: shape.path, namespace: element.namespace,
|
|
228
|
+
text: element.text, spec: shape.spec, line: element.line)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# The schema key of an element, or nil for a namespace the schema does
|
|
232
|
+
# not cover here.
|
|
233
|
+
def segment(element)
|
|
234
|
+
case element.namespace
|
|
235
|
+
when NAMESPACE then element.name
|
|
236
|
+
when ACTIVITY_EXTENSION_NAMESPACE then AX2_SEGMENTS[element.name] || "ax2:#{element.name}"
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def attribute_segment(attribute)
|
|
241
|
+
case attribute.namespace
|
|
242
|
+
when nil then "@#{attribute.name}"
|
|
243
|
+
when XSI_NAMESPACE then "@xsi:#{attribute.name}"
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def document_message(element)
|
|
248
|
+
frame = new_frame(:document, element, {}, Schema::EMPTY)
|
|
249
|
+
add_attributes(Node.new(:message, element, frame, frame.shape))
|
|
250
|
+
frame.to_message
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TcxKit
|
|
4
|
+
# Streams the messages of a TCX v2 document.
|
|
5
|
+
#
|
|
6
|
+
# TcxKit::Reader.new("activity.tcx").each_message { |message| ... }
|
|
7
|
+
#
|
|
8
|
+
# source is a path, a Pathname or an IO-like object that responds to read.
|
|
9
|
+
# Caller-owned IO is closed only with close_io: true. A seekable IO is read
|
|
10
|
+
# from its position at construction on every pass; a non-seekable one can
|
|
11
|
+
# be read once.
|
|
12
|
+
#
|
|
13
|
+
# limits: a TcxKit::Limits, or a Hash of its keywords. Limits::DEFAULT
|
|
14
|
+
# unless given; Limits.none switches them off.
|
|
15
|
+
#
|
|
16
|
+
# Each message is yielded when its element closes, so a lap's trackpoints
|
|
17
|
+
# come before the lap and an activity's laps before the activity; the
|
|
18
|
+
# last message is :document. See Message and SPECIFICATION.md 6.2.
|
|
19
|
+
class Reader
|
|
20
|
+
attr_reader :source_label, :limits
|
|
21
|
+
|
|
22
|
+
def initialize(source, close_io: false, source_label: nil, limits: Limits::DEFAULT)
|
|
23
|
+
@close_io = close_io
|
|
24
|
+
@limits = Limits.coerce(limits)
|
|
25
|
+
@consumed = false
|
|
26
|
+
@closed_by_reader = false
|
|
27
|
+
|
|
28
|
+
# Pathname responds to read (it reads the whole file), so paths come first.
|
|
29
|
+
if source.is_a?(String) || source.is_a?(Pathname)
|
|
30
|
+
@path = source.to_s
|
|
31
|
+
@source_label = source_label || @path
|
|
32
|
+
elsif source.respond_to?(:read)
|
|
33
|
+
@io = source
|
|
34
|
+
@source_label = source_label || (source.respond_to?(:path) ? source.path : source.class.name)
|
|
35
|
+
@initial_position = initial_position(source)
|
|
36
|
+
raise ArgumentError, "close_io requires a closeable IO-like source" if @close_io && !source.respond_to?(:close)
|
|
37
|
+
elsif source.respond_to?(:to_path)
|
|
38
|
+
@path = source.to_path
|
|
39
|
+
@source_label = source_label || @path
|
|
40
|
+
else
|
|
41
|
+
raise ArgumentError, "source must be a filesystem path or an IO-like object"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def each_message(&block)
|
|
46
|
+
return enum_for(__method__) unless block
|
|
47
|
+
|
|
48
|
+
io, owned = open_io
|
|
49
|
+
begin
|
|
50
|
+
parser = XML::Parser.new(io, limits: @limits, source_label: @source_label)
|
|
51
|
+
builder = MessageBuilder.new { |message| parser.outside { block.call(message) } }
|
|
52
|
+
parser.each_event do |type, element|
|
|
53
|
+
check_root(element) if type == :start && element.depth == 1
|
|
54
|
+
builder.handle(type, element)
|
|
55
|
+
end
|
|
56
|
+
ensure
|
|
57
|
+
@closed_by_reader = true if @close_io && @io.equal?(io)
|
|
58
|
+
io.close if (owned || @close_io) && io.respond_to?(:close) && !(io.respond_to?(:closed?) && io.closed?)
|
|
59
|
+
end
|
|
60
|
+
nil
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def check_root(element)
|
|
66
|
+
unless element.name == "TrainingCenterDatabase"
|
|
67
|
+
raise UnsupportedFormatError.new("The root element is #{short(element.name)}, not TrainingCenterDatabase",
|
|
68
|
+
source: @source_label, line: element.line, column: element.column)
|
|
69
|
+
end
|
|
70
|
+
return if element.namespace == NAMESPACE
|
|
71
|
+
|
|
72
|
+
message =
|
|
73
|
+
if element.namespace == NAMESPACE_V1 then "TCX v1 is not supported"
|
|
74
|
+
else "TrainingCenterDatabase is not in the TCX v2 namespace"
|
|
75
|
+
end
|
|
76
|
+
raise UnsupportedFormatError.new(message, source: @source_label, line: element.line, column: element.column)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def short(name)
|
|
80
|
+
name.length > 64 ? "#{name[0, 64]}..." : name
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def initial_position(io)
|
|
84
|
+
return unless io.respond_to?(:pos) && io.respond_to?(:seek)
|
|
85
|
+
|
|
86
|
+
io.pos
|
|
87
|
+
rescue IOError, Errno::ESPIPE
|
|
88
|
+
nil
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def open_io
|
|
92
|
+
return [File.open(@path, "rb"), true] if @path
|
|
93
|
+
|
|
94
|
+
if @closed_by_reader || (@io.respond_to?(:closed?) && @io.closed?)
|
|
95
|
+
raise Error.new("The IO source is closed", source: @source_label)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
if @initial_position
|
|
99
|
+
@io.seek(@initial_position)
|
|
100
|
+
elsif @consumed
|
|
101
|
+
raise Error.new("A non-seekable IO source can only be read once", source: @source_label)
|
|
102
|
+
else
|
|
103
|
+
@consumed = true
|
|
104
|
+
end
|
|
105
|
+
[@io, false]
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TcxKit
|
|
4
|
+
# Field types per message, written from Garmin's published schemas
|
|
5
|
+
# TrainingCenterDatabase v2 and ActivityExtension v2 (type names in
|
|
6
|
+
# comments). The schemas themselves are not part of this repository.
|
|
7
|
+
#
|
|
8
|
+
# Keys are paths relative to the message element. A segment in the TCX v2
|
|
9
|
+
# namespace is its local name, one in the Activity Extension v2 namespace
|
|
10
|
+
# is "ax2:" plus its local name, an attribute is "@" plus its name (an
|
|
11
|
+
# xsi attribute "@xsi:" plus its name). A path with a segment in any other
|
|
12
|
+
# namespace never matches, so an element is only typed where the schema
|
|
13
|
+
# puts it.
|
|
14
|
+
module Schema
|
|
15
|
+
T = Types
|
|
16
|
+
|
|
17
|
+
DEGREES_LATITUDE = T.double(:degrees_latitude, min: -90.0, max: 90.0) # DegreesLatitude_t
|
|
18
|
+
DEGREES_LONGITUDE = T.double(:degrees_longitude, min: -180.0, max_exclusive: 180.0) # DegreesLongitude_t
|
|
19
|
+
POSITIVE_BYTE = T.integer(:positive_byte, min: 1, max: 255) # HeartRateInBeatsPerMinute_t/Value
|
|
20
|
+
CADENCE_VALUE = T.integer(:cadence_value, min: 0, max: 254) # CadenceValue_t
|
|
21
|
+
UNSIGNED_SHORT = T.integer(:unsigned_short, min: 0, max: 65_535)
|
|
22
|
+
UNSIGNED_INT = T.integer(:unsigned_int, min: 0, max: 4_294_967_295)
|
|
23
|
+
|
|
24
|
+
RESTRICTED_TOKEN = T.token(:restricted_token, min_length: 1, max_length: 15) # RestrictedToken_t
|
|
25
|
+
COURSE_POINT_NAME = T.token(:course_point_name, min_length: 1, max_length: 10) # CoursePointName_t
|
|
26
|
+
LANG_ID = T.token(:lang_id, length: 2) # LangID_t
|
|
27
|
+
PART_NUMBER = T.token(:part_number, pattern: '[\p{Lu}\d]{3}-[\p{Lu}\d]{5}-[\p{Lu}\d]{2}') # PartNumber_t
|
|
28
|
+
|
|
29
|
+
SPORT = T.enumeration(:sport, %w[Running Biking Other])
|
|
30
|
+
INTENSITY = T.enumeration(:intensity, %w[Active Resting])
|
|
31
|
+
TRIGGER_METHOD = T.enumeration(:trigger_method, %w[Manual Distance Location Time HeartRate])
|
|
32
|
+
SENSOR_STATE = T.enumeration(:sensor_state, %w[Present Absent])
|
|
33
|
+
CADENCE_SENSOR_TYPE = T.enumeration(:cadence_sensor_type, %w[Footpod Bike])
|
|
34
|
+
BUILD_TYPE = T.enumeration(:build_type, %w[Internal Alpha Beta Release])
|
|
35
|
+
TRAINING_TYPE = T.enumeration(:training_type, %w[Workout Course])
|
|
36
|
+
COURSE_POINT_TYPE = T.enumeration(:course_point_type, [
|
|
37
|
+
"Generic", "Summit", "Valley", "Water", "Food", "Danger", "Left", "Right", "Straight", "First Aid",
|
|
38
|
+
"4th Category", "3rd Category", "2nd Category", "1st Category", "Hors Category", "Sprint"
|
|
39
|
+
])
|
|
40
|
+
|
|
41
|
+
VERSION = {
|
|
42
|
+
"VersionMajor" => UNSIGNED_SHORT, "VersionMinor" => UNSIGNED_SHORT,
|
|
43
|
+
"BuildMajor" => UNSIGNED_SHORT, "BuildMinor" => UNSIGNED_SHORT
|
|
44
|
+
}.freeze
|
|
45
|
+
|
|
46
|
+
def self.within(prefix, table)
|
|
47
|
+
table.transform_keys { |key| "#{prefix}/#{key}" }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Trackpoint_t, with ActivityTrackpointExtension_t (TPX).
|
|
51
|
+
TRACKPOINT = {
|
|
52
|
+
"Time" => T.date_time,
|
|
53
|
+
"Position/LatitudeDegrees" => DEGREES_LATITUDE,
|
|
54
|
+
"Position/LongitudeDegrees" => DEGREES_LONGITUDE,
|
|
55
|
+
"AltitudeMeters" => T.double,
|
|
56
|
+
"DistanceMeters" => T.double,
|
|
57
|
+
"HeartRateBpm/Value" => POSITIVE_BYTE,
|
|
58
|
+
"Cadence" => CADENCE_VALUE,
|
|
59
|
+
"SensorState" => SENSOR_STATE,
|
|
60
|
+
"Extensions/ax2:TPX/ax2:Speed" => T.double,
|
|
61
|
+
"Extensions/ax2:TPX/ax2:RunCadence" => CADENCE_VALUE,
|
|
62
|
+
"Extensions/ax2:TPX/ax2:Watts" => UNSIGNED_SHORT,
|
|
63
|
+
"Extensions/ax2:TPX/@CadenceSensor" => CADENCE_SENSOR_TYPE
|
|
64
|
+
}.freeze
|
|
65
|
+
|
|
66
|
+
# ActivityLap_t, with ActivityLapExtension_t (LX).
|
|
67
|
+
ACTIVITY_LAP = {
|
|
68
|
+
"@StartTime" => T.date_time,
|
|
69
|
+
"TotalTimeSeconds" => T.double,
|
|
70
|
+
"DistanceMeters" => T.double,
|
|
71
|
+
"MaximumSpeed" => T.double,
|
|
72
|
+
"Calories" => UNSIGNED_SHORT,
|
|
73
|
+
"AverageHeartRateBpm/Value" => POSITIVE_BYTE,
|
|
74
|
+
"MaximumHeartRateBpm/Value" => POSITIVE_BYTE,
|
|
75
|
+
"Intensity" => INTENSITY,
|
|
76
|
+
"Cadence" => CADENCE_VALUE,
|
|
77
|
+
"TriggerMethod" => TRIGGER_METHOD,
|
|
78
|
+
"Notes" => T.string,
|
|
79
|
+
"Extensions/ax2:LX/ax2:AvgSpeed" => T.double,
|
|
80
|
+
"Extensions/ax2:LX/ax2:MaxBikeCadence" => CADENCE_VALUE,
|
|
81
|
+
"Extensions/ax2:LX/ax2:AvgRunCadence" => CADENCE_VALUE,
|
|
82
|
+
"Extensions/ax2:LX/ax2:MaxRunCadence" => CADENCE_VALUE,
|
|
83
|
+
"Extensions/ax2:LX/ax2:Steps" => UNSIGNED_SHORT,
|
|
84
|
+
"Extensions/ax2:LX/ax2:AvgWatts" => UNSIGNED_SHORT,
|
|
85
|
+
"Extensions/ax2:LX/ax2:MaxWatts" => UNSIGNED_SHORT
|
|
86
|
+
}.freeze
|
|
87
|
+
|
|
88
|
+
# CourseLap_t.
|
|
89
|
+
COURSE_LAP = {
|
|
90
|
+
"TotalTimeSeconds" => T.double,
|
|
91
|
+
"DistanceMeters" => T.double,
|
|
92
|
+
"BeginPosition/LatitudeDegrees" => DEGREES_LATITUDE,
|
|
93
|
+
"BeginPosition/LongitudeDegrees" => DEGREES_LONGITUDE,
|
|
94
|
+
"BeginAltitudeMeters" => T.double,
|
|
95
|
+
"EndPosition/LatitudeDegrees" => DEGREES_LATITUDE,
|
|
96
|
+
"EndPosition/LongitudeDegrees" => DEGREES_LONGITUDE,
|
|
97
|
+
"EndAltitudeMeters" => T.double,
|
|
98
|
+
"AverageHeartRateBpm/Value" => POSITIVE_BYTE,
|
|
99
|
+
"MaximumHeartRateBpm/Value" => POSITIVE_BYTE,
|
|
100
|
+
"Intensity" => INTENSITY,
|
|
101
|
+
"Cadence" => CADENCE_VALUE
|
|
102
|
+
}.freeze
|
|
103
|
+
|
|
104
|
+
# Activity_t, with Training_t, QuickWorkout_t and Plan_t.
|
|
105
|
+
ACTIVITY = {
|
|
106
|
+
"@Sport" => SPORT,
|
|
107
|
+
"Id" => T.date_time,
|
|
108
|
+
"Notes" => T.string,
|
|
109
|
+
"Training/@VirtualPartner" => T.boolean,
|
|
110
|
+
"Training/QuickWorkoutResults/TotalTimeSeconds" => T.double,
|
|
111
|
+
"Training/QuickWorkoutResults/DistanceMeters" => T.double,
|
|
112
|
+
"Training/Plan/@Type" => TRAINING_TYPE,
|
|
113
|
+
"Training/Plan/@IntervalWorkout" => T.boolean,
|
|
114
|
+
"Training/Plan/Name" => RESTRICTED_TOKEN
|
|
115
|
+
}.freeze
|
|
116
|
+
|
|
117
|
+
# AbstractSource_t, as Device_t or Application_t (named by xsi:type).
|
|
118
|
+
SOURCE = {
|
|
119
|
+
"@xsi:type" => T.token,
|
|
120
|
+
"Name" => T.token,
|
|
121
|
+
"UnitId" => UNSIGNED_INT,
|
|
122
|
+
"ProductID" => UNSIGNED_SHORT,
|
|
123
|
+
"Build/Type" => BUILD_TYPE,
|
|
124
|
+
"Build/Time" => T.token,
|
|
125
|
+
"Build/Builder" => T.token,
|
|
126
|
+
"LangID" => LANG_ID,
|
|
127
|
+
"PartNumber" => PART_NUMBER
|
|
128
|
+
}.merge(within("Version", VERSION), within("Build/Version", VERSION)).freeze
|
|
129
|
+
|
|
130
|
+
# MultiSportSession_t.
|
|
131
|
+
MULTI_SPORT_SESSION = { "Id" => T.date_time, "Notes" => T.string }.freeze
|
|
132
|
+
|
|
133
|
+
# Course_t and CoursePoint_t.
|
|
134
|
+
COURSE = { "Name" => RESTRICTED_TOKEN, "Notes" => T.string }.freeze
|
|
135
|
+
COURSE_POINT = {
|
|
136
|
+
"Name" => COURSE_POINT_NAME,
|
|
137
|
+
"Time" => T.date_time,
|
|
138
|
+
"Position/LatitudeDegrees" => DEGREES_LATITUDE,
|
|
139
|
+
"Position/LongitudeDegrees" => DEGREES_LONGITUDE,
|
|
140
|
+
"AltitudeMeters" => T.double,
|
|
141
|
+
"PointType" => COURSE_POINT_TYPE,
|
|
142
|
+
"Notes" => T.string
|
|
143
|
+
}.freeze
|
|
144
|
+
|
|
145
|
+
EMPTY = {}.freeze
|
|
146
|
+
|
|
147
|
+
# The table for a message kind; laps differ inside a course.
|
|
148
|
+
def self.table(kind, context)
|
|
149
|
+
case kind
|
|
150
|
+
when :trackpoint then TRACKPOINT
|
|
151
|
+
when :lap then context.key?(:course) ? COURSE_LAP : ACTIVITY_LAP
|
|
152
|
+
when :transition then ACTIVITY_LAP
|
|
153
|
+
when :activity then ACTIVITY
|
|
154
|
+
when :creator, :author then SOURCE
|
|
155
|
+
when :multi_sport_session then MULTI_SPORT_SESSION
|
|
156
|
+
when :course then COURSE
|
|
157
|
+
when :course_point then COURSE_POINT
|
|
158
|
+
else EMPTY
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|