-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
122 lines (104 loc) · 4.22 KB
/
Copy pathapp.py
File metadata and controls
122 lines (104 loc) · 4.22 KB
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
from flask import Flask, render_template, Response, session, escape, request
from flask_api import status
from db_connector import *
from importlib import import_module
import os
import time
from camera_opencv import Camera
webapp = Flask(__name__)
db_connection = connect_to_database()
#TODO: generate this unique everytime using os.urandom(16)
webapp.secret_key = b'8\x9f\xf5\x83j\x9e\xaa\x83\x07+Ai\x9f\xd9\xc5_'
@webapp.route("/")
def main():
return render_template('record.html')
@webapp.route('/trail')
#the name of this function is just a cosmetic thing
def trail_show():
print("Fetching and rendering people web page")
query = "SELECT * from trail_action_log;"
result = execute_query(db_connection, query).fetchall();
print(result)
return render_template('trail.html', rows=result)
@webapp.route('/trail-action/record/<action>', methods=['GET'])
#records a trail action
def trail_action_record(action):
#Don't record an action if the trail hasn't started!
#TODO: Make this error actually appear as error the Jquery Ajax requester in groundedCoding.js
if not 'trail_id' in session or session['trail_id'] is None:
error = "You need to start the trail before you can record an action!"
print(error)
return error, status.HTTP_500_INTERNAL_SERVER_ERROR
print("Recording an action on the trail")
query = """
INSERT INTO trail_action_log
(trail_id, time_in_trail, action)
VALUES
(%s, %s, %s)
"""
#TODO: calculate the timestamp
timestamp = time.time() - float(session['trail_start_time'])
data = (session['trail_id'], timestamp, action)
execute_query(db_connection, query, data)
return("Action recorded");
@webapp.route('/trail/start', methods=['POST'])
def start_trail():
print("Hi");
session['trail_start_time'] = time.time() #Time in UTC!
#TODO: Get data about this trail from the form!
recorder_name = request.form['username']
trail_number = request.form['trialnumber']
print(recorder_name);
print(trail_number);
#Create a record for this trail in traildb
print("Saving the notes for this trail by updating this trail's record")
query = """
INSERT INTO trail
(recorder_name, trail_number, started_on)
VALUES
(%s, %s, %s)
"""
#TODO: FIXIT: now() doesn't actually insert the now time in the database for some reason
data = (recorder_name, trail_number, time.strftime('%Y-%m-%d %H:%M:%S'));
trail_id = execute_query(db_connection, query, data, send_last_inserted_id=True)
#TODO: Get the last inserted id and put it as teh trail id in the sesion data
session['trail_id'] = trail_id
#Record this action of starting the trail!
trail_action_record('Starting the trail')
print ("Trail started with the id %s and on time %s" % (session['trail_id'], session['trail_start_time']));
return str(session['trail_start_time'])
#this will save the trail
@webapp.route('/trail/stop', methods=['POST'])
def stop_trail():
#TODO: Stop video recording and save the video!
#do the magic somehow!
pass
#save the notes to the database for this specific trail
print("Saving the notes for this trail by updating this trail's record")
query = """
UPDATE trail
SET trail_notes = %s,
stopped_on = %s
WHERE trail_id = %s
"""
data = (request.form['notes'], time.strftime('%Y-%m-%d %H:%M:%S'), session['trail_id']);
execute_query(db_connection, query, data)
#Create an entry in the trail log about this stop action
trail_action_record("Stopping the trail")
#Delete the session data!
session.pop('trail_id', None)
session.pop('trail_start_time', None)
return("Trail Stopped!")
def gen(camera):
"""Video streaming generator function."""
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@webapp.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen(Camera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
webapp.run(host='0.0.0.0', threaded=True, debug=False)