157 lines
5.9 KiB
Python
157 lines
5.9 KiB
Python
import sys
|
|
import os
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
|
|
|
|
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
|
|
from component.widget_filter.audio_filter_model import AudioFilterModel
|
|
from component.widget_filter.audio_filter_controller import AudioFilterController
|
|
from component.widget_card.widget_card import ParamData
|
|
from component.widget_log.widget_log import Widget_Log
|
|
from param_struct_test.service_manager import ServiceManager
|
|
from application.application_controller import ApplicationController
|
|
|
|
from datetime import date
|
|
from PySide6.QtWidgets import QMainWindow, QPushButton, QVBoxLayout
|
|
from PySide6.QtWidgets import QWidget
|
|
from PySide6.QtCore import QObject
|
|
import yaml # 添加 yaml 导入
|
|
import logging
|
|
from component.widget_log.log_handler import setup_logger
|
|
from component.widget_log.log_handler import logger
|
|
|
|
class MainWindow(QWidget):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
# 初始化日志系统
|
|
self.log_handler = setup_logger()
|
|
# 初始化服务
|
|
ServiceManager.instance().init_services("192.168.5.4", 12345)
|
|
# 初始化应用控制器
|
|
self.app_controller = ApplicationController.instance()
|
|
|
|
self.widget_main = Widget_Main()
|
|
self.widget_channel = Widget_Channel()
|
|
self.widget_card = Widget_Card()
|
|
self.widget_log = Widget_Log()
|
|
|
|
# 设置日志处理器的目标窗口
|
|
self.log_handler.set_widget(self.widget_log)
|
|
|
|
# 加载配置文件
|
|
self.config = self.load_config()
|
|
|
|
# 设置通道显示数量
|
|
channel_count = self.config['channels']['count']
|
|
logger.info(f"通道数量: {channel_count}")
|
|
|
|
self.widget_channel.set_visible_channels(channel_count)
|
|
|
|
self.widget_main.ui.ListWidget_vLayout.addWidget(self.widget_card)
|
|
self.widget_main.ui.Channel_hLayout.addWidget(self.widget_channel)
|
|
self.widget_main.ui.verticalLayout_Log.addWidget(self.widget_log)
|
|
|
|
self.widget_filter_list = []
|
|
self.filter_controllers = [] # 存储控制器实例
|
|
|
|
# 添加测试按钮
|
|
self.test_button = QPushButton("Get_All")
|
|
self.test_button.clicked.connect(self.Get_All)
|
|
self.widget_main.ui.ListWidget_vLayout.addWidget(self.test_button)
|
|
|
|
self.create_filter_widget()
|
|
self.setup_connections()
|
|
|
|
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}}
|
|
|
|
def create_filter_widget(self):
|
|
# 从配置文件获取通道数量
|
|
channel_count = self.config['channels']['count']
|
|
|
|
for i in range(channel_count):
|
|
# 创建widget
|
|
filter_widget = AudioFilterWidget()
|
|
# filter_widget.set_channel_id(i)
|
|
# filter_widget.set_channel_name(f"Channel {i+1}")
|
|
|
|
# 创建model和controller
|
|
model = AudioFilterModel(channel_id = i, channel_name=f"Channel {i+1}")
|
|
controller = AudioFilterController(model)
|
|
controller.set_widget(filter_widget)
|
|
|
|
# 连接控制器信号
|
|
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"))
|
|
|
|
# 存储实例
|
|
self.widget_filter_list.append(filter_widget)
|
|
self.filter_controllers.append(controller)
|
|
|
|
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()
|
|
|
|
def test_communication(self):
|
|
"""测试通信功能"""
|
|
|
|
# 测试第一个控制器的通信
|
|
if self.filter_controllers:
|
|
controller = self.filter_controllers[0]
|
|
|
|
# 测试从服务器加载数据
|
|
logger.info("load from server...")
|
|
controller.load_from_server()
|
|
|
|
# 测试同步数据到服务器
|
|
logger.info("sync to server...")
|
|
controller.sync_to_server()
|
|
|
|
def Get_All(self):
|
|
try:
|
|
for controller in self.filter_controllers:
|
|
controller.load_from_server()
|
|
print("Successfully loaded all filter data")
|
|
except Exception as e:
|
|
print(f"Error loading filter data: {e}")
|
|
|
|
if __name__ == '__main__':
|
|
import sys
|
|
from PySide6.QtWidgets import QApplication
|
|
|
|
app = QApplication(sys.argv)
|
|
main_window = MainWindow()
|
|
logger.info("软件启动")
|
|
# for i in range(1, 6):
|
|
# data = CardData(
|
|
# name=f"测试项目 {i}",
|
|
# date=date.today().strftime("%Y-%m-%d"),
|
|
# description="",
|
|
# params=[
|
|
# ParamData("温度设定", f"{20 + i}°C"),
|
|
# ParamData("运行时间", f"{i * 2}小时"),
|
|
# ParamData("功率", f"{100 * i}W"),
|
|
# ParamData("状态", "正常运行" if i % 2 == 0 else "待机中")
|
|
# ]
|
|
# )
|
|
# main_window.widget_card.add_card_item(data)
|
|
|
|
main_window.widget_main.show()
|
|
sys.exit(app.exec()) |