The Encoder class allows you to construct FIT files. You can add messages using on_mesg() or write_mesg(), and then finalize the file with close().
on_mesg(mesg_num, data): Adds a message to the encoder. Unknown values in the data dictionary are ignored.write_mesg(data): Expects the mesg_num to be included inside the data dictionary. Internally calls on_mesg().close(): Returns the encoded data as a uint8_array (bytes).
Note: Subfield values and unknown custom fields in the message dictionary are ignored by the encoder.
from datetime import datetime, timezone
from garmin_fit_sdk import Encoder, Profile
encoder = Encoder()
# Using on_mesg
encoder.on_mesg(Profile['mesg_num']['FILE_ID'], {
'manufacturer': 'development',
'product': 1,
'time_created': datetime.now(tz=timezone.utc),
'type': 'activity',
})
# Using write_mesg (mesg_num must be in the dict)
encoder.write_mesg({
'mesg_num': Profile['mesg_num']['FILE_ID'],
'manufacturer': 'development',
'product': 1,
'time_created': datetime.now(tz=timezone.utc),
'type': 'activity',
})
uint8_array = encoder.close()
with open('example.fit', 'wb') as f:
f.write(uint8_array)
from datetime import datetime, timezone
from garmin_fit_sdk import Encoder, Profile
encoder = Encoder()
# Pass the MesgNum and message data as separate parameters to the onMesg() method
encoder.on_mesg(Profile['mesg_num']['FILE_ID'], {
'manufacturer': 'development',
'product': 1,
'time_created': datetime.now(tz=timezone.utc),
'type': 'activity',
})
# The writeMesg() method expects the mesgNum to be included in the message data
# Internally, writeMesg() calls onMesg()
encoder.write_mesg({
'mesg_num': Profile['mesg_num']['FILE_ID'],
'manufacturer': 'development',
'product': 1,
'time_created': datetime.now(tz=timezone.utc),
'type': 'activity',
})
# Unknown values in the message will be ignored by the Encoder
encoder.on_mesg(Profile['mesg_num']['FILE_ID'], {
'manufacturer': 'development',
'product': 1,
'time_created': datetime.now(tz=timezone.utc),
'type': 'activity',
'customField': 12345, # This value will be ignored by the Encoder
})
# Subfield values in the message will be ignored by the Encoder
encoder.on_mesg(Profile['mesg_num']['FILE_ID'], {
'manufacturer': 'development',
'product': 4440, # This is the main product field, which is a uint16
'garmin_product': 'edge_1050', # This value will be ignored by the Encoder, use the main field value instead
'time_created': datetime.now(tz=timezone.utc),
'type': 'activity',
})
uint8_array = encoder.close()
# Write the bytes to a file
with open('example.fit', 'wb') as f:
f.write(uint8_array)