#pragma once #include #include "UsbDeviceManager.h" #include "ByteCommand.h" #include "ExeGet.h" #include "SimpleCommandInfo.h" #include "DeviceWebApiSimpleCommand.h" namespace Incart::DeviceWebApi { // Используется для Регистратора class DeviceWebApiByteCommand : public Net::WebApi::exeGet { private: Usb::UsbDeviceManager* m_usbDeviceManager; public: DeviceWebApiByteCommand(Usb::UsbDeviceManager* usbDeviceManager) : exeGet(), m_usbDeviceManager(usbDeviceManager) { } public: void exe(Net::WebApi::ParsingURI& url, QTcpSocket* socket) override { QMap urlArgs = url.getArgs(); QString searchArgName = "data"; for (auto it = urlArgs.begin(); it != urlArgs.end(); it++) { if (searchArgName.compare(it.key(), Qt::CaseInsensitive) == 0) { std::vector commandBytes; if (!Common::ByteArrayProvider::tryParseFormatFF(it.value().toStdString(), commandBytes)) { m_resp->retError("400 Bad Request"); sendResponse(socket); return; } Usb::ByteCommandQueue* commandQueue = m_usbDeviceManager->getCommandQueue(); std::shared_ptr webApiCommand = std::make_shared("", commandQueue->generateUid(), socket, std::move(commandBytes)); connect(webApiCommand.get(), &DeviceWebApiSimpleCommand::answerIsReady, this, &DeviceWebApiByteCommand::handleCommandAnswerIsReady); commandQueue->enqueue(webApiCommand); return; } } m_resp->retError("503 Service Unavailable"); sendResponse(socket); } private slots: void handleCommandAnswerIsReady(std::shared_ptr command, std::vector answer, int32_t status) { std::shared_ptr webApiCommand = std::dynamic_pointer_cast(command); if (status == Usb::DeviceCommand::EStatus::OK) { QByteArray byteAnswer; for (size_t i = 0; i < answer.size(); i++) { byteAnswer.append(answer[i]); } m_resp->ret200("txt", byteAnswer); } else if (status == Usb::DeviceCommand::EStatus::TIMEOUT) { m_resp->retError("504 Gateway Timeout"); } else { m_resp->retError("503 Service Unavailable"); } sendResponse(webApiCommand->getSocket()); } }; } // namespace Incart::DeviceWebApi