Quickstart
This section provides some examples, demonstrating what the library is capable of.
Iterating over the messages in a FIT file
The following code example will
- Iterate over each message in the FIT file.
- Print the message type (this describes what kind of information the message contains)
- Iterate over each field in the message and print out
def parser = new FitParser()
List<DataMessage> messages = parser.parse(new File("fit/fit.fit"))
// Iterate over each of the data messages in the FIT file
messages.each {message ->
// Print the data message name (for example, 'record' or 'file_id')
println message.getType()
// Iterate over the fields
message.fields.each { field ->
// Print the name and value of each field in the data message `message`
println "\t ${field.getKey()} : ${field.getValue()}"
}
}
Formatting the data messages
The same output can be achieved by using a formatter.
new FitParser().parse(new File("fit/fit.fit"))each {message->
println new TextFITFormatter().formatDataMessage(message)
}
Printing out the units
The unit symbol is printed out next to the field value.
new FitParser().parse("fit/fit.fit").each {message ->
message.fields.each { field ->
// Look up the unit symbol in the hashmap
String unitSymbol = message.unitSymbols[field.getKey()]
println "\t ${field.getKey()} : ${field.getValue()} (${unitSymbol})"
}
}
Converting the units
The following code will convert the units in the following circumstances
- The field has the unit ‘semicircle’ (in which case it will be convertd to degrees)
- The field has the unit ‘metres’ (in which case it will be converted to miles)
- The field is called ‘altitude’ (in which case it will be converted to feet, not miles)
HashMap<Unit, Unit> unitPolicy = [
(Unit.SEMICIRCLE) : Unit.DEGREE,
(Unit.METRE) : Unit.MILE,
]
HashMap<String, Unit> fieldPolicy = [
altitude: Unit.FEET
]
def converter = new MessageConverter(new ConversionPolicy(fieldPolicy, unitPolicy))
new FitParser().parse(new File("fit/fit.fit")).each { message ->
println message.getType()
DataMessage converted = converter.convertMessage(message)
converted.fields.each{ field ->
def unit = converted.unitSymbols[field.getKey()]
println "\t ${field.getKey()}: ${field.getValue()} (${unit})"
}
}