main
DESKTOP-3D7M4SA\Hicent 1 month ago
parent be4f422da7
commit 3975f0be05

@ -43,12 +43,12 @@ class ProtocolManage(object):
self.historyDBManage = Globals.getValue('historyDBManage')
self.variableValueCache = {} # {varName: value}
self.profibusManager = None
self.profibusVarMap = {}
self.profibusDeviceMeta = {}
self.profibusEnabled = GlobalConfigManager.isModuleEnabled('profibusModule')
self._profibusConnected = False
self._profibusLastUpdate = 0
self.profibusLock = threading.Lock()
if self.profibusEnabled:
# print('yeyeye')
self._initializeProfibusSupport()
@ -102,7 +102,8 @@ class ProtocolManage(object):
except Exception as e:
print(f"刷新缓存时出错: {modelClass.__name__}: {e}")
if self.profibusEnabled and self.profibusManager:
self._refreshProfibusVarCache()
self._registerProfibusVariables()
def lookupVariable(self, variableName):
@ -126,8 +127,13 @@ class ProtocolManage(object):
}
self.varInfoCache[variableName] = result # 写入缓存
return result
if self.profibusEnabled and self.profibusManager:
profibusResult = self._lookupProfibusVariable(variableName)
if profibusResult:
return profibusResult
return None
def setClentMode(self, clentName, rabbitmqHost='localhost'):
if self.RpcClient:
self.RpcClient.close()
@ -255,6 +261,7 @@ class ProtocolManage(object):
return False
value = normalizedValue
# HART模拟变量处理
elif modelType == 'HartSimulateVar':
@ -325,19 +332,19 @@ class ProtocolManage(object):
def _initializeProfibusSupport(self):
try:
self.profibusManager = DevicesManange()
self._loadProfibusDevicesFromDB()
self._loadProfibusConfiguration()
self.profibusManager.recalculateAddress()
self._registerProfibusVariables()
except Exception as e:
print(f"初始化PROFIBUS管理器失败: {e}")
self.profibusManager = None
self.profibusEnabled = False
def _loadProfibusDevicesFromDB(self):
def _loadProfibusConfiguration(self):
self.profibusDeviceMeta = {}
allDevices = DevicesManange.getAllDevice()
if not allDevices or isinstance(allDevices, str):
return
for deviceRow in allDevices:
if not deviceRow:
continue
@ -365,9 +372,9 @@ class ProtocolManage(object):
except Exception as e:
print(f"初始化设备 {deviceName} 失败: {e}")
continue
self._loadAreasForDevice(deviceName, areaJson)
self._addProfibusAreas(deviceName, areaJson)
def _loadAreasForDevice(self, deviceName, areaJson):
def _addProfibusAreas(self, deviceName, areaJson):
if not areaJson:
return
try:
@ -403,19 +410,20 @@ class ProtocolManage(object):
print(f"添加设备 {deviceName} 的区域 {valueName} 失败: {e}")
continue
def _refreshProfibusVarCache(self):
self.profibusVarMap.clear()
if not self.profibusEnabled:
def _registerProfibusVariables(self):
if not self.profibusEnabled or not self.profibusManager:
return
with self.profibusLock:
try:
self.profibusManager = DevicesManange()
self._loadProfibusDevicesFromDB()
self.profibusManager.recalculateAddress()
self._profibusConnected = False
self._profibusLastUpdate = 0
except Exception as e:
print(f"刷新PROFIBUS设备失败: {e}")
for areaInfo in self._iterProfibusAreas():
varName = areaInfo.get('valueName')
if not varName:
continue
self.varInfoCache[varName] = {
'modelType': 'ProfibusVar',
'variableData': dict(areaInfo)
}
def _iterProfibusAreas(self):
if not self.profibusManager:
return
deviceGroups = [
self.profibusManager.dpMasterDevices,
@ -423,21 +431,17 @@ class ProtocolManage(object):
self.profibusManager.paMasterDevices,
self.profibusManager.paSlaveDevices
]
snapshot = []
for devicesDict in deviceGroups:
for deviceName, device in devicesDict.items():
deviceMeta = self.profibusDeviceMeta.get(deviceName, {})
for areaIndex, area in enumerate(device.areas):
valueName = getattr(area, 'valueName', None)
if not valueName:
continue
areaInfo = {
yield {
'deviceName': deviceName,
'areaIndex': areaIndex,
'areaType': area.type,
'bytes': area.bytes,
'order': area.order,
'valueName': valueName,
'bytes': getattr(area, 'bytes', 0),
'order': getattr(area, 'order', 'ABCD'),
'valueName': getattr(area, 'valueName', None),
'proType': device.type,
'masterSlaveModel': device.masterOrSlave,
'min': deviceMeta.get('pvLowerLimit'),
@ -445,16 +449,22 @@ class ProtocolManage(object):
'unit': deviceMeta.get('pvUnit'),
'varType': area.type
}
snapshot.append((valueName, areaInfo))
for valueName, areaInfo in snapshot:
self.profibusVarMap[valueName] = areaInfo
self.varInfoCache[valueName] = {
def _lookupProfibusVariable(self, variableName):
if not self.profibusEnabled or not self.profibusManager:
return None
for areaInfo in self._iterProfibusAreas() or []:
if areaInfo.get('valueName') == variableName:
result = {
'modelType': 'ProfibusVar',
'variableData': areaInfo
'variableData': dict(areaInfo)
}
self.varInfoCache[variableName] = result
return result
return None
def _ensureProfibusConnected(self):
if not self.profibusManager:
return False
if self._profibusConnected:
@ -637,40 +647,14 @@ class ProtocolManage(object):
return False
return self._ensureProfibusConnected()
def refreshProfibusVariables(self):
if self.profibusEnabled:
self._refreshProfibusVarCache()
def updateProfibusAreas(self, force: bool = False) -> bool:
if not self.hasProfibusSupport():
return False
self._updateProfibusAreas(force=force)
return True
def listProfibusVariables(self) -> Dict[str, Dict[str, Any]]:
return {name: dict(info) for name, info in self.profibusVarMap.items()}
def getProfibusVariableInfo(self, variableName: str) -> Optional[Dict[str, Any]]:
return self.profibusVarMap.get(variableName)
def readProfibusValue(self, variableName: str, useCache: bool = True):
if not self.hasProfibusSupport():
return None
if useCache:
with self.cacheLock:
if variableName in self.variableValueCache:
return self.variableValueCache[variableName]
value = self.readVariableValue(variableName)
with self.cacheLock:
self.variableValueCache[variableName] = value
return value
def writeProfibusValue(self, variableName: str, value) -> bool:
if not self.hasProfibusSupport():
return False
return bool(self.writeVariableValue(variableName, value))
def getAllVariableNames(self):
return list(self.varInfoCache.keys())

Loading…
Cancel
Save