import json import re import base64 from PyQt6 import * from PyQt6.QtCore import * from PyQt6.QtGui import * from PyQt6.QtWidgets import * from ui.Ui_insertInstructionDlg import Ui_insertInstructionDlg from taskModel.taskManager import taskManager from deviceModel.deviceManager import deviceManager from devGroupModel.devGroupManager import devGroupManager from instructionModel.instructionManager import instructionManager from taskInstructionModel.taskInstructionManager import taskInstructionManager class InsertInstructionDlg(QDialog): insertFinished = pyqtSignal() def __init__(self, parent=None): super().__init__(parent) self.ui = Ui_insertInstructionDlg() self.ui.setupUi(self) self.deviceModelBlockSignals = False self.taskId = "" self.supportedTypes = instructionManager.getSupportedType() self.targetType = "instruction" self.instructionPosition = -1 self.interface_index = 0 self.deviceModel = QStandardItemModel() self.devGroupModel = QStandardItemModel() self.devInstructionModel = QStandardItemModel() self.instructionModel = QStandardItemModel() self.devGroupProxyModel = QSortFilterProxyModel() self.devInstructionProxyModel = QSortFilterProxyModel() self.deviceProxyModel = QSortFilterProxyModel() self.devGroupProxyModel.setSourceModel(self.devGroupModel) self.deviceProxyModel.setSourceModel(self.deviceModel) self.devInstructionProxyModel.setSourceModel(self.devInstructionModel) self.ui.tableViewDevInstruction.setModel(self.devInstructionProxyModel) self.ui.tableViewDevGroup.setModel(self.devGroupProxyModel) self.ui.pbRight.setEnabled(False) self.ui.pbLeft.setEnabled(False) self.ui.pbUp.setEnabled(False) self.ui.pbDown.setEnabled(False) self.ui.tableViewDevice.setModel(self.deviceProxyModel) self.ui.tableViewInstruction.setModel(self.instructionModel) self.ui.leSearchDevGroup.textChanged.connect(self.handleDevGroupSearch) self.ui.leSearchDevice.textChanged.connect(self.handleDeviceSearch) self.ui.leSearchInstruction.textChanged.connect(self.handleInstructionSearch) self.ui.tableViewDevGroup.selectionModel().selectionChanged.connect(self.handleDevGroupSelection) self.ui.tableViewDevice.selectionModel().selectionChanged.connect(self.handleDeviceSelection) self.ui.tableViewDevInstruction.selectionModel().selectionChanged.connect(self.handleDevInstructionSelection) self.ui.tableViewDevInstruction.doubleClicked.connect(self.handleDevInstructionDoubleClick) self.ui.tableViewInstruction.selectionModel().selectionChanged.connect(self.handleInstructionSelection) self.ui.tableViewInstruction.doubleClicked.connect(self.handleInstructionDoubleClick) self.ui.pbRight.clicked.connect(self.handleRightClick) self.ui.pbLeft.clicked.connect(self.handleLeftClick) self.ui.pbUp.clicked.connect(self.handleUpClick) self.ui.pbDown.clicked.connect(self.handleDownClick) self.ui.pbSave.clicked.connect(self.handleSave) self.ui.pbCancel.clicked.connect(self.reject) def handleSave(self): for row in range(self.instructionModel.rowCount()): target_id = self.instructionModel.item(row, 0).text() device_id = self.instructionModel.item(row, 1).text() interface_index = self.instructionModel.item(row, 4).text() level = self.instructionPosition + row param = { 'task_id': self.taskId, 'device_id': device_id, 'loop': 1, 'delay': 0, 'target_type': self.targetType, 'target_id': target_id, 'interface_index': interface_index, 'level': level } id = taskInstructionManager.create(param) if id == "": print("insert error") self.accept() def showDlg(self, taskId, position): self.taskId = taskId self.instructionPosition = position self.setWindowTitle('插入指令') self.deviceModel.clear() self.devInstructionModel.clear() self.instructionModel.clear() self.loadDevGroup() self.exec() def batchOperation(self): self.setAllColumnWidth() self.hideAllColumn() self.setAllHorizontalHeaderLabels() def handleInstructionSelection(self): selectedRows = self.ui.tableViewInstruction.selectionModel().selectedRows() if len(selectedRows) > 0: self.ui.pbLeft.setEnabled(True) if len(selectedRows) == 1: row = self.ui.tableViewInstruction.selectionModel().selectedRows()[0].row() if row > 0: self.ui.pbUp.setEnabled(True) else: self.ui.pbUp.setEnabled(False) if row < self.ui.tableViewInstruction.model().rowCount() - 1: self.ui.pbDown.setEnabled(True) else: self.ui.pbDown.setEnabled(False) else: self.ui.pbUp.setEnabled(False) self.ui.pbDown.setEnabled(False) else: self.ui.pbLeft.setEnabled(False) self.ui.pbUp.setEnabled(False) self.ui.pbDown.setEnabled(False) def handleDevInstructionSelection(self): selectedRows = self.ui.tableViewDevInstruction.selectionModel().selectedRows() if len(selectedRows) > 0: self.ui.pbRight.setEnabled(True) else: self.ui.pbRight.setEnabled(False) def handleDevInstructionDoubleClick(self): self.handleRightClick() # def handleRightClick(self): # if len(self.ui.tableViewDevice.selectionModel().selectedRows())== 1: # row = self.ui.tableViewDevice.selectionModel().selectedRows()[0].row() # row_deviceId_index = self.deviceModel.index(row, 0, QModelIndex()) # row_devName_index = self.deviceModel.index(row, 2, QModelIndex()) # deviceId = self.deviceModel.data(row_deviceId_index, Qt.ItemDataRole.DisplayRole) # deviceName = self.deviceModel.data(row_devName_index, Qt.ItemDataRole.DisplayRole) # selectedRows = self.ui.tableViewDevInstruction.selectionModel().selectedRows() # if len(selectedRows) > 0: # for row in selectedRows: # index = row.model().index(row.row(), 0) # targetId = index.data() # index = row.model().index(row.row(), 1) # name = index.data() # self.instructionModel.appendRow([QStandardItem(targetId), QStandardItem(deviceId), QStandardItem(name), QStandardItem(deviceName)]) # self.ui.tableViewDevInstruction.clearSelection() # self.batchOperation() def handleRightClick(self): selectedRows = self.ui.tableViewDevice.selectionModel().selectedRows() if len(selectedRows) == 1: # 获取选中的设备行 row = self.ui.tableViewDevice.selectionModel().selectedRows()[0].row() # 从代理模型获取真实的设备行索引 sourceRowIndex = self.deviceProxyModel.mapToSource(selectedRows[0]).row() # 获取设备 ID 和设备名称 row_deviceId_index = self.deviceModel.index(sourceRowIndex, 0, QModelIndex()) row_devName_index = self.deviceModel.index(sourceRowIndex, 2, QModelIndex()) row_interface_id_index = self.deviceModel.index(sourceRowIndex, 3, QModelIndex()) deviceId = self.deviceModel.data(row_deviceId_index, Qt.ItemDataRole.DisplayRole) deviceName = self.deviceModel.data(row_devName_index, Qt.ItemDataRole.DisplayRole) interface_id = self.deviceModel.data(row_interface_id_index, Qt.ItemDataRole.DisplayRole) # 获取选中的指令行 selectedRows = self.ui.tableViewDevInstruction.selectionModel().selectedRows() if len(selectedRows) > 0: for selectedRow in selectedRows: # 从代理模型获取真实的指令行索引 targetRowIndex = self.devInstructionProxyModel.mapToSource(selectedRow).row() index = self.devInstructionModel.index(targetRowIndex, 0) targetId = index.data() index = self.devInstructionModel.index(targetRowIndex, 1) name = index.data() # 将指令添加到指令模型 self.instructionModel.appendRow([QStandardItem(targetId), QStandardItem(deviceId), QStandardItem(name), QStandardItem(deviceName), QStandardItem(interface_id)]) # 清除选中的指令 self.ui.tableViewDevInstruction.clearSelection() # 批量操作 self.batchOperation() def handleInstructionDoubleClick(self): self.handleLeftClick() def handleLeftClick(self): selectedRows = self.ui.tableViewInstruction.selectionModel().selectedRows() if len(selectedRows) > 0: selectedRows.reverse() for row in selectedRows: self.instructionModel.removeRow(row.row()) def handleUpClick(self): selectedRows = self.ui.tableViewInstruction.selectionModel().selectedRows() if len(selectedRows) == 1: row = selectedRows[0].row() item = self.instructionModel.takeRow(row) self.instructionModel.insertRow(row - 1, item) self.ui.tableViewInstruction.selectRow(row - 1) def handleDownClick(self): selectedRows = self.ui.tableViewInstruction.selectionModel().selectedRows() if len(selectedRows) == 1: row = selectedRows[0].row() item = self.instructionModel.takeRow(row) self.instructionModel.insertRow(row + 1, item) self.ui.tableViewInstruction.selectRow(row + 1) # 批量设置标题 def setAllHorizontalHeaderLabels(self): self.instructionModel.setHorizontalHeaderLabels(['ID','设备ID', '名称', '设备','接口ID']) self.devInstructionModel.setHorizontalHeaderLabels(['ID','名称', '摘要', '备注']) # 隐藏指定列 def hideAllColumn(self): self.ui.tableViewDevGroup.setColumnHidden(0, True) self.ui.tableViewDevice.setColumnHidden(0, True) self.ui.tableViewDevice.setColumnHidden(1, True) self.ui.tableViewDevice.setColumnHidden(3, True) self.ui.tableViewDevInstruction.setColumnHidden(0, True) self.ui.tableViewInstruction.setColumnHidden(0, True) self.ui.tableViewInstruction.setColumnHidden(1, True) self.ui.tableViewInstruction.setColumnHidden(4, True) # 批量设置列宽 def setAllColumnWidth(self): if self.devGroupModel.columnCount() > 0: self.ui.tableViewDevGroup.horizontalHeader().setSectionResizeMode(1,QHeaderView.ResizeMode.Stretch) if self.deviceModel.columnCount() > 0: self.ui.tableViewDevice.horizontalHeader().setSectionResizeMode(2,QHeaderView.ResizeMode.Stretch) if self.instructionModel.columnCount() > 0: self.ui.tableViewInstruction.horizontalHeader().setSectionResizeMode(2,QHeaderView.ResizeMode.Stretch) self.ui.tableViewInstruction.horizontalHeader().setSectionResizeMode(3,QHeaderView.ResizeMode.Stretch) if self.devInstructionModel.columnCount() > 0: self.ui.tableViewDevInstruction.horizontalHeader().setSectionResizeMode(1,QHeaderView.ResizeMode.ResizeToContents) self.ui.tableViewDevInstruction.horizontalHeader().setSectionResizeMode(2,QHeaderView.ResizeMode.Stretch) self.ui.tableViewDevInstruction.horizontalHeader().setSectionResizeMode(3,QHeaderView.ResizeMode.Stretch) # 加载设备指令 def loadDevInstruction(self, devModelId): instructions = instructionManager.getDevmodelInstructions(devModelId) if len(instructions) > 0: for instruction in instructions: index = self.devInstructionModel.rowCount() self.devInstructionModel.setItem(index, 0, QStandardItem(str(instruction['id']))) self.devInstructionModel.setItem(index, 1, QStandardItem(instruction['name'])) if instruction["type"] is not None and instruction["type"] in self.supportedTypes: self.devInstructionModel.setItem(index, 2, QStandardItem(instruction["attr"][self.supportedTypes[instruction["type"]]["digestAttr"]])) else: self.devInstructionModel.setItem(index, 2, QStandardItem("")) self.devInstructionModel.setItem(index, 3, QStandardItem(instruction["attr"]["remark"])) def handleDevGroupSearch(self): self.devGroupProxyModel.setFilterRegularExpression(re.escape(self.ui.leSearchDevGroup.text())) self.devGroupProxyModel.setFilterKeyColumn(1) def handleDeviceSearch(self): self.deviceProxyModel.setFilterRegularExpression(re.escape(self.ui.leSearchDevice.text())) self.deviceProxyModel.setFilterKeyColumn(2) def handleInstructionSearch(self): self.devInstructionProxyModel.setFilterRegularExpression(re.escape(self.ui.leSearchInstruction.text())) self.devInstructionProxyModel.setFilterKeyColumn(1) def handleDevGroupSelection(self): self.deviceModel.clear() selectedRows = self.ui.tableViewDevGroup.selectionModel().selectedRows() if len(selectedRows) == 1: # 从代理模型获取真实的行索引 sourceRowIndex = self.devGroupProxyModel.mapToSource(selectedRows[0]).row() row_index = self.devGroupModel.index(sourceRowIndex, 0, QModelIndex()) devGroupId = self.devGroupModel.data(row_index, Qt.ItemDataRole.DisplayRole) self.loadDevice(devGroupId) self.batchOperation() def handleDeviceSelection(self): self.devInstructionModel.clear() selectedRows = self.ui.tableViewDevice.selectionModel().selectedRows() if len(selectedRows) == 1: # 从代理模型获取真实的行索引 sourceRowIndex = self.deviceProxyModel.mapToSource(selectedRows[0]).row() row_index = self.deviceModel.index(sourceRowIndex, 1, QModelIndex()) devModelId = self.deviceModel.data(row_index, Qt.ItemDataRole.DisplayRole) self.loadDevInstruction(devModelId) self.batchOperation() def loadDevGroup(self): self.devGroupModel.clear() devGroups = devGroupManager.getInfo() if len(devGroups) > 0: for devGroup in devGroups: index = self.devGroupModel.rowCount() self.devGroupModel.setItem(index, 0, QStandardItem(str(devGroup['id']))) self.devGroupModel.setItem(index, 1, QStandardItem(devGroup['name'])) self.batchOperation() # 加载设备 def loadDevice(self, devGroupId): self.deviceModel.clear() devices = deviceManager.getGroupDevices(devGroupId) if len(devices) > 0: for device in devices: index = self.deviceModel.rowCount() self.deviceModel.setItem(index, 0, QStandardItem(str(device['id']))) self.deviceModel.setItem(index, 1, QStandardItem(str(device['dev_model_id']))) self.deviceModel.setItem(index, 2, QStandardItem(device['name'])) if 'interfaceIndex' in device['attr']: self.deviceModel.setItem(index, 3, QStandardItem(str(device['attr']['interfaceIndex']))) else: self.deviceModel.setItem(index, 3, QStandardItem("0"))