Skip to content

Commit 0a7f499

Browse files
Custom connection strings to settings.json
1 parent dcbd034 commit 0a7f499

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

ardupilot_methodic_configurator/backend_filesystem_program_settings.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def __init__(self) -> None:
7575
pass
7676

7777
@classmethod
78-
def _get_settings_defaults(cls) -> dict[str, Union[int, bool, str, float, dict]]:
78+
def _get_settings_defaults(cls) -> dict[str, Union[int, bool, str, float, dict, list]]:
7979
"""
8080
Get the default settings dictionary with dynamically computed paths.
8181
@@ -89,6 +89,7 @@ def _get_settings_defaults(cls) -> dict[str, Union[int, bool, str, float, dict]]
8989
return {
9090
"Format version": 1,
9191
"display_usage_popup": dict.fromkeys(USAGE_POPUP_WINDOWS, True),
92+
"connection_history": [], # New Connection list
9293
"directory_selection": {
9394
"template_dir": os_path.join(cls.get_templates_base_dir(), "ArduCopter", "empty_4.6.x"),
9495
"new_base_dir": os_path.join(settings_directory, "vehicles"),
@@ -455,3 +456,34 @@ def motor_diagram_exists(frame_class: int, frame_type: int) -> bool:
455456
"""
456457
filepath, _error_msg = ProgramSettings.motor_diagram_filepath(frame_class, frame_type)
457458
return filepath != "" and os_path.exists(filepath)
459+
460+
@staticmethod
461+
def get_connection_history() -> list[str]:
462+
"""Get the list of previously used connection strings."""
463+
settings = ProgramSettings._get_settings_as_dict()
464+
history = settings.get("connection_history")
465+
466+
if history is None:
467+
return []
468+
469+
return history
470+
471+
@staticmethod
472+
def store_connection(connection_string: str) -> None:
473+
"""Save a new connection string to history."""
474+
if not connection_string:
475+
return
476+
477+
settings = ProgramSettings._get_settings_as_dict()
478+
history = settings.get("connection_history", [])
479+
480+
if connection_string in history:
481+
history.remove(connection_string)
482+
483+
history.insert(0, connection_string)
484+
485+
if len(history) > 10:
486+
history = history[:10]
487+
488+
settings["connection_history"] = history
489+
ProgramSettings._set_settings_from_dict(settings)

ardupilot_methodic_configurator/frontend_tkinter_connection_selection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from typing import Union
2323

2424
from ardupilot_methodic_configurator import _, __version__
25+
from ardupilot_methodic_configurator.backend_filesystem_program_settings import ProgramSettings
2526
from ardupilot_methodic_configurator.backend_flightcontroller import SUPPORTED_BAUDRATES, FlightController
2627
from ardupilot_methodic_configurator.common_arguments import add_common_arguments
2728
from ardupilot_methodic_configurator.frontend_tkinter_base_window import BaseWindow
@@ -73,6 +74,9 @@ def __init__( # pylint: disable=too-many-arguments, too-many-positional-argumen
7374
# Create a label for port
7475
port_label = ttk.Label(selection_frame, text=_("Port:"))
7576
port_label.pack(side=tk.LEFT, padx=(0, 5))
77+
# Load saved connection history from ProgramSettings
78+
for conn in ProgramSettings.get_connection_history():
79+
self.flight_controller.add_connection(conn)
7680

7781
# Create a read-only combobox for flight controller connection selection
7882
self.conn_selection_combobox = PairTupleCombobox(
@@ -157,6 +161,7 @@ def add_connection(self) -> str:
157161
),
158162
)
159163
if selected_connection:
164+
ProgramSettings.store_connection(selected_connection)
160165
error_msg = _("Will add new connection: {selected_connection} if not duplicated")
161166
logging_debug(error_msg.format(**locals()))
162167
self.flight_controller.add_connection(selected_connection)

0 commit comments

Comments
 (0)