forked from AnhellO/HackMTY_2016
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
85 lines (71 loc) · 2.1 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
from flask import Flask
from flask import jsonify
from flask import request
from flask import render_template
from flask import make_response
from flask.ext.httpauth import HTTPBasicAuth
import json
import datetime
app = Flask(__name__)
trucks = {}
last_pos = {}
# ============== Authentication =================
auth = HTTPBasicAuth()
@auth.get_password
def get_password(username):
if username == 'hack_mty':
return 'pumas'
return None
@auth.error_handler
def unauthorized():
return make_response(jsonify({'error': 'Unauthorized access'}), 401)
# ============== Data reset ================
@app.route('/reset', methods=['GET'])
@auth.login_required
def reset():
global trucks
global last_pos
trucks = {}
last_pos = {}
return jsonify({'response': '200'}), 201
# ============== Data input ================
@app.route('/truck', methods=['POST'])
@auth.login_required
def new_pos():
global trucks
global last_pos
if not request.json:
abort(400)
timestamp = datetime.datetime.utcnow() - datetime.timedelta(hours=6)
truck = {
'id': request.json['id'],
'timestamp': timestamp.strftime('%H:%M:%S - %d/%b/%y'),
'pos': request.json['pos'],
'dir': 'none'
}
if truck['id'] not in trucks:
trucks[truck['id']] = truck
last_pos[truck['id']] = truck
else:
trucks[truck['id']] = truck
if truck['pos'][1] < last_pos[truck['id']]['pos'][1]:
# Arteaga to Saltillo
trucks[truck['id']]['dir'] = 'Arteaga -> Saltillo'
else:
# Saltillo to Arteaga
trucks[truck['id']]['dir'] = 'Saltillo -> Arteaga'
last_pos[truck['id']] = truck
return jsonify({'truck': truck}), 201
# ============ Data output =================
@app.route("/data")
def data():
global trucks
result = {'trucks': [trucks[x] for x in trucks.keys()]}
#turn the results into valid JSON
print(trucks)
return jsonify(result)
@app.route('/')
def get_students():
return render_template("index.html")
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')