diff --git a/controllers/media_controller.py b/controllers/media_controller.py index ed0c961..aa4af29 100644 --- a/controllers/media_controller.py +++ b/controllers/media_controller.py @@ -4,7 +4,7 @@ from abc import ABC, abstractmethod from dataclasses import dataclass from typing import Any -from PySide6.QtCore import QObject, Signal +from PySide6.QtCore import QObject, Signal, QSettings @dataclass @@ -71,9 +71,21 @@ class MediaSourceController(ABC): class BluetoothController(MediaSourceController): """Контроллер для Bluetooth аудио.""" - + def __init__(self, bt_service: Any): self._bt_service = bt_service + self._music_mac: str | None = None + + def set_music_mac(self, mac: str | None) -> None: + """Установить приоритетный MAC для музыки.""" + self._music_mac = mac + # Синхронизируем с сервисом + if self._bt_service: + self._bt_service.set_music_mac(mac) + + def get_music_mac(self) -> str | None: + """Получить приоритетный MAC для музыки.""" + return self._music_mac @property def name(self) -> str: @@ -184,15 +196,21 @@ class MediaController(QObject): def __init__(self, bt_service: Any = None, parent: QObject = None): super().__init__(parent) self._bt_service = bt_service + self._settings = QSettings("car_ui", "ui") self._controllers: dict[str, MediaSourceController] = {} self._current_mode: str = "bluetooth" - + # Регистрируем контроллеры self._register_controllers() def _register_controllers(self) -> None: """Зарегистрировать все доступные контроллеры.""" - self._controllers["bluetooth"] = BluetoothController(self._bt_service) + bt_controller = BluetoothController(self._bt_service) + # Читаем приоритетный MAC из настроек + music_mac = self._settings.value("bluetooth/music_mac", "") + if music_mac: + bt_controller.set_music_mac(music_mac) + self._controllers["bluetooth"] = bt_controller self._controllers["carplay"] = CarPlayController() # В будущем можно добавить: # self._controllers["aux"] = AuxController() @@ -203,8 +221,15 @@ class MediaController(QObject): # Если переключаемся с Bluetooth на CarPlay - ставим паузу if self._current_mode == "bluetooth" and mode == "carplay": self._controllers["bluetooth"].pause() - elif self._current_mode == "carplay" and mode == "bluetooth": - self._controllers["bluetooth"].play() + + # Если переключаемся на Bluetooth - подключаем приоритетное устройство + elif mode == "bluetooth": + bt_controller = self._controllers["bluetooth"] + music_mac = bt_controller.get_music_mac() + if music_mac: + # Пытаемся подключить приоритетное устройство + bt_controller.connect_device(music_mac) + self._current_mode = mode # Сигнал о смене режима для обновления UI self.metadata_changed.emit(self.get_metadata()) diff --git a/services/bluetooth_service.py b/services/bluetooth_service.py index ab3b232..ff7507e 100644 --- a/services/bluetooth_service.py +++ b/services/bluetooth_service.py @@ -140,6 +140,13 @@ class BluetoothService(QObject): self._settings.setValue("bluetooth/music_mac", "") return success + def set_music_mac(self, mac: str | None) -> None: + """Установить приоритетный MAC для музыки.""" + if mac: + self._settings.setValue("bluetooth/music_mac", mac) + else: + self._settings.setValue("bluetooth/music_mac", "") + def make_discoverable(self, timeout_sec: int = 10) -> bool: """Сделать устройство видимым для сопряжения.