diff --git a/rowers/dataprep.py b/rowers/dataprep.py index f6eb2dbf..27036a41 100644 --- a/rowers/dataprep.py +++ b/rowers/dataprep.py @@ -70,6 +70,7 @@ import zipfile import pandas as pd import numpy as np import itertools +from fitparse import FitFile import math from rowers.tasks import ( handle_sendemail_unrecognized, handle_sendemail_breakthrough, @@ -94,6 +95,7 @@ queuehigh = django_rq.get_queue('default') from rowsandall_app.settings import SITE_URL from rowers.mytypes import otwtypes,otetypes +from rowers import mytypes from rowers.database import * from rowers.opaque import encoder @@ -1533,6 +1535,40 @@ def handle_nonpainsled(f2, fileformat, summary=''): # This routine should be used everywhere in views.py and mailprocessing.py # Currently there is code duplication +def get_workouttype_from_fit(filename): + fitfile = FitFile(filename,check_crc=False) + records = fitfile.messages + fittype = 'rowing' + workouttype = 'water' + for record in records: + if record.name == 'sport': + fittype = record.get_values()['sport'].lower() + try: + workouttype = mytypes.fitmappinginv[fittype] + except KeyError: + workouttype = 'other' + + return workouttype + +import rowingdata.tcxtools as tcxtools + +def get_workouttype_from_tcx(filename): + d = tcxtools.tcx_getdict(filename) + tcxtype = 'rowing' + workouttype = 'water' + try: + tcxtype = d['Activities']['Activity']['@Sport'].lower() + if tcxtype == 'other': + tcxtype = 'rowing' + except KeyError: + tcxtype = 'rowing' + + try: + workouttype = mytypes.garminmappinginv[tcxtype.upper()] + except KeyError: + workouttype = 'water' + + return workouttype def new_workout_from_file(r, f2, workouttype='rower', @@ -1554,6 +1590,8 @@ def new_workout_from_file(r, f2, summary = '' oarlength = 2.89 inboard = 0.88 + + # Save zip files to email box for further processing if len(fileformat) == 3 and fileformat[0] == 'zip': uploadoptions['fromuploadform'] = True bodyyaml = yaml.safe_dump(uploadoptions,default_flow_style=False) @@ -1625,6 +1663,12 @@ def new_workout_from_file(r, f2, # email attachment which can safely be ignored return (0, '', f2) + # Get workout type from fit & tcx + if (fileformat == 'fit'): + workouttype = get_workouttype_from_fit(f2) + if (fileformat == 'tcx'): + workouttype = get_workouttype_from_tcx(f2) + # handle non-Painsled by converting it to painsled compatible CSV if (fileformat != 'csv'): f2, summary, oarlength, inboard, fileformat, impeller = handle_nonpainsled( @@ -1636,6 +1680,8 @@ def new_workout_from_file(r, f2, message = 'Something went wrong' return (0, message, '') + + dosummary = (fileformat != 'fit' and 'speedcoach2' not in fileformat) dosummary = dosummary or summary == '' diff --git a/rowers/mytypes.py b/rowers/mytypes.py index 992f536f..36699446 100644 --- a/rowers/mytypes.py +++ b/rowers/mytypes.py @@ -112,6 +112,42 @@ garmincollection = ( garminmapping = {key:value for key,value in Reverse(garmincollection)} +fitcollection = ( +('water','rowing'), +('rower','rowing'), +('skierg','cross_country_skiing'), +('Bike','cycling'), +('bikeerg','cycling'), +('dynamic','rowing'), +('slides','rowing'), +('paddle','paddling'), +('snow','cross_country_skiing'), +('coastal','rowing'), +('c-boat','rowing'), +('churchboat','rowing'), +('Ride','cycling'), +('Run','running'), +('NordicSki','cross_country_skiing'), +('Swim','swimming'), +('Hike','hiking'), +('RollerSki','cross_country_skiing'), +('Walk','walking'), +('Canoeing','boating'), +('Crossfit','fitness_equipment'), +('StandUpPaddling','stand_up_paddle_boarding'), +('IceSkate','ice_skating'), +('WeightTraining','training'), +('InlineSkate','inline_skating'), +('Kayaking','kayaking'), +('Workout','generic'), +('Yoga','generic'), +('other','generic'), +) + + + +fitmapping = {key:value for key,value in Reverse(fitcollection)} + stcollection = ( ('water','Rowing'), ('rower','Rowing'), @@ -289,6 +325,8 @@ polarmappinginv = {value:key for key,value in Reverse(polarcollection) if value garminmappinginv = {value:key for key, value in Reverse(garmincollection) if value is not None} +fitmappinginv = {value:key for key,value in Reverse(fitcollection) if value is not None} + otwtypes = ( 'water', 'coastal', diff --git a/rowers/tasks.py b/rowers/tasks.py index 1d978ed0..7be5e3b9 100644 --- a/rowers/tasks.py +++ b/rowers/tasks.py @@ -707,7 +707,6 @@ def handle_get_garmin_file(client_id, )+filetype response = garmin.get(url, stream=True) - print(response.status_code,filename) if response.status_code == 200: with open(filename, 'wb') as out_file: shutil.copyfileobj(response.raw, out_file)