* @brief Add a register group */
| 522 | * @brief Add a register group |
| 523 | */ |
| 524 | API::CommandResponse API::Handlers::ModbusHandler::addRegisterGroup(const QString& id, |
| 525 | const QJsonObject& params) |
| 526 | { |
| 527 | if (!params.contains(QStringLiteral("type")) || !params.contains(QStringLiteral("startAddress")) |
| 528 | || !params.contains(QStringLiteral("count"))) { |
| 529 | return CommandResponse::makeError( |
| 530 | id, |
| 531 | ErrorCode::MissingParam, |
| 532 | QStringLiteral("Missing required parameters: type, startAddress, count")); |
| 533 | } |
| 534 | |
| 535 | const int type = params.value(QStringLiteral("type")).toInt(); |
| 536 | const int startAddress = params.value(QStringLiteral("startAddress")).toInt(); |
| 537 | const int count = params.value(QStringLiteral("count")).toInt(); |
| 538 | |
| 539 | auto* modbus = IO::ConnectionManager::instance().modbus(); |
| 540 | const auto& typeList = modbus->registerTypeList(); |
| 541 | |
| 542 | if (type < 0 || type >= typeList.count()) { |
| 543 | return CommandResponse::makeError( |
| 544 | id, |
| 545 | ErrorCode::InvalidParam, |
| 546 | QStringLiteral("Invalid type: %1. Valid range: 0-%2") |
| 547 | .arg(QString::number(type), QString::number(typeList.count() - 1))); |
| 548 | } |
| 549 | |
| 550 | if (startAddress < 0 || startAddress > 65535) { |
| 551 | return CommandResponse::makeError( |
| 552 | id, |
| 553 | ErrorCode::InvalidParam, |
| 554 | QStringLiteral("Invalid startAddress: %1. Valid range: 0-65535").arg(startAddress)); |
| 555 | } |
| 556 | |
| 557 | if (count < 1 || count > 125) { |
| 558 | return CommandResponse::makeError( |
| 559 | id, |
| 560 | ErrorCode::InvalidParam, |
| 561 | QStringLiteral("Invalid count: %1. Valid range: 1-125").arg(count)); |
| 562 | } |
| 563 | |
| 564 | modbus->addRegisterGroup( |
| 565 | static_cast<quint8>(type), static_cast<quint16>(startAddress), static_cast<quint16>(count)); |
| 566 | |
| 567 | QJsonObject result; |
| 568 | result[QStringLiteral("type")] = type; |
| 569 | result[QStringLiteral("typeName")] = typeList.at(type); |
| 570 | result[QStringLiteral("startAddress")] = startAddress; |
| 571 | result[QStringLiteral("count")] = count; |
| 572 | return CommandResponse::makeSuccess(id, result); |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * @brief Remove a register group by index |
nothing calls this directly
no test coverage detected