forked from acasadoalonso/SWiface
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathognddbfuncs.py
executable file
·197 lines (159 loc) · 7.52 KB
/
ognddbfuncs.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
#!/usr/bin/python3
import json
import socket
import urllib.request
import urllib.error
import urllib.parse
import requests
import config
from ping3 import ping
global _ogninfo_ # the OGN info data
_ogninfo_ = {} # the OGN info data
NOinfo = {"return": "NOinfo"}
####################################################################
HOST =config.DDBhost # OGN DDB host name to try first
PORT =config.DDBport # port to try
DDB_URL1 =config.DDBurl1 # url of where to get initially the DDB data
DDB_URL2 =config.DDBurl2 # second choice
#url = "http://ddb.glidernet.org/download/?j=2" # the OGN DDB source
prt =config.prt
####################################################################
def findfastestaprs(): # find the fastest APRS server
aprs=["glidern1.glidernet.org", # list of aprs.glidernet.org server
"glidern2.glidernet.org",
"glidern3.glidernet.org",
"glidern4.glidernet.org",
"glidern5.glidernet.org"]
p=999 # start with a high value
url=''
for u in aprs: # got thru all the servers
try:
pp=ping(u) # ping the server
except:
return (u)
if pp < p: # if faster ?
p=pp # remember the ping time
url=u # remember the URL
return(url) # return the URL of the fastest server
####################################################################
def servertest(host, port):
args = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM)
for family, socktype, proto, canonname, sockaddr in args:
s = socket.socket(family, socktype, proto)
try:
s.connect(sockaddr)
except socket.error:
return False
else:
s.close()
return True
####################################################################
def getddbdata(prt=False): # get the data from the API server
global _ogninfo_ # the OGN info data
if servertest(HOST, PORT):
DDB_URL=DDB_URL1
else:
DDB_URL=DDB_URL2
if prt:
print("DDB Connecting with: ", DDB_URL, HOST, PORT)
print("PING time: ", ping(HOST))
req = urllib.request.Request(DDB_URL)
req.add_header("Accept", "application/json") # it return a JSON string
req.add_header("Content-Type", "application/hal+json")
r = urllib.request.urlopen(req) # open the url resource
js=r.read().decode('UTF-8')
j_obj = json.loads(js) # convert to JSON
_ogninfo_ = j_obj # save the data on the global storage
return j_obj # return the JSON objecta
####################################################################
def getogninfo(devid): # return the OGN DDB infor for this device
global _ogninfo_ # the OGN info data
if len(_ogninfo_) == 0:
_ogninfo_=getddbdata(prt)
devices=_ogninfo_["devices"] # access to the ddbdata
for dev in devices: # loop into the registrations
if dev["device_id"] == devid: # if matches ??
return dev # return the information
return "NOInfo" # if not found !!!
####################################################################
def getognreg(devid): # get the ogn registration from the flarmID
global _ogninfo_ # the OGN info data
if len(_ogninfo_) == 0:
_ogninfo_=getddbdata()
devices=_ogninfo_["devices"] # access to the ddbdata
for dev in devices: # loop into the registrations
if dev["device_id"] == devid: # if matches ??
return dev["registration"] # return the registration
return "NOReg " # if not found !!!
####################################################################
def getognchk(devid): # Check if the FlarmID exist or NOT
global _ogninfo_ # the OGN info data
if len(_ogninfo_) == 0:
_ogninfo_ = getddbdata() # get the table from OGN DDB
devices = _ogninfo_["devices"] # access to the ddbdata
for dev in devices: # loop into the devices
if dev["device_id"] == devid: # if matches ??
return True
return False
####################################################################
def getognflarmid(registration): # get the FlarmID based on the registration
global _ogninfo_ # the OGN info data
if len(_ogninfo_) == 0:
_ogninfo_ = getddbdata()
devices = _ogninfo_["devices"] # access to the ddbdata
for dev in devices: # loop into the registrations
if dev["registration"] == registration: # if matches ??
if dev['device_type'] == "F":
dvce = "FLR"+dev['device_id']
elif dev['device_type'] == "I":
dvce = "ICA"+dev['device_id']
elif dev['device_type'] == "O":
dvce = "OGN"+dev['device_id']
else:
continue # Registrion found, but is not a Flarm/tracker
return dvce # return the flarmID
return "NOFlarm" # if not found !!!
####################################################################
def getogncn(devid): # get the ogn competition ID from the flarmID
global _ogninfo_ # the OGN info data
if len(_ogninfo_) == 0:
_ogninfo_ = getddbdata()
devices = _ogninfo_["devices"] # access to the ddbdata
for dev in devices: # loop into the compet
if dev["device_id"] == devid: # if matches ??
return dev["cn"] # return the competitionID
return "NID" # if not found !!!
####################################################################
def getognmodel(devid): # get the ogn aircraft model from the flarmID
global _ogninfo_ # the OGN info data
if len(_ogninfo_) == 0:
_ogninfo_ = getddbdata()
devices = _ogninfo_["devices"] # access to the ddbdata
for dev in devices: # loop into the registrations
if dev["device_id"] == devid: # if matches ??
return dev["aircraft_model"] # return the aircraft model
return "NoModel" # if not found !!!
###################################################################
def get_ddb_devices():
if servertest(HOST, PORT):
DDB_URL=DDB_URL1
else:
DDB_URL=DDB_URL2
r = requests.get(DDB_URL, allow_redirects=False)
for device in r.json()['devices']:
device.update({'identified': device['identified'] == 'Y',
'tracked': device['tracked'] == 'Y'})
yield device
####################################################################
def get_by_dvt(devdvt, dvt):
global _ogninfo_ # the OGN info dataa
cnt = 0
if len(_ogninfo_) == 0:
_ogninfo_ = getddbdata()
devices = _ogninfo_["devices"] # access to the ddbdata
for device in devices:
if device['device_type'] == dvt:
devdvt.append(device)
cnt += 1
return (cnt)
####################################################################