From 4003e69ecd20f166a329d9479f83bedec1cdcc5d Mon Sep 17 00:00:00 2001 From: zcwBit Date: Mon, 21 Jul 2025 10:53:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=83=A8=E5=88=86=E8=A7=84?= =?UTF-8?q?=E5=88=99=E7=9B=B8=E5=85=B3bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UI/ControlManage/ControlWidget.py | 2 +- UI/Main/Main.py | 3 ++ UI/VarManages/RpcVarModel.py | 4 ++- UI/VarManages/VarTable.py | 2 +- bin.py | 1 + control/ControlManager.py | 56 ++++++++++++++++++++++++++++- model/ProjectModel/ProjectManage.py | 3 ++ 7 files changed, 67 insertions(+), 4 deletions(-) diff --git a/UI/ControlManage/ControlWidget.py b/UI/ControlManage/ControlWidget.py index a2addfb..86e7333 100644 --- a/UI/ControlManage/ControlWidget.py +++ b/UI/ControlManage/ControlWidget.py @@ -922,7 +922,7 @@ class ControlWidget(QWidget): if not self.controlManager.isInitialized: self.controlManager.initialize() else: - # 如果已初始化,重新加载配置 + # 如果已初始化,重新加载配置(包含自动启动) self.controlManager._loadConfiguration() # 刷新界面 diff --git a/UI/Main/Main.py b/UI/Main/Main.py index fd26770..f4cefac 100644 --- a/UI/Main/Main.py +++ b/UI/Main/Main.py @@ -231,6 +231,9 @@ class MainWindow(QMainWindow): # 重新加载控制系统(确保规则正确加载) self.controlWidget.reloadControlSystem() self.exButtonClicked(6) + + + def loadVar(self): file_path, _ = QFileDialog.getOpenFileName(self, '选择Excel文件', '', 'Excel Files (*.xlsx *.xls)') diff --git a/UI/VarManages/RpcVarModel.py b/UI/VarManages/RpcVarModel.py index 6aa1794..5fe93f3 100644 --- a/UI/VarManages/RpcVarModel.py +++ b/UI/VarManages/RpcVarModel.py @@ -15,6 +15,7 @@ class RpcVarModel(QAbstractTableModel): self.refreshTimer = QTimer() self.refreshTimer.timeout.connect(self.refreshData) self.refreshTimer.start(500) # 每秒刷新一次 + self.table = parent def refreshData(self): """刷新所有远程变量数据,并按类型排序""" @@ -22,6 +23,7 @@ class RpcVarModel(QAbstractTableModel): if protocolManage and protocolManage.RpcServer: allVars = protocolManage.RpcServer.broadcastRead() temp = [] + # print(allVars) for client in allVars: clientName = client.get('client', '') variables = client.get('variables', {}) @@ -40,7 +42,7 @@ class RpcVarModel(QAbstractTableModel): # 按类型排序 temp.sort(key=lambda x: x[3]) self.datas = temp - self.layoutChanged.emit() + self.table.proxy.invalidate() def rowCount(self, parent=None): return len(self.datas) diff --git a/UI/VarManages/VarTable.py b/UI/VarManages/VarTable.py index a0900bd..20e511e 100644 --- a/UI/VarManages/VarTable.py +++ b/UI/VarManages/VarTable.py @@ -209,7 +209,7 @@ class RpcVarTableView(QTableView): """RPC变量表格搜索接口""" try: # 设置搜索列为变量名列(索引1) - self.proxy.setFilterKeyColumn(1) + self.proxy.setFilterKeyColumn(2) search = QtCore.QRegExp(searchText, QtCore.Qt.CaseInsensitive, QtCore.QRegExp.RegExp) self.proxy.setFilterRegExp(search) return True diff --git a/bin.py b/bin.py index 1344d30..9b010ff 100644 --- a/bin.py +++ b/bin.py @@ -29,4 +29,5 @@ if __name__ == '__main__': ex = MainWindow() Globals.setValue('MainWindow', ex) ex.show() + sys.exit(app.exec_()) \ No newline at end of file diff --git a/control/ControlManager.py b/control/ControlManager.py index a3a6f5e..2a318e8 100644 --- a/control/ControlManager.py +++ b/control/ControlManager.py @@ -124,9 +124,42 @@ class ControlManager: if os.path.exists(rulesFile): self.ruleEngine.loadRulesFromFile(rulesFile) print(f"从工程配置加载规则: {rulesFile}") + # 自动启动规则 + self._autoStartRules() else: print(f"工程规则文件不存在,将创建新文件: {rulesFile}") + def _autoStartRules(self): + """自动启动所有启用的规则""" + try: + rules = self.ruleEngine.getAllRules() + if not rules: + print("没有找到规则,跳过自动启动") + return + + # 自动添加所有规则中涉及的变量到监控器 + monitoredVars = set() + enabledRulesCount = 0 + + for rule in rules: + if rule.enabled: + enabledRulesCount += 1 + # 添加规则条件中的变量到监控 + for condition in rule.conditions: + if condition.variable not in monitoredVars: + self.addMonitorVariable(condition.variable) + monitoredVars.add(condition.variable) + + print(f"自动启动完成: {enabledRulesCount} 个启用规则, {len(monitoredVars)} 个监控变量") + + # 确保变量监控器正在运行 + if not self.variableMonitor.isRunning: + self.variableMonitor.startMonitoring() + print("已启动变量监控器") + + except Exception as e: + print(f"自动启动规则失败: {e}") + def saveConfiguration(self): """保存配置""" # 动态更新配置路径 @@ -151,6 +184,10 @@ class ControlManager: self.ruleEngine.saveRulesToFile(oldRulesFile) print(f"已保存旧工程规则: {oldConfigPath}") + # 停止当前的变量监控 + if self.variableMonitor.isRunning: + self.variableMonitor.stopMonitoring() + # 清空当前规则和变量监控 with self._lock: self.ruleEngine.rules.clear() @@ -160,7 +197,7 @@ class ControlManager: # 更新配置路径(基于新的当前工程) self.configPath = self._getConfigPath() - # 加载新工程的规则 + # 加载新工程的规则(包含自动启动) self._loadConfiguration() print(f"已切换到工程规则配置: {self.configPath}") @@ -168,6 +205,23 @@ class ControlManager: except Exception as e: print(f"切换工程规则配置失败: {e}") + def delayedAutoStart(self, delay: int = 2000): + """延迟自动启动规则(给协议管理器时间初始化)""" + def autoStartAfterDelay(): + time.sleep(delay / 1000.0) # 转换为秒 + try: + # 重新集成协议管理器 + self._integrateWithProtocolManager() + # 自动启动规则 + self._autoStartRules() + print("延迟自动启动规则完成") + except Exception as e: + print(f"延迟自动启动规则失败: {e}") + + # 在后台线程中执行延迟启动 + import threading + threading.Thread(target=autoStartAfterDelay, daemon=True).start() + def addMonitorVariable(self, varName: str, threshold: float = 0.1): """添加监控变量""" self.variableMonitor.addVariable(varName, threshold) diff --git a/model/ProjectModel/ProjectManage.py b/model/ProjectModel/ProjectManage.py index 6297023..94c9d3d 100644 --- a/model/ProjectModel/ProjectManage.py +++ b/model/ProjectModel/ProjectManage.py @@ -169,6 +169,9 @@ class ProjectManage(object): if not controlManager.isInitialized: controlManager.initialize() controlManager.switchProject() + + # 延迟自动启动规则,确保协议管理器完全初始化 + controlManager.delayedAutoStart(3000) # 3秒延迟 except Exception as e: print(f"通知控制系统工程切换失败: {e}")