2025-02-23 18:52:15 +08:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
|
2025-02-21 17:49:10 +08:00
|
|
|
|
2025-02-18 22:05:52 +08:00
|
|
|
from component.widget_main.widget_main import Widget_Main
|
|
|
|
from component.widget_channel.widget_channel import Widget_Channel
|
|
|
|
from component.widget_card.widget_card import Widget_Card
|
|
|
|
from component.widget_card.widget_card import CardData
|
|
|
|
from component.widget_filter.audio_filter_componet import AudioFilterWidget
|
2025-02-20 22:52:03 +08:00
|
|
|
from component.widget_filter.audio_filter_model import AudioFilterModel
|
|
|
|
from component.widget_filter.audio_filter_controller import AudioFilterController
|
2025-02-23 18:52:15 +08:00
|
|
|
from component.widget_card.widget_card import ParamData
|
|
|
|
from component.widget_log.widget_log import Widget_Log
|
2025-02-20 22:52:03 +08:00
|
|
|
from param_struct_test.service_manager import ServiceManager
|
|
|
|
from application.application_controller import ApplicationController
|
2025-02-18 22:05:52 +08:00
|
|
|
|
|
|
|
from datetime import date
|
2025-02-20 22:52:03 +08:00
|
|
|
from PySide6.QtWidgets import QMainWindow, QPushButton, QVBoxLayout
|
2025-02-18 22:05:52 +08:00
|
|
|
from PySide6.QtWidgets import QWidget
|
2025-02-25 00:16:44 +08:00
|
|
|
from PySide6.QtCore import QObject, QTimer
|
2025-02-23 18:52:15 +08:00
|
|
|
import yaml # 添加 yaml 导入
|
|
|
|
import logging
|
|
|
|
from component.widget_log.log_handler import setup_logger
|
|
|
|
from component.widget_log.log_handler import logger
|
2025-02-21 17:49:10 +08:00
|
|
|
|
2025-02-18 22:05:52 +08:00
|
|
|
class MainWindow(QWidget):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2025-02-23 18:52:15 +08:00
|
|
|
|
|
|
|
# 初始化日志系统
|
|
|
|
self.log_handler = setup_logger()
|
2025-02-25 00:16:44 +08:00
|
|
|
|
|
|
|
# 加载配置文件
|
|
|
|
self.config = self.load_config()
|
|
|
|
|
|
|
|
# 从配置文件获取服务器设置
|
|
|
|
server_host = self.config['server']['host']
|
|
|
|
server_port = self.config['server']['port']
|
|
|
|
|
2025-02-20 22:52:03 +08:00
|
|
|
# 初始化服务
|
2025-02-25 00:16:44 +08:00
|
|
|
ServiceManager.instance().init_services(server_host, server_port)
|
|
|
|
|
2025-02-20 22:52:03 +08:00
|
|
|
# 初始化应用控制器
|
|
|
|
self.app_controller = ApplicationController.instance()
|
2025-02-23 18:52:15 +08:00
|
|
|
|
2025-02-18 22:05:52 +08:00
|
|
|
self.widget_main = Widget_Main()
|
|
|
|
self.widget_channel = Widget_Channel()
|
2025-02-23 18:52:15 +08:00
|
|
|
self.widget_card = Widget_Card()
|
|
|
|
self.widget_log = Widget_Log()
|
|
|
|
|
|
|
|
# 设置日志处理器的目标窗口
|
|
|
|
self.log_handler.set_widget(self.widget_log)
|
|
|
|
|
2025-02-25 00:16:44 +08:00
|
|
|
# 设置通道显示数量
|
2025-02-23 18:52:15 +08:00
|
|
|
channel_count = self.config['channels']['count']
|
2025-02-24 09:38:33 +08:00
|
|
|
logger.info(f"通道数量: {channel_count}")
|
|
|
|
|
2025-02-23 18:52:15 +08:00
|
|
|
self.widget_channel.set_visible_channels(channel_count)
|
2025-02-18 22:05:52 +08:00
|
|
|
|
2025-02-21 17:49:10 +08:00
|
|
|
self.widget_main.ui.ListWidget_vLayout.addWidget(self.widget_card)
|
2025-02-18 22:05:52 +08:00
|
|
|
self.widget_main.ui.Channel_hLayout.addWidget(self.widget_channel)
|
2025-02-23 18:52:15 +08:00
|
|
|
self.widget_main.ui.verticalLayout_Log.addWidget(self.widget_log)
|
|
|
|
|
2025-02-18 22:05:52 +08:00
|
|
|
self.widget_filter_list = []
|
2025-02-20 22:52:03 +08:00
|
|
|
self.filter_controllers = [] # 存储控制器实例
|
|
|
|
|
2025-02-23 18:52:15 +08:00
|
|
|
# 添加测试按钮
|
|
|
|
self.test_button = QPushButton("Get_All")
|
|
|
|
self.test_button.clicked.connect(self.Get_All)
|
|
|
|
self.widget_main.ui.ListWidget_vLayout.addWidget(self.test_button)
|
|
|
|
|
2025-02-18 22:05:52 +08:00
|
|
|
self.create_filter_widget()
|
|
|
|
self.setup_connections()
|
2025-02-23 18:52:15 +08:00
|
|
|
|
|
|
|
def load_config(self):
|
|
|
|
"""加载通道配置文件"""
|
|
|
|
try:
|
|
|
|
with open('config/channel_config.yaml', 'r', encoding='utf-8') as f:
|
|
|
|
config = yaml.safe_load(f)
|
|
|
|
logging.info("配置文件加载成功")
|
|
|
|
return config
|
|
|
|
except Exception as e:
|
|
|
|
logging.error(f"加载配置文件失败: {e}")
|
|
|
|
return {"channels": {"count": 24}}
|
|
|
|
|
2025-02-18 22:05:52 +08:00
|
|
|
def create_filter_widget(self):
|
2025-02-23 18:52:15 +08:00
|
|
|
# 从配置文件获取通道数量
|
|
|
|
channel_count = self.config['channels']['count']
|
|
|
|
|
|
|
|
for i in range(channel_count):
|
2025-02-20 22:52:03 +08:00
|
|
|
# 创建widget
|
2025-02-18 22:05:52 +08:00
|
|
|
filter_widget = AudioFilterWidget()
|
2025-02-24 10:54:09 +08:00
|
|
|
# filter_widget.set_channel_id(i)
|
|
|
|
# filter_widget.set_channel_name(f"Channel {i+1}")
|
2025-02-24 17:02:51 +08:00
|
|
|
|
2025-02-20 22:52:03 +08:00
|
|
|
# 创建model和controller
|
2025-02-24 10:54:09 +08:00
|
|
|
model = AudioFilterModel(channel_id = i, channel_name=f"Channel {i+1}")
|
2025-02-20 22:52:03 +08:00
|
|
|
controller = AudioFilterController(model)
|
|
|
|
controller.set_widget(filter_widget)
|
2025-02-23 18:52:15 +08:00
|
|
|
|
2025-02-20 22:52:03 +08:00
|
|
|
# 连接控制器信号
|
|
|
|
controller.error_occurred.connect(lambda msg: print(f"Error: {msg}"))
|
|
|
|
controller.state_changed.connect(lambda state: print(f"State changed: {state}"))
|
|
|
|
controller.params_synced.connect(lambda: print("Params synced"))
|
2025-02-23 18:52:15 +08:00
|
|
|
|
2025-02-20 22:52:03 +08:00
|
|
|
# 存储实例
|
2025-02-18 22:05:52 +08:00
|
|
|
self.widget_filter_list.append(filter_widget)
|
2025-02-20 22:52:03 +08:00
|
|
|
self.filter_controllers.append(controller)
|
2025-02-18 22:05:52 +08:00
|
|
|
|
|
|
|
def setup_connections(self):
|
|
|
|
print("setup_connections")
|
|
|
|
self.widget_channel.channel_btn_clicked.connect(self.on_channel_btn_clicked)
|
|
|
|
|
|
|
|
def on_channel_btn_clicked(self, channel_id: int):
|
|
|
|
print(f"channel_id: {channel_id}")
|
|
|
|
self.widget_filter_list[channel_id].show()
|
|
|
|
|
2025-02-20 22:52:03 +08:00
|
|
|
def test_communication(self):
|
|
|
|
"""测试通信功能"""
|
2025-02-24 10:54:09 +08:00
|
|
|
|
2025-02-20 22:52:03 +08:00
|
|
|
# 测试第一个控制器的通信
|
|
|
|
if self.filter_controllers:
|
|
|
|
controller = self.filter_controllers[0]
|
2025-02-23 18:52:15 +08:00
|
|
|
|
2025-02-20 22:52:03 +08:00
|
|
|
# 测试从服务器加载数据
|
2025-02-24 10:54:09 +08:00
|
|
|
logger.info("load from server...")
|
2025-02-20 22:52:03 +08:00
|
|
|
controller.load_from_server()
|
2025-02-23 18:52:15 +08:00
|
|
|
|
2025-02-20 22:52:03 +08:00
|
|
|
# 测试同步数据到服务器
|
2025-02-24 10:54:09 +08:00
|
|
|
logger.info("sync to server...")
|
2025-02-21 17:49:10 +08:00
|
|
|
controller.sync_to_server()
|
|
|
|
|
|
|
|
def Get_All(self):
|
2025-02-25 00:16:44 +08:00
|
|
|
"""一次性获取所有通道的所有参数"""
|
2025-02-21 17:49:10 +08:00
|
|
|
try:
|
2025-02-25 00:16:44 +08:00
|
|
|
# 收集所有通道的所有参数
|
|
|
|
all_params = []
|
|
|
|
channel_count = len(self.filter_controllers)
|
|
|
|
|
|
|
|
# 收集所有通道的基础参数和滤波器参数
|
|
|
|
for channel_id in range(channel_count):
|
|
|
|
# 基础参数
|
|
|
|
all_params.extend([
|
|
|
|
f'tuning_parameters.mix_parameters[{channel_id}].ch_n',
|
|
|
|
f'tuning_parameters.mix_parameters[{channel_id}].mix_left_data',
|
|
|
|
f'tuning_parameters.mix_parameters[{channel_id}].mix_right_data',
|
|
|
|
f'tuning_parameters.delay_parameters[{channel_id}].ch_n',
|
|
|
|
f'tuning_parameters.delay_parameters[{channel_id}].delay_data',
|
|
|
|
f'tuning_parameters.volume_parameters[{channel_id}].ch_n',
|
|
|
|
f'tuning_parameters.volume_parameters[{channel_id}].vol_data'
|
|
|
|
])
|
|
|
|
|
|
|
|
# 滤波器参数
|
|
|
|
base_idx = channel_id * 20
|
|
|
|
for i in range(20): # 每个通道20个滤波器
|
|
|
|
idx = base_idx + i
|
|
|
|
all_params.extend([
|
|
|
|
f'tuning_parameters.eq_parameters[{idx}].fc',
|
|
|
|
f'tuning_parameters.eq_parameters[{idx}].q',
|
|
|
|
f'tuning_parameters.eq_parameters[{idx}].gain',
|
|
|
|
f'tuning_parameters.eq_parameters[{idx}].slope',
|
|
|
|
f'tuning_parameters.eq_parameters[{idx}].filterType'
|
|
|
|
])
|
|
|
|
|
|
|
|
def handle_all_response(response):
|
|
|
|
"""处理所有参数的响应"""
|
|
|
|
try:
|
|
|
|
if not hasattr(response, 'data'):
|
|
|
|
raise ValueError("Invalid response format")
|
|
|
|
|
|
|
|
# 将响应数据分发给各个控制器
|
|
|
|
for controller in self.filter_controllers:
|
|
|
|
channel_id = controller.model.channel_id
|
|
|
|
channel_data = self._extract_channel_data(response.data, channel_id)
|
|
|
|
controller._on_params_updated_new(Response(
|
|
|
|
token="combined",
|
|
|
|
cmd="get_params",
|
|
|
|
data=channel_data
|
|
|
|
))
|
|
|
|
print("Successfully loaded all filter data")
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error handling response: {e}")
|
|
|
|
|
|
|
|
# 一次性请求所有参数
|
|
|
|
ServiceManager.instance().params_service.get_params(
|
|
|
|
self.widget_main,
|
|
|
|
all_params,
|
|
|
|
handle_all_response
|
|
|
|
)
|
|
|
|
|
2025-02-21 17:49:10 +08:00
|
|
|
except Exception as e:
|
2025-02-25 00:16:44 +08:00
|
|
|
print(f"Error in Get_All: {e}")
|
|
|
|
|
|
|
|
def _extract_channel_data(self, response_data: dict, channel_id: int) -> dict:
|
|
|
|
"""从完整响应中提取指定通道的数据"""
|
|
|
|
channel_data = {}
|
|
|
|
for key, value in response_data.items():
|
|
|
|
# 检查是否属于指定通道的参数
|
|
|
|
if f'[{channel_id}]' in key or f'[{channel_id * 20}:{(channel_id + 1) * 20}]' in key:
|
|
|
|
channel_data[key] = value
|
|
|
|
return channel_data
|
2025-02-21 17:49:10 +08:00
|
|
|
|
2025-02-18 22:05:52 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
from PySide6.QtWidgets import QApplication
|
|
|
|
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
main_window = MainWindow()
|
2025-02-24 09:38:33 +08:00
|
|
|
logger.info("软件启动")
|
2025-02-23 18:52:15 +08:00
|
|
|
# for i in range(1, 6):
|
2025-02-20 22:52:03 +08:00
|
|
|
# data = CardData(
|
2025-02-23 18:52:15 +08:00
|
|
|
# name=f"测试项目 {i}",
|
2025-02-20 22:52:03 +08:00
|
|
|
# date=date.today().strftime("%Y-%m-%d"),
|
2025-02-23 18:52:15 +08:00
|
|
|
# description="",
|
|
|
|
# params=[
|
|
|
|
# ParamData("温度设定", f"{20 + i}°C"),
|
|
|
|
# ParamData("运行时间", f"{i * 2}小时"),
|
|
|
|
# ParamData("功率", f"{100 * i}W"),
|
|
|
|
# ParamData("状态", "正常运行" if i % 2 == 0 else "待机中")
|
|
|
|
# ]
|
2025-02-20 22:52:03 +08:00
|
|
|
# )
|
|
|
|
# main_window.widget_card.add_card_item(data)
|
2025-02-23 18:52:15 +08:00
|
|
|
|
2025-02-18 22:05:52 +08:00
|
|
|
main_window.widget_main.show()
|
2025-02-20 22:52:03 +08:00
|
|
|
sys.exit(app.exec())
|