-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
119 lines (90 loc) · 3.21 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def get_power_right_left(message_fields):
power = 0.
lrencoded = None
for message_field in message_fields:
if message_field['name'] == 'power':
power = message_field['value']
if message_field['name'] == 'left_right_balance':
lrencoded = message_field['value']
if lrencoded == 'right':
lrencoded = 0x80
if lrencoded is None:
return (None, None)
if lrencoded & 0x80:
factor_right = (lrencoded & 0x7f)/100.
else:
factor_right = 1 - (lrencoded & 0x7f)/100.
factor_left = 1 - factor_right
return (factor_right * power, factor_left * power)
def get_speed(message_fields):
""" get_speed
return the speed as float in km/h from a message.as_dict()['fields'] object
Args:
message_fields: a message.as_dict()['fields'] object (with name 'record')
Returns:
the speed as float in km/h, or 0. if not found
"""
for message_field in message_fields:
try:
if message_field['name'] == 'speed':
return 3.6 * message_field['value']
except TypeError:
# NoneType from message_field['value']
pass
for message_field in message_fields:
if message_field['name'] == 'enhanced_speed':
try:
return 3.6 * message_field['value']
except TypeError:
# NoneType or something???
pass
return 0.
get_speed.__name__ = "Speed [km/h]"
def get_power(message_fields):
""" get_power
return the power as float in Watts from a message.as_dict()['fields'] object
Args:
message_fields: a message.as_dict()['fields'] object (with name 'record')
Returns:
the power as float in watts, or 0. if not found
"""
for message_field in message_fields:
if message_field['name'] == 'power':
return message_field['value']
return 0.
get_power.__name__ = "Power [Watts]"
def get_heart_rate(message_fields):
""" get_heart_rate
return the heart rate as float from a message.as_dict()['fields'] object
Args:
message_fields: a message.as_dict()['fields'] object (with name 'record')
Returns:
heart rate in bpm or 50. if not found
"""
for message_field in message_fields:
if message_field['name'] == 'heart_rate':
return message_field['value']
return 50.
get_heart_rate.__name__ = "heart rate [bpm]"
def get_pace(message_fields):
for message_field in message_fields:
if message_field['name'] == 'speed':
try:
return 60. / (3.6 * message_field['value'])
except ZeroDivisionError:
return 0.
return 0.
get_pace.__name__ = "pace [min/km]"
def get_cadence(message_fields):
""" get_cadence
return the cadence as float in 1/min from a message.as_dict()['fields'] object
Args:
message_fields: a message.as_dict()['fields'] object (with name 'record')
Returns:
the cadence as float in 1/min, or 0 if not found
"""
for message_field in message_fields:
if message_field['name'] == 'cadence':
return message_field['value']
return 0.
get_cadence.__name__ = "Cadence [1/min]"