From 8d72874841a8416e3a755ec35b4bfdc2d1a1bc6c Mon Sep 17 00:00:00 2001 From: "artem30801@gmail.com" Date: Fri, 29 Mar 2019 11:08:56 +0300 Subject: [PATCH] All UI functions are now properly working with selected (checked) copters --- Server/server.py | 17 +++++++------ Server/server_qt.py | 58 ++++++++++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/Server/server.py b/Server/server.py index cc1a2e3..8a01581 100644 --- a/Server/server.py +++ b/Server/server.py @@ -164,13 +164,13 @@ class Server: broadcast_client.close() logging.info("Broadcast listener thread stopped, socked closed!") - def send_starttime(self, dt=0): + def send_starttime(self, copter, dt=0): if self.USE_NTP: timenow = Server.get_ntp_time(self.NTP_HOST, self.NTP_PORT) else: timenow = time.time() print('Now:', time.ctime(timenow), "+ dt =", dt) - Client.send_to_selected(Client.form_message("starttime", {"time": str(timenow + dt)})) + copter.send(Client.form_message("starttime", {"time": str(timenow + dt)})) def requires_connect(f): @@ -211,7 +211,7 @@ class Client: self._send_lock = threading.Lock() self.copter_id = None - self.selected = False # Use to select copters for certain purposes + self.selected = False # Use to select copters for certain purposes DEPRECATED Client.clients[ip] = self @@ -356,14 +356,14 @@ class Client: @staticmethod @requires_any_connected - def send_to_selected(message): + def send_to_selected(message): # DEPRECATED for client in Client.clients.values(): if client.connected and client.selected: client.send(message) @staticmethod @requires_any_connected - def request_to_selected(requested_value): + def request_to_selected(requested_value): # DEPRECATED for client in Client.clients.values(): if client.connected and client.selected: client.get_response(requested_value) @@ -375,13 +375,12 @@ class Client: if client.connected or force_all: client.send(message) - @staticmethod - def send_config_options(*options: ConfigOption): + def send_config_options(self, *options: ConfigOption): for option in options: - Client.send_to_selected( + self.send( Client.form_message('config_write', {'section': option.section, 'option': option.option, 'value': option.value})) - Client.send_to_selected(Client.form_message("config_reload")) + self.send(Client.form_message("config_reload")) @staticmethod def get_by_id(copter_id): diff --git a/Server/server_qt.py b/Server/server_qt.py index 45acaf2..60cfc1b 100644 --- a/Server/server_qt.py +++ b/Server/server_qt.py @@ -50,11 +50,12 @@ class MainWindow(QtWidgets.QMainWindow): item = model.item(row_num, 0) if item.isCheckable() and item.checkState() == Qt.Checked: print("Copter {} checked".format(model.item(row_num, 0).text())) - batt_total = float(Client.get_by_id(item.text()).get_response("batt_voltage")) - batt_cell = float(Client.get_by_id(item.text()).get_response("cell_voltage")) - selfcheck = Client.get_by_id(item.text()).get_response("selfcheck") + copter = Client.get_by_id(item.text()) + batt_total = float(copter.get_response("batt_voltage")) + batt_cell = float(copter.get_response("cell_voltage")) + selfcheck = copter.get_response("selfcheck") - batt_percent = (batt_cell-3.2)/(4.2-3.2) + batt_percent = ((batt_cell-3.2)/(4.2-3.2))*100 model.setData(model.index(row_num, 2), "{} V.".format(round(batt_total, 3))) model.setData(model.index(row_num, 3), "{} %".format(round(batt_percent, 3))) @@ -72,7 +73,12 @@ class MainWindow(QtWidgets.QMainWindow): @pyqtSlot() def send_starttime(self): dt = self.ui.start_delay_spin.value() - server.send_starttime(dt) + for row_num in range(model.rowCount()): + item = 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()) + server.send_starttime(copter, dt) @pyqtSlot() def stop_all(self): @@ -89,11 +95,21 @@ class MainWindow(QtWidgets.QMainWindow): @pyqtSlot() def test_leds(self): - Client.send_to_selected(Client.form_message("led_test")) + for row_num in range(model.rowCount()): + item = 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()) + copter.send(Client.form_message("led_test")) @pyqtSlot() def takeoff_selected(self): - Client.send_to_selected(Client.form_message("takeoff")) + for row_num in range(model.rowCount()): + item = 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()) + copter.send(Client.form_message("takeoff")) @pyqtSlot() def land_all(self): @@ -112,11 +128,14 @@ 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 copter in Client.clients.values(): - if name == copter.copter_id: - copter.send_file(file, "animation.csv") # TODO config - else: - print("Filename not matches with any drone connected") + for row_num in range(model.rowCount()): + item = 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: + copter.send_file(file, "animation.csv") # TODO config + else: + print("Filename not matches with any drone connected") @pyqtSlot() def send_configurations(self): @@ -131,7 +150,11 @@ class MainWindow(QtWidgets.QMainWindow): value = sendable_config[section][option] print("Got item from config:", section, option, value) options.append(ConfigOption(section, option, value)) - Client.send_config_options(*options) + for row_num in range(model.rowCount()): + item = 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) @pyqtSlot() def send_aruco(self): @@ -139,8 +162,11 @@ class MainWindow(QtWidgets.QMainWindow): if path: filename = os.path.basename(path) print("Selected file:", path, filename) - for copter in Client.clients.values(): - copter.send_file(path, "/home/pi/catkin_ws/src/clever/aruco_pose/map/animation_map.txt") + for row_num in range(model.rowCount()): + item = 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") model = QStandardItemModel() @@ -154,7 +180,7 @@ 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, )) # TODO: get responses for another columns + model.appendRow((copter_id_item, )) Client.on_connect = client_connected