|
|
@ -124,9 +124,42 @@ class ControlManager:
|
|
|
|
if os.path.exists(rulesFile):
|
|
|
|
if os.path.exists(rulesFile):
|
|
|
|
self.ruleEngine.loadRulesFromFile(rulesFile)
|
|
|
|
self.ruleEngine.loadRulesFromFile(rulesFile)
|
|
|
|
print(f"从工程配置加载规则: {rulesFile}")
|
|
|
|
print(f"从工程配置加载规则: {rulesFile}")
|
|
|
|
|
|
|
|
# 自动启动规则
|
|
|
|
|
|
|
|
self._autoStartRules()
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
print(f"工程规则文件不存在,将创建新文件: {rulesFile}")
|
|
|
|
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):
|
|
|
|
def saveConfiguration(self):
|
|
|
|
"""保存配置"""
|
|
|
|
"""保存配置"""
|
|
|
|
# 动态更新配置路径
|
|
|
|
# 动态更新配置路径
|
|
|
@ -151,6 +184,10 @@ class ControlManager:
|
|
|
|
self.ruleEngine.saveRulesToFile(oldRulesFile)
|
|
|
|
self.ruleEngine.saveRulesToFile(oldRulesFile)
|
|
|
|
print(f"已保存旧工程规则: {oldConfigPath}")
|
|
|
|
print(f"已保存旧工程规则: {oldConfigPath}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 停止当前的变量监控
|
|
|
|
|
|
|
|
if self.variableMonitor.isRunning:
|
|
|
|
|
|
|
|
self.variableMonitor.stopMonitoring()
|
|
|
|
|
|
|
|
|
|
|
|
# 清空当前规则和变量监控
|
|
|
|
# 清空当前规则和变量监控
|
|
|
|
with self._lock:
|
|
|
|
with self._lock:
|
|
|
|
self.ruleEngine.rules.clear()
|
|
|
|
self.ruleEngine.rules.clear()
|
|
|
@ -160,7 +197,7 @@ class ControlManager:
|
|
|
|
# 更新配置路径(基于新的当前工程)
|
|
|
|
# 更新配置路径(基于新的当前工程)
|
|
|
|
self.configPath = self._getConfigPath()
|
|
|
|
self.configPath = self._getConfigPath()
|
|
|
|
|
|
|
|
|
|
|
|
# 加载新工程的规则
|
|
|
|
# 加载新工程的规则(包含自动启动)
|
|
|
|
self._loadConfiguration()
|
|
|
|
self._loadConfiguration()
|
|
|
|
|
|
|
|
|
|
|
|
print(f"已切换到工程规则配置: {self.configPath}")
|
|
|
|
print(f"已切换到工程规则配置: {self.configPath}")
|
|
|
@ -168,6 +205,23 @@ class ControlManager:
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
print(f"切换工程规则配置失败: {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):
|
|
|
|
def addMonitorVariable(self, varName: str, threshold: float = 0.1):
|
|
|
|
"""添加监控变量"""
|
|
|
|
"""添加监控变量"""
|
|
|
|
self.variableMonitor.addVariable(varName, threshold)
|
|
|
|
self.variableMonitor.addVariable(varName, threshold)
|
|
|
|