jiateng_ws/ui/electricity_settings_ui.py
2025-06-26 18:26:22 +08:00

231 lines
8.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PySide6.QtWidgets import (
QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout,
QFormLayout, QSpinBox, QGroupBox, QFrame
)
from PySide6.QtCore import Qt, Signal, QTimer
from PySide6.QtGui import QFont
from datetime import datetime, timedelta
from utils.electricity_monitor import ElectricityMonitor
from dao.electricity_dao import ElectricityDAO
import logging
class ElectricitySettingsUI(QWidget):
"""电力监控设置UI组件"""
# 定义信号
settings_changed = Signal()
def __init__(self, parent=None):
super().__init__(parent)
self.parent = parent
self.electricity_monitor = ElectricityMonitor.get_instance()
self.electricity_dao = ElectricityDAO()
# 确保电力消耗表已创建
self.electricity_dao.create_table_if_not_exists()
self.init_ui()
# 创建定时器用于更新UI
self.update_timer = QTimer(self)
self.update_timer.timeout.connect(self.update_status)
self.update_timer.start(1000) # 每秒更新一次
# 初始更新状态
self.update_status()
def init_ui(self):
"""初始化UI"""
# 设置字体
self.title_font = QFont("微软雅黑", 14, QFont.Bold)
self.normal_font = QFont("微软雅黑", 10)
self.small_font = QFont("微软雅黑", 9)
# 创建主布局
main_layout = QVBoxLayout(self)
main_layout.setContentsMargins(20, 20, 20, 20)
main_layout.setSpacing(15)
# 创建标题
title_label = QLabel("电力监控设置")
title_label.setFont(self.title_font)
title_label.setAlignment(Qt.AlignCenter)
title_label.setStyleSheet("color: #1a237e; margin-bottom: 10px;")
main_layout.addWidget(title_label)
# 创建状态组
status_group = QGroupBox("监控状态")
status_group.setFont(self.normal_font)
status_layout = QFormLayout()
# 监控状态
self.status_label = QLabel("未知")
self.status_label.setFont(self.normal_font)
status_layout.addRow("监控状态:", self.status_label)
# 当前电量
self.electricity_label = QLabel("--")
self.electricity_label.setFont(self.normal_font)
status_layout.addRow("当前电量:", self.electricity_label)
# 上次监听时间
self.last_time_label = QLabel("--")
self.last_time_label.setFont(self.normal_font)
status_layout.addRow("上次监听时间:", self.last_time_label)
# 下次监听时间
self.next_time_label = QLabel("--")
self.next_time_label.setFont(self.normal_font)
status_layout.addRow("下次监听时间:", self.next_time_label)
status_group.setLayout(status_layout)
main_layout.addWidget(status_group)
# 创建设置组
settings_group = QGroupBox("监控设置")
settings_group.setFont(self.normal_font)
settings_layout = QFormLayout()
# 监听间隔
self.interval_spinbox = QSpinBox()
self.interval_spinbox.setFont(self.normal_font)
self.interval_spinbox.setRange(1, 60) # 1-60分钟
self.interval_spinbox.setValue(self.electricity_monitor.interval_minutes) # 使用当前设置的值
self.interval_spinbox.setSuffix(" 分钟")
self.interval_spinbox.valueChanged.connect(self.on_interval_changed)
settings_layout.addRow("监听间隔:", self.interval_spinbox)
settings_group.setLayout(settings_layout)
main_layout.addWidget(settings_group)
# 创建按钮组
button_layout = QHBoxLayout()
button_layout.setSpacing(20)
# 开始监听按钮
self.start_button = QPushButton("开始监听")
self.start_button.setFont(self.normal_font)
self.start_button.setMinimumHeight(40)
self.start_button.setStyleSheet("background-color: #4CAF50; color: white;")
self.start_button.clicked.connect(self.start_monitoring)
button_layout.addWidget(self.start_button)
# 暂停监听按钮
self.stop_button = QPushButton("暂停监听")
self.stop_button.setFont(self.normal_font)
self.stop_button.setMinimumHeight(40)
self.stop_button.setStyleSheet("background-color: #F44336; color: white;")
self.stop_button.clicked.connect(self.stop_monitoring)
button_layout.addWidget(self.stop_button)
main_layout.addLayout(button_layout)
# 添加分隔线
separator = QFrame()
separator.setFrameShape(QFrame.HLine)
separator.setFrameShadow(QFrame.Sunken)
main_layout.addWidget(separator)
# 添加历史数据标签
history_label = QLabel("历史数据")
history_label.setFont(self.title_font)
history_label.setAlignment(Qt.AlignCenter)
history_label.setStyleSheet("color: #1a237e; margin: 10px 0;")
main_layout.addWidget(history_label)
# 占位符,用于未来扩展
placeholder = QLabel("历史数据功能将在未来版本中提供")
placeholder.setFont(self.normal_font)
placeholder.setAlignment(Qt.AlignCenter)
placeholder.setStyleSheet("color: #888888; padding: 20px;")
main_layout.addWidget(placeholder)
# 添加弹性空间
main_layout.addStretch(1)
def update_status(self):
"""更新状态显示"""
try:
# 检查监控器状态
is_monitoring = self.electricity_monitor.is_monitoring()
# 更新状态标签
if is_monitoring:
self.status_label.setText("正在监听")
self.status_label.setStyleSheet("color: green; font-weight: bold;")
self.start_button.setEnabled(False)
self.stop_button.setEnabled(True)
else:
self.status_label.setText("已暂停")
self.status_label.setStyleSheet("color: red; font-weight: bold;")
self.start_button.setEnabled(True)
self.stop_button.setEnabled(False)
# 获取最新电力数据
latest_data = self.electricity_dao.get_latest_electricity_data()
if latest_data:
self.electricity_label.setText(str(latest_data['electricity_number']))
self.last_time_label.setText(latest_data['sync_time'])
# 计算下次监听时间
if is_monitoring:
next_time = self.electricity_monitor.get_next_read_time()
if next_time:
self.next_time_label.setText(next_time.strftime('%Y-%m-%d %H:%M:%S'))
else:
self.next_time_label.setText("计算中...")
else:
self.next_time_label.setText("监听已暂停")
else:
self.electricity_label.setText("--")
self.last_time_label.setText("--")
if is_monitoring:
self.next_time_label.setText("等待首次读取...")
else:
self.next_time_label.setText("监听已暂停")
except Exception as e:
logging.error(f"更新电力监控状态时发生错误: {str(e)}")
def on_interval_changed(self, value):
"""监听间隔变更处理
Args:
value: 新的间隔值(分钟)
"""
try:
# 更新监控器间隔
self.electricity_monitor.set_interval(value)
# 发出设置变更信号
self.settings_changed.emit()
except Exception as e:
logging.error(f"设置电力监控间隔时发生错误: {str(e)}")
def start_monitoring(self):
"""开始监听"""
try:
# 启动电力监控器
self.electricity_monitor.start()
# 更新UI
self.update_status()
# 发出设置变更信号
self.settings_changed.emit()
except Exception as e:
logging.error(f"启动电力监控时发生错误: {str(e)}")
def stop_monitoring(self):
"""暂停监听"""
try:
# 停止电力监控器
self.electricity_monitor.stop()
# 更新UI
self.update_status()
# 发出设置变更信号
self.settings_changed.emit()
except Exception as e:
logging.error(f"停止电力监控时发生错误: {str(e)}")