-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
114 lines (87 loc) · 3.9 KB
/
api.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
from flask import Flask
from flask_restful import Api, Resource, reqparse, abort
from flask_socketio import SocketIO
import atexit
from apscheduler.schedulers.background import BackgroundScheduler
import math
import pymongo
app = Flask(__name__)
app.config['SECRET_KEY'] = '!brahim123'
api = Api(app)
socketIo = SocketIO(app)
cluster = pymongo.MongoClient("mongodb+srv://driver:01858692mik@cluster0.imemt.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
db = cluster["driver"]
collection = db["rating"]
rider_put_args = reqparse.RequestParser()
rider_put_args.add_argument("id", type=int, help="rider id is required!", required=True)
rider_put_args.add_argument("name", type=str, help="name of a rider is required!", required=True)
rider_put_args.add_argument("current_location", action='append', help="current_location of a rider is required!", required=True)
rider_put_args.add_argument("destination", action='append', help="destination of a rider is required!", required=True)
riders = []
driver_put_args = reqparse.RequestParser()
driver_put_args.add_argument("id", type=int, help="driver id is required!", required=True)
driver_put_args.add_argument("name", type=str, help="name of a driver is required!", required=True)
driver_put_args.add_argument("car_number", type=str, help="car_number of a driver is required!", required=True)
driver_put_args.add_argument("current_location", action='append', help="current_location of a driver is required!", required=True)
drivers = []
rating_put_args = reqparse.RequestParser()
rating_put_args.add_argument("_id", type=int, help="driver id is required!", required=True)
rating_put_args.add_argument("name", type=str, help="name of a driver is required!", required=True)
rating_put_args.add_argument("rating", type=str, help="car_number of a driver is required!", required=True)
def abort_if_rider_already_exist(rider_id):
if rider_id in riders:
abort(409, message="rider already exist!!")
def abort_if_driver_already_exist(driver_id):
if driver_id in drivers:
abort(409, message="driver already exist!!")
@socketIo.on('message')
def serve(rider, driver):
print(f'rider {rider} is assign to driver {driver}')
socketIo.emit('message', {'rider': rider['name'], 'driver': driver['name'], 'driver_id': driver['id']}, namespace='/communication')
riders.remove(rider)
drivers.remove(driver)
def find_best_match():
for rider in riders:
x1 = float(rider['current_location'][0])
y1 = float(rider['current_location'][1])
# print(x1, y1)
min_distance = math.inf
best_match = None
for driver in drivers:
x2 = float(driver['current_location'][0])
y2 = float(driver['current_location'][1])
# print(x2, y2)
distance = math.sqrt(((x2-x1)**2) + ((y2-y1)**2))
if distance < min_distance:
min_distance = distance
best_match = driver
serve(rider, best_match)
scheduler = BackgroundScheduler()
scheduler.add_job(func=find_best_match, trigger="interval", seconds=20)
scheduler.start()
atexit.register(lambda: scheduler.shutdown())
class Rider(Resource):
def post(self):
# abort_if_rider_already_exist()
args = rider_put_args.parse_args()
riders.append(args)
# print(riders[rider_id]["current_location"])
return "recieved", 201
class Driver(Resource):
def post(self):
# abort_if_driver_already_exist(driver_id)
args = driver_put_args.parse_args()
drivers.append(args)
return "recieved", 201
class Rating(Resource):
def post(self):
args = rating_put_args.parse_args()
collection.insert_one(args)
print(args)
return "added to database", 201
api.add_resource(Rider, "/rider")
api.add_resource(Driver, "/driver")
api.add_resource(Rating, "/rating")
if __name__ == '__main__':
socketIo.run(app, debug=True)
scheduler.start()