forked from rafaeldsd/RSA-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation_obu.py
310 lines (281 loc) · 15 KB
/
simulation_obu.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import paho.mqtt.client as mqtt
import sys, json, multiprocessing as mp
from datetime import datetime
from coordinates import get_coords
import sqlite3, math, time
from db_obu import obu_db_create
from geographiclib.geodesic import Geodesic
from simulation_rsu import *
stop = None
def on_connect(client, userdata, flags, rc):
if rc==0:
print("OBU "+ str(client._client_id.decode("utf-8")) +": Connected OK")
else:
print("OBU "+ str(client._client_id.decode("utf-8")) +": Bad connection Returned code=",rc)
def on_disconnect(client, userdata, flags, rc=0):
print("OBU "+ str(client._client_id.decode("utf-8")) +": Disconnected result code "+str(rc))
def dif_heading (relative_heading, evr_heading):
if relative_heading >= evr_heading:
ref_heading=relative_heading-evr_heading
else:
ref_heading=evr_heading-relative_heading
if ref_heading>180:
ref_heading=360-ref_heading
return ref_heading
def get_obu_signalGoup (camHeading):
street_headdin_one_way, street_headdin_the_other_way = street_headding()
if int(camHeading) in range((street_headdin_one_way - 10), (street_headdin_one_way + 10)):
signalGroup = 0
elif int(camHeading) in range ((street_headdin_the_other_way - 10), (street_headdin_the_other_way + 10)):
signalGroup = 2
elif int(camHeading) in range((street_headdin_one_way - 80), (street_headdin_one_way -100)):
signalGroup = 1
elif int(camHeading) in range ((street_headdin_the_other_way - 80), (street_headdin_the_other_way - 100)):
signalGroup = 3
else:
signalGroup = 0
return signalGroup
def on_message(client, userdata, msg):
topic=msg.topic
m_decode=str(msg.payload.decode("utf-8","ignore"))
cam = None
denm = None
spatem = None
# get cam and denm messages
if topic == "vanetza/out/cam":
cam = json.loads(m_decode)
elif topic == "vanetza/out/denm":
denm = json.loads(m_decode)
elif topic == "vanetza/out/spatem":
spatem = json.loads(m_decode)
else:
print("RSU " + str(client._client_id.decode("utf-8")) + " received an unknown message")
return
broker = client._client_id.decode("utf-8")
# get the coordinates of this OBU
conn = sqlite3.connect('obu.db')
c = conn.cursor()
c.execute("SELECT * FROM obu WHERE ip=?", (broker,))
obu = c.fetchone()
conn.close()
if cam != None:
signalGroup = get_obu_signalGoup(cam['heading'])
else:
signalGroup = 0
# check what type of obu is
# if it is a car and an emergency vehicle without an emergency
if obu[6] == "CAR" or (obu[6] in ["AMBULANCE", "FIRE", "POLICE"] and obu[7] == False):
if denm != None:
# check if the car is within 200 meters of the OBU and if it is moving
if get_dis_dir(obu[1], obu[2], denm['fields']['denm']['management']['eventPosition']['latitude'], denm['fields']['denm']['management']['eventPosition']['longitude'])[0]<200 and denm['fields']['denm']['situation']['informationQuality'] == 7:
print("Other information: ")
print("\t-Detected a DENM: id(" + str(denm['fields']['denm']['management']['actionID']['originatingStationID']) + "), informationQuality(" + str(denm['fields']['denm']['situation']['informationQuality']) + ")", end=" ")
# check if it is emergency vehicle
if denm['fields']['denm']['situation']['eventType']['causeCode'] == 4:
print("and Emergency vehicle (", end="")
if denm['fields']['denm']['situation']['eventType']['subCauseCode'] == 2:
vehicle = "Ambulance"
print("Ambulance)")
print("\t-An ambulance is " + str(get_dis_dir(obu[1], obu[2], denm['fields']['denm']['management']['eventPosition']['latitude'], denm['fields']['denm']['management']['eventPosition']['longitude'])[0]) + " meters away in direction " + str(get_dis_dir(obu[1], obu[2], denm['fields']['denm']['management']['eventPosition']['latitude'], denm['fields']['denm']['management']['eventPosition']['longitude'])[1]) + ". Please take precautions!")
elif denm['fields']['denm']['situation']['eventType']['subCauseCode'] == 3:
vehicle = "Fire truck"
print("Fire truck)")
print("\t-A fire truck is " + str(get_dis_dir(obu[1], obu[2], denm['fields']['denm']['management']['eventPosition']['latitude'], denm['fields']['denm']['management']['eventPosition']['longitude'])[0]) + " meters away in direction " + str(get_dis_dir(obu[1], obu[2], denm['fields']['denm']['management']['eventPosition']['latitude'], denm['fields']['denm']['management']['eventPosition']['longitude'])[1]) + ". Please take precautions!")
elif denm['fields']['denm']['situation']['eventType']['subCauseCode'] == 4:
vehicle = "Police car"
print("Police car)")
print("\t-A police car is " + str(get_dis_dir(obu[1], obu[2], denm['fields']['denm']['management']['eventPosition']['latitude'], denm['fields']['denm']['management']['eventPosition']['longitude'])[0]) + " meters away in direction " + str(get_dis_dir(obu[1], obu[2], denm['fields']['denm']['management']['eventPosition']['latitude'], denm['fields']['denm']['management']['eventPosition']['longitude'])[1]) + ". Please take precautions!")
else:
print("Unknown)")
vehicle = "Unknown vehicle"
# check if the car is within 50 meters of the OBU
if get_dis_dir(obu[1], obu[2], denm['fields']['denm']['management']['eventPosition']['latitude'], denm['fields']['denm']['management']['eventPosition']['longitude'])[0] < 50:
print('\033[91m' + "\t-ATTENTION! "+ vehicle +" at " + str(get_dis_dir(obu[1], obu[2], denm['fields']['denm']['management']['eventPosition']['latitude'], denm['fields']['denm']['management']['eventPosition']['longitude'])[0]) + " meters in direction " + str(get_dis_dir(obu[1], obu[2], denm['fields']['denm']['management']['eventPosition']['latitude'], denm['fields']['denm']['management']['eventPosition']['longitude'])[1]) + "! Please take precautions immediately!" + '\033[0m')
print("\n")
if cam != None:
global stop
# check if the car is within 200 meters of the OBU and if it is moving
if get_dis_dir(obu[1], obu[2], cam['latitude'], cam['longitude'])[0]<200 and cam['speed'] > 0:
if 'emergencyContainer' in cam["specialVehicle"]:
if cam["specialVehicle"]["emergencyContainer"]["lightBarSirenInUse"]["lightBarActivated"] == True and cam["specialVehicle"]["emergencyContainer"]["lightBarSirenInUse"]["sirenActivated"] == True:
print("OBU " + str(client._client_id.decode("utf-8")) + ": " )
relative_heading = vehicle_heading(cam['latitude'], cam['longitude'], obu[1], obu[2])
print("Displaying emergency vehicle information: ")
if get_dis_dir(obu[1], obu[2], cam['latitude'], cam['longitude'])[0]<10:
print('\033[91m' + "\t -The emergency response vehicle is next to this veícle" + '\033[0m')
stop = True
elif dif_heading(relative_heading,int(cam['heading'])) < 60:
print('\033[93m' + "\t-There is an emergency response vehicle approaching" + '\033[0m')
stop = True
elif dif_heading(relative_heading,int(cam['heading'])) > 130:
print("\t-The emergency response vehicle is heading away")
stop = False
else:
print("\t-The emergency response vehicle is not coming on this direction")
stop = False
if spatem != None:
if spatem['fields']['spat']['intersections'][0]['states'][0]['state-time-speed'][0]['eventState'] == 6:
print("OBU " + str(client._client_id.decode("utf-8")) + ": " )
print("\t-Detected a SPATEM: signalGroup(" + str(spatem['fields']['spat']['intersections'][0]['states'][signalGroup]['signalGroup']) + ") - Displaying traffic light maneuver information: ")
print('\033[92m' + "\tThe Traffic Light is green, this vehicle can go trought the next crossroad safely\n" + '\033[0m')
def sendCam(client,obu,next_coord):
if obu[6] == "AMBULANCE" or obu[6] == "FIRE" or obu[6] == "POLICE":
f = open("messages/CAM_emergency.json", "r")
else:
f = open("messages/CAM_car.json", "r")
cam = json.load(f)
cam['stationID'] = obu[0]
cam['heading'] = vehicle_heading(obu[1], obu[2], next_coord['latitude'],next_coord['longitude'])
cam['latitude'] = obu[1]
cam['longitude'] = obu[2]
if obu[7] == True:
cam['specialVehicle']['emergencyContainer']["lightBarSirenInUse"]["lightBarActivated"] = True
cam['specialVehicle']['emergencyContainer']["lightBarSirenInUse"]["sirenActivated"] = True
client.publish("vanetza/in/cam", json.dumps(cam))
# print("Client " + client._client_id.decode("utf-8") + " published a CAM message")
f.close()
def sendDenm(client,obu,next_coord):
# Check what type of vehicle is
if obu[6] == "AMBULANCE":
f = open("messages/DENM_ambulance.json", "r")
elif obu[6] == "FIRE":
f = open("messages/DENM_firefighters.json", "r")
elif obu[6] == "POLICE":
f = open("messages/DENM_police.json", "r")
else:
print("OBU " + str(obu[0]) + " has a invalid type")
return
denm = json.load(f)
denm['management']['actionID']['sequenceNumber'] += 1
denm['management']['actionID']['originatingStationID'] = obu[0]
denm['management']['eventPosition']['latitude'] = obu[1]
denm['management']['eventPosition']['longitude'] = obu[2]
denm['management']['eventPosition']['positionConfidenceEllipse']['semiMajorOrientation'] = vehicle_heading(obu[1], obu[2], next_coord['latitude'],next_coord['longitude'])
denm['management']['detectionTime'] = datetime.timestamp(datetime.now())
denm['management']['referenceTime'] = datetime.timestamp(datetime.now())
client.publish("vanetza/in/denm", json.dumps(denm))
# print("Client " + client._client_id.decode("utf-8") + " published a DENM message")
f.close()
def get_dis_dir(lat1, lon1, lat2, lon2):
geo = Geodesic.WGS84.Inverse(lat1, lon1, lat2, lon2)
distance = geo['s12'] #in meters
heading = geo['azi2'] #in degrees clockwise from north
# heading from -180 to 180
if heading >-30 and heading <30:
heading = "North"
elif heading >30 and heading <60:
heading = "North-East"
elif heading >60 and heading <120:
heading = "East"
elif heading >120 and heading <150:
heading = "South-East"
elif heading >150 or heading <-150:
heading = "South"
elif heading >-150 and heading <-120:
heading = "South-West"
elif heading >-120 and heading <-60:
heading = "West"
elif heading >-60 and heading <-30:
heading = "North-West"
else:
heading = "Unknown"
return round(distance,3), heading
def vehicle_heading(lat1,lon1,lat2,lon2):
# only get values 0-360
degree = Geodesic.WGS84.Inverse(lat1, lon1, lat2, lon2)['azi2']
# cast degree to int
degree = int(round(degree,0))
if degree < 0:
degree += 360
# not valid degree
if degree not in range(0,360):
degree = 361
return degree
def obu_process(broker,id):
# Connect to MQTT broker
client = mqtt.Client(broker)
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
client.loop_start()
try:
client.connect(broker)
except:
print("Could not connect to broker: " + broker)
sys.exit(1)
# wait until the connection is established
while not client.is_connected():
pass
time.sleep(0.5)
client.subscribe('vanetza/out/denm')
client.subscribe('vanetza/out/cam')
client.subscribe('vanetza/out/spatem')
# read the coordinates from the file
with open('paths/coords'+str(id)+'.json') as json_file:
coords = json.load(json_file)
i = 0
print("\n")
# until it reaches the end of the path
while i < len(coords):
# get updated latitude and longitude of the OBU from the database
conn = sqlite3.connect('obu.db')
c = conn.cursor()
c.execute("SELECT * FROM obu WHERE id=?", (id,))
obu = c.fetchone()
conn.close()
# Obu has not finished the path yet
if (obu[1] != obu[3]) or (obu[2] != obu[4]):
# No emergency vehicle
if(obu[7] == False):
# Emergency vehicle nearby
if(stop == True):
# send CAM messages
sendCam(client,obu,coords[i])
else:
# update the OBU's position
conn = sqlite3.connect('obu.db')
c = conn.cursor()
c.execute("UPDATE obu SET ilatitude=?, ilongitude=? WHERE id=?", (coords[i]['latitude'], coords[i]['longitude'], id))
conn.commit()
conn.close()
# send CAM messages
sendCam(client,obu,coords[i])
i += 1
# Emergency vehicle
if(obu[7] == True):
# update the OBU's position
conn = sqlite3.connect('obu.db')
c = conn.cursor()
c.execute("UPDATE obu SET ilatitude=?, ilongitude=? WHERE id=?", (coords[i]['latitude'], coords[i]['longitude'], id))
conn.commit()
conn.close()
# send CAM messages
sendCam(client,obu,coords[i])
# send DENM messages
sendDenm(client,obu,coords[i])
i += 1
time.sleep(0.7)
print("OBU " + str(id) + " finished the path at " + str(datetime.now().time())[:8])
client.loop_stop()
client.disconnect()
def obu_sim(brokers):
processes = []
print("Starting OBU processes")
for broker in brokers:
print("Starting OBU process for broker: " + broker[0])
p = mp.Process(target=obu_process, args=[broker[0],broker[1]])
p.start()
processes.append(p)
for p in processes:
p.join()
print("OBU processes finished")
if __name__ == '__main__':
try:
obu_db_create()
obu_sim([("192.168.98.30",1), ("192.168.98.40",2), ("192.168.98.50",3), ("192.168.98.60",4)])
except KeyboardInterrupt:
print("Received interrupt signal. Stopping OBU processes...")
for p in mp.active_children():
p.terminate()
print("OBU processes stopped")
sys.exit(0)