mirror of
https://github.com/CopterExpress/clever-show.git
synced 2026-06-08 13:04:31 +00:00
New! Realization of table model for storing all copter data. Auto refresh, sorting, data checks and coloring based on checks, thread-safety
This commit is contained in:
@@ -3,7 +3,7 @@ import glob
|
||||
|
||||
from PyQt5 import QtWidgets
|
||||
from PyQt5.QtGui import QStandardItemModel, QStandardItem
|
||||
from PyQt5.QtCore import Qt, pyqtSlot
|
||||
from PyQt5.QtCore import Qt, pyqtSlot, QAbstractTableModel
|
||||
|
||||
from PyQt5.QtWidgets import QFileDialog, QMessageBox
|
||||
|
||||
@@ -11,21 +11,50 @@ from PyQt5.QtWidgets import QFileDialog, QMessageBox
|
||||
from server_gui import Ui_MainWindow
|
||||
|
||||
from server import *
|
||||
from copter_table_models import *
|
||||
from emergency import *
|
||||
|
||||
|
||||
class MyTableModel(QAbstractTableModel):
|
||||
def __init__(self, parent, headers, *args):
|
||||
QAbstractTableModel.__init__(self, parent, *args)
|
||||
|
||||
|
||||
# noinspection PyArgumentList,PyCallByClass
|
||||
class MainWindow(QtWidgets.QMainWindow):
|
||||
def __init__(self):
|
||||
super(MainWindow, self).__init__()
|
||||
|
||||
self.ui = Ui_MainWindow()
|
||||
self.ui.setupUi(self)
|
||||
self.init_ui()
|
||||
|
||||
self.model = CopterDataModel()
|
||||
self.proxy_model = CopterProxyModel()
|
||||
self.signals = SignalManager()
|
||||
|
||||
self.init_model()
|
||||
|
||||
self.show()
|
||||
|
||||
def init_model(self):
|
||||
self.proxy_model.setDynamicSortFilter(True)
|
||||
self.proxy_model.setSourceModel(self.model)
|
||||
|
||||
# Initing table and table self.model
|
||||
self.ui.tableView.setModel(self.proxy_model)
|
||||
self.ui.tableView.horizontalHeader().setStretchLastSection(True)
|
||||
self.ui.tableView.setSortingEnabled(True)
|
||||
|
||||
self.signals.update_data_signal.connect(self.model.update_item)
|
||||
self.signals.add_client_signal.connect(self.model.add_client)
|
||||
|
||||
def client_connected(self, client: Client):
|
||||
self.signals.add_client_signal.emit(CopterData(copter_id=client.copter_id, client=client))
|
||||
|
||||
def init_ui(self):
|
||||
# Connecting
|
||||
self.ui.check_button.clicked.connect(self.check_selected)
|
||||
self.ui.check_button.clicked.connect(self.selfcheck_selected)
|
||||
self.ui.start_button.clicked.connect(self.send_starttime)
|
||||
self.ui.pause_button.clicked.connect(self.pause_all)
|
||||
self.ui.stop_button.clicked.connect(self.stop_all)
|
||||
@@ -40,41 +69,42 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
self.ui.action_send_configurations.triggered.connect(self.send_configurations)
|
||||
self.ui.action_send_Aruco_map.triggered.connect(self.send_aruco)
|
||||
|
||||
# Initing table and table model
|
||||
self.ui.tableView.setModel(model)
|
||||
self.ui.tableView.horizontalHeader().setStretchLastSection(True)
|
||||
|
||||
@pyqtSlot()
|
||||
def check_selected(self):
|
||||
for row_num in range(model.rowCount()):
|
||||
item = model.item(row_num, 0)
|
||||
if item.isCheckable() and item.checkState() == Qt.Checked:
|
||||
print("Copter {} checked".format(model.item(row_num, 0).text()))
|
||||
copter = Client.get_by_id(item.text())
|
||||
copter.get_response("anim_id", self._set_copter_data, callback_args=(row_num, 1))
|
||||
copter.get_response("batt_voltage", self._set_copter_data, callback_args=(row_num, 2))
|
||||
copter.get_response("cell_voltage", self._set_copter_data, callback_args=(row_num, 3))
|
||||
copter.get_response("selfcheck", self._set_copter_data, callback_args=(row_num, 4))
|
||||
copter.get_response("time", self._set_copter_data, callback_args=(row_num, 5))
|
||||
def selfcheck_selected(self):
|
||||
for copter_data in self.model.user_selected():
|
||||
copter = copter_data.client
|
||||
|
||||
self.ui.start_button.setEnabled(True)
|
||||
self.ui.takeoff_button.setEnabled(True)
|
||||
copter.get_response("anim_id", self._set_copter_data, callback_args=(1, copter_data.copter_id))
|
||||
copter.get_response("batt_voltage", self._set_copter_data, callback_args=(2, copter_data.copter_id))
|
||||
copter.get_response("cell_voltage", self._set_copter_data, callback_args=(3, copter_data.copter_id))
|
||||
copter.get_response("selfcheck", self._set_copter_data, callback_args=(4, copter_data.copter_id))
|
||||
copter.get_response("time", self._set_copter_data, callback_args=(5, copter_data.copter_id))
|
||||
|
||||
#self.ui.start_button.setEnabled(True)
|
||||
#self.ui.takeoff_button.setEnabled(True)
|
||||
|
||||
def _set_copter_data(self, value, col, copter_id):
|
||||
row = self.model.data_contents.index(next(
|
||||
filter(lambda x: x.copter_id == copter_id, self.model.data_contents)))
|
||||
|
||||
def _set_copter_data(self, value, row, col):
|
||||
if col == 1:
|
||||
model.setData(model.index(row, col), value)
|
||||
data = value
|
||||
elif col == 2:
|
||||
model.setData(model.index(row, col), "{} V".format(round(float(value), 3)))
|
||||
data = "{} V.".format(round(float(value), 3))
|
||||
elif col == 3:
|
||||
batt_percent = ((float(value) - 3.2) / (4.2 - 3.2)) * 100
|
||||
model.setData(model.index(row, col), "{} %".format(round(batt_percent, 3)))
|
||||
batt_percent = ((float(value) - 3.2) / (4.2 - 3.2)) * 100 # TODO config
|
||||
data = "{} %".format(round(batt_percent, 3))
|
||||
elif col == 4:
|
||||
if value != "OK":
|
||||
model.setData(model.index(row, col), str(value)) # TODO different handling
|
||||
else:
|
||||
model.setData(model.index(row, col), str(value))
|
||||
data = str(value)
|
||||
elif col == 5:
|
||||
model.setData(model.index(row, col), time.ctime(int(value)))
|
||||
data = time.ctime(int(value))
|
||||
data2 = "{} sec.".format(round(int(value) - time.time(), 3))
|
||||
self.signals.update_data_signal.emit(row, col + 1, data2)
|
||||
else:
|
||||
print("No column matched for response")
|
||||
return
|
||||
|
||||
self.signals.update_data_signal.emit(row, col, data)
|
||||
|
||||
@pyqtSlot()
|
||||
def send_starttime(self):
|
||||
@@ -86,8 +116,8 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
)
|
||||
if reply == QMessageBox.Yes:
|
||||
print("Accepted")
|
||||
for row_num in range(model.rowCount()):
|
||||
item = model.item(row_num, 0)
|
||||
for row_num in range(self.model.rowCount()):
|
||||
item = self.model.item(row_num, 0)
|
||||
if item.isCheckable() and item.checkState() == Qt.Checked:
|
||||
if True: # TODO checks for batt/selfckeck here
|
||||
copter = Client.get_by_id(item.text())
|
||||
@@ -110,10 +140,10 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
|
||||
@pyqtSlot()
|
||||
def test_leds(self):
|
||||
for row_num in range(model.rowCount()):
|
||||
item = model.item(row_num, 0)
|
||||
for row_num in range(self.model.rowCount()):
|
||||
item = self.model.item(row_num, 0)
|
||||
if item.isCheckable() and item.checkState() == Qt.Checked:
|
||||
if True: # TODO checks for batt/selfckeck here
|
||||
if True:
|
||||
copter = Client.get_by_id(item.text())
|
||||
copter.send_message("led_test")
|
||||
|
||||
@@ -126,8 +156,8 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
)
|
||||
if reply == QMessageBox.Yes:
|
||||
print("Accepted")
|
||||
for row_num in range(model.rowCount()):
|
||||
item = model.item(row_num, 0)
|
||||
for row_num in range(self.model.rowCount()):
|
||||
item = self.model.item(row_num, 0)
|
||||
if item.isCheckable() and item.checkState() == Qt.Checked:
|
||||
if True: # TODO checks for batt/selfckeck here
|
||||
copter = Client.get_by_id(item.text())
|
||||
@@ -154,8 +184,8 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
names = [os.path.basename(file).split(".")[0] for file in files]
|
||||
print(files)
|
||||
for file, name in zip(files, names):
|
||||
for row_num in range(model.rowCount()):
|
||||
item = model.item(row_num, 0)
|
||||
for row_num in range(self.model.rowCount()):
|
||||
item = self.model.item(row_num, 0)
|
||||
if item.isCheckable() and item.checkState() == Qt.Checked:
|
||||
copter = Client.get_by_id(item.text())
|
||||
if name == copter.copter_id:
|
||||
@@ -176,8 +206,9 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
value = sendable_config[section][option]
|
||||
logging.debug("Got item from config:".format(section, option, value))
|
||||
options.append(ConfigOption(section, option, value))
|
||||
for row_num in range(model.rowCount()):
|
||||
item = model.item(row_num, 0)
|
||||
|
||||
for row_num in range(self.model.rowCount()):
|
||||
item = self.model.item(row_num, 0)
|
||||
if item.isCheckable() and item.checkState() == Qt.Checked:
|
||||
copter = Client.get_by_id(item.text())
|
||||
copter.send_config_options(*options)
|
||||
@@ -188,16 +219,16 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
if path:
|
||||
filename = os.path.basename(path)
|
||||
print("Selected file:", path, filename)
|
||||
for row_num in range(model.rowCount()):
|
||||
item = model.item(row_num, 0)
|
||||
for row_num in range(self.model.rowCount()):
|
||||
item = self.model.item(row_num, 0)
|
||||
if item.isCheckable() and item.checkState() == Qt.Checked:
|
||||
copter = Client.get_by_id(item.text())
|
||||
copter.send_file(path, "/home/pi/catkin_ws/src/clever/aruco_pose/map/animation_map.txt")
|
||||
copter.send_message("service_restart", {"name": "clever"})
|
||||
@pyqtSlot()
|
||||
def emergency(self):
|
||||
for row_num in range(model.rowCount()):
|
||||
item = model.item(row_num, 0)
|
||||
for row_num in range(self.model.rowCount()):
|
||||
item = self.model.item(row_num, 0)
|
||||
if item.isCheckable() and item.checkState() == Qt.Checked:
|
||||
copter = Client.get_by_id(item.text())
|
||||
copter.send_message("emergency")
|
||||
@@ -216,8 +247,8 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
)
|
||||
if reply == QMessageBox.Yes:
|
||||
print("Accepted")
|
||||
for row_num in range(model.rowCount()):
|
||||
item = model.item(row_num, 0)
|
||||
for row_num in range(self.model.rowCount()):
|
||||
item = self.model.item(row_num, 0)
|
||||
if item.isCheckable() and item.checkState() == Qt.Checked:
|
||||
if True: # TODO checks for batt/selfckeck here
|
||||
copter = Client.get_by_id(item.text())
|
||||
@@ -226,29 +257,13 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
print("Cancelled")
|
||||
pass
|
||||
|
||||
|
||||
|
||||
model = QStandardItemModel()
|
||||
model.setHorizontalHeaderLabels(
|
||||
('copter ID', 'animation ID', 'battery V', 'battery %', 'selfcheck', 'time UTC')
|
||||
)
|
||||
model.setColumnCount(6)
|
||||
model.setRowCount(0)
|
||||
|
||||
|
||||
def client_connected(self: Client):
|
||||
copter_id_item = QStandardItem(self.copter_id)
|
||||
copter_id_item.setCheckable(True)
|
||||
model.appendRow((copter_id_item, ))
|
||||
|
||||
|
||||
Client.on_first_connect = client_connected
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = QtWidgets.QApplication(sys.argv)
|
||||
window = MainWindow()
|
||||
|
||||
Client.on_first_connect = window.client_connected
|
||||
|
||||
server = Server(on_stop=app.quit)
|
||||
server.start()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user