-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvehicles.py
56 lines (47 loc) · 1.54 KB
/
vehicles.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
from flask import Flask
from flask import request
import random # generate random data
import requests # send http requests
import time # time stamp and sleep
import json
import ip_address
EXP_TIME = 300 # seconds
SLEEP_TIME = 0.2
app = Flask(__name__)
PORT = 4000
parents = [ip_address.IP_T1]
ACK = 'ACK'
class Vehicle:
def __init__(self, id):
self.id = id
def send_data(self):
"""
generate random speed data and send it to the parent
each second for 100 seconds
"""
global parents
speed = random.randint(1, 101) # select a random speed
parent_index = random.randint(0, len(parents)-1) # select a parent out of the available with eq probability
payload = {'id':self.id, 'speed':speed}
ret = requests.get(parents[parent_index] + 'recv_data/',
params={'json':json.dumps(payload)})
if ret:
print("Vehicle {} successfully sent data to {}".format(self.id, parents[parent_index]))
else:
print("Vehicle {} counldn't send data to {}".format(self.id, parents[parent_index]))
@app.route('/simulate_vehicles', methods=['GET','POST'])
def send_data():
for i in range(EXP_TIME):
time.sleep(SLEEP_TIME)
for j in range(int(request.data)):
# rand_vehicle_id = random.randint(0, int(request.data)-1)
Vehicle(j).send_data()
return ACK
@app.route('/service_coordination/notify_divide')
def get_divide_notification():
global parents
payload = json.loads(request.args['json'])
parents = payload['active_siblings']
return ACK
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=PORT)