main
ZHANGXUXU\95193 4 months ago
parent 4fff1a8782
commit 456c7c5248

@ -165,6 +165,17 @@ QPushButton#createProject:checked, QPushButton#openProject:checked, QPushButton#
} }
QPushButton#importBtn {
color: #0076ff;
font-size: 22px;
margin-bottom: 6px
}
QPushButton#forceBtn, QPushButton#exportBtn, QPushButton#importBtn, QPushButton#createBtn, QPushButton#delBtn, QPushButton#messageBtn, QPushButton#startProtocolBtn, QPushButton#clearBtn{ QPushButton#forceBtn, QPushButton#exportBtn, QPushButton#importBtn, QPushButton#createBtn, QPushButton#delBtn, QPushButton#messageBtn, QPushButton#startProtocolBtn, QPushButton#clearBtn{
color: #0076ff; color: #0076ff;
@ -173,6 +184,17 @@ QPushButton#forceBtn, QPushButton#exportBtn, QPushButton#importBtn, QPushButton#
} }
QPushButton#forceBtn:hover, QPushButton#exportBtn:hover, QPushButton#importBtn:hover,
QPushButton#createBtn:hover, QPushButton#delBtn:hover, QPushButton#messageBtn:hover,
QPushButton#startProtocolBtn:hover, QPushButton#clearBtn:hover {
font: bold;
border: none;
margin-bottom: -2px
}
QPushButton#loginButton, QPushButton#exitButton{ QPushButton#loginButton, QPushButton#exitButton{
width: 54.75px; width: 54.75px;
@ -193,6 +215,15 @@ QPushButton#loginButton, QPushButton#exitButton{
} }
QPushButton#loginButton:hover, QPushButton#exitButton:hover{
font: bold;
border: none;
margin-bottom: -2px
}
QPushButton#mesButton{ QPushButton#mesButton{
width: 80px; width: 80px;

@ -223,19 +223,22 @@ class MainWindow(QMainWindow):
repeated_names = [str(name) for name in set(allVarNames) if allVarNames.count(name) > 1] repeated_names = [str(name) for name in set(allVarNames) if allVarNames.count(name) > 1]
QMessageBox.warning(self, '警告', f'发现重复的变量名: {", ".join(repeated_names)}') QMessageBox.warning(self, '警告', f'发现重复的变量名: {", ".join(repeated_names)}')
return return
allErrInfo = []
for sheet_name, data in excelData.items(): for sheet_name, data in excelData.items():
if '变量名' in data.columns: if '变量名' in data.columns:
GlobalVarManager.importVar(sheet_name, data) importInfo = GlobalVarManager.importVar(sheet_name, data)
allErrInfo.append(importInfo)
if allErrInfo:
# 过滤掉空字符串
allErrInfo = [info for info in allErrInfo if info]
if not allErrInfo:
QMessageBox.information(self, '导入信息', '所有变量导入成功')
else:
errorMessages = '\n'.join(allErrInfo)
QMessageBox.information(self, '导入信息', errorMessages)
# Uncomment the following lines if you want to update or insert variables into the database
# db_manager = DBManager() # Assuming you have a DBManager class to handle database operations
# for _, row in data.iterrows():
# variable_name = row['变量名']
# value = row.iloc[1] if len(row) > 1 else None
# db_manager.update_or_insert_variable(variable_name, value)
def enum(self,**enums): def enum(self,**enums):
return type('Enum', (), enums) return type('Enum', (), enums)

@ -276,6 +276,7 @@ class VarButtonDelegate(QItemDelegate):
button2.oldName = False button2.oldName = False
button2.isEdit = True button2.isEdit = True
button3.editButton = button2
# button4.setStyleSheet("border:none;") # button4.setStyleSheet("border:none;")
button1.index = [index.row(), index.column()] button1.index = [index.row(), index.column()]
@ -449,6 +450,7 @@ class VarButtonDelegate(QItemDelegate):
model = self.parent().model model = self.parent().model
name = str(model.datas[sender.index[0]][3]) name = str(model.datas[sender.index[0]][3])
modbusType = self.parent().modbusType modbusType = self.parent().modbusType
if sender.editButton.isEdit:
ModbusVarManage.deleteVar(name = name, modbusType = modbusType) ModbusVarManage.deleteVar(name = name, modbusType = modbusType)
model.remove_row(sender.index[0]) model.remove_row(sender.index[0])

@ -1,5 +1,9 @@
from ast import mod
import re
from numpy import var
import pandas as pd
from utils import Globals from utils import Globals
import openpyxl import openpyxl
from utils.DBModels.ProtocolModel import ModbusTcpMasterVar, ModbusRtuMasterVar, ModbusRtuSlaveVar,\ from utils.DBModels.ProtocolModel import ModbusTcpMasterVar, ModbusRtuMasterVar, ModbusRtuSlaveVar,\
@ -25,8 +29,12 @@ class ModbusVarManage(object):
return self.ModBusVarClass.get(modbusType) return self.ModBusVarClass.get(modbusType)
@classmethod @classmethod
def importVarForm(self, varName, varType, des, address, slaveID, min, max, order, modbusType, varModel): def importVarForm(self, varName, varType, des, address, slaveID, min, max, order, modbusType, varModel, index):
varClass = self.getVarClass(modbusType) varClass = self.getVarClass(modbusType)
if varType not in [0, 1, 3, 4]:
errorInfo = '{}{}行导入失败,{}'.format(str(modbusType), str(index + 1), '变量类型有误')
return errorInfo
if varClass.getByName(varName): if varClass.getByName(varName):
# 如果变量已存在,更新其信息 # 如果变量已存在,更新其信息
varClass.update(varType=varType, description=des, address=address, slaveID=slaveID, min=min, max=max, order=order, varModel=varModel).where(varClass.varName == varName).execute() varClass.update(varType=varType, description=des, address=address, slaveID=slaveID, min=min, max=max, order=order, varModel=varModel).where(varClass.varName == varName).execute()
@ -619,106 +627,103 @@ class GlobalVarManager(object):
else: else:
return False return False
@classmethod @classmethod
def importVar(cls,sheetName, data): def importVar(cls, sheetName, data):
"""导入变量""" """导入变量"""
errorConList = []
match sheetName: match sheetName:
case 'ModbusTCP主站': case 'ModbusTCP主站' | 'ModbusTCP从站' | 'ModbusRTU主站' | 'ModbusRTU从站':
requiredFields = ['变量名', '变量类型', '寄存器地址', '从站地址']
for index, row in data.iterrows(): for index, row in data.iterrows():
ModbusVarManage.importVarForm( # 检查关键字段是否为空
varName=row['变量名'], if any(pd.isna(row[field]) for field in requiredFields):
varType=row['变量类型'], errorConList.append(f'{index+1}行关键字段为空')
des=row['变量描述'], continue
address=row['寄存器地址'],
slaveID=row['从站地址'], try:
min=row['工程量下限'], # 统一处理Modbus导入
max=row['工程量上限'], modbusType = 'ModbusTcpMaster' if sheetName == 'ModbusTCP主站' else \
order=row['字节顺序'], 'ModbusTcpSlave' if sheetName == 'ModbusTCP从站' else \
modbusType='ModbusTcpMaster', 'ModbusRtuMaster' if sheetName == 'ModbusRTU主站' else 'ModbusRtuSlave'
varModel=row['值类型']
) errInfo = ModbusVarManage.importVarForm(
case 'ModbusTCP从站':
for index, row in data.iterrows():
ModbusVarManage.importVarForm(
varName=row['变量名'],
varType=row['变量类型'],
des=row['变量描述'],
address=row['寄存器地址'],
slaveID=row['从站地址'],
min=row['工程量下限'],
max=row['工程量上限'],
order=row['字节顺序'],
modbusType='ModbusTcpSlave',
varModel=row['值类型']
)
case 'ModbusRTU主站':
for index, row in data.iterrows():
ModbusVarManage.importVarForm(
varName=row['变量名'],
varType=row['变量类型'],
des=row['变量描述'],
address=row['寄存器地址'],
slaveID=row['从站地址'],
min=row['工程量下限'],
max=row['工程量上限'],
order=row['字节顺序'],
modbusType='ModbusTcpSlave',
varModel=row['值类型']
)
case 'ModbusRTU从站':
for index, row in data.iterrows():
ModbusVarManage.importVarForm(
varName=row['变量名'], varName=row['变量名'],
varType=row['变量类型'], varType=row['变量类型'],
des=row['变量描述'], des=row.get('变量描述', ''),
address=row['寄存器地址'], address=row['寄存器地址'],
slaveID=row['从站地址'], slaveID=row['从站地址'],
min=row['工程量下限'], min=row.get('工程量下限', ''),
max=row['工程量上限'], max=row.get('工程量上限', ''),
order=row['字节顺序'], order=row.get('字节顺序', 'int'),
modbusType='ModbusTcpSlave', modbusType=modbusType,
varModel=row['值类型'] varModel=row.get('值类型', ''),
index=index
) )
case 'IO': if errInfo:
errorConList.append(errInfo)
except Exception as e:
errorConList.append(f'{index+1}行导入失败: {str(e)}')
case 'IO' | 'TCRTD':
requiredFields = ['变量名', '通道序号', '变量类型'] if sheetName == 'IO' else \
['变量名', '通道序号', '变量类型', '补偿值']
importFunc = AnalogManage.importVarForm if sheetName == 'IO' else TcRtdManage.importVarForm
for index, row in data.iterrows(): for index, row in data.iterrows():
AnalogManage.importVarForm( if any(pd.isna(row[field]) for field in requiredFields):
errorConList.append(f'{index+1}行关键字段为空')
continue
try:
if sheetName == 'IO':
importFunc(
varName=row['变量名'], varName=row['变量名'],
channelNumber=row['通道序号'], channelNumber=row['通道序号'],
varType=row['变量类型'], varType=row['变量类型'],
des=row['变量描述'], des=row.get('变量描述', ''),
min=row['工程量下限'], min=row.get('工程量下限', ''),
max=row['工程量上限'], max=row.get('工程量上限', ''),
varModel=row['值类型'] varModel=row.get('值类型', '')
) )
else: # TCRTD
case 'TCRTD': importFunc(
for index, row in data.iterrows():
TcRtdManage.importVarForm(
varName=row['变量名'], varName=row['变量名'],
channelNumber=row['通道序号'], channelNumber=row['通道序号'],
varType=row['变量类型'], varType=row['变量类型'],
des=row['变量描述'], des=row.get('变量描述', ''),
min=row['工程量下限'], min=row.get('工程量下限', ''),
max=row['工程量上限'], max=row.get('工程量上限', ''),
compensationVar=row['补偿值'], compensationVar=row['补偿值'],
varModel=row['值类型'] varModel=row.get('值类型', '')
) )
case 'Hart模拟': except Exception as e:
for index, row in data.iterrows(): errorConList.append(f'{index+1}行导入失败: {str(e)}')
HartSimulateVarManage.importVarForm(
varName=row['变量名'], case 'Hart模拟' | 'Hart读取':
des=row['变量描述'], requiredFields = ['变量名']
varModel=row['值类型'] importFunc = HartSimulateVarManage.importVarForm if sheetName == 'Hart模拟' else HartVarManage.importVarForm
)
case 'Hart读取':
for index, row in data.iterrows(): for index, row in data.iterrows():
HartVarManage.importVarForm( if pd.isna(row['变量名']):
errorConList.append(f'{index+1}行变量名为空')
continue
try:
importFunc(
varName=row['变量名'], varName=row['变量名'],
des=row['变量描述'], des=row.get('变量描述', ''),
varModel=row['值类型'] varModel=row.get('值类型', '')
) )
except Exception as e:
errorConList.append(f'{index+1}行导入失败: {str(e)}')
modelLists = ['ModbusTcpMasterTable', 'ModbusTcpSlaveTable', 'ModbusRtuMasterTable', \ modelLists = ['ModbusTcpMasterTable', 'ModbusTcpSlaveTable', 'ModbusRtuMasterTable',
'ModbusRtuSlaveTable', 'HartTable', 'TcRtdTable', 'AnalogTable', 'HartSimulateTable', 'userTable'] 'ModbusRtuSlaveTable', 'HartTable', 'TcRtdTable', 'AnalogTable', 'HartSimulateTable', 'userTable']
for l in modelLists: for l in modelLists:
Globals.getValue(l).model.initTable() Globals.getValue(l).model.initTable()
if errorConList:
return '\r\n'.join(errorConList)

Loading…
Cancel
Save