diff --git a/rowers/.#views.py b/rowers/.#views.py new file mode 100644 index 00000000..8bec0378 --- /dev/null +++ b/rowers/.#views.py @@ -0,0 +1 @@ +E408191@CZ27LT9RCGN72.494924:1516366764 \ No newline at end of file diff --git a/rowers/c2stuff.py b/rowers/c2stuff.py index d380fc45..7cb8dc4a 100644 --- a/rowers/c2stuff.py +++ b/rowers/c2stuff.py @@ -101,7 +101,6 @@ def add_stroke_data(user,c2id,workoutid,startdatetime,csvfilename): s = "Token expired. Needs to refresh." return custom_exception_handler(401,s) else: - # ready to fetch. Hurray starttimeunix = arrow.get(startdatetime).timestamp job = myqueue(queue, diff --git a/rowers/dataprepnodjango.py b/rowers/dataprepnodjango.py index f085c945..5f15f1e6 100644 --- a/rowers/dataprepnodjango.py +++ b/rowers/dataprepnodjango.py @@ -131,6 +131,102 @@ def rdata(file,rower=rrower()): return res + +def totaltime_sec_to_string(totaltime): + hours = int(totaltime / 3600.) + if hours > 23: + message = 'Warning: The workout duration was longer than 23 hours. ' + hours = 23 + + minutes = int((totaltime - 3600. * hours) / 60.) + if minutes > 59: + minutes = 59 + if not message: + message = 'Warning: there is something wrong with the workout duration' + + seconds = int(totaltime - 3600. * hours - 60. * minutes) + if seconds > 59: + seconds = 59 + if not message: + message = 'Warning: there is something wrong with the workout duration' + + tenths = int(10 * (totaltime - 3600. * hours - 60. * minutes - seconds)) + if tenths > 9: + tenths = 9 + if not message: + message = 'Warning: there is something wrong with the workout duration' + + duration = "%s:%s:%s.%s" % (hours, minutes, seconds, tenths) + + return duration + +# Creates C2 stroke data +def create_c2_stroke_data_db( + distance,duration,workouttype, + workoutid,starttimeunix,csvfilename,debug=False): + + nr_strokes = int(distance/10.) + + + totalseconds = duration.hour*3600. + totalseconds += duration.minute*60. + totalseconds += duration.second + totalseconds += duration.microsecond/1.e6 + + + spm = 60.*nr_strokes/totalseconds + + step = totalseconds/float(nr_strokes) + + elapsed = np.arange(nr_strokes)*totalseconds/(float(nr_strokes-1)) + + dstep = distance/float(nr_strokes) + + d = np.arange(nr_strokes)*distance/(float(nr_strokes-1)) + + unixtime = starttimeunix + elapsed + + pace = 500.*totalseconds/distance + + if workouttype in ['rower','slides','dynamic']: + velo = distance/totalseconds + power = 2.8*velo**3 + else: + power = 0 + + + df = pd.DataFrame({ + 'TimeStamp (sec)': unixtime, + ' Horizontal (meters)': d, + ' Cadence (stokes/min)': spm, + ' Stroke500mPace (sec/500m)':pace, + ' ElapsedTime (sec)':elapsed, + ' Power (watts)':power, + ' HRCur (bpm)':np.zeros(nr_strokes), + ' longitude':np.zeros(nr_strokes), + ' latitude':np.zeros(nr_strokes), + ' DragFactor':np.zeros(nr_strokes), + ' DriveLength (meters)':np.zeros(nr_strokes), + ' StrokeDistance (meters)':np.zeros(nr_strokes), + ' DriveTime (ms)':np.zeros(nr_strokes), + ' StrokeRecoveryTime (ms)':np.zeros(nr_strokes), + ' AverageDriveForce (lbs)':np.zeros(nr_strokes), + ' PeakDriveForce (lbs)':np.zeros(nr_strokes), + ' lapIdx':np.zeros(nr_strokes), + 'cum_dist': d + }) + + timestr = strftime("%Y%m%d-%H%M%S") + + df[' ElapsedTime (sec)'] = df['TimeStamp (sec)'] + + res = df.to_csv(csvfilename,index_label='index', + compression='gzip') + + data = dataprep(df,id=workoutid,bands=False,debug=debug) + + return data + # Saves C2 stroke data to CSV and database def add_c2_stroke_data_db(strokedata,workoutid,starttimeunix,csvfilename, debug=False): diff --git a/rowers/tasks.py b/rowers/tasks.py index cf53af73..aaacd8c3 100644 --- a/rowers/tasks.py +++ b/rowers/tasks.py @@ -13,6 +13,9 @@ import rowingdata from rowingdata import rowingdata as rdata from celery import app +import datetime +import pytz +import iso8601 from matplotlib.backends.backend_agg import FigureCanvas #from matplotlib.backends.backend_cairo import FigureCanvasCairo as FigureCanvas @@ -33,7 +36,8 @@ from rowers.dataprepnodjango import ( update_strokedata, new_workout_from_file, getsmallrowdata_db, updatecpdata_sql, update_agegroup_db,fitnessmetric_to_sql, - add_c2_stroke_data_db + add_c2_stroke_data_db,totaltime_sec_to_string, + create_c2_stroke_data_db ) from django.core.mail import send_mail, EmailMessage @@ -71,9 +75,47 @@ def handle_c2_import_stroke_data(c2token, csvfilename,debug=debug, ) - return 1 else: + url = "https://log.concept2.com/api/users/me/results/"+str(c2id) + s = requests.get(url,headers=headers) + + if s.status_code == 200: + workoutdata = s.json()['data'] + distance = workoutdata['distance'] + c2id = workoutdata['id'] + workouttype = workoutdata['type'] + verified = workoutdata['verified'] + startdatetime = iso8601.parse_date(workoutdata['date']) + weightclass = workoutdata['weight_class'] + weightcategory = 'hwt' + if weightclass == "L": + weightcategory = 'lwt' + totaltime = workoutdata['time']/10. + duration = totaltime_sec_to_string(totaltime) + duration = datetime.datetime.strptime(duration,'%H:%M:%S.%f').time() + + try: + timezone_str = tz(workoutdata['timezone']) + except: + timezone_str = 'UTC' + + workoutdate = startdatetime.astimezone( + pytz.timezone(timezone_str) + ).strftime('%Y-%m-%d') + starttime = startdatetime.astimezone( + pytz.timezone(timezone_str) + ).strftime('%H:%M:%S') + + result = create_c2_stroke_data_db( + distance,duration,workouttype, + workoutid,starttimeunix, + csvfilename,debug=debug, + ) + + + return 1 + return 0 return 0 diff --git a/rowers/views.py b/rowers/views.py index eeac89c1..c2ad7059 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -9108,7 +9108,7 @@ def workout_getc2workout_all(request,page=1,message=""): messages.error(request,message) else: r = getrower(request.user) - c2ids = [item['id'] for item in res.json()['data'] if item['source'] != 'Web'] + c2ids = [item['id'] for item in res.json()['data']] alldata = {} for item in res.json()['data']: alldata[item['id']] = item