-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateHistoryServer.py
63 lines (44 loc) · 1.93 KB
/
gateHistoryServer.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
##################### Library Imports #####################
import gateHistory
from flask import Flask, request, jsonify, abort
import datetime
# -- ADINT Final Project
# -- Made by: Diogo Ferreira and Rafael Cordeiro
# ----------------------------------------
# --------------Gate History Server----------
# ----------------------------------------
#Flask
app = Flask(__name__)
@app.route("/gate/occurrences/history",methods = ['GET'])
def listHistRequest():
# Call a query to list the occurrences
User_list = gateHistory.getgateHistory()
result1 = [{'id_gate_occurence':item.id_gate_occurence,'gate_id':item.gate_id,'Status':item.Status,'Date':item.Date} for item in User_list]
return jsonify({"history": result1})
@app.route("/gate/occurrences/<path:gateID>/history",methods = ['GET'])
def listGateHistRequest(gateID):
User_list = gateHistory.GetGateOccurrences(gateID)
result1 = [{'id_gate_occurence':item.id_gate_occurence,'gate_id':item.gate_id,'Status':item.Status,'Date':item.Date} for item in User_list]
return jsonify({"history": result1})
@app.route("/gate/occurrences/newOccurrence",methods = ['POST'])
def newOccurrenceRequest():
#retrieve data from input JSON body
data = request.json
try:
int(data["gate_id"])
data["Status"]
except:
abort(400)
try:
status = gateHistory.newOcurrence(int(data["gate_id"]),data["Status"],datetime.datetime.now())
except:
return jsonify({'StatusCode':'2', 'Description':'Err'})
if status:
#return the secret number as JSON
return jsonify({'StatusCode':'1', 'Description':'OK'})
else:
# The ID already exists - Not Admitted
return jsonify({'StatusCode':'3', 'Description':'ID Already Exists'})
#Start server
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8002, debug=True)