-
Notifications
You must be signed in to change notification settings - Fork 4
/
Thunder_Viewer.py
423 lines (343 loc) · 14.6 KB
/
Thunder_Viewer.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
import os
import sys
import requests
from PyQt5.QtGui import QColor
from PyQt5.QtCore import QProcess, pyqtSlot, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QTableWidgetItem
from pySerialTransfer import pySerialTransfer as transfer
from gui.remotePlayGui import Ui_PlayerManager
from gui.usbFieldsGui import Ui_usbFieldManager
from gui.gui import Ui_ThunderViewer
from gui.overlay import Ui_Overlay
from mqtt_thread import MqttSubThread
from stream_thread import StreamThread, StreamHandler
from record_thread import RecordThread
from constants import APP_DIR, LOGS_DIR
class AppWindow(QMainWindow):
'''
Description:
------------
Main GUI window class
'''
def __init__(self):
super().__init__()
self.ui = Ui_ThunderViewer()
self.ui.setupUi(self)
self.show()
self.setup_overlay()
self.setup_player_manager()
self.setup_usb_manager()
self.connect_signals()
self.init_recording_status()
self.update_port_list()
self.ui.acmi_path.setText(LOGS_DIR)
self.enable_inputs()
self.find_tacview_install()
self.player_names = []
def setup_overlay(self):
'''
Description:
------------
TODO
'''
self.Overlay = QMainWindow()
self.Overlay_ui = Ui_Overlay()
self.Overlay_ui.setupUi(self.Overlay)
self.Overlay.setWindowFlags(Qt.WindowStaysOnTopHint |
Qt.FramelessWindowHint |
Qt.X11BypassWindowManagerHint)
self.Overlay.setAttribute(Qt.WA_TranslucentBackground)
self.Overlay_ui.telem_table.setStyleSheet("QTableWidget {background-color: transparent;}"
"QHeaderView::section {background-color: transparent;}"
"QHeaderView {background-color: transparent;}"
"QTableCornerButton::section {background-color: transparent;}")
self.Overlay_ui.telem_table.setColumnCount(3)
self.Overlay_ui.telem_table.setRowCount(1)
self.Overlay_ui.field_select_table.setColumnCount(1)
self.Overlay_ui.field_select_table.setRowCount(0)
self.Overlay.move(0, 0)
self.overlay_fields = []
def setup_player_manager(self):
'''
Description:
------------
TODO
'''
self.PlayerManager = QMainWindow()
self.PlayerManager_ui = Ui_PlayerManager()
self.PlayerManager_ui.setupUi(self.PlayerManager)
def setup_usb_manager(self):
'''
Description:
------------
TODO
'''
self.UsbManager = QMainWindow()
self.UsbManager_ui = Ui_usbFieldManager()
self.UsbManager_ui.setupUi(self.UsbManager)
def connect_signals(self):
'''
Description:
------------
Connect button clicks to callback functions
'''
self.ui.tacview_select.clicked.connect(self.get_tacview_install)
self.ui.acmi_select.clicked.connect(self.get_acmi_dir)
self.ui.launch_tacview_live.clicked.connect(self.launch_live)
self.ui.record.clicked.connect(self.record_data)
self.ui.stop.clicked.connect(self.stop_recording_data)
self.ui.manage_players.clicked.connect(self.launch_remote_player_window)
self.PlayerManager_ui.apply.clicked.connect(self.block_players)
self.ui.manage_usb_fields.clicked.connect(self.UsbManager.show)
self.UsbManager_ui.apply.clicked.connect(self.update_usb_fields)
self.ui.port_refresh.clicked.connect(self.update_port_list)
self.ui.launch_overlay.clicked.connect(self.Overlay.showFullScreen)
self.Overlay_ui.close_button.clicked.connect(self.Overlay.close)
def find_tacview_install(self):
'''
Description:
------------
TODO
'''
x86_folder = r'C:\Program Files (x86)'
indep_install_folder = os.path.join(x86_folder, 'Tacview')
steam_install_folder = os.path.join(x86_folder, 'Steam', 'steamapps', 'common', 'Tacview')
appName = 'Tacview64.exe'
app_found = False
if os.path.exists(indep_install_folder):
if appName in os.listdir(indep_install_folder):
app_found = True
self.ui.tacview_path.setText(os.path.join(indep_install_folder, appName))
if os.path.exists(steam_install_folder) and not app_found:
if appName in os.listdir(steam_install_folder):
app_found = True
self.ui.tacview_path.setText(os.path.join(steam_install_folder, appName))
def get_tacview_install(self):
path = QFileDialog.getOpenFileName(self, filter='Tacview (Tacview.exe Tacview64.exe)')[0]
if path:
self.ui.tacview_path.setText(path)
def get_acmi_dir(self):
path = QFileDialog.getExistingDirectory(self, 'Select Directory', APP_DIR)
if path:
self.ui.acmi_path.setText(path)
def launch_live(self):
'''
Description:
------------
Launch the application "Tacview" at the path as specified in the GUI
'''
try:
if not os.path.exists(self.ui.tacview_path.text()):
raise FileNotFoundError
self.process = QProcess()
self.process.startDetached('"{}" {}'.format(self.ui.tacview_path.text(), '/ConnectRealTimeTelemetry'))
except (FileNotFoundError, OSError):
print('ERROR: Tacview.exe not found')
def record_data(self):
'''
Description:
------------
Begin recording and streaming (if any streaming options are enabled)
'''
if not self.ui.recording.isChecked():
self.disable_inputs()
if self.ui.live_usb.isChecked():
self.usb_port = self.ui.usb_ports.currentText()
self.usb_baud = int(self.ui.usb_baud.currentText())
self.transfer = transfer.SerialTransfer(self.usb_port, self.usb_baud)
if self.ui.mqtt.isChecked():
self.mqtt_sub_th = MqttSubThread(self)
self.mqtt_sub_th.start()
self.mqtt_sub_th.update_names.connect(self.update_player_names)
self.mqtt_sub_th.send_stream_data.connect(self.send_to_stream)
if self.ui.live_telem.isChecked():
self.stream_th = StreamThread(self)
self.stream_th.start()
self.rec_th = RecordThread(self)
self.rec_th.start()
self.rec_th.send_stream_data.connect(self.send_to_stream)
self.rec_th.send_overlay_data.connect(self.update_overlay)
self.ui.recording.setChecked(True)
def stop_recording_data(self):
'''
Description:
------------
Stops recording and streaming (if any streaming options are enabled)
by closing all currently running threads
'''
self.enable_inputs()
if self.ui.live_usb.isChecked():
self.transfer.close()
try:
if self.rec_th.isRunning():
self.rec_th.terminate()
except AttributeError:
pass
try:
if self.stream_th.isRunning():
self.stream_th.terminate()
except AttributeError:
pass
try:
if self.mqtt_sub_th.isRunning():
self.mqtt_sub_th.terminate()
except AttributeError:
pass
self.ui.recording.setChecked(False)
def init_recording_status(self):
'''
Description:
------------
Control radio button to display the recording status
'''
self.ui.recording.setDisabled(True)
self.ui.recording.setChecked(False)
def update_port_list(self):
'''
Description:
------------
Find the names of all currently available serial ports
'''
ports = transfer.open_ports()
self.ui.usb_ports.clear()
self.ui.usb_ports.addItems(ports)
def launch_remote_player_window(self):
'''
Description:
------------
TODO
'''
self.PlayerManager_ui.player_list.addItems(self.player_names)
self.PlayerManager.show()
@pyqtSlot(list)
def update_player_names(self, names):
self.player_names = names
@pyqtSlot(str)
def send_to_stream(self, line):
try:
StreamHandler.remote_data_buff.append(line)
except AttributeError:
pass
@pyqtSlot(dict)
def update_overlay(self, telem_dict):
# find all valid fields
found_fields = telem_dict.keys()
# remove any fields that are no longer valid
for i in range(len(self.overlay_fields)-1, -1, -1):
if self.overlay_fields[i] not in found_fields:
for row in range(self.Overlay_ui.field_select_table.rowCount()):
field = self.Overlay_ui.field_select_table.item(row, 0).text()
scrubbed_field = self.overlay_fields[i].replace('_', ' ').upper().split(',')[0]
if field == scrubbed_field:
self.Overlay_ui.field_select_table.removeRow(row)
break
self.overlay_fields.pop(i)
# add new fields
for field in found_fields:
if field not in self.overlay_fields:
self.overlay_fields.append(field)
#add row
new_row_num = self.Overlay_ui.field_select_table.rowCount()
self.Overlay_ui.field_select_table.insertRow(new_row_num)
chkBoxItem = QTableWidgetItem()
chkBoxItem.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
chkBoxItem.setCheckState(Qt.Unchecked)
chkBoxItem.setTextAlignment(Qt.AlignLeft | Qt.AlignVCenter)
chkBoxItem.setText(field.replace('_', ' ').upper().split(',')[0])
self.Overlay_ui.field_select_table.setItem(new_row_num, 0, chkBoxItem)
selected_fields_list = []
for row in range(self.Overlay_ui.field_select_table.rowCount()):
try:
if self.Overlay_ui.field_select_table.item(row, 0).checkState():
selected_fields_list.append(self.Overlay_ui.field_select_table.item(row, 0).text())
except AttributeError:
pass
# reset table
self.Overlay_ui.telem_table.setRowCount(0)
# fill the table with new telemetry data
index = 0
for datum in telem_dict.keys():
datum_str = datum.replace('_', ' ').upper().split(',')[0]
if datum_str in selected_fields_list:
self.Overlay_ui.telem_table.insertRow(index)
self.Overlay_ui.telem_table.setItem(index, 0, QTableWidgetItem(datum_str + ' '))
self.Overlay_ui.telem_table.setItem(index, 1, QTableWidgetItem(str(telem_dict[datum]).upper()))
index += 1
# apply font color to each cell
for row in range(self.Overlay_ui.telem_table.rowCount()):
for col in range(self.Overlay_ui.telem_table.columnCount()):
try:
self.Overlay_ui.telem_table.item(row, col).setForeground(QColor(255, 255, 255))
except AttributeError:
pass
# resize table to contents
self.Overlay_ui.telem_table.resizeColumnsToContents()
self.Overlay_ui.telem_table.resizeRowsToContents()
self.Overlay_ui.telem_table.resize(10000, 10000)
def block_players(self):
'''
Description:
------------
TODO
'''
blocked = []
try:
num_players = self.PlayerManager_ui.player_list.count()
player_list = self.PlayerManager_ui.player_list
for i in range(num_players):
if not player_list.item(i).isSelected():
blocked.append(player_list.item(i).text())
self.mqtt_sub_th.blocked_players = blocked
except AttributeError:
pass
def update_usb_fields(self):
'''
Description:
------------
TODO
'''
try:
self.rec_th.usb_fields = [item.text() for item in self.UsbManager_ui.usb_fields.selectedItems()]
except AttributeError:
pass
def enable_inputs(self):
self.change_inputs(True)
def disable_inputs(self):
self.change_inputs(False)
def change_inputs(self, enable):
'''
Description:
------------
Enables/disables GUI widgets
'''
self.ui.acmi_path.setEnabled(enable)
self.ui.acmi_select.setEnabled(enable)
self.ui.live_telem.setEnabled(enable)
self.ui.live_telem_port.setEnabled(enable)
self.ui.mqtt.setEnabled(enable)
self.ui.mqtt_id.setEnabled(enable)
self.ui.manage_players.setEnabled(enable)
self.ui.usb_ports.setEnabled(enable)
self.ui.live_usb.setEnabled(enable)
self.ui.port_refresh.setEnabled(enable)
self.ui.usb_baud.setEnabled(enable)
self.ui.manage_usb_fields.setEnabled(enable)
self.ui.team.setEnabled(enable)
self.ui.sample_rate.setEnabled(enable)
self.ui.record.setEnabled(enable)
self.ui.stop.setEnabled(not enable)
def main():
'''
Description:
------------
Main program to run
'''
app = QApplication(sys.argv)
w = AppWindow()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
try:
main()
except (SystemExit, KeyboardInterrupt, requests.exceptions.ConnectionError):
pass