添加了 消息列表弹窗(系统状态消息、预警消息),以及存储消息的数据库

This commit is contained in:
2025-11-13 09:37:41 +08:00
parent aa7dd7974a
commit 3d860b22fd
13 changed files with 615 additions and 29 deletions

View File

@ -0,0 +1,52 @@
from PySide6.QtCore import QThread, Signal
from .msg_recorder import MessageRecorder
"""
定时查询消息数据库, 并发送最新的消息数据
"""
class MsgQueryThread(QThread):
# 定义信号发送消息类型1=系统2=预警)和 消息信息列表
new_messages = Signal(int, list) # (message_type, [(content, is_processed, create_time, last_modified), ...])
def __init__(self, interval=1000, query_limit = 16):
super().__init__()
self.interval = interval # 查询间隔(毫秒)
self.query_limit = query_limit # 单次查询的项目条数如每次查询16条消息
self.running = True # 线程运行标志
self.recorder = MessageRecorder() # 消息管理器
# 记录上次查询到的“最新last_modified时间”按类型区分
self.last_normal_modified = ""
self.last_warning_modified = ""
def run(self):
"""线程主逻辑: 每隔interval查询一次消息"""
while self.running:
# 1. 处理系统消息类型1
normal_msgs = self.recorder.get_latest_normal_messages(self.query_limit)
if normal_msgs:
# 找到这16条消息中最新的last_modified时间即最大的
latest_modified = max([msg[3] for msg in normal_msgs]) # msg[3]是last_modified
# 比较是否有更新(新增或状态变化)
# 注意是根据last_modified来决定是否进行界面更新的
if latest_modified != self.last_normal_modified:
self.last_normal_modified = latest_modified
self.new_messages.emit(1, normal_msgs) # 发送信号更新UI1表示系统消息
# 2. 处理预警消息类型2
warning_msgs = self.recorder.get_latest_warning_messages(self.query_limit)
if warning_msgs:
latest_modified = max([msg[3] for msg in warning_msgs])
if latest_modified != self.last_warning_modified:
self.last_warning_modified = latest_modified
self.new_messages.emit(2, warning_msgs)
self.msleep(self.interval)
def stop(self):
"""停止消息查询线程"""
self.running = False
self.wait(500)
if self.isRunning():
self.terminate()

44
service/msg_recorder.py Normal file
View File

@ -0,0 +1,44 @@
from common.msg_db_helper import DBHelper
from PySide6.QtCore import QMutex
"""
消息管理器: 系统状态消息 和 预警消息
"""
class MessageRecorder:
_instance = None # 单例模式
_mutex = QMutex()
def __new__(cls):
cls._mutex.lock()
try:
if not cls._instance:
cls._instance = super().__new__(cls)
cls._instance.db = DBHelper()
finally:
cls._mutex.unlock()
return cls._instance
def normal_record(self, content, is_processed=0):
"""记录系统状态消息"""
self.db.insert_message(content, message_type=1, is_processed=is_processed)
def warning_record(self, content, is_processed=0):
"""记录预警消息"""
self.db.insert_message(content, message_type=2, is_processed=is_processed)
def update_warning_status(self, content, is_processed=1):
"""更新预警消息的处理状态: is_processed为1表示已经处理, 字体变为灰色"""
self.db.update_processed_status(content, message_type=2, is_processed=is_processed)
def update_normal_status(self, content, is_processed=1):
"""更新系统消息的处理状态: is_processed为1表示已经处理, 字体变为灰色"""
self.db.update_processed_status(content, message_type=1, is_processed=is_processed)
def get_latest_normal_messages(self, limit=16):
"""获取最新的系统状态消息(message_type=1), limit为获取的消息的条数, 默认16条"""
return self.db.get_latest_messages(message_type=1, limit=limit)
def get_latest_warning_messages(self, limit=16):
"""获取最新的预警消息(message_type=2), limit为获取的消息的条数, 默认16条"""
return self.db.get_latest_messages(message_type=2, limit=limit)