-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
110 lines (81 loc) · 3.08 KB
/
service.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
from flask import Flask, jsonify, request
from flask_cors import CORS
import json
from plot_driver_location import *
# Plot and save truck path on the server
# Plot and save truck path on the server
is_plot_server = False
## Part 0: loading legs, stops and driver location data
f = open(".\data\legs.json")
legs = json.load(f)
f.close()
f = open(".\data\stops.json")
stops = json.load(f)
f.close()
f = open(".\data\driver_location.json")
driver_location = json.load(f)
f.close()
f = open(".\data\\bonus_driver_location.json")
bonus_driver_location = json.load(f)
f.close()
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
## Part 1: adding a REST API to retrieve the given list of stops and legs from server
# GET list of legs
@app.route("/legs", methods=["GET"])
def get_legs():
return jsonify({
"legs": legs
})
# GET list of legs
@app.route("/stops", methods=["GET"])
def get_stops():
return jsonify({
"stops": stops
})
## Part 3: adding a REST API to get the driver’s current position
# GET driver’s current position
@app.route("/driver", methods=["GET"])
def get_driver_location():
return jsonify({
"Driver’s current position": driver_location
})
## Part 7: adding a REST API to update the driver’s current position
# PUT driver’s current position
@app.route("/driver", methods=["PUT"])
def put_driver_location():
provided_driver_location = request.json["Driver’s current position"]
globals()["driver_location"] = provided_driver_location
if is_plot_server:
# Data visualization
if "stops_dict" not in globals():
globals()["legs_dict"] = convert_list_to_dict(legs, "legID")
globals()["stops_dict"] = convert_list_to_dict(stops, "name")
plot_truck_data(stops, driver_location, globals()["stops_dict"])
return jsonify({
"Driver’s current position": driver_location
})
## BONUS 1 (Part 2): adding a REST API to update the bonus driver’s current position
# PUT bonus driver’s current position
@app.route("/bonusdriver", methods=["PUT"])
def put_bonus_driver_location():
provided_driver_location = request.json["Driver’s current position"]
globals()["bonus_driver_location"] = provided_driver_location
return jsonify({
"Driver’s current position": bonus_driver_location
})
## BONUS 1 (Part 3): adding a REST API to get the bonus driver’s current position
# GET bonus driver’s current position
@app.route("/bonusdriver", methods=["GET"])
def get_bonus_driver_location():
return jsonify({
"Driver’s current position": bonus_driver_location
})
if __name__=="__main__":
if is_plot_server:
# Initial data visualization
if "stops_dict" not in globals():
legs_dict = convert_list_to_dict(legs, "legID")
stops_dict = convert_list_to_dict(stops, "name")
plot_truck_data(stops, driver_location, stops_dict)
app.run(debug=True, port=8080)