diff --git a/ComConfigCopy.py b/ComConfigCopy.py index 212d36f..ea11314 100644 --- a/ComConfigCopy.py +++ b/ComConfigCopy.py @@ -760,6 +760,11 @@ class SerialAppGUI(tk.Tk): # Добавляем VERSION как атрибут класса self.VERSION = VERSION + # Добавляем атрибуты для управления выполнением файла + self.execution_thread = None + self.execution_paused = False + self.execution_stop = False + # Инициализация проверки обновлений self.update_checker = UpdateChecker( VERSION, @@ -1029,7 +1034,20 @@ class SerialAppGUI(tk.Tk): self.file_exec_var = StringVar(value=self.settings.get("config_file") or "") CustomEntry(file_frame, textvariable=self.file_exec_var, width=40).pack(side=LEFT, padx=5) ttk.Button(file_frame, text="Выбрать", command=self.select_config_file_fileexec).pack(side=LEFT, padx=5) - ttk.Button(frame, text="Выполнить команды", command=self.execute_file_commands).pack(pady=5) + + # Создаем фрейм для кнопок управления + control_frame = ttk.Frame(frame) + control_frame.pack(pady=5) + + # Кнопки управления выполнением + self.start_button = ttk.Button(control_frame, text="▶ Старт", command=self.start_execution) + self.start_button.pack(side=LEFT, padx=5) + + self.pause_button = ttk.Button(control_frame, text="⏸ Пауза", command=self.pause_execution, state="disabled") + self.pause_button.pack(side=LEFT, padx=5) + + self.stop_button = ttk.Button(control_frame, text="⏹ Остановить", command=self.stop_execution, state="disabled") + self.stop_button.pack(side=LEFT, padx=5) # Используем новый TerminalWidget вместо CustomText self.file_exec_text = TerminalWidget(frame, height=15) @@ -1064,21 +1082,86 @@ class SerialAppGUI(tk.Tk): if not self.connection: self.append_file_exec_text("[ERROR] Не удалось установить соединение.\n") return - threading.Thread( - target=execute_commands_from_file, - args=( - self.connection, - self.file_exec_var.get(), - self.settings.get("timeout", 10), - self.settings.get("copy_mode", "line"), - self.settings.get("block_size", 15), - self.append_file_exec_text, - self.settings.get("login"), - self.settings.get("password"), - True, - ), - daemon=True, - ).start() + + try: + with open(self.file_exec_var.get(), "r", encoding="utf-8") as file: + self.commands = [line.strip() for line in file if line.strip()] + + self.current_command_index = 0 + self.execution_paused = False + self.execution_stop = False + + # Запускаем выполнение команд + self.execute_next_command() + + except Exception as e: + self.append_file_exec_text(f"[ERROR] Ошибка при чтении файла: {str(e)}\n") + self.reset_execution_buttons() + + def execute_next_command(self): + if self.execution_stop: + self.reset_execution_buttons() + self.append_file_exec_text("[INFO] Выполнение остановлено.\n") + return + + if self.execution_paused: + self.after(1000, self.execute_next_command) + return + + if self.current_command_index < len(self.commands): + cmd = self.commands[self.current_command_index] + try: + success, response = send_command_and_process_response( + self.connection, + cmd, + self.settings.get("timeout", 10), + max_attempts=3, + log_callback=self.append_file_exec_text, + login=self.settings.get("login"), + password=self.settings.get("password"), + is_gui=True + ) + + self.current_command_index += 1 + # Планируем выполнение следующей команды + self.after(1000, self.execute_next_command) + + except Exception as e: + self.append_file_exec_text(f"[ERROR] Ошибка при выполнении команды: {str(e)}\n") + self.reset_execution_buttons() + else: + self.append_file_exec_text("[INFO] Выполнение завершено.\n") + self.reset_execution_buttons() + + def start_execution(self): + if not self.execution_thread or not self.execution_thread.is_alive(): + self.start_button.config(state="disabled") + self.pause_button.config(state="normal") + self.stop_button.config(state="normal") + self.execution_thread = threading.Thread(target=self.execute_file_commands, daemon=True) + self.execution_thread.start() + + def pause_execution(self): + if not self.execution_paused: + self.execution_paused = True + self.pause_button.config(text="▶ Продолжить") + self.append_file_exec_text("[INFO] Выполнение приостановлено.\n") + else: + self.execution_paused = False + self.pause_button.config(text="⏸ Пауза") + self.append_file_exec_text("[INFO] Выполнение возобновлено.\n") + + def stop_execution(self): + self.execution_stop = True + self.execution_paused = False + self.append_file_exec_text("[INFO] Остановка выполнения...\n") + + def reset_execution_buttons(self): + self.start_button.config(state="normal") + self.pause_button.config(state="disabled", text="⏸ Пауза") + self.stop_button.config(state="disabled") + self.execution_paused = False + self.execution_stop = False # Создание вкладки "Редактор конфигурационного файла" def create_config_editor_tab(self, frame):