incomplete version of garmin training payload
This commit is contained in:
@@ -132,7 +132,7 @@ def get_garmin_file(r,callbackURL,starttime,fileType):
|
|||||||
|
|
||||||
def get_garmin_workout_list(user):
|
def get_garmin_workout_list(user):
|
||||||
r = Rower.objects.get(user=user)
|
r = Rower.objects.get(user=user)
|
||||||
if (r.garmintoken == '') or (r.stravatoken is None):
|
if (r.garmintoken == '') or (r.garmintoken is None):
|
||||||
s = "Token doesn't exist. Need to authorize"
|
s = "Token doesn't exist. Need to authorize"
|
||||||
return custom_exception_handler(401,s)
|
return custom_exception_handler(401,s)
|
||||||
|
|
||||||
@@ -148,6 +148,111 @@ def get_garmin_workout_list(user):
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def garmin_can_export_session(user):
|
||||||
|
result = get_garmin_permissions(user)
|
||||||
|
if 'WORKOUT_IMPORT' in result:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
from rowers import utils
|
||||||
|
|
||||||
|
def step_to_garmin(step):
|
||||||
|
out = {
|
||||||
|
'type': step['type']
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
steps = step['steps']
|
||||||
|
lijst = []
|
||||||
|
for s in steps:
|
||||||
|
sout = step_to_garmin(s)
|
||||||
|
lijst.append(sout)
|
||||||
|
out['steps'] = lijst
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
|
def ps_to_garmin(ps):
|
||||||
|
payload = {
|
||||||
|
'workoutName': ps.name,
|
||||||
|
'sport': 'GENERIC',
|
||||||
|
'description':'Uploaded from Rowsandall.com',
|
||||||
|
'estimatedDurationInSecs':60*ps.approximate_duration,
|
||||||
|
'estimatedDistanceInMeters': ps.approximate_distance,
|
||||||
|
'workoutProvider': 'Rowsandall.com',
|
||||||
|
'workoutSourceId': 'Rowsandall.com',
|
||||||
|
}
|
||||||
|
|
||||||
|
steps = []
|
||||||
|
|
||||||
|
steplist = utils.ps_dict_order_dict(ps.steps)
|
||||||
|
|
||||||
|
|
||||||
|
while steplist:
|
||||||
|
step, steplist = utils.peel(steplist)
|
||||||
|
steps.append(step)
|
||||||
|
|
||||||
|
steps.append(step)
|
||||||
|
|
||||||
|
lijst = []
|
||||||
|
for step in steps:
|
||||||
|
lijst.append(step_to_garmin(step))
|
||||||
|
|
||||||
|
payload['steps'] = lijst
|
||||||
|
|
||||||
|
return payload
|
||||||
|
|
||||||
|
|
||||||
|
def get_garmin_permissions(user):
|
||||||
|
r = Rower.objects.get(user=user)
|
||||||
|
if (r.garmintoken == '') or (r.garmintoken is None):
|
||||||
|
s = "Token doesn't exist. Need to authorize"
|
||||||
|
return custom_exception_handler(401,s)
|
||||||
|
|
||||||
|
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/userPermissions/'
|
||||||
|
|
||||||
|
result = garmin.get(url)
|
||||||
|
|
||||||
|
if result.status_code == 200:
|
||||||
|
return result.json()
|
||||||
|
|
||||||
|
return []
|
||||||
|
|
||||||
|
def garmin_session_create(ps,user):
|
||||||
|
if not ps.steps:
|
||||||
|
return 0
|
||||||
|
if not garmin_can_export_session(user):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
garmindict = ps_to_garmin(ps)
|
||||||
|
|
||||||
|
r = Rower.objects.get(user=user)
|
||||||
|
if (r.garmintoken == '') or (r.garmintoken is None):
|
||||||
|
s = "Token doesn't exist. Need to authorize"
|
||||||
|
return custom_exception_handler(401,s)
|
||||||
|
|
||||||
|
garmin = OAuth1Session(oauth_data['client_id'],
|
||||||
|
client_secret=oauth_data['client_secret'],
|
||||||
|
resource_owner_key=r.garmintoken,
|
||||||
|
resource_owner_secret=r.garminrefreshtoken,
|
||||||
|
)
|
||||||
|
|
||||||
|
url = 'http://apis.garmin.com/training-api/schedule/'
|
||||||
|
|
||||||
|
response = garmin.post(url,data=garmindict)
|
||||||
|
|
||||||
|
if response.status_code != 200:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
return response.json()['workoutId']
|
||||||
|
|
||||||
def garmin_getworkout(garminid,r,activity):
|
def garmin_getworkout(garminid,r,activity):
|
||||||
starttime = activity['startTimeInSeconds']
|
starttime = activity['startTimeInSeconds']
|
||||||
startdatetime = arrow.get(starttime)
|
startdatetime = arrow.get(starttime)
|
||||||
|
|||||||
@@ -638,6 +638,56 @@ def step_to_time_dist(step,avgspeed = 3.7):
|
|||||||
|
|
||||||
return seconds,distance
|
return seconds,distance
|
||||||
|
|
||||||
|
def get_step_type(step):
|
||||||
|
t = 'WorkoutStep'
|
||||||
|
|
||||||
|
if step['durationType'] in ['RepeatUntilStepsCmplt','RepeatUntilHrLessThan','RepeatUntilHrGreaterThan']:
|
||||||
|
t = 'WorkoutRepeatStep'
|
||||||
|
|
||||||
|
return t
|
||||||
|
|
||||||
|
def peel(l):
|
||||||
|
if len(l)==0:
|
||||||
|
return None,None
|
||||||
|
if len(l)==1:
|
||||||
|
return l[0],None
|
||||||
|
|
||||||
|
first = l[0]
|
||||||
|
rest = l[1:]
|
||||||
|
|
||||||
|
if first['type'] == 'Step':
|
||||||
|
return first, rest
|
||||||
|
# repeatstep
|
||||||
|
theID = -1
|
||||||
|
lijst = []
|
||||||
|
while theID != first['repeatID']:
|
||||||
|
f, rest = peel(rest)
|
||||||
|
lijst.append(f)
|
||||||
|
theID = f['stepID']
|
||||||
|
first['steps'] = list(reversed(lijst))
|
||||||
|
|
||||||
|
return first,rest
|
||||||
|
|
||||||
|
|
||||||
|
def ps_dict_order_dict(d):
|
||||||
|
steps = d['steps']
|
||||||
|
sdicts = []
|
||||||
|
for step in steps:
|
||||||
|
sstring, type, stepID, repeatID, repeatValue = step_to_string(step)
|
||||||
|
seconds, meters = step_to_time_dist(step)
|
||||||
|
|
||||||
|
sdict = {
|
||||||
|
'type':type,
|
||||||
|
'stepID': stepID,
|
||||||
|
'repeatID': repeatID,
|
||||||
|
'repeatValue': repeatValue,
|
||||||
|
'dict': step,
|
||||||
|
}
|
||||||
|
sdicts.append(sdict)
|
||||||
|
|
||||||
|
sdict2 = list(reversed(sdicts))
|
||||||
|
|
||||||
|
return sdict2
|
||||||
|
|
||||||
def ps_dict_order(d):
|
def ps_dict_order(d):
|
||||||
sdict = collections.OrderedDict({})
|
sdict = collections.OrderedDict({})
|
||||||
@@ -659,6 +709,22 @@ def ps_dict_order(d):
|
|||||||
|
|
||||||
sdict2 = collections.OrderedDict(reversed(list(sdict.items())))
|
sdict2 = collections.OrderedDict(reversed(list(sdict.items())))
|
||||||
|
|
||||||
|
for step in steps:
|
||||||
|
sstring, type, stepID, repeatID, repeatValue = step_to_string(step)
|
||||||
|
seconds, meters = step_to_time_dist(step)
|
||||||
|
|
||||||
|
sdict[stepID] = {
|
||||||
|
'string':sstring,
|
||||||
|
'type':type,
|
||||||
|
'stepID': stepID,
|
||||||
|
'repeatID': repeatID,
|
||||||
|
'repeatValue': repeatValue,
|
||||||
|
'seconds': seconds,
|
||||||
|
'meters': meters,
|
||||||
|
}
|
||||||
|
|
||||||
|
sdict2 = collections.OrderedDict(reversed(list(sdict.items())))
|
||||||
|
|
||||||
sdict3 = []
|
sdict3 = []
|
||||||
hold = []
|
hold = []
|
||||||
multiplier = []
|
multiplier = []
|
||||||
|
|||||||
Reference in New Issue
Block a user