|
|
import os
|
|
|
import json
|
|
|
from utils import Globals
|
|
|
|
|
|
|
|
|
class GlobalConfigManager:
|
|
|
"""全局配置管理器 - 所有工程使用统一配置"""
|
|
|
|
|
|
CONFIG_FILE = 'Static/global_config.json'
|
|
|
|
|
|
@classmethod
|
|
|
def loadGlobalConfig(cls):
|
|
|
"""加载全局配置文件"""
|
|
|
try:
|
|
|
with open(cls.CONFIG_FILE, 'r', encoding='utf-8') as f:
|
|
|
data = json.load(f)
|
|
|
return data.get('globalConfig', cls.getDefaultConfig())
|
|
|
except Exception as e:
|
|
|
print(f"加载全局配置文件失败: {e}")
|
|
|
return cls.getDefaultConfig()
|
|
|
|
|
|
@classmethod
|
|
|
def getDefaultConfig(cls):
|
|
|
"""获取默认配置(当配置文件不存在时使用)"""
|
|
|
return {
|
|
|
"name": "默认全局配置",
|
|
|
"description": "所有工程统一使用的默认配置",
|
|
|
"variableModules": {
|
|
|
"modbusTcpMaster": True,
|
|
|
"modbusTcpSlave": True,
|
|
|
"modbusRtuMaster": True,
|
|
|
"modbusRtuSlave": True,
|
|
|
"ioModule": True,
|
|
|
"tcRtdModule": True,
|
|
|
"hartReadModule": True,
|
|
|
"hartSimulateModule": True,
|
|
|
"profibusModule": True,
|
|
|
"rpcModule": True
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@classmethod
|
|
|
def saveGlobalConfig(cls, config):
|
|
|
"""保存全局配置文件"""
|
|
|
try:
|
|
|
data = {"globalConfig": config}
|
|
|
with open(cls.CONFIG_FILE, 'w', encoding='utf-8') as f:
|
|
|
json.dump(data, f, indent=2, ensure_ascii=False)
|
|
|
return True
|
|
|
except Exception as e:
|
|
|
print(f"保存全局配置文件失败: {e}")
|
|
|
return False
|
|
|
|
|
|
@classmethod
|
|
|
def getModuleDisplayName(cls, moduleKey):
|
|
|
"""获取模块显示名称"""
|
|
|
displayNames = {
|
|
|
'modbusTcpMaster': 'MODBUSTCP主站',
|
|
|
'modbusTcpSlave': 'MODBUSTCP从站',
|
|
|
'modbusRtuMaster': 'MODBUSRTU主站',
|
|
|
'modbusRtuSlave': 'MODBUSRTU从站',
|
|
|
'ioModule': 'IO模块',
|
|
|
'tcRtdModule': 'TCRTD模块',
|
|
|
'hartReadModule': 'HART读取模块',
|
|
|
'hartSimulateModule': 'HART模拟模块',
|
|
|
'profibusModule': 'PROFIBUS模块',
|
|
|
'rpcModule': '远程通讯模块'
|
|
|
}
|
|
|
return displayNames.get(moduleKey, moduleKey)
|
|
|
|
|
|
@classmethod
|
|
|
def getModuleWidgetClass(cls, moduleKey):
|
|
|
"""获取模块对应的Widget类"""
|
|
|
from UI.VarManages.VarWidget import VarWidgets, TcRtdWidgets, AnalogWidgets, HartSimulateWidgets
|
|
|
from UI.HartWidgets.HartMainWindow import HartMainWindow
|
|
|
from UI.VarManages.VarTable import RpcVarTableView
|
|
|
from UI.ProfibusWidgets.ProfibusWindow import ProfibusWidgets
|
|
|
|
|
|
widgetClasses = {
|
|
|
'modbusTcpMaster': lambda: VarWidgets('ModbusTcpMaster'),
|
|
|
'modbusTcpSlave': lambda: VarWidgets('ModbusTcpSlave'),
|
|
|
'modbusRtuMaster': lambda: VarWidgets('ModbusRtuMaster'),
|
|
|
'modbusRtuSlave': lambda: VarWidgets('ModbusRtuSlave'),
|
|
|
'ioModule': AnalogWidgets,
|
|
|
'tcRtdModule': TcRtdWidgets,
|
|
|
'hartReadModule': HartMainWindow,
|
|
|
'hartSimulateModule': HartSimulateWidgets,
|
|
|
'profibusModule': ProfibusWidgets,
|
|
|
'rpcModule': RpcVarTableView
|
|
|
}
|
|
|
return widgetClasses.get(moduleKey)
|
|
|
|
|
|
@classmethod
|
|
|
def getEnabledModules(cls):
|
|
|
"""获取启用的模块列表"""
|
|
|
config = cls.loadGlobalConfig()
|
|
|
variableModules = config.get('variableModules', {})
|
|
|
return [moduleKey for moduleKey, enabled in variableModules.items() if enabled]
|
|
|
|
|
|
@classmethod
|
|
|
def isModuleEnabled(cls, moduleKey):
|
|
|
"""检查模块是否启用"""
|
|
|
config = cls.loadGlobalConfig()
|
|
|
variableModules = config.get('variableModules', {})
|
|
|
return variableModules.get(moduleKey, False)
|
|
|
|
|
|
@classmethod
|
|
|
def getEnabledModelTables(cls):
|
|
|
"""获取启用模块对应的模型表列表"""
|
|
|
config = cls.loadGlobalConfig()
|
|
|
variableModules = config.get('variableModules', {})
|
|
|
|
|
|
# 模块与模型表的映射关系
|
|
|
moduleToModelMapping = {
|
|
|
'modbusTcpMaster': 'ModbusTcpMasterTable',
|
|
|
'modbusTcpSlave': 'ModbusTcpSlaveTable',
|
|
|
'modbusRtuMaster': 'ModbusRtuMasterTable',
|
|
|
'modbusRtuSlave': 'ModbusRtuSlaveTable',
|
|
|
'tcRtdModule': 'TcRtdTable',
|
|
|
'ioModule': 'AnalogTable',
|
|
|
'hartSimulateModule': 'HartSimulateTable'
|
|
|
# 注意:userTable 不在变量管理模块中,始终需要初始化
|
|
|
# hartReadModule 和 profibusModule 可能没有对应的表模型
|
|
|
# rpcModule 可能也没有对应的表模型
|
|
|
}
|
|
|
|
|
|
enabledTables = []
|
|
|
|
|
|
# 添加启用模块对应的表
|
|
|
for moduleKey, enabled in variableModules.items():
|
|
|
if enabled and moduleKey in moduleToModelMapping:
|
|
|
enabledTables.append(moduleToModelMapping[moduleKey])
|
|
|
|
|
|
# 始终添加用户表(非变量管理模块)
|
|
|
enabledTables.append('userTable')
|
|
|
|
|
|
return enabledTables
|
|
|
|
|
|
@classmethod
|
|
|
def getEnabledModelClasses(cls):
|
|
|
"""获取启用模块对应的数据库模型类列表"""
|
|
|
from utils.DBModels.ProtocolModel import (
|
|
|
ModbusTcpMasterVar, ModbusTcpSlaveVar, ModbusRtuMasterVar, ModbusRtuSlaveVar,
|
|
|
HartVar, TcRtdVar, AnalogVar, HartSimulateVar
|
|
|
)
|
|
|
|
|
|
config = cls.loadGlobalConfig()
|
|
|
variableModules = config.get('variableModules', {})
|
|
|
|
|
|
# 模块与数据库模型类的映射关系
|
|
|
moduleToModelClassMapping = {
|
|
|
'modbusTcpMaster': ModbusTcpMasterVar,
|
|
|
'modbusTcpSlave': ModbusTcpSlaveVar,
|
|
|
'modbusRtuMaster': ModbusRtuMasterVar,
|
|
|
'modbusRtuSlave': ModbusRtuSlaveVar,
|
|
|
'hartReadModule': HartVar,
|
|
|
'tcRtdModule': TcRtdVar,
|
|
|
'ioModule': AnalogVar,
|
|
|
'hartSimulateModule': HartSimulateVar
|
|
|
}
|
|
|
|
|
|
enabledModelClasses = []
|
|
|
|
|
|
# 添加启用模块对应的模型类
|
|
|
for moduleKey, enabled in variableModules.items():
|
|
|
if enabled and moduleKey in moduleToModelClassMapping:
|
|
|
enabledModelClasses.append(moduleToModelClassMapping[moduleKey])
|
|
|
|
|
|
return enabledModelClasses
|
|
|
|
|
|
@classmethod
|
|
|
def needsTcpVarManager(cls):
|
|
|
"""检查是否需要初始化TCPVarManager(当启用IO或TCRTD模块时)"""
|
|
|
config = cls.loadGlobalConfig()
|
|
|
variableModules = config.get('variableModules', {})
|
|
|
|
|
|
# TCPVarManager相关的模块
|
|
|
tcpVarModules = ['ioModule', 'tcRtdModule']
|
|
|
|
|
|
# 检查是否有任何相关模块被启用
|
|
|
for moduleKey in tcpVarModules:
|
|
|
if variableModules.get(moduleKey, False):
|
|
|
return True
|
|
|
|
|
|
return False |