* @brief Process an incoming message and generate a response */
| 100 | * @brief Process an incoming message and generate a response |
| 101 | */ |
| 102 | QByteArray API::CommandHandler::processMessage(const QByteArray& data, const CommandOrigin origin) |
| 103 | { |
| 104 | QString type; |
| 105 | QJsonObject json; |
| 106 | |
| 107 | if (!parseMessage(data, type, json)) { |
| 108 | return CommandResponse::makeError( |
| 109 | QString(), ErrorCode::InvalidJson, QStringLiteral("Failed to parse JSON message")) |
| 110 | .toJsonBytes(); |
| 111 | } |
| 112 | |
| 113 | if (type == MessageType::Command) { |
| 114 | const auto request = CommandRequest::fromJson(json); |
| 115 | if (!request.isValid()) { |
| 116 | return CommandResponse::makeError( |
| 117 | request.id, ErrorCode::InvalidMessageType, QStringLiteral("Missing 'command' field")) |
| 118 | .toJsonBytes(); |
| 119 | } |
| 120 | return processCommand(request, origin).toJsonBytes(); |
| 121 | } |
| 122 | |
| 123 | else if (type == MessageType::Batch) { |
| 124 | const QString batchId = json.value(QStringLiteral("id")).toString(); |
| 125 | const auto commandsArray = json.value(QStringLiteral("commands")).toArray(); |
| 126 | |
| 127 | if (commandsArray.isEmpty()) { |
| 128 | return CommandResponse::makeError(batchId, |
| 129 | ErrorCode::InvalidMessageType, |
| 130 | QStringLiteral("Empty or invalid 'commands' array")) |
| 131 | .toJsonBytes(); |
| 132 | } |
| 133 | |
| 134 | if (commandsArray.size() > kMaxBatchCommands) { |
| 135 | return CommandResponse::makeError( |
| 136 | batchId, ErrorCode::InvalidParam, QStringLiteral("Batch size exceeds limit")) |
| 137 | .toJsonBytes(); |
| 138 | } |
| 139 | |
| 140 | const auto batch = BatchRequest::fromJson(json); |
| 141 | return processBatch(batch, origin).toJsonBytes(); |
| 142 | } |
| 143 | |
| 144 | return CommandResponse::makeError(QString(), |
| 145 | ErrorCode::InvalidMessageType, |
| 146 | QStringLiteral("Unknown message type: %1").arg(type)) |
| 147 | .toJsonBytes(); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @brief Process a single command request; remote-origin device-write commands must clear the |
nothing calls this directly
no test coverage detected