/
FlightTracker.py
303 lines (243 loc) · 9.17 KB
/
FlightTracker.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
import wx
import time
import sys
import os
import stomp
import wx.html2
import math
import wx.lib.scrolledpanel as scrolled
from datetime import datetime
from FGObject import FGObject
from FGEnvironmentObject import FGEnvironmentObject
import FGParser
from EnvironmentPanel import EnvironmentPanel
from PlaneSelectPanel import PlaneSelectPanel
from GPSPanel import GPSPanel
from FlightPanel import FlightPanel
from MyListener import MyListener
from PyMap import PyMap
from Log import Log
from FGLog import FGLog
from ConnectionDialog import ConnectionDialog
EXIT = wx.NewId()
LOG = wx.NewId()
CONNECTION = wx.NewId()
#This Class is in charge of the main program: FlightTracker. It creates a window in which all the functionalities of the other classes lie.
class FlightTracker(wx.Frame):
def __init__(self, parent, id,):
#Setup the frame
super(FlightTracker, self).__init__(parent, size=(1024,700), style = wx.DEFAULT_FRAME_STYLE)
self.currentDisplayFlight = "Player"
self.menubar = wx.MenuBar()
self.fileMenu = wx.Menu()
self.optionsMenu = wx.Menu()
self.connectionMenu = wx.Menu()
#menubar setup
self.fileMenu.Append(EXIT, 'Exit', 'Exit')
self.connectionMenu.Append(CONNECTION,'Edit Connection','Edit Connection')
self.menubar.Append(self.fileMenu,'File')
self.menubar.Append(self.optionsMenu,'Logs')
self.menubar.Append(self.connectionMenu,'Connection')
self.SetMenuBar(self.menubar)
self.Bind(wx.EVT_MENU, self.ExitProgram, id=EXIT)
self.Bind(wx.EVT_MENU, self.EditConnection, id=CONNECTION)
TopHorizSizer = wx.BoxSizer(wx.HORIZONTAL)
self.RightVertSizer = wx.BoxSizer(wx.VERTICAL)
LeftVertSizer = wx.BoxSizer(wx.VERTICAL)
GPSSizer = wx.BoxSizer()
#The main panel all others will be parented to
self.MainPanel = wx.Panel(self)
self.MainPanel.SetBackgroundColour("black")
#The other sub panels
self.EnvironmentInfo = EnvironmentPanel(self.MainPanel,-1)
self.FlightInfo = FlightPanel(self.MainPanel,-1)
self.GPS = GPSPanel(self.MainPanel,-1)
self.PlaneSelectPanel = PlaneSelectPanel(self.MainPanel,-1)
self.PlaneSelectPanel.updateFlightInfoObject(self.FlightInfo)
#add sub panels to the right sizer
self.RightVertSizer.Add(self.GPS,2,wx.EXPAND|wx.ALL,5)
self.RightVertSizer.Add(self.FlightInfo,1,wx.EXPAND|wx.BOTTOM|wx.RIGHT|wx.LEFT,5)#|wx.ALL,5)
#add sub panels to the left sizer
LeftVertSizer.Add(self.EnvironmentInfo,2,wx.EXPAND|wx.LEFT|wx.TOP|wx.BOTTOM,5)#|wx.ALL,5)
LeftVertSizer.Add(self.PlaneSelectPanel,1,wx.EXPAND|wx.BOTTOM|wx.LEFT,5)#|wx.ALL,5)
#combine and set sizers
TopHorizSizer.Add(LeftVertSizer,1,wx.EXPAND)
TopHorizSizer.Add(self.RightVertSizer,3,wx.EXPAND)
self.MainPanel.SetSizer(TopHorizSizer)
self.MainPanel.Fit()
self.CenterOnScreen()
self.CreateStatusBar()
self.SetStatusText("Boeing - 2014")
#initiate timer and timer event
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.timer.Start(300)
#initialize empty data containers
self.fgObjects = {}
self.fgEnvironmentObject = {}
self.fgLogObjects = {}
#called on every timer event, updates the main panel
def OnTimer(self, event):
self.updateLogs()
self.updateTheView()
#update the log object
def updateLogs(self):
for name,playerDict in self.fgObjects.items():
if(name in self.fgLogObjects):
#update flight
self.fgLogObjects[name].updateLog(self.fgObjects[name].prop_list)
else:
#add flight to log
newLog = FGLog(name, self.fgObjects[name].prop_list)
self.fgLogObjects[name] = newLog
#add new menubar item
newID = wx.NewId()
item = wx.MenuItem(self.optionsMenu, newID, name)
self.optionsMenu.AppendItem(item)
self.Bind(wx.EVT_MENU, self.Log, id=newID)
#updates main panel which will update all sub panels
def updateTheView(self):
for name,playerDict in self.fgObjects.items():
if(self.fgObjects[name].prop_list['updated'] == False):
#Remove flight marker from map
scriptString = """removeMarker("%s")""" %(str(name))
self.MainPanel.html_view.RunScript(scriptString)
#Remove player from fgObjects
del self.fgObjects[name]
#flight removed from 'Select a Flight' panel
self.PlaneSelectPanel.deleteRadio(name)
#update flight information
else:
lat = ""
lon = ""
speed = ""
alt = ""
heading = ""
currentFuel = ""
fuelCapacity = ""
localTime = ""
for property,value in playerDict.prop_list.iteritems():
if(property == "latitude-deg"):
lat = value
elif(property == "longitude-deg"):
lon = value
elif(property == "airspeed-kt"):
speed = value
elif(property == "altitude"):
alt = value
elif(property == "heading-deg"):
heading = value
elif(property == "total-fuel-gals"):
currentFuel = value
elif(property == "total-fuel-capacity"):
fuelCapacity = value
currentTime = datetime.now()
elapsedTime = currentTime - self.fgObjects[name].prop_list['startTime']
self.fgObjects[name].prop_list['timeElapsed'] = elapsedTime
self.fgObjects[name].prop_list['pastLat'] = lat;
self.fgObjects[name].prop_list['pastLon'] = lon;
selected = self.fgObjects[name].prop_list['selected']
#update the GPS map
scriptString = """updateMarker(%s,%s,"%s",%s,"%s")""" % (str(lat),str(lon),str(name),str(heading),selected)
self.MainPanel.html_view.RunScript(scriptString)
self.PlaneSelectPanel.addRadio(name)
#handle currently selected flight
if(name == self.currentDisplayFlight):
self.fgObjects[name].prop_list['selected'] = "yes"
self.FlightInfo.updateText(name,lat,lon,speed,heading,alt,currentFuel,fuelCapacity,self.fgObjects[name].prop_list['timeElapsed'])
else:
self.fgObjects[name].prop_list['selected'] = "no"
#update environment data
stationID = ""
skyConditions = ""
temperature = ""
windSpeed = ""
windDirection = ""
pressure = ""
for property,value in self.fgEnvironmentObject.iteritems():
if(property == "station-id"):
stationID = value
elif(property == "temperature-degf"):
temperature = value
elif(property == "wind-from-heading-deg"):
windDirection = value
elif(property == "wind-speed-kt"):
windSpeed = value
elif(property == "pressure"):
pressure = value
self.EnvironmentInfo.UpdateText(stationID,temperature,windSpeed,windDirection,pressure)
#receive ActiveMQ broker message string and parse it. Update data containers as needed.
def updateFGObjs(self, string):
if string is not None:
fgDictsList = FGParser.parse(string)
if len(fgDictsList) == 0:
return
for player,dict in self.fgObjects.iteritems():
self.fgObjects[player].prop_list['updated'] = False;
for dict in fgDictsList:
if "playername" in dict:
playerid = dict["playername"]
if playerid in self.fgObjects:
self.fgObjects[playerid].updateFromMessage(dict)
else:
newPlayer = FGObject(playerid, dict)
self.fgObjects[playerid] = newPlayer
elif "Environment" in dict:
self.fgEnvironmentObject = dict
#handle log menu event
def Log(self,e):
item = self.GetMenuBar().FindItemById(e.GetId())
name = item.GetText()
log = Log(parent = frame,player = name,logObject =self.fgLogObjects[name], id = -1)
log.Show()
#exit the program
def ExitProgram(self,e):
self.Destroy()
#handle the connection speed menu event
def EditConnection(self,e):
dlg = ConnectionDialog(self,-1, "Edit Connection", size=(300,300),style = wx.DEFAULT_DIALOG_STYLE,)
dlg.CenterOnScreen()
val = dlg.ShowModal()
#send message to broker to be received by FlightGear detailing new connection speed.
if val == wx.ID_OK:
try:
float(dlg.text.GetValue())
user = os.getenv("ACTIVEMQ_USER") or "admin"
password = os.getenv("ACTIVEMQ_PASSWORD") or "password"
host = os.getenv("ACTIVEMQ_HOST") or "localhost"
port = os.getenv("ACTIVEMQ_PORT") or 61613
conn = stomp.Connection(host_and_ports = [(host, port)])
conn.start()
conn.connect(login=user,passcode=password)
conn.send(body = dlg.text.GetValue(),destination='MESSAGE_INTERVAL')
conn.disconnect()
except ValueError:
print("not a number")
else:
print("You pressed Cancel\n")
dlg.Destroy()
if __name__ == '__main__':
#initialize the Javascript string for the GPS map panel
g = PyMap()
open('test.htm','wb').write(g.showhtml()) # generate test file
#initialize frame
app=wx.PySimpleApp()
frame = FlightTracker(parent = None, id=-1)
frame.SetTitle("Flight Tracker")
#------------------------------------------------------------------------
###
### The Stomp/broker information
###
user = os.getenv("ACTIVEMQ_USER") or "admin"
password = os.getenv("ACTIVEMQ_PASSWORD") or "password"
host = os.getenv("ACTIVEMQ_HOST") or "localhost"
port = os.getenv("ACTIVEMQ_PORT") or 61613
conn2 = stomp.Connection(host_and_ports = [(host, port)])
conn2.set_listener('', MyListener(conn2,frame))
conn2.start()
conn2.connect(login=user,passcode=password)
conn2.subscribe(destination="TEST.FOO", id=1, ack='auto')
print("Waiting for messages...")
#-----------------------------------------------------------------------------
frame.Show()
app.MainLoop()