-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfr24ListenerJsonDBnoMQ.py
433 lines (331 loc) · 13.6 KB
/
fr24ListenerJsonDBnoMQ.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import sys
import os
import json
import time
#from datetime import date
import datetime
import requests
from termcolor import colored
import sqlite3
from sqlite3 import Error
#sys.path.append('/home/beed2112/fr24')
from aircraft import Aircraft
from nohitAircraft import noHit
# cleanup aged Noaircraft
def cleanNoHitAircraft():
# global aircraftSession
count = 0
removed = 0
for p in noHitSession:
age = lastCleanupTimeAircraft - p.noHitWhenSeenComputer
ageMinutes = age.total_seconds() / 60
if (ageMinutes > (purgeMinutesNoHit)):
# if (p.aircraftID in aircraftSession):
noHitSession.pop(count)
removed +=1
count += 1
outLine = " ----------> " + time.asctime(time.localtime(time.time()))+ " Removed NoHit Aircraft " + str(removed)
outcolor = "blue"
print(colored(outLine, outcolor))
return
# cleanup aged aircraft
def cleanAircraft():
# global aircraftSession
count = 0
removed = 0
for p in aircraftSession:
age = lastCleanupTimeNoHit - p.aircraftWhenSeenComputer
ageMinutes = age.total_seconds() / 60
if (ageMinutes > purgeMinutesAircraft):
# if (p.aircraftID in aircraftSession):
aircraftSession.pop(count)
removed +=1
count += 1
outLine = " ----------> " + time.asctime(time.localtime(time.time()))+ " Removed Aircraft " + str(removed)
outcolor = "blue"
print(colored(outLine, outcolor))
return
#check Object to see if aircraft has been seen this session
def isKnownPlane(aircraftID):
global aircraftSession
for p in aircraftSession:
if( p.aircraftID == aircraftID):
return True
return False
# return the index of the aircraft
def returnPlaneIndex(aircraftID):
global aircraftSession
count = 0
for p in aircraftSession:
if( p.aircraftID == aircraftID):
return count
count = count + 1
return -1
# see if we have already failed to find info on the aircraft
def isKnownNoHitCheck(aircraftID):
for p in noHitSession:
if( p == aircraftID):
return True
return False
# add an aircraft to the list used during the session
def addNoHit(aircraftID):
p = noHit(str(aircraftID))
localtime = time.asctime( time.localtime(time.time()) )
localtimeComputer = datetime.datetime.now()
p.set_noHitWhenSeen(str(localtime))
p.set_noHitWhenSeenComputer(localtimeComputer)
noHitSession.append(p)
# and records to DB when interesting aircraft identified
def addAircraftDB(icaohex):
if (not isKnownPlaneDB(str(icaohex))):
conn = create_connection(database)
cur = conn.cursor()
epochTime = time.time()
cur.execute("INSERT INTO AIRCRAFT VALUES(?,?,?,?,?,?);",(icaohex, owners, strICAO, strReg, strType, epochTime ))
#cur.execute("INSERT INTO AIRCRAFT VALUES('icaohex2','2','3','4','5', 6);")
cur = conn.commit
cur = conn.close
#add an aircraft to the session object
def addAircraft(aircraftID):
global aircraftSession
global icaoData
global localtime
global interestingAircraftCount
global strICAO
global strReg
global strType
global strAircraftID
global owners
# icao_response = requests.get(f'https://hexdb.io/api/v1/aircraft/{aircraftID}')
# icao_data = icao_response.json()
icaoData ="False"
# if icao_response.status_code == 200:
icaoData ="True"
if str(icao_data['OperatorFlagCode']) not in excludeOperatorList:
owners=icao_data['RegisteredOwners']
strICAO = str(icao_data['OperatorFlagCode'])
strReg = str(icao_data['Registration'])
strType =str(icao_data['Type'])
strAircraftID = str(aircraftID)
p = Aircraft(str(aircraftID))
p.set_Registration(str(icao_data['Registration']))
p.set_OperatorFlagCode(str(icao_data['OperatorFlagCode']))
p.set_Type(str(icao_data['Type']))
p.set_Owner(str(icao_data['RegisteredOwners']))
localtime = time.asctime( time.localtime(time.time()) )
localtimeComputer = datetime.datetime.now()
p.set_WhenSeen(str(localtime))
p.set_WhenSeenComputer(localtimeComputer)
# this version is being used on a mobil experiment... we want to log all planes we see so we can look into later
#add the aircraft to the database
addAircraftDB(aircraftID)
conn = create_connection(database)
cur = conn.cursor()
epochTime = time.time()
cur.execute("INSERT INTO AIRCRAFTSIGHTINGS VALUES(?,?);",(icaohex,epochTime ))
cur = conn.commit
cur = conn.close
if (interestingAircraft()):
p.set_Interesting("True")
p.set_AlertTime(localtimeComputer)
else:
p.set_Interesting("False")
addIfNewNotInterestinig(aircraftID)
aircraftSession.append(p)
# return
#is this an aircraft we are interested in
def interestingAircraft():
owners=icao_data['RegisteredOwners']
strICAO = str(icao_data['OperatorFlagCode'])
strReg = str(icao_data['Registration'])
global interestingAircraftCount
interestCount = 0
o= 0
while o < len(watchlistOwner):
if watchlistOwner[o] in owners:
interestCount += 1
o += 1
if strICAO in watchICAO:
interestCount += 1
if strReg in watchReg:
interestCount += 1
if interestCount > 0:
interestingAircraftCount += 1
return True
else:
return False
##-----------------------------------------------------------------------------------------------------------------------------###
## this is DB interaction code
def create_connection(db_file):
conn = None
try:
conn = sqlite3.connect(db_file , isolation_level = None)
except Error as e:
print(e)
return conn
def isKnownPlaneDB(aircraftID):
# create a database connection
conn = create_connection(database)
cur = conn.cursor()
cur.execute("SELECT * FROM AIRCRAFT WHERE AIRCRAFTID=?;", (aircraftID,))
rows = cur.fetchall()
cur.close
for row in rows:
#print(row)
return True
return False
def addIfNewNoHit(aircraftID):
# create a database connection
conn = create_connection(database)
cur = conn.cursor()
cur.execute("SELECT * FROM NOHITAIRCRAFT WHERE AIRCRAFTID=?;", (aircraftID,))
rows = cur.fetchall()
cur.close
for row in rows:
#print(row)
return False
conn1 = create_connection(database)
cur1 = conn1.cursor()
epochTime = time.time()
cur1.execute("INSERT INTO NOHITAIRCRAFT VALUES(?,?);",(aircraftID,epochTime ))
cur1 = conn1.commit
cur1 = conn1.close
return True
def addIfNewNotInterestinig(aircraftID):
# create a database connection
conn = create_connection(database)
cur = conn.cursor()
cur.execute("SELECT * FROM AIRCRAFTNOTINTERESTING WHERE AIRCRAFTID=?;", (aircraftID,))
rows = cur.fetchall()
cur.close
for row in rows:
#print(row)
return False
conn = create_connection(database)
cur = conn.cursor()
epochTime = time.time()
cur.execute("INSERT INTO AIRCRAFTNOTINTERESTING VALUES(?,?,?,?,?,?);",(icaohex, owners, strICAO, strReg, strType, epochTime ))
#cur.execute("INSERT INTO AIRCRAFT VALUES('icaohex2','2','3','4','5', 6);")
cur = conn.commit
cur = conn.close
return True
##-----------------------------------------------------------------------------------------------------------------------------###
### mainline
sampling_period =60
sampling_period_seconds = int(sampling_period)
#excludeOperatorList="AAL,ASA,UAL,SWA,FFT,SKW,WJA,FLE,AAY,ASH,DAL,ENY,NKS,VOI,JBU,WSW"
excludeOperatorList=" "
watchlistOwner= ["India","Federal", "United States", "Oprah", "Police", "State Farm", "Sherrif", "Arizona Department", "NASA", "Air Force", "Museum", "Google", "Apple", "Penske" , "Cardinals"]
watchReg="N44SF,N812LE,N353P,N781MM,N88WR,N383LS,N78HV"
watchICAO="F16,S211,BE18,AJET,KMAX,HGT,ST75,RRR,MRF1,CKS,L1P,T6,BGR"
global aircraftSession
global noHitSession
global icao_data
global icao_response
global icaohex
global database
global purgeMinutesAircraft
global lastCleanupTimeAircraft
global purgeMinutesNoHit
global lastCleanupTimeNoHit
database = "aircraft/aircraftMon.db"
aircraftSession = []
noHitSession = []
interestingAircraftCount = 0
alertCount = 0
nohit=0
lastCleanupTimeAircraft = datetime.datetime.now()
purgeMinutesAircraft = 180
lastCleanupTimeNoHit = datetime.datetime.now()
purgeMinutesNoHit = 15
#wwII trainer no hit example https://globe.adsbexchange.com/?icao=a2d458
# grab aircraft.json from the reciever
receiver_url ='http://localhost'
adsbExchangeBase = 'https://globe.adsbexchange.com/?icao='
go = 0
while go == 0 :
#r = requests.get(f'{receiver_url}/dump1090/{getIt}')
r = requests.get(f'{receiver_url}/dump1090/data/aircraft.json')
#readsb version location
r = requests.get(f'{receiver_url}/aircraft.json')
if r.status_code != 200:
raise ValueError(f'ERROR: getting aircraft json data :{r.text}')
aircraft_data = r.json()
now = aircraft_data['now']
info_data = {
'now': now,
'aircraft_count' : len(aircraft_data['aircraft']),
'messages': aircraft_data['messages']
}
print ("+++--------------------------------------------------------------------------------------------------------------------------------------------------"+ time.asctime(time.localtime(time.time()))+ "-----" + str(info_data['aircraft_count']) + "----+++")
#loop thru the aircraft
i = 00
while i < info_data['aircraft_count']:
icaohex = aircraft_data['aircraft'][i]['hex']
knownNoHit = "False"
if ( not isKnownNoHitCheck(icaohex) ):
knownNoHit = "False"
icao_response = requests.get(f'https://hexdb.io/api/v1/aircraft/{icaohex}')
icao_data = icao_response.json()
if (icao_response.status_code == 200 ):
if str(icao_data['OperatorFlagCode']) not in excludeOperatorList:
knownPlane= "False"
if(len(aircraftSession) > 0):
#Check if Plane ID Exists
if( isKnownPlane(str(icaohex))):
#Get Plane Item # so we can update them
itemNum = returnPlaneIndex(str(icaohex))
#Update Plane seen time if current time + 15 min > current time seen value
knownPlane= "True"
else:
#Add Plane
addAircraft(str(icaohex))
itemNum = len(aircraftSession)-1
else:
#Add Plane
addAircraft(str(icaohex))
itemNum = len(aircraftSession)-1
adsbExchangeBaseFull = adsbExchangeBase + str(icaohex)
outcolor="white"
minutes = 0
if (str(aircraftSession[itemNum].get_Interesting()) == 'True'):
outcolor="green"
timeSince = datetime.datetime.now() - aircraftSession[itemNum].get_AlertTime()
minutes = timeSince.total_seconds() / 60
if ((aircraftSession[itemNum].get_AlertTime()) == aircraftSession[itemNum].get_WhenSeenComputer() or minutes > 15):
outcolor="yellow"
localtimeComputer = datetime.datetime.now()
aircraftSession[itemNum].set_AlertTime(localtimeComputer)
conn = create_connection(database)
cur = conn.cursor()
epochTime = time.time()
cur.execute("INSERT INTO AIRCRAFTSIGHTINGS VALUES(?,?);",(icaohex,epochTime ))
cur = conn.commit
cur = conn.close
outLine = time.asctime(time.localtime(time.time()))+ " | " + str(aircraftSession[itemNum].get_WhenSeen()) + " | " + str(aircraftSession[itemNum].get_aircraftID())+ " | "+ str(aircraftSession[itemNum].get_Registration()) + " | " + str(aircraftSession[itemNum].get_Owner())+ " | " + str(aircraftSession[itemNum].get_OperatorFlagCode()) + " | " + str(aircraftSession[itemNum].get_Type() + " | " + adsbExchangeBaseFull)
print(colored(outLine, outcolor))
else:
#noHitSession.append(icaohex)
addNoHit(icaohex)
outcolor = "blue"
outLine = time.asctime(time.localtime(time.time()))+ " | " + str(icaohex)
print(colored(outLine, outcolor))
nohit += 1
addIfNewNoHit(icaohex)
timeSinceLastCleanupAircraft = datetime.datetime.now() - lastCleanupTimeAircraft
minutesSinceLastCleanupAircraft = timeSinceLastCleanupAircraft.total_seconds() / 60
if (minutesSinceLastCleanupAircraft> purgeMinutesAircraft):
cleanAircraft()
lastCleanupTimeAircraft = datetime.datetime.now()
timeSinceLastCleanupNohit = datetime.datetime.now() - lastCleanupTimeNoHit
minutesSinceLastCleanupNohit = timeSinceLastCleanupNohit.total_seconds() / 60
if (minutesSinceLastCleanupNohit > purgeMinutesNoHit):
cleanNoHitAircraft()
lastCleanupTimeNoHit = datetime.datetime.now()
i += 1
myobj = datetime.datetime.now()
myHour = myobj.hour
myMinute = myobj.minute
# dealing with time https://thispointer.com/how-to-add-minutes-to-datetime-in-python/
# https://globe.adsbexchange.com/?icao=aa8ef0
# aa43ee | N7600P | SAS Institute Inc | F9EX | Falcon 900EX EASy z