将变量导入逻辑修改为每次导入前删除对应表的数据库后再只导入表中的变量

main
ZHANGXUXU\95193 2 months ago
parent fca9b3dae5
commit 529508f5f2

@ -54,6 +54,9 @@ class TcRtdModel(VarTableModel):
self.checkList = ['Unchecked'] * len(self.datas)
# self.layoutChanged.emit()
self.table.proxy.invalidate()
self.table.reset()#强制重绘
# 初始化后立即刷新缓存
self.refreshValueCache()
@ -341,14 +344,38 @@ class TcRtdTypeDelegate(TcRtdButtonDelegate):
painter.fillRect(paint_rect, index.data(Qt.BackgroundRole))
if not self.parent().indexWidget(index):
comboBox = QComboBox(self.parent())
if index.row() < 8:
item = ['R', 'S', 'B', 'J', 'T','E', 'K', 'N', 'C', 'A']
# 根据实际的变量类型来设置下拉框选项,而不是根据行号
currentValue = self.parent().model.datas[index.row()][index.column()]
# 定义所有可能的类型
tcTypes = ['R', 'S', 'B', 'J', 'T','E', 'K', 'N', 'C', 'A']
rtdTypes = ['PT100']
print(currentValue,2321312)
# 如果当前值是热电偶类型
if currentValue in tcTypes:
item = tcTypes
comboBox.addItems(item)
i = item.index(self.parent().model.datas[index.row()][index.column()])
comboBox.setCurrentIndex(i)
else:
item = ['PT100']
try:
i = item.index(currentValue)
comboBox.setCurrentIndex(i)
except ValueError:
comboBox.setCurrentIndex(0)
# 如果当前值是热电阻类型
elif currentValue in rtdTypes:
item = rtdTypes
comboBox.addItems(item)
comboBox.setCurrentIndex(0)
# 如果当前值不在任何已知类型中,显示所有可用选项
else:
# 显示所有可用的类型选项,让用户自由选择
all_types = tcTypes + rtdTypes
comboBox.addItems(all_types)
# 不设置当前索引,让用户看到空值状态
comboBox.setCurrentText('')
comboBox.currentIndexChanged.connect(self.indexChange)
comboBox.setObjectName('TcRtdTypeBox')
comboBox.setEditable(True)

@ -451,33 +451,38 @@ class TcRtdManage(object):
varTypeList = ['R', 'S', 'B', 'J', 'T', 'E', 'K', 'N', 'C', 'A']
pattern = re.compile(r'[^0-9\.-]+')
# or re.findall(pattern, varName)
if not varName or not varType or not varModel or not min or not max or not channelNumber :
if not varName or not varType or not varModel or not channelNumber :
errorInfo = '表TCRTD第{}行导入失败,有关键字段为空'.format(str(index + 1))
return errorInfo
if re.findall(pattern, str(channelNumber)) or re.findall(pattern, str(min)) or re.findall(pattern, str(max)):
errorInfo = '表TCRTD第{}行导入失败,{}'.format(str(index + 1), '通道号、工程量下限、工程量上限只能为数字')
return errorInfo
if varType not in varTypeList or varType != 'PT100':
errorInfo = '表TCRTD第{}行导入失败,{}'.format(str(index + 1), str(index + 1), '变量类型错误')
# 检查变量类型是否有效热电偶类型必须在varTypeList中或者必须是PT100
if varType not in varTypeList and varType != 'PT100':
errorInfo = '表TCRTD第{}行导入失败,变量类型错误:{}'.format(str(index + 1), varType)
return errorInfo
if varModel not in ['本地值', '远程值', '模拟值']:
errorInfo = '表TCRTD第{}行导入失败,{}'.format(str(index + 1), str(index + 1), '变量模型有误')
return errorInfo
if not varNumberManage.validateChannelNumber(varType, channelNumber):
errorInfo = '表TCRTD第{}行导入失败,{}'.format(str(index + 1), '通道号超出范围')
return errorInfo
if TcRtdVar.getByChannelNumber(channelNumber):
# 如果通道号已存在,更新其信息
if varType in varTypeList:
#导入热电偶
TcRtdVar.update(varName = varName, channelNumber = channelNumber, description=des, varType=varType,
min=min, max=max, compensationVar = compensationVar, unit = unit,
initialValue=initialValue).where((TcRtdVar.channelNumber == channelNumber) & (TcRtdVar.varType != 'PT100')).execute()
else:
#导入热电阻
TcRtdVar.update(varName = varName, channelNumber = channelNumber, description=des, varType=varType,
min=min, max=max, compensationVar = compensationVar, unit = unit,
initialValue=initialValue).where((TcRtdVar.channelNumber == channelNumber) & (TcRtdVar.varType == 'PT100')).execute()
else:
tcRtdVarType = TcRtdVar()
tcRtdVarType.createVar(varName = varName, channelNumber=channelNumber, des = des, varType = varType, min = min, max = max, compensationVar = compensationVar, varModel = varModel, unit = unit,nitialValue=initialValue)
# if TcRtdVar.getByChannelNumber(channelNumber):
# # 如果通道号已存在,更新其信息
# if varType in varTypeList:
# #导入热电偶
# TcRtdVar.update(varName = varName, channelNumber = channelNumber, description=des, varType=varType,
# min=min, max=max, compensationVar = compensationVar, unit = unit,
# initialValue=initialValue).where((TcRtdVar.channelNumber == channelNumber) & (TcRtdVar.varType != 'PT100')).execute()
# else:
# #导入热电阻
# TcRtdVar.update(varName = varName, channelNumber = channelNumber, description=des, varType=varType,
# min=min, max=max, compensationVar = compensationVar, unit = unit,
# initialValue=initialValue).where((TcRtdVar.channelNumber == channelNumber) & (TcRtdVar.varType == 'PT100')).execute()
# else:
tcRtdVarType = TcRtdVar()
tcRtdVarType.createVar(varName = varName, channelNumber=channelNumber, des = des, varType = varType, min = min, max = max, compensationVar = compensationVar, varModel = varModel, unit = unit, initialValue=initialValue)
# 操作后刷新缓存
refreshCache()
@ -610,13 +615,15 @@ class AnalogManage(object):
if varModel not in ['本地值', '远程值', '模拟值']:
errorInfo = '表IO第{}行导入失败,{}'.format(str(index + 1), '变量模型有误')
return errorInfo
if AnalogVar.getByChannelNumber(channelNumber):
# 如果通道号已存在,更新其信息
AnalogVar.update(varName = varName, description=des, varType=varType, min=min, max=max, unit = unit, initialValue=initialValue).where((AnalogVar.channelNumber == channelNumber) & (AnalogVar.varType == varType)).execute()
else:
analogVarType = AnalogVar()
analogVarType.createVar(varName = varName, channelNumber = channelNumber, des = des, varType = varType, min = min, max = max, varModel = varModel,unit = unit, initialValue=initialValue)
if not varNumberManage.validateChannelNumber(varType, channelNumber):
errorInfo = '表IO第{}行导入失败,{}'.format(str(index + 1), '通道号超出范围')
return errorInfo
# if AnalogVar.getByChannelNumber(channelNumber):
# # 如果通道号已存在,更新其信息
# AnalogVar.update(varName = varName, description=des, varType=varType, min=min, max=max, unit = unit, initialValue=initialValue).where((AnalogVar.channelNumber == channelNumber) & (AnalogVar.varType == varType)).execute()
analogVarType = AnalogVar()
analogVarType.createVar(varName = varName, channelNumber = channelNumber, des = des, varType = varType, min = min, max = max, varModel = varModel,unit = unit, initialValue=initialValue)
# 操作后刷新缓存
refreshCache()
@ -791,13 +798,26 @@ class GlobalVarManager(object):
case 'IO' | 'TCRTD':
requiredFields = ['变量名', '通道序号', '变量类型'] if sheetName == 'IO' else \
['变量名', '通道序号', '变量类型', '补偿值']
importFunc = AnalogManage.importVarForm if sheetName == 'IO' else TcRtdManage.importVarForm
# 先清空现有数据,再导入新数据
if sheetName == 'IO':
try:
deleted_count = AnalogVar.delete().execute()
print(f"已清空 {deleted_count} 条IO变量数据")
except Exception as e:
print(f"清空IO数据失败: {str(e)}")
else: # TCRTD
try:
deleted_count = TcRtdVar.delete().execute()
print(f"已清空 {deleted_count} 条TCRTD变量数据")
except Exception as e:
print(f"清空TCRTD数据失败: {str(e)}")
importFunc = AnalogManage.importVarForm if sheetName == 'IO' else TcRtdManage.importVarForm
for index, row in data.iterrows():
if any(pd.isna(row[field]) for field in requiredFields):
errorConList.append(f'{index+1}行关键字段为空')
continue
try:
if sheetName == 'IO':
errInfo = importFunc(
@ -832,6 +852,9 @@ class GlobalVarManager(object):
errorConList.append(errInfo)
except Exception as e:
errorConList.append(f'{index+1}行导入失败: {str(e)}')
case 'Hart模拟' | 'Hart读取':
requiredFields = ['变量名']
@ -860,5 +883,56 @@ class GlobalVarManager(object):
class varNumberManage(object):
def __init__(self):
super(varNumberManage, self).__init__()
@classmethod
def validateChannelNumber(cls, varType, channelNumber):
"""
验证通道号是否在正确的范围内
Args:
varType (str): 变量类型
channelNumber (int): 通道号
Returns:
bool: 通道号在范围内返回True否则返回False
"""
# 确保channelNumber是整数
try:
channelNumber = int(channelNumber)
except (ValueError, TypeError):
return False
# 定义各类型的通道号范围
channelRanges = {
'AO': (1, 16),
'DO': (1, 16),
'AI': (1, 8),
'DI': (1, 16),
'PT100': (1, 8),
# 热电偶类型
'R': (1, 8),
'S': (1, 8),
'B': (1, 8),
'J': (1, 8),
'T': (1, 8),
'E': (1, 8),
'K': (1, 8),
'N': (1, 8),
'C': (1, 8),
'A': (1, 8)
}
# 检查变量类型是否在支持的类型中
if varType not in channelRanges:
return False
# 获取该类型的通道号范围
minChannel, maxChannel = channelRanges[varType]
# 验证通道号是否在范围内
return minChannel <= channelNumber <= maxChannel

Loading…
Cancel
Save