-
Notifications
You must be signed in to change notification settings - Fork 0
/
offlineApp.py
180 lines (159 loc) · 8.38 KB
/
offlineApp.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from flask import Flask, redirect, render_template, request, jsonify, session, url_for
from DexcomAPI import defs, stat_functions as stats
import requests
from authlib.integrations.flask_client import OAuth
import requests
import os
app = Flask(__name__,
template_folder="DexcomAPI/templates",
static_folder="DexcomAPI/static")
app.secret_key = defs.get_secret_key()
# Load environment variables for Auth0
AUTH0_CLIENT_ID = os.getenv('AUTH0_CLIENT_ID')
AUTH0_CLIENT_SECRET = os.getenv('AUTH0_CLIENT_SECRET')
AUTH0_DOMAIN = os.getenv('AUTH0_DOMAIN')
AUTH0_CALLBACK_URL = os.getenv('AUTH0_CALLBACK_URL')
# Define Auth0 URLs
AUTH0_BASE_URL = f'https://{AUTH0_DOMAIN}'
AUTH0_ACCESS_TOKEN_URL = f'https://{AUTH0_DOMAIN}/oauth/token'
AUTH0_AUTHORIZE_URL = f'https://{AUTH0_DOMAIN}/authorize'
oauth = OAuth(app)
auth0 = oauth.register(
'auth0',
client_id=AUTH0_CLIENT_ID,
client_secret=AUTH0_CLIENT_SECRET,
api_base_url=AUTH0_BASE_URL,
access_token_url=AUTH0_ACCESS_TOKEN_URL,
authorize_url=AUTH0_AUTHORIZE_URL,
client_kwargs={
'scope': 'openid profile email',
},
)
# Initialize Dexcom object with appropriate credentials
dexcom = defs.get_dexcom_connection()
def safe_get_value(func, *args):
"""Helper function to safely get values or return 'N/A' if None."""
return func(*args) or 'N/A'
@app.route('/')
def index():
# Dictionary to store glucose data
glucose_data = {
'current_glucose_mgdl': safe_get_value(stats.get_current_value_mdgl, dexcom),
'current_glucose_mmol': safe_get_value(stats.get_current_value_mmol, dexcom),
'glucose_state_mdgl': safe_get_value(stats.get_glucose_state_mdgl, dexcom),
'glucose_state_mmol': safe_get_value(stats.get_glucose_state_mmol, dexcom),
'average_glucose_mgdl': safe_get_value(stats.get_average_glucose_mgdl, dexcom),
'average_glucose_mmol': safe_get_value(stats.get_average_glucose_mmol, dexcom),
'median_glucose_mgdl': safe_get_value(stats.get_median_glucose_mgdl, dexcom),
'median_glucose_mmol': safe_get_value(stats.get_median_glucose_mmol, dexcom),
'stdev_glucose_mgdl': safe_get_value(stats.get_stdev_glucose_mgdl, dexcom),
'stdev_glucose_mmol': safe_get_value(stats.get_stdev_glucose_mmol, dexcom),
'min_glucose_mgdl': safe_get_value(stats.get_min_glucose_mgdl, dexcom),
'min_glucose_mmol': safe_get_value(stats.get_min_glucose_mmol, dexcom),
'max_glucose_mgdl': safe_get_value(stats.get_max_glucose_mgdl, dexcom),
'max_glucose_mmol': safe_get_value(stats.get_max_glucose_mmol, dexcom),
'glucose_range_mgdl': safe_get_value(stats.get_glucose_range_mgdl, dexcom),
'glucose_range_mmol': safe_get_value(stats.get_glucose_range_mmol, dexcom),
'coef_variation_percentage': safe_get_value(stats.get_coef_variation_percentage, dexcom),
'glycemic_variability_index': safe_get_value(stats.get_glycemic_variability_index, dexcom),
'estimated_a1c': safe_get_value(stats.get_estimated_a1c, dexcom),
'time_in_range_percentage': safe_get_value(lambda: stats.time_in_range_percentage) # Assuming this is a direct variable
}
# Render template with glucose data
return render_template('index.html', **glucose_data)
# def index():
# # Get current glucose data
# current_glucose_mgdl = stats.get_current_value_mdgl(dexcom) or 'N/A'
# current_glucose_mmol = stats.get_current_value_mmol(dexcom) or 'N/A'
# glucose_state_mdgl = stats.get_glucose_state_mdgl(dexcom) or 'N/A'
# glucose_state_mmol = stats.get_glucose_state_mmol(dexcom) or 'N/A'
# average_glucose_mgdl = stats.get_average_glucose_mgdl(dexcom) or 'N/A'
# average_glucose_mmol = stats.get_average_glucose_mmol(dexcom) or 'N/A'
# median_glucose_mgdl = stats.get_median_glucose_mgdl(dexcom) or 'N/A'
# median_glucose_mmol = stats.get_median_glucose_mmol(dexcom) or 'N/A'
# stdev_glucose_mgdl = stats.get_stdev_glucose_mgdl(dexcom) or 'N/A'
# stdev_glucose_mmol = stats.get_stdev_glucose_mmol(dexcom) or 'N/A'
# min_glucose_mgdl = stats.get_min_glucose_mgdl(dexcom) or 'N/A'
# min_glucose_mmol = stats.get_min_glucose_mmol(dexcom) or 'N/A'
# max_glucose_mgdl = stats.get_max_glucose_mgdl(dexcom) or 'N/A'
# max_glucose_mmol = stats.get_max_glucose_mmol(dexcom) or 'N/A'
# glucose_range_mgdl = stats.get_glucose_range_mgdl(dexcom) or 'N/A'
# glucose_range_mmol = stats.get_glucose_range_mmol(dexcom) or 'N/A'
# coef_variation_percentage = stats.get_coef_variation_percentage(dexcom) or 'N/A'
# glycemic_variability_index = stats.get_glycemic_variability_index(dexcom) or 'N/A'
# estimated_a1c = stats.get_estimated_a1c(dexcom) or 'N/A'
# time_in_range_percentage = stats.time_in_range_percentage or 'N/A'
# # Render template with the glucose data
# return render_template('index.html',
# current_glucose_mgdl=current_glucose_mgdl,
# current_glucose_mmol=current_glucose_mmol,
# glucose_state_mdgl=glucose_state_mdgl,
# glucose_state_mmol=glucose_state_mmol,
# average_glucose_mgdl=average_glucose_mgdl,
# average_glucose_mmol=average_glucose_mmol,
# median_glucose_mgdl=median_glucose_mgdl,
# median_glucose_mmol=median_glucose_mmol,
# stdev_glucose_mgdl=stdev_glucose_mgdl,
# stdev_glucose_mmol=stdev_glucose_mmol,
# min_glucose_mgdl=min_glucose_mgdl,
# min_glucose_mmol=min_glucose_mmol,
# max_glucose_mgdl=max_glucose_mgdl,
# max_glucose_mmol=max_glucose_mmol,
# glucose_range_mgdl=glucose_range_mgdl,
# glucose_range_mmol=glucose_range_mmol,
# coef_variation_percentage=coef_variation_percentage,
# glycemic_variability_index=glycemic_variability_index,
# estimated_a1c=estimated_a1c,
# time_in_range_percentage=time_in_range_percentage)
@app.route('/login')
def login():
return auth0.authorize_redirect(redirect_uri=url_for('auth_callback', _external=True))
@app.route('/update_global_mdgl_range', methods=['POST'])
def update_mdgl():
data = request.json
stats.high_mgdl = data['high_mgdl']
stats.low_mgdl = data['low_mgdl']
return jsonify({"message": "mdgl value(s) updated successfully"}), 200
@app.route('/update_global_mmol_range', methods=['POST'])
def update_mmol():
data = request.json
stats.high_mmol = data['high_mmol']
stats.low_mmol = data['low_mmol']
return jsonify({"message": "mmol value(s) updated successfully"}), 200
@app.route('/callback')
def auth_callback():
token = auth0.authorize_access_token() # Get token using Authlib
session['auth_token'] = token
userinfo = auth0.get('userinfo')
session['profile'] = userinfo.json()
return redirect('/')
# Dexcom sign-in route
@app.route('/dexcom-signin')
def dexcom_signin():
dexcom_client_id = os.getenv('DEXCOM_CLIENT_ID')
# redirect_uri = url_for('dexcom_callback', _external=True)
redirect_uri = os.getenv('DEXCOM_REDIRECT_URI')
return redirect(f'https://api.dexcom.com/v2/oauth2/login?client_id={dexcom_client_id}&redirect_uri={redirect_uri}&response_type=code&scope=offline_access')
# Dexcom callback route
@app.route('/dexcom-callback')
def dexcom_callback():
dexcom_client_id = os.getenv('DEXCOM_CLIENT_ID')
dexcom_client_secret = os.getenv('DEXCOM_CLIENT_SECRET')
auth_code = request.args.get('code')
dexcom_token_url = "https://api.dexcom.com/v2/oauth2/token"
payload = {
"grant_type": "authorization_code",
"code": auth_code,
"redirect_uri": "http://localhost:5001/dexcom-callback",
"client_id": dexcom_client_id,
"client_secret": dexcom_client_secret,
}
headers = {"Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(dexcom_token_url, data=payload, headers=headers)
if response.status_code == 200:
session['dexcom_token'] = response.json()['access_token']
return redirect('/')
else:
return f"Error: {response.status_code} - {response.text}"
if __name__ == '__main__':
app.run(port=5001, debug=True)