-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathF1_HotLaps.py
355 lines (288 loc) · 14.4 KB
/
F1_HotLaps.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# Records messages for a set number of seconds, then writes a file
import sys
import socket
import time
import struct
from threading import Thread
from queue import Queue
#import subprocess
from datetime import datetime
#import oci
from base64 import b64encode, b64decode
# Postgres DB stuff
import psycopg2
from psycopg2 import sql
from config import config
# Postgres insert sessionID and lapnumber (unique record)
# timestamp, sessionID, port, lapnumber, lapTimeMS, sector1MS, sector2MS, sector3MS
def insert_hotlap(tableName, timestamp, sessionID, port, lapnumber, lapTimeMS, sector1MS, sector2MS, sector3MS):
""" insert a new hotlap into the hotlaps table """
query = sql.SQL("INSERT INTO {table} (timestamp, sessionid, port, lapnumber, laptimems, sector1ms, sector2ms, sector3ms) VALUES(%s,%s,%s,%s,%s,%s,%s,%s) ON CONFLICT DO NOTHING").format(table=sql.Identifier(tableName))
# sql = """INSERT INTO %s(timestamp, sessionid, port, lapnumber, laptimems, sector1ms, sector2ms, sector3ms)
# VALUES(%s,%s,%s,%s,%s,%s,%s,%s)
# ON CONFLICT DO NOTHING;"""
conn = None
hotlap_id = None
try:
# read database configuration
params = config()
# connect to the PostgreSQL database
conn = psycopg2.connect(**params)
# create a new cursor
cur = conn.cursor()
# execute the INSERT statement
cur.execute(query, (timestamp, sessionID, port, lapnumber, lapTimeMS, sector1MS, sector2MS, sector3MS))
# get the generated id back
#hotlap_id = cur.fetchone()[0]
# commit the changes to the database
conn.commit()
# close communication with the database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return hotlap_id
# Postgres update trackID
# sessionID, trackID
def update_trackID(tableName, sessionID, trackID):
""" update a hotlap with trackID into the hotlaps table """
query = sql.SQL("UPDATE {table} SET trackid = %s WHERE sessionid = %s").format(table=sql.Identifier(tableName))
#sql = """UPDATE %s SET trackid = %s
# WHERE sessionid = %s"""
conn = None
hotlap_id = None
try:
# read database configuration
params = config()
# connect to the PostgreSQL database
conn = psycopg2.connect(**params)
# create a new cursor
cur = conn.cursor()
# execute the INSERT statement
cur.execute(query, (trackID, sessionID))
# get the generated id back
#hotlap_id = cur.fetchone()[0]
# commit the changes to the database
conn.commit()
# close communication with the database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return hotlap_id
# Postgres update participant
# sessionID, participant
def update_participant(tableName, sessionID, participant):
""" update a hotlap with trackID into the hotlaps table """
query = sql.SQL("UPDATE {table} SET participant = %s WHERE sessionid = %s").format(table=sql.Identifier(tableName))
#sql = """UPDATE %s SET participant = %s
# WHERE sessionid = %s"""
conn = None
hotlap_id = None
try:
# read database configuration
params = config()
# connect to the PostgreSQL database
conn = psycopg2.connect(**params)
# create a new cursor
cur = conn.cursor()
# execute the INSERT statement
cur.execute(query, (participant, sessionID))
# get the generated id back
#hotlap_id = cur.fetchone()[0]
# commit the changes to the database
conn.commit()
# close communication with the database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return hotlap_id
listeningPort = 20777
for item in sys.argv:
if "port" in item:
if len(item.split("=")) > 1:
if item.split("=")[1].isdigit():
listeningPort = int(item.split("=")[1])
print("HotLaps: Game data is on port:", listeningPort)
#Receiver socket from splitter
UDP_PORT = 20774
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.settimeout(15)
server_socket.bind(('', UDP_PORT))
print(" F1_HotLaps receiving daa from splitter on port: ", UDP_PORT)
keepGoing = True
counterLap = 0
counterLapSent = 0
counterTrack = 0
counterTrackSent = 0
counterParticipants = 0
counterParticipantsSent = 0
while keepGoing:
message = None
try:
message, address = server_socket.recvfrom(1024000)
except:
print("Timeout: waiting for more data.", str(datetime.now()))
# Just the hotlaps messages
# Meters each message if hotlap data is requested
if message is not None:
# Check minimal length for header
if len(message) < 24:
print("Bad message, length: ", len(message))
continue
# This is the session history packet with laps, packet 11
if message[5:6] == b'\x0b':
# Check message is correct length, per documentation
if len(message) != 1155:
print("Bad packet 11 message, should be 1155, was length: ", len(message))
continue
counterLap = counterLap + 1
# Unpack and send for now
# Header
header = struct.unpack('<HBBBBQfIBB', message[0:24])
#print(header)
sessionID = header[5]
timeInSession = header[6]
# Bytes
byteCode = '<BBBBBBB'
for x in range(100):
byteCode = byteCode + 'IHHHB'
for x in range(8):
byteCode = byteCode + 'BBB'
unpacked = struct.unpack(byteCode, message[24:1191])
player1Index = header[8]
thisDataIndex = unpacked[0]
startIndex = 7
slugSize = 5
#print("P1 = :", player1Index, "This data index:", thisDataIndex)
if player1Index == thisDataIndex:
#print(unpacked)
bestLapTimeLapNum = unpacked[3]
if bestLapTimeLapNum > 0 and bestLapTimeLapNum <= 100:
# Grab just the fastest lap
#print(" Fastest Lap Number: ", unpacked[3])
#print(unpacked[0], unpacked[1], unpacked[2], unpacked[3], unpacked[4], unpacked[5], unpacked[6], unpacked[7])
lapTime = unpacked[startIndex + (bestLapTimeLapNum - 1) * slugSize]
sector1 = unpacked[startIndex + (bestLapTimeLapNum - 1) * slugSize + 1]
sector2 = unpacked[startIndex + (bestLapTimeLapNum - 1) * slugSize + 2]
sector3 = unpacked[startIndex + (bestLapTimeLapNum - 1) * slugSize + 3]
validFlag = unpacked[startIndex + (bestLapTimeLapNum - 1) * slugSize + 4]
timeStamp = int(time.time() * 1000)
if validFlag == 15:
if lapTime > 0 and sector1 > 0 and sector2 > 0 and sector3 > 0:
print(" Fastest Lap Number: ", bestLapTimeLapNum, lapTime/1000, sector1/1000, sector2/1000, sector3/1000)
#print(timeStamp, sessionID, listeningPort, bestLapTimeLapNum, lapTime, sector1, sector2, sector3, validFlag)
insert_hotlap("hotlaps", timeStamp, sessionID, listeningPort, bestLapTimeLapNum, lapTime, sector1, sector2, sector3)
insert_hotlap("hotlaps_archive", timeStamp, sessionID, listeningPort, bestLapTimeLapNum, lapTime, sector1, sector2, sector3)
counterLapSent = counterLapSent + 1
#print(unpacked)
else:
# Have to loop through and find the fastest valid lap
lapTime = 0
sector1 = 0
sector2 = 0
sector3 = 0
validFlag = 0
fastestValidLapIndex = 0
fastestLapTimeSoFar = 0
for i in range(0,100):
validFlag = unpacked[startIndex + (i * slugSize) + 4]
if validFlag == 15:
lapTime = unpacked[startIndex + (i * slugSize)]
if lapTime > 0:
if fastestValidLapIndex == 0:
fastestValidLapIndex = i + 1
fastestLapTimeSoFar = lapTime
else:
if lapTime > 0 and lapTime < fastestLapTimeSoFar:
fastestValidLapIndex = i + 1
fastestLapTimeSoFar = lapTime
if fastestValidLapIndex < 1 or fastestValidLapIndex > 100:
print("---No valid laps so far...", "time in session:", timeInSession)
else:
#print(" Fastest Lap Number: ", fastestValidLapIndex)
lapTime = unpacked[startIndex + (fastestValidLapIndex - 1) * slugSize]
sector1 = unpacked[startIndex + (fastestValidLapIndex - 1) * slugSize + 1]
sector2 = unpacked[startIndex + (fastestValidLapIndex - 1) * slugSize + 2]
sector3 = unpacked[startIndex + (fastestValidLapIndex - 1) * slugSize + 3]
validFlag = unpacked[startIndex + (fastestValidLapIndex - 1) * slugSize + 4]
timeStamp = int(time.time() * 1000)
if validFlag == 15:
if lapTime > 0 and sector1 > 0 and sector2 > 0 and sector3 > 0:
#print(timeStamp, sessionID, listeningPort, fastestValidLapIndex, lapTime, sector1, sector2, sector3, validFlag)
print(" Fastest Lap Number: ", fastestValidLapIndex, lapTime/1000, sector1/1000, sector2/1000, sector3/1000)
insert_hotlap("hotlaps", timeStamp, sessionID, listeningPort, fastestValidLapIndex, lapTime, sector1, sector2, sector3)
insert_hotlap("hotlaps_archive", timeStamp, sessionID, listeningPort, fastestValidLapIndex, lapTime, sector1, sector2, sector3)
counterLapSent = counterLapSent + 1
#print(unpacked)
if counterLap > 0 and counterLap % 500 == 0:
print(" Packet 11 messages received: " + str(counterLap))
if counterLapSent > 0 and counterLapSent % 500 == 0:
print(" Packet 11 messages sent to DB: " + str(counterLapSent))
# This is the trackID
elif message[5:6] == b'\x01':
# Check message is correct length, per documentation
if len(message) != 632:
continue
counterTrack = counterTrack + 1
# Unpack and send for now
# Header
header = struct.unpack('<HBBBBQfIBB', message[0:24])
#print(header)
sessionID = header[5]
packet_part_1 = struct.unpack('<BbbBHBbBHHBBBBBB', message[24:43])
trackID = packet_part_1[6]
if trackID >= 0:
update_trackID("hotlaps", sessionID, trackID)
update_trackID("hotlaps_archive", sessionID, trackID)
#print("trackID", trackID)
counterTrackSent = counterTrackSent + 1
if counterTrack > 0 and counterTrack % 500 == 0:
print(" Packet 01 messages received: " + str(counterTrack))
if counterTrackSent > 0 and counterTrackSent % 500 == 0:
print(" Packet 01 messages sent to DB: " + str(counterTrackSent))
# This is the participants
elif message[5:6] == b'\x04':
# Check message is correct length, per documentation
if len(message) != 1257:
continue
counterParticipants = counterParticipants + 1
# Unpack and send for now
# Header
header = struct.unpack('<HBBBBQfIBB', message[0:24])
#print(header)
sessionID = header[5]
player1Index = header[8]
# Bytes - starts with numCars B
byteCode = '<B'
for x in range(22):
byteCode = byteCode + 'BBBBBBB48sB'
unpacked = struct.unpack(byteCode, message[24:1257])
# Decode Driver Name strings
unpackedList = list(unpacked)
#print(unpackedList[8].decode('utf8', 'strict'), "hey")
position = 7
nameList = []
for x in range(22):
nameList.append(str(unpackedList[(x * 9) + position + 1].decode('utf8', 'strict').replace('\x00', '')))
for x in range(22):
unpackedList[(x * 9) + position + 1] = nameList[x]
unpacked = tuple(unpackedList)
participant = unpacked[1 + (player1Index * 9) + position]
if len(participant) > 0:
update_participant("hotlaps", sessionID, participant)
update_participant("hotlaps_archive", sessionID, participant)
#print(sessionID, participant)
counterParticipantsSent = counterParticipantsSent + 1
#lapInfo = unpacked[player1Index * slugSize: player1Index * slugSize + slugSize]
if counterParticipants > 0 and counterParticipants % 500 == 0:
print(" Packet 04 messages received: " + str(counterParticipants))
if counterParticipantsSent > 0 and counterParticipantsSent % 500 == 0:
print(" Packet 04 messages sent to DB: " + str(counterParticipantsSent))