diff --git a/rowers/garmin_stuff.py b/rowers/garmin_stuff.py index a96d03d9..2e379acd 100644 --- a/rowers/garmin_stuff.py +++ b/rowers/garmin_stuff.py @@ -157,23 +157,80 @@ def garmin_can_export_session(user): from rowers import utils -def step_to_garmin(step): +def step_to_garmin(step,order=0): + durationtype = step['dict']['durationType'] + durationvalue = step['dict']['durationValue'] + durationvaluetype = '' + if durationtype == 'Time': + durationtype = 'TIME' + durationvalue = int(durationvalue/1000.) + elif durationtype == 'Distance': + durationtype = 'DISTANCE' + durationvalue = int(durationvalue/100) + durationvaluetype = 'METER' + elif durationtype == 'HrLessThan': + durationtype = 'HR_LESS_THAN' + if durationvalue <= 100: + durationvaluetype = 'PERCENT' + else: + durationvaluetype = '' + durationvalue -= 100 + elif durationtype == 'HrGreaterThan': + durationtype = 'HR_GREATER_THAN' + if durationvalue <= 100: + durationvaluetype = 'PERCENT' + else: + durationvaluetype = '' + durationvalue -= 100 + elif durationtype == 'PowerLessThan': + durationtype = 'POWER_LESS_THAN' + if durationvalue <= 1000: + durationvaluetype = 'PERCENT' + else: + durationvaluetype = '' + durationvalue -= 1000 + elif durationtype == 'PowerGreaterThan': + durationtype = 'POWER_GREATER_THAN' + if durationvalue <= 1000: + durationvaluetype = 'PERCENT' + else: + durationvaluetype = '' + durationvalue -= 1000 + elif durationtype == 'Reps': + durationtype = 'REPS' + out = { - 'type': step['type'] + 'type': step['type'], + 'stepOrder':order, + 'repeatType':step['type'], + 'repeatValue':step['repeatValue'], + 'intensity':step['dict']['intensity'], + 'description':step['dict']['wkt_step_name'], + 'durationType':durationtype, + 'durationValue':durationvalue, + 'durationValueType':durationvaluetype, + 'targetType':step['dict']['targetType'], + 'targetValue':step['dict']['targetValue'], + 'targetValueLow':step['dict']['targetValueLow'], + 'targetValueHigh':step['dict']['targetValueHigh'], } try: steps = step['steps'] lijst = [] + order += 1 for s in steps: - sout = step_to_garmin(s) + sout, order = step_to_garmin(s, order) lijst.append(sout) out['steps'] = lijst + order -= 1 except KeyError: pass - return out + order += 1 -def ps_to_garmin(ps): + return out, order + +def ps_to_garmin(ps,r): payload = { 'workoutName': ps.name, 'sport': 'GENERIC', @@ -193,15 +250,27 @@ def ps_to_garmin(ps): step, steplist = utils.peel(steplist) steps.append(step) - steps.append(step) + steps = list(reversed(steps)) lijst = [] + i = 0 for step in steps: - lijst.append(step_to_garmin(step)) + gstep, i = step_to_garmin(step,i) + lijst.append(gstep) payload['steps'] = lijst - return payload + garmin = OAuth1Session(oauth_data['client_id'], + client_secret=oauth_data['client_secret'], + resource_owner_key=r.garmintoken, + resource_owner_secret=r.garminrefreshtoken, + ) + + url = 'https://apis.garmin.com/training-api/schedule/' + + response = garmin.post(url,data=payload) + + return response def get_garmin_permissions(user):