-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
305 lines (277 loc) · 11 KB
/
app.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
import sqlite3
from flask import Flask, make_response, request
from flask import jsonify, g
from flask_cors import CORS
from celery import Celery
import serial
import copy
try:
ser = serial.Serial('/dev/ttyUSB1', 9600)
except Exception as e:
ser = serial.Serial('/dev/ttyUSB0', 9600)
DATABASE = './.physio.db'
flask_app = Flask(__name__)
CORS(flask_app)
flask_app.config.update(
CELERY_BROKER_URL='redis://localhost:6379',
CELERY_RESULT_BACKEND='redis://localhost:6379'
)
START, CALIB, TRANSMIT, BEG_T, CON_T = b's\n', b'c\n', b't\n', b'r\n', b'j\n'
ACK = [str.encode('a' + str(i) + '\n') for i in range(9)]
flask_app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
flask_app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'
celery = Celery(flask_app.name, broker=flask_app.config['CELERY_BROKER_URL'])
celery.conf.update(flask_app.config)
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
def make_dicts(cursor, row):
return dict((cursor.description[idx][0], value)
for idx, value in enumerate(row))
db.row_factory = make_dicts
return db
def query_db(query, args=(), one=False):
cursor = get_db().execute(query, args)
rowvalues = cursor.fetchall()
cursor.close()
return (rowvalues[0] if rowvalues else None) if one else rowvalues
@flask_app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
def init_db():
with flask_app.app_context():
db = get_db()
with flask_app.open_resource('schema.sql', 'r') as f:
db.cursor().executescript(f.read())
db.commit()
init_db()
@flask_app.route('/start_calibration/<int:doctorid>/<int:patientid>/<description>', methods=['GET'])
def start_calibration(doctorid, patientid, description):
result = async_start_calibration.delay(doctorid, patientid, description)
return 'Starting Calibration...'
@flask_app.route('/start_training/<int:doctorid>/<int:patientid>/<int:profileid>/<int:reps>', methods=['GET'])
def start_training(doctorid, patientid, profileid, reps):
result = async_start_training.delay(doctorid, patientid, profileid, reps)
return 'Training...'
<<<<<<< HEAD
@flask_app.route('/get_profiles', methods=['GET'])
def get_profiles():
import sqlite3 as sql
connection = sql.connect('/home/pi/physioServer/PhysioServer/.physio.db')
cursor = connection.cursor()
cursor.execute("""SELECT profile_no FROM profiles;""")
profile_nos = []
for row in cursor:
for column in row:
profile_nos.append(column)
print(profile_nos)
return jsonify({"profile_nos": profile_nos})
=======
@flask_app.route('/get_profiles/<int:patientid>', methods=['GET'])
def get_profiles(patientid):
sql = "SELECT E.profileid, E.description, E.lastused, D.name AS doctorname\
FROM ExerciseProfiles E INNER JOIN DP ON DP.patientid = E.patientid\
INNER JOIN Doctors D ON D.doctorid = DP.doctorid WHERE E.patientid is ? ;"
profiles = query_db(sql, [patientid])
return jsonify({"profiles": profiles})
>>>>>>> SIH
@flask_app.route('/delete_profile/<int:profile_no>', methods=['GET'])
def delete_profile(profile_no):
async_delete_profile.delay(profile_no)
return 'Deleting...'
@flask_app.route('/loginverify', methods=['POST'])
def loginverify():
try:
email = "\"" + request.form['email'] + "\""
password = "\"" + request.form['password'] + "\""
print(email, password)
sql = "SELECT * FROM Doctors WHERE email is " + email + " AND password is " + password + ";"
print(sql)
count = query_db(sql, one=True)
if count is not None:
return jsonify({"status": "OK", "doctorid": count["doctorid"], "doctorname": count["name"]})
else:
return jsonify({"status": "NOK"})
except Exception as e:
print(e)
return jsonify({"status": "NOK"})
@flask_app.route('/getpatients/<int:doctorid>', methods=['GET'])
def getpatients(doctorid):
sql = "SELECT Patients.patientid, Patients.name, Patients.age, Patients.sex, Patients.description from Patients INNER JOIN DP ON Patients.patientid = DP.patientid WHERE DP.doctorid = ? ;"
result = query_db(sql, [doctorid])
return jsonify({"result": result})
@flask_app.route('/doctorsignup', methods=['POST'])
def doctorsignup():
email = request.form['email']
password = request.form['password']
name = request.form['name']
sql = "INSERT INTO Doctors(name, email, password) VALUES( ? , ? , ? );"
args = [name, email, password]
get_db().execute(sql, args)
get_db().commit()
return 'Doctor Signed Up Successfully!'
@flask_app.route('/patientsignup', methods=['POST'])
def patientsignup():
doctorid = request.form['doctorid']
name = request.form['name']
age = request.form['age']
sex = request.form['sex']
description = request.form['description']
sql = "INSERT INTO Patients(name, age, sex, description) VALUES( ? , ? , ? , ? );"
args = [name, age, sex, description]
get_db().execute(sql, args)
get_db().commit()
sql = "SELECT count(*) FROM PATIENTS;"
result = query_db(sql, one=True)
patientid = result['count(*)']
sql = "INSERT INTO DP VALUES( ? , ? );"
args = [doctorid, patientid]
get_db().execute(sql, args)
get_db().commit()
@flask_app.route('/gettrainings/<int:patientid>', methods=['GET'])
def gettrainings(patientid):
sql = 'SELECT T.trainingid, T.doctorid, D.name AS doctorname, T.profileid, P.description,\
T.repetitions, T.timestamp FROM TrainingsLedger T INNER JOIN ExerciseProfiles P on\
T.profileid = P.profileid INNER JOIN Doctors D ON T.doctorid = D.doctorid\
WHERE T.patientid IS ? ;'
args = [patientid]
result = query_db(sql, args)
return jsonify({"result": result})
@flask_app.route('/getrepdata/<int:patientid>', methods=['GET'])
def getrepdata(patientid):
sql = 'SELECT repetitions, timestamp from TrainingsLedger WHERE patientid = ? ;'
args = [patientid]
result = query_db(sql, args)
return jsonify({'result': result})
@flask_app.route('/getrangedata/<int:patientid>', methods=['GET'])
def getrangedata(patientid):
sql = 'SELECT E.profile, T.timestamp from TrainingsLedger T LEFT JOIN ExerciseProfiles E\
ON E.profileid = T.profileid WHERE E.patientid = ? ;'
args = [patientid]
result = query_db(sql, args)
return jsonify({'result': result})
@celery.task()
def async_start_calibration(patientid, doctorid, description):
print('Starting to calibrate...')
data_s = "1,1,1,1"
global ser
global CALIB
global ACK
ser.write(CALIB)
ack = b'nack'
while ack != ACK[1]:
ack = ser.readline()
print('Calibration has started...')
while ack != ACK[2]:
ack = ser.readline()
print('Calibration ended! Waiting for data...')
global TRANSMIT
ser.write(TRANSMIT)
while ack != ACK[5]:
ack = ser.readline()
print('Receiving Data...')
data_bytes = ser.readline()
data_s = bytes.decode(data_bytes)
while ack != ACK[6]:
ack = ser.readline()
print('Data Received! Inserting into Database...')
if description[0] is "\"":
description = description[1:]
if description[len(description) - 1] is "\"":
description = description[:len(description) - 1]
sql = "INSERT INTO ExerciseProfiles(doctorid, patientid, description, profile) VALUES(\
? , ? , ? , ? );"
args = [doctorid, patientid, description, data_s]
with flask_app.app_context():
get_db().execute(sql, args)
get_db().commit()
print('Calibration and Data Insertion has been completed successfully...!')
return
@celery.task()
def async_start_training(doctorid, patientid, profileid, reps):
data_s = ""
with flask_app.app_context():
sql_statement = "SELECT profile FROM ExerciseProfiles WHERE profileid IS ? ;"
args = [profileid]
result = query_db(sql_statement, args, one=True)
data_s = result['profile']
print(data_s)
print('Data Retrieved from Database...')
data_bytes = data_s.encode('ascii')
data_length = len(data_s)
packet_length = 10
total_v = data_s.count('v')
full_packets = total_v // packet_length
packet_count, byte_count = 0, 0
print('Starting transmission of data...')
ser.write(b'r\n')
ack = ser.readline()
while ack != b'a7\n':
ack = ser.readline()
print('Ready for transmission...')
while packet_count < full_packets:
v_count = 0
while v_count < packet_length:
ser.write(data_bytes[byte_count])
v_count += (1 if data_bytes[byte_count] == b'v' else 0)
byte_count += 1
ser.write(b'\n')
print('Packet ', packet_count, ' sent...')
packet_count += 1
ack = ser.readline()
while ack != b'a8\n':
ack = ser.readline()
print('Packet ', packet_count, ' received...')
if packet_count >= full_packets:
print('Data Completely transmitted...')
break
print('Starting transmission of data...')
ser.write(b'j\n')
ack = ser.readline()
while ack != b'a7\n':
ack = ser.readline()
print('Ready for transmission...')
repstr=format(reps, '03d')
print('Starting training for ', reps, ' rep\(s\)...')
ser.write(str.encode('m' + repstr + '\n'))
ack = ser.readline()
while ack != b'a3\n':
ack = ser.readline()
print('Training Started...')
ack = ser.readline()
while ack != b'a4\n':
ack = ser.readline()
print('Training Successful!')
with flask_app.app_context():
sql = "INSERT INTO TrainingsLedger(doctorid, patientid, profileid, repetitions, timestamp) VALUES( ? , ? , ? , ? , date('now'));"
args = [doctorid, patientid, profileid, reps]
get_db().execute(sql, args)
get_db().commit()
sql = "UPDATE ExerciseProfiles SET lastused = date('now') WHERE profileid is ? ;"
args = [profileid]
get_db().execute(sql, args)
get_db().commit()
return
@celery.task()
def async_delete_profile(profile_no):
import sqlite3 as sql
connection = sql.connect("/home/pi/physioServer/PhysioServer/.physio.db")
cursor = connection.cursor()
if profile_no is not 0:
sql_statement = "DELETE FROM profiles WHERE profile_no IS {0};"\
.format(profile_no)
cursor.execute(sql_statement)
else:
sql_statement = """DROP TABLE IF EXISTS profiles;"""
cursor.execute(sql_statement)
connection.commit()
sql_statement = "CREATE TABLE IF NOT EXISTS profiles(profile_no INTEGER\
PRIMARY KEY AUTOINCREMENT, profile_desc varchar(1500) NOT NULL);"
cursor.execute(sql_statement)
connection.commit()
if __name__ == '__main__':
flask_app.run(debug=True, threaded=True, host='0.0.0.0', port=5000)