|
|
@@ -0,0 +1,580 @@
|
|
|
+#include "TaskConfigWidget.h"
|
|
|
+#include "api/SoftBusAPI.h"
|
|
|
+#include "utils/logging.h"
|
|
|
+#include <QFormLayout>
|
|
|
+#include <QHBoxLayout>
|
|
|
+#include <QVBoxLayout>
|
|
|
+#include <QLabel>
|
|
|
+#include <QMap>
|
|
|
+#include <QLineEdit>
|
|
|
+#include <QCheckBox>
|
|
|
+#include <QComboBox>
|
|
|
+#include <QSpinBox>
|
|
|
+#include <QDoubleSpinBox>
|
|
|
+#include <QTextEdit>
|
|
|
+#include <QGroupBox>
|
|
|
+#include <QDateTime>
|
|
|
+
|
|
|
+TaskConfigWidget::TaskConfigWidget(QWidget *parent)
|
|
|
+ : QWidget(parent)
|
|
|
+ , m_taskIdEdit(nullptr)
|
|
|
+ , m_taskNameEdit(nullptr)
|
|
|
+ , m_enabledCheckBox(nullptr)
|
|
|
+ , m_transportTypeCombo(nullptr)
|
|
|
+ , m_protocolTypeCombo(nullptr)
|
|
|
+ , m_deviceBindCombo(nullptr)
|
|
|
+ , m_triggerModeCombo(nullptr)
|
|
|
+ , m_parameterGroup(nullptr)
|
|
|
+ , m_parameterLayout(nullptr)
|
|
|
+ , m_statusLabel(nullptr)
|
|
|
+ , m_lastCommTimeLabel(nullptr)
|
|
|
+{
|
|
|
+ createUI();
|
|
|
+}
|
|
|
+
|
|
|
+TaskConfigWidget::~TaskConfigWidget() = default;
|
|
|
+
|
|
|
+void TaskConfigWidget::createUI()
|
|
|
+{
|
|
|
+ QVBoxLayout *mainLayout = new QVBoxLayout(this);
|
|
|
+
|
|
|
+ // 基本信息组
|
|
|
+ QGroupBox *basicGroup = new QGroupBox(tr("基本信息"), this);
|
|
|
+ QFormLayout *basicLayout = new QFormLayout(basicGroup);
|
|
|
+
|
|
|
+ m_taskIdEdit = new QLineEdit(this);
|
|
|
+ m_taskIdEdit->setPlaceholderText(tr("自动生成UUID"));
|
|
|
+ basicLayout->addRow(tr("任务ID:"), m_taskIdEdit);
|
|
|
+
|
|
|
+ m_taskNameEdit = new QLineEdit(this);
|
|
|
+ m_taskNameEdit->setPlaceholderText(tr("输入任务名称"));
|
|
|
+ basicLayout->addRow(tr("任务名称:"), m_taskNameEdit);
|
|
|
+
|
|
|
+ m_enabledCheckBox = new QCheckBox(tr("启用任务"), this);
|
|
|
+ basicLayout->addRow(tr(""), m_enabledCheckBox);
|
|
|
+
|
|
|
+ basicGroup->setLayout(basicLayout);
|
|
|
+ mainLayout->addWidget(basicGroup);
|
|
|
+
|
|
|
+ // 传输配置组
|
|
|
+ QGroupBox *transportGroup = new QGroupBox(tr("传输配置"), this);
|
|
|
+ QFormLayout *transportLayout = new QFormLayout(transportGroup);
|
|
|
+
|
|
|
+ m_transportTypeCombo = new QComboBox(this);
|
|
|
+ m_transportTypeCombo->addItem(tr("串口"), static_cast<int>(TransportType::Serial));
|
|
|
+ m_transportTypeCombo->addItem(tr("TCP服务器"), static_cast<int>(TransportType::TCP_Server));
|
|
|
+ m_transportTypeCombo->addItem(tr("TCP客户端"), static_cast<int>(TransportType::TCP_Client));
|
|
|
+ m_transportTypeCombo->addItem(tr("UDP服务器"), static_cast<int>(TransportType::UDP_Server));
|
|
|
+ m_transportTypeCombo->addItem(tr("UDP客户端"), static_cast<int>(TransportType::UDP_Client));
|
|
|
+ connect(m_transportTypeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
|
|
+ this, &TaskConfigWidget::onTransportTypeChanged);
|
|
|
+ transportLayout->addRow(tr("传输类型:"), m_transportTypeCombo);
|
|
|
+
|
|
|
+ m_protocolTypeCombo = new QComboBox(this);
|
|
|
+ m_protocolTypeCombo->addItem(tr("Modbus RTU"), static_cast<int>(ProtocolType::Modbus_RTU));
|
|
|
+ m_protocolTypeCombo->addItem(tr("Modbus TCP"), static_cast<int>(ProtocolType::Modbus_TCP));
|
|
|
+ m_protocolTypeCombo->addItem(tr("CAN标准帧"), static_cast<int>(ProtocolType::CAN_Standard));
|
|
|
+ m_protocolTypeCombo->addItem(tr("原始数据"), static_cast<int>(ProtocolType::Raw_Data));
|
|
|
+ connect(m_protocolTypeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
|
|
+ this, &TaskConfigWidget::onProtocolTypeChanged);
|
|
|
+ transportLayout->addRow(tr("协议类型:"), m_protocolTypeCombo);
|
|
|
+
|
|
|
+ m_deviceBindCombo = new QComboBox(this);
|
|
|
+ m_deviceBindCombo->setEditable(true);
|
|
|
+ m_deviceBindCombo->setPlaceholderText(tr("选择或输入设备ID"));
|
|
|
+ transportLayout->addRow(tr("绑定设备:"), m_deviceBindCombo);
|
|
|
+
|
|
|
+ m_triggerModeCombo = new QComboBox(this);
|
|
|
+ m_triggerModeCombo->addItem(tr("轮询"), static_cast<int>(TriggerMode::Polling));
|
|
|
+ m_triggerModeCombo->addItem(tr("监听"), static_cast<int>(TriggerMode::Event));
|
|
|
+ connect(m_triggerModeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
|
|
+ this, &TaskConfigWidget::onTriggerModeChanged);
|
|
|
+ transportLayout->addRow(tr("触发模式:"), m_triggerModeCombo);
|
|
|
+
|
|
|
+ transportGroup->setLayout(transportLayout);
|
|
|
+ mainLayout->addWidget(transportGroup);
|
|
|
+
|
|
|
+ // 参数配置组(动态显示)
|
|
|
+ m_parameterGroup = new QGroupBox(tr("参数配置"), this);
|
|
|
+ m_parameterLayout = new QFormLayout(m_parameterGroup);
|
|
|
+ m_parameterGroup->setLayout(m_parameterLayout);
|
|
|
+ mainLayout->addWidget(m_parameterGroup);
|
|
|
+
|
|
|
+ // 状态信息组
|
|
|
+ QGroupBox *statusGroup = new QGroupBox(tr("状态信息"), this);
|
|
|
+ QFormLayout *statusLayout = new QFormLayout(statusGroup);
|
|
|
+
|
|
|
+ m_statusLabel = new QLabel(tr("已停止"), this);
|
|
|
+ statusLayout->addRow(tr("状态:"), m_statusLabel);
|
|
|
+
|
|
|
+ m_lastCommTimeLabel = new QLabel(tr("无"), this);
|
|
|
+ statusLayout->addRow(tr("最后通信时间:"), m_lastCommTimeLabel);
|
|
|
+
|
|
|
+ statusGroup->setLayout(statusLayout);
|
|
|
+ mainLayout->addWidget(statusGroup);
|
|
|
+
|
|
|
+ mainLayout->addStretch();
|
|
|
+ setLayout(mainLayout);
|
|
|
+
|
|
|
+ // 初始化参数控件
|
|
|
+ createParameterWidgets();
|
|
|
+ updateParameterWidgetsVisibility();
|
|
|
+
|
|
|
+ // 刷新设备列表
|
|
|
+ refreshDeviceList();
|
|
|
+}
|
|
|
+
|
|
|
+void TaskConfigWidget::createParameterWidgets()
|
|
|
+{
|
|
|
+ // Modbus RTU/TCP 参数
|
|
|
+ QSpinBox *slaveIdSpin = new QSpinBox(this);
|
|
|
+ slaveIdSpin->setRange(1, 247);
|
|
|
+ slaveIdSpin->setValue(1);
|
|
|
+ m_parameterWidgets["modbus_slave_id"] = slaveIdSpin;
|
|
|
+
|
|
|
+ QSpinBox *startAddrSpin = new QSpinBox(this);
|
|
|
+ startAddrSpin->setRange(0, 65535);
|
|
|
+ startAddrSpin->setValue(0);
|
|
|
+ m_parameterWidgets["modbus_start_addr"] = startAddrSpin;
|
|
|
+
|
|
|
+ QSpinBox *quantitySpin = new QSpinBox(this);
|
|
|
+ quantitySpin->setRange(1, 125);
|
|
|
+ quantitySpin->setValue(10);
|
|
|
+ m_parameterWidgets["modbus_quantity"] = quantitySpin;
|
|
|
+
|
|
|
+ QComboBox *functionCodeCombo = new QComboBox(this);
|
|
|
+ functionCodeCombo->addItem(tr("读取保持寄存器 (03)"), 3);
|
|
|
+ functionCodeCombo->addItem(tr("读取输入寄存器 (04)"), 4);
|
|
|
+ functionCodeCombo->addItem(tr("读取线圈状态 (01)"), 1);
|
|
|
+ functionCodeCombo->addItem(tr("读取输入状态 (02)"), 2);
|
|
|
+ m_parameterWidgets["modbus_function_code"] = functionCodeCombo;
|
|
|
+
|
|
|
+ // 轮询间隔(毫秒)
|
|
|
+ QSpinBox *pollIntervalSpin = new QSpinBox(this);
|
|
|
+ pollIntervalSpin->setRange(100, 60000);
|
|
|
+ pollIntervalSpin->setValue(1000);
|
|
|
+ pollIntervalSpin->setSuffix(tr(" ms"));
|
|
|
+ m_parameterWidgets["poll_interval"] = pollIntervalSpin;
|
|
|
+
|
|
|
+ // TCP/UDP 端口
|
|
|
+ QSpinBox *portSpin = new QSpinBox(this);
|
|
|
+ portSpin->setRange(1, 65535);
|
|
|
+ portSpin->setValue(502);
|
|
|
+ m_parameterWidgets["port"] = portSpin;
|
|
|
+
|
|
|
+ // TCP/UDP 地址
|
|
|
+ QLineEdit *addressEdit = new QLineEdit(this);
|
|
|
+ addressEdit->setPlaceholderText(tr("127.0.0.1"));
|
|
|
+ addressEdit->setText(tr("127.0.0.1"));
|
|
|
+ m_parameterWidgets["address"] = addressEdit;
|
|
|
+
|
|
|
+ // 超时时间(毫秒)
|
|
|
+ QSpinBox *timeoutSpin = new QSpinBox(this);
|
|
|
+ timeoutSpin->setRange(100, 30000);
|
|
|
+ timeoutSpin->setValue(1000);
|
|
|
+ timeoutSpin->setSuffix(tr(" ms"));
|
|
|
+ m_parameterWidgets["timeout"] = timeoutSpin;
|
|
|
+}
|
|
|
+
|
|
|
+void TaskConfigWidget::updateParameterWidgetsVisibility()
|
|
|
+{
|
|
|
+ // 清除所有控件
|
|
|
+ QLayoutItem *item;
|
|
|
+ while ((item = m_parameterLayout->takeAt(0)) != nullptr) {
|
|
|
+ if (item->widget()) {
|
|
|
+ item->widget()->hide();
|
|
|
+ }
|
|
|
+ delete item;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!m_protocolTypeCombo || !m_transportTypeCombo || !m_triggerModeCombo) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ ProtocolType protocolType = static_cast<ProtocolType>(
|
|
|
+ m_protocolTypeCombo->currentData().toInt());
|
|
|
+ TransportType transportType = static_cast<TransportType>(
|
|
|
+ m_transportTypeCombo->currentData().toInt());
|
|
|
+ TriggerMode triggerMode = static_cast<TriggerMode>(
|
|
|
+ m_triggerModeCombo->currentData().toInt());
|
|
|
+
|
|
|
+ // Modbus 参数
|
|
|
+ if (protocolType == ProtocolType::Modbus_RTU || protocolType == ProtocolType::Modbus_TCP) {
|
|
|
+ if (m_parameterWidgets.contains("modbus_slave_id")) {
|
|
|
+ m_parameterLayout->addRow(tr("从站地址:"), m_parameterWidgets["modbus_slave_id"]);
|
|
|
+ m_parameterWidgets["modbus_slave_id"]->show();
|
|
|
+ }
|
|
|
+ if (triggerMode == TriggerMode::Polling) {
|
|
|
+ if (m_parameterWidgets.contains("modbus_function_code")) {
|
|
|
+ m_parameterLayout->addRow(tr("功能码:"), m_parameterWidgets["modbus_function_code"]);
|
|
|
+ m_parameterWidgets["modbus_function_code"]->show();
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("modbus_start_addr")) {
|
|
|
+ m_parameterLayout->addRow(tr("起始地址:"), m_parameterWidgets["modbus_start_addr"]);
|
|
|
+ m_parameterWidgets["modbus_start_addr"]->show();
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("modbus_quantity")) {
|
|
|
+ m_parameterLayout->addRow(tr("数量:"), m_parameterWidgets["modbus_quantity"]);
|
|
|
+ m_parameterWidgets["modbus_quantity"]->show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 轮询间隔(轮询模式)
|
|
|
+ if (triggerMode == TriggerMode::Polling) {
|
|
|
+ if (m_parameterWidgets.contains("poll_interval")) {
|
|
|
+ m_parameterLayout->addRow(tr("轮询间隔:"), m_parameterWidgets["poll_interval"]);
|
|
|
+ m_parameterWidgets["poll_interval"]->show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 网络参数(TCP/UDP)
|
|
|
+ if (transportType == TransportType::TCP_Server || transportType == TransportType::TCP_Client ||
|
|
|
+ transportType == TransportType::UDP_Server || transportType == TransportType::UDP_Client) {
|
|
|
+ if (transportType == TransportType::TCP_Server || transportType == TransportType::UDP_Server) {
|
|
|
+ if (m_parameterWidgets.contains("port")) {
|
|
|
+ m_parameterLayout->addRow(tr("监听端口:"), m_parameterWidgets["port"]);
|
|
|
+ m_parameterWidgets["port"]->show();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (m_parameterWidgets.contains("address")) {
|
|
|
+ m_parameterLayout->addRow(tr("服务器地址:"), m_parameterWidgets["address"]);
|
|
|
+ m_parameterWidgets["address"]->show();
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("port")) {
|
|
|
+ m_parameterLayout->addRow(tr("服务器端口:"), m_parameterWidgets["port"]);
|
|
|
+ m_parameterWidgets["port"]->show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 超时时间
|
|
|
+ if (m_parameterWidgets.contains("timeout")) {
|
|
|
+ m_parameterLayout->addRow(tr("超时时间:"), m_parameterWidgets["timeout"]);
|
|
|
+ m_parameterWidgets["timeout"]->show();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void TaskConfigWidget::refreshDeviceList()
|
|
|
+{
|
|
|
+ if (!m_deviceBindCombo) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ SoftBusAPI *api = SoftBusAPI::instance();
|
|
|
+ if (!api || !api->isInitialized()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ m_deviceBindCombo->clear();
|
|
|
+ QList<DeviceInfo> devices = api->getAllDevices();
|
|
|
+ for (const DeviceInfo &device : devices) {
|
|
|
+ QString displayText = QString("%1 (%2)").arg(device.portname).arg(device.type);
|
|
|
+ m_deviceBindCombo->addItem(displayText, device.portname);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void TaskConfigWidget::loadTask(const TransferTask &task)
|
|
|
+{
|
|
|
+ if (m_taskIdEdit) {
|
|
|
+ m_taskIdEdit->setText(task.task_id);
|
|
|
+ }
|
|
|
+ if (m_taskNameEdit) {
|
|
|
+ m_taskNameEdit->setText(task.task_name);
|
|
|
+ }
|
|
|
+ if (m_enabledCheckBox) {
|
|
|
+ m_enabledCheckBox->setChecked(task.is_enabled);
|
|
|
+ }
|
|
|
+ if (m_transportTypeCombo) {
|
|
|
+ int index = m_transportTypeCombo->findData(static_cast<int>(task.transport_type));
|
|
|
+ if (index >= 0) {
|
|
|
+ m_transportTypeCombo->setCurrentIndex(index);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_protocolTypeCombo) {
|
|
|
+ int index = m_protocolTypeCombo->findData(static_cast<int>(task.protocol_type));
|
|
|
+ if (index >= 0) {
|
|
|
+ m_protocolTypeCombo->setCurrentIndex(index);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_deviceBindCombo) {
|
|
|
+ int index = m_deviceBindCombo->findData(task.device_bind);
|
|
|
+ if (index >= 0) {
|
|
|
+ m_deviceBindCombo->setCurrentIndex(index);
|
|
|
+ } else {
|
|
|
+ m_deviceBindCombo->setCurrentText(task.device_bind);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_triggerModeCombo) {
|
|
|
+ int index = m_triggerModeCombo->findData(static_cast<int>(task.trigger_mode));
|
|
|
+ if (index >= 0) {
|
|
|
+ m_triggerModeCombo->setCurrentIndex(index);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 加载参数
|
|
|
+ QJsonObject params = task.params_json;
|
|
|
+ if (m_parameterWidgets.contains("modbus_slave_id") && params.contains("modbus_slave_id")) {
|
|
|
+ QSpinBox *spin = qobject_cast<QSpinBox*>(m_parameterWidgets["modbus_slave_id"]);
|
|
|
+ if (spin) {
|
|
|
+ spin->setValue(params["modbus_slave_id"].toInt());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("modbus_function_code") && params.contains("modbus_function_code")) {
|
|
|
+ QComboBox *combo = qobject_cast<QComboBox*>(m_parameterWidgets["modbus_function_code"]);
|
|
|
+ if (combo) {
|
|
|
+ int index = combo->findData(params["modbus_function_code"].toInt());
|
|
|
+ if (index >= 0) {
|
|
|
+ combo->setCurrentIndex(index);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("modbus_start_addr") && params.contains("modbus_start_addr")) {
|
|
|
+ QSpinBox *spin = qobject_cast<QSpinBox*>(m_parameterWidgets["modbus_start_addr"]);
|
|
|
+ if (spin) {
|
|
|
+ spin->setValue(params["modbus_start_addr"].toInt());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("modbus_quantity") && params.contains("modbus_quantity")) {
|
|
|
+ QSpinBox *spin = qobject_cast<QSpinBox*>(m_parameterWidgets["modbus_quantity"]);
|
|
|
+ if (spin) {
|
|
|
+ spin->setValue(params["modbus_quantity"].toInt());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("poll_interval") && params.contains("poll_interval")) {
|
|
|
+ QSpinBox *spin = qobject_cast<QSpinBox*>(m_parameterWidgets["poll_interval"]);
|
|
|
+ if (spin) {
|
|
|
+ spin->setValue(params["poll_interval"].toInt());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("port") && params.contains("port")) {
|
|
|
+ QSpinBox *spin = qobject_cast<QSpinBox*>(m_parameterWidgets["port"]);
|
|
|
+ if (spin) {
|
|
|
+ spin->setValue(params["port"].toInt());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("address") && params.contains("address")) {
|
|
|
+ QLineEdit *edit = qobject_cast<QLineEdit*>(m_parameterWidgets["address"]);
|
|
|
+ if (edit) {
|
|
|
+ edit->setText(params["address"].toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("timeout") && params.contains("timeout")) {
|
|
|
+ QSpinBox *spin = qobject_cast<QSpinBox*>(m_parameterWidgets["timeout"]);
|
|
|
+ if (spin) {
|
|
|
+ spin->setValue(params["timeout"].toInt());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新状态显示
|
|
|
+ if (m_statusLabel) {
|
|
|
+ QString statusText;
|
|
|
+ switch (task.status) {
|
|
|
+ case TaskStatus::Stopped:
|
|
|
+ statusText = tr("已停止");
|
|
|
+ break;
|
|
|
+ case TaskStatus::Running:
|
|
|
+ statusText = tr("运行中");
|
|
|
+ break;
|
|
|
+ case TaskStatus::Error:
|
|
|
+ statusText = tr("错误");
|
|
|
+ break;
|
|
|
+ case TaskStatus::Paused:
|
|
|
+ statusText = tr("已暂停");
|
|
|
+ break;
|
|
|
+ case TaskStatus::Waiting:
|
|
|
+ statusText = tr("等待中");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ m_statusLabel->setText(statusText);
|
|
|
+ }
|
|
|
+ if (m_lastCommTimeLabel) {
|
|
|
+ if (task.last_comm_time.isValid()) {
|
|
|
+ m_lastCommTimeLabel->setText(task.last_comm_time.toString(Qt::ISODate));
|
|
|
+ } else {
|
|
|
+ m_lastCommTimeLabel->setText(tr("无"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ updateParameterWidgetsVisibility();
|
|
|
+ emit configChanged();
|
|
|
+}
|
|
|
+
|
|
|
+TransferTask TaskConfigWidget::getTask() const
|
|
|
+{
|
|
|
+ TransferTask task;
|
|
|
+
|
|
|
+ if (m_taskIdEdit) {
|
|
|
+ task.task_id = m_taskIdEdit->text().trimmed();
|
|
|
+ }
|
|
|
+ if (m_taskNameEdit) {
|
|
|
+ task.task_name = m_taskNameEdit->text().trimmed();
|
|
|
+ }
|
|
|
+ if (m_enabledCheckBox) {
|
|
|
+ task.is_enabled = m_enabledCheckBox->isChecked();
|
|
|
+ }
|
|
|
+ if (m_transportTypeCombo) {
|
|
|
+ task.transport_type = static_cast<TransportType>(m_transportTypeCombo->currentData().toInt());
|
|
|
+ }
|
|
|
+ if (m_protocolTypeCombo) {
|
|
|
+ task.protocol_type = static_cast<ProtocolType>(m_protocolTypeCombo->currentData().toInt());
|
|
|
+ }
|
|
|
+ if (m_deviceBindCombo) {
|
|
|
+ task.device_bind = m_deviceBindCombo->currentData().toString();
|
|
|
+ if (task.device_bind.isEmpty()) {
|
|
|
+ task.device_bind = m_deviceBindCombo->currentText().trimmed();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_triggerModeCombo) {
|
|
|
+ task.trigger_mode = static_cast<TriggerMode>(m_triggerModeCombo->currentData().toInt());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 收集参数
|
|
|
+ QJsonObject params;
|
|
|
+ if (m_parameterWidgets.contains("modbus_slave_id")) {
|
|
|
+ QSpinBox *spin = qobject_cast<QSpinBox*>(m_parameterWidgets["modbus_slave_id"]);
|
|
|
+ if (spin && spin->isVisible()) {
|
|
|
+ params["modbus_slave_id"] = spin->value();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("modbus_function_code")) {
|
|
|
+ QComboBox *combo = qobject_cast<QComboBox*>(m_parameterWidgets["modbus_function_code"]);
|
|
|
+ if (combo && combo->isVisible()) {
|
|
|
+ params["modbus_function_code"] = combo->currentData().toInt();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("modbus_start_addr")) {
|
|
|
+ QSpinBox *spin = qobject_cast<QSpinBox*>(m_parameterWidgets["modbus_start_addr"]);
|
|
|
+ if (spin && spin->isVisible()) {
|
|
|
+ params["modbus_start_addr"] = spin->value();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("modbus_quantity")) {
|
|
|
+ QSpinBox *spin = qobject_cast<QSpinBox*>(m_parameterWidgets["modbus_quantity"]);
|
|
|
+ if (spin && spin->isVisible()) {
|
|
|
+ params["modbus_quantity"] = spin->value();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("poll_interval")) {
|
|
|
+ QSpinBox *spin = qobject_cast<QSpinBox*>(m_parameterWidgets["poll_interval"]);
|
|
|
+ if (spin && spin->isVisible()) {
|
|
|
+ params["poll_interval"] = spin->value();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("port")) {
|
|
|
+ QSpinBox *spin = qobject_cast<QSpinBox*>(m_parameterWidgets["port"]);
|
|
|
+ if (spin && spin->isVisible()) {
|
|
|
+ params["port"] = spin->value();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("address")) {
|
|
|
+ QLineEdit *edit = qobject_cast<QLineEdit*>(m_parameterWidgets["address"]);
|
|
|
+ if (edit && edit->isVisible()) {
|
|
|
+ params["address"] = edit->text().trimmed();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m_parameterWidgets.contains("timeout")) {
|
|
|
+ QSpinBox *spin = qobject_cast<QSpinBox*>(m_parameterWidgets["timeout"]);
|
|
|
+ if (spin && spin->isVisible()) {
|
|
|
+ params["timeout"] = spin->value();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ task.params_json = params;
|
|
|
+
|
|
|
+ task.updated_at = QDateTime::currentDateTimeUtc();
|
|
|
+ if (task.task_id.isEmpty()) {
|
|
|
+ task.created_at = QDateTime::currentDateTimeUtc();
|
|
|
+ }
|
|
|
+
|
|
|
+ return task;
|
|
|
+}
|
|
|
+
|
|
|
+void TaskConfigWidget::clear()
|
|
|
+{
|
|
|
+ if (m_taskIdEdit) {
|
|
|
+ m_taskIdEdit->clear();
|
|
|
+ }
|
|
|
+ if (m_taskNameEdit) {
|
|
|
+ m_taskNameEdit->clear();
|
|
|
+ }
|
|
|
+ if (m_enabledCheckBox) {
|
|
|
+ m_enabledCheckBox->setChecked(false);
|
|
|
+ }
|
|
|
+ if (m_transportTypeCombo) {
|
|
|
+ m_transportTypeCombo->setCurrentIndex(0);
|
|
|
+ }
|
|
|
+ if (m_protocolTypeCombo) {
|
|
|
+ m_protocolTypeCombo->setCurrentIndex(0);
|
|
|
+ }
|
|
|
+ if (m_deviceBindCombo) {
|
|
|
+ m_deviceBindCombo->setCurrentIndex(-1);
|
|
|
+ }
|
|
|
+ if (m_triggerModeCombo) {
|
|
|
+ m_triggerModeCombo->setCurrentIndex(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 重置参数控件
|
|
|
+ for (auto it = m_parameterWidgets.begin(); it != m_parameterWidgets.end(); ++it) {
|
|
|
+ if (QSpinBox *spin = qobject_cast<QSpinBox*>(it.value())) {
|
|
|
+ spin->setValue(spin->minimum());
|
|
|
+ } else if (QLineEdit *edit = qobject_cast<QLineEdit*>(it.value())) {
|
|
|
+ edit->clear();
|
|
|
+ } else if (QComboBox *combo = qobject_cast<QComboBox*>(it.value())) {
|
|
|
+ combo->setCurrentIndex(0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (m_statusLabel) {
|
|
|
+ m_statusLabel->setText(tr("已停止"));
|
|
|
+ }
|
|
|
+ if (m_lastCommTimeLabel) {
|
|
|
+ m_lastCommTimeLabel->setText(tr("无"));
|
|
|
+ }
|
|
|
+
|
|
|
+ updateParameterWidgetsVisibility();
|
|
|
+}
|
|
|
+
|
|
|
+bool TaskConfigWidget::isValid() const
|
|
|
+{
|
|
|
+ if (!m_taskNameEdit || m_taskNameEdit->text().trimmed().isEmpty()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (!m_deviceBindCombo || m_deviceBindCombo->currentText().trimmed().isEmpty()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+void TaskConfigWidget::setTaskId(const QString &taskId)
|
|
|
+{
|
|
|
+ if (m_taskIdEdit) {
|
|
|
+ m_taskIdEdit->setText(taskId);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void TaskConfigWidget::onTransportTypeChanged(int index)
|
|
|
+{
|
|
|
+ Q_UNUSED(index);
|
|
|
+ updateParameterWidgetsVisibility();
|
|
|
+ emit configChanged();
|
|
|
+}
|
|
|
+
|
|
|
+void TaskConfigWidget::onProtocolTypeChanged(int index)
|
|
|
+{
|
|
|
+ Q_UNUSED(index);
|
|
|
+ updateParameterWidgetsVisibility();
|
|
|
+ emit configChanged();
|
|
|
+}
|
|
|
+
|
|
|
+void TaskConfigWidget::onTriggerModeChanged(int index)
|
|
|
+{
|
|
|
+ Q_UNUSED(index);
|
|
|
+ updateParameterWidgetsVisibility();
|
|
|
+ emit configChanged();
|
|
|
+}
|
|
|
+
|
|
|
+void TaskConfigWidget::updateParameterWidgets()
|
|
|
+{
|
|
|
+ updateParameterWidgetsVisibility();
|
|
|
+}
|
|
|
+
|