-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: #584 change fit-tool to garmin-fit-sdk #590
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f5a22a
fix: 584 change fit-tool to garmin-fit-sdk
ben-29 b10727c
feat: garmin - handle gpx(if exist) when sync with fit
ben-29 e785f9c
fix import error
NaturezzZ 92bf138
doc: SEMICIRCLE
ben-29 909100a
fix: wrap_device_info using fit-tool
ben-29 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ stravaweblib | |
tenacity | ||
numpy | ||
tzlocal | ||
fit-tool | ||
garmin-fit-sdk | ||
haversine==2.8.0 | ||
garth | ||
pycryptodome | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,14 +12,8 @@ | |
import lxml | ||
import polyline | ||
import s2sphere as s2 | ||
from fit_tool.fit_file import FitFile | ||
from fit_tool.profile.messages.activity_message import ActivityMessage | ||
from fit_tool.profile.messages.device_info_message import DeviceInfoMessage | ||
from fit_tool.profile.messages.file_id_message import FileIdMessage | ||
from fit_tool.profile.messages.record_message import RecordMessage | ||
from fit_tool.profile.messages.session_message import SessionMessage | ||
from fit_tool.profile.messages.software_message import SoftwareMessage | ||
from fit_tool.profile.profile_type import Sport | ||
from garmin_fit_sdk import Decoder, Stream | ||
from garmin_fit_sdk.util import FIT_EPOCH_S | ||
from polyline_processor import filter_out | ||
from rich import print | ||
from tcxreader.tcxreader import TCXReader | ||
|
@@ -31,6 +25,7 @@ | |
run_map = namedtuple("polyline", "summary_polyline") | ||
|
||
IGNORE_BEFORE_SAVING = os.getenv("IGNORE_BEFORE_SAVING", False) | ||
SEMICIRCLE = 11930465 | ||
|
||
|
||
class Track: | ||
|
@@ -91,9 +86,12 @@ def load_fit(self, file_name): | |
# (for example, treadmill runs pulled via garmin-connect-export) | ||
if os.path.getsize(file_name) == 0: | ||
raise TrackLoadError("Empty FIT file") | ||
|
||
fit = FitFile.from_file(file_name) | ||
self._load_fit_data(fit) | ||
stream = Stream.from_file(file_name) | ||
decoder = Decoder(stream) | ||
messages, errors = decoder.read(convert_datetimes_to_dates=False) | ||
if len(errors) > 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if errors: |
||
print(f"FIT file read fail: {errors}") | ||
self._load_fit_data(messages) | ||
except Exception as e: | ||
print( | ||
f"Something went wrong when loading FIT. for file {self.file_names[0]}, we just ignore this file and continue" | ||
|
@@ -223,59 +221,55 @@ def _load_gpx_data(self, gpx): | |
) | ||
self.moving_dict = self._get_moving_data(gpx) | ||
|
||
def _load_fit_data(self, fit: FitFile): | ||
def _load_fit_data(self, fit: dict): | ||
_polylines = [] | ||
self.polyline_container = [] | ||
message = fit["session_mesgs"][0] | ||
self.start_time = datetime.datetime.utcfromtimestamp( | ||
(message["start_time"] + FIT_EPOCH_S) | ||
) | ||
self.run_id = self.__make_run_id(self.start_time) | ||
self.end_time = datetime.datetime.utcfromtimestamp( | ||
(message["start_time"] + FIT_EPOCH_S + message["total_elapsed_time"]) | ||
) | ||
self.length = message["total_distance"] | ||
self.average_heartrate = ( | ||
message["avg_heart_rate"] if "avg_heart_rate" in message else None | ||
) | ||
self.type = message["sport"].lower() | ||
|
||
for record in fit.records: | ||
message = record.message | ||
|
||
if isinstance(message, RecordMessage): | ||
if message.position_lat and message.position_long: | ||
_polylines.append( | ||
s2.LatLng.from_degrees( | ||
message.position_lat, message.position_long | ||
) | ||
) | ||
self.polyline_container.append( | ||
[message.position_lat, message.position_long] | ||
) | ||
elif isinstance(message, SessionMessage): | ||
self.start_time = datetime.datetime.utcfromtimestamp( | ||
message.start_time / 1000 | ||
) | ||
self.run_id = message.start_time | ||
self.end_time = datetime.datetime.utcfromtimestamp( | ||
(message.start_time + message.total_elapsed_time * 1000) / 1000 | ||
) | ||
self.length = message.total_distance | ||
self.average_heartrate = ( | ||
message.avg_heart_rate if message.avg_heart_rate != 0 else None | ||
) | ||
self.type = Sport(message.sport).name.lower() | ||
|
||
# moving_dict | ||
self.moving_dict["distance"] = message.total_distance | ||
self.moving_dict["moving_time"] = datetime.timedelta( | ||
seconds=message.total_moving_time | ||
if message.total_moving_time | ||
else message.total_timer_time | ||
) | ||
self.moving_dict["elapsed_time"] = datetime.timedelta( | ||
seconds=message.total_elapsed_time | ||
) | ||
self.moving_dict["average_speed"] = ( | ||
message.enhanced_avg_speed | ||
if message.enhanced_avg_speed | ||
else message.avg_speed | ||
) | ||
|
||
self.start_time_local, self.end_time_local = parse_datetime_to_local( | ||
self.start_time, self.end_time, self.polyline_container[0] | ||
# moving_dict | ||
self.moving_dict["distance"] = message["total_distance"] | ||
self.moving_dict["moving_time"] = datetime.timedelta( | ||
seconds=message["total_moving_time"] | ||
if "total_moving_time" in message | ||
else message["total_timer_time"] | ||
) | ||
self.moving_dict["elapsed_time"] = datetime.timedelta( | ||
seconds=message["total_elapsed_time"] | ||
) | ||
self.start_latlng = start_point(*self.polyline_container[0]) | ||
self.polylines.append(_polylines) | ||
self.polyline_str = polyline.encode(self.polyline_container) | ||
self.moving_dict["average_speed"] = ( | ||
message["enhanced_avg_speed"] | ||
if message["enhanced_avg_speed"] | ||
else message["avg_speed"] | ||
) | ||
for record in fit["record_mesgs"]: | ||
if "position_lat" in record and "position_long" in record: | ||
lat = record["position_lat"] / SEMICIRCLE | ||
lng = record["position_long"] / SEMICIRCLE | ||
_polylines.append(s2.LatLng.from_degrees(lat, lng)) | ||
self.polyline_container.append([lat, lng]) | ||
if len(self.polyline_container) > 0: | ||
self.start_time_local, self.end_time_local = parse_datetime_to_local( | ||
self.start_time, self.end_time, self.polyline_container[0] | ||
) | ||
self.start_latlng = start_point(*self.polyline_container[0]) | ||
self.polylines.append(_polylines) | ||
self.polyline_str = polyline.encode(self.polyline_container) | ||
else: | ||
self.start_time_local, self.end_time_local = parse_datetime_to_local( | ||
self.start_time, self.end_time, None | ||
) | ||
|
||
def append(self, other): | ||
"""Append other track to self.""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add comment here for this number