#pragma once #include "DeviceComplexJsonCommand.h" #include "GetDataFromEepromCommand.h" #include #include "UsbDeviceManager.h" #include "ComplexCommandCreateMultidata.h" #include "EEeramVersion9Address.h" #include "ByteArrayProvider.h" #include "InfoMap.h" namespace Incart::DeviceWebApi { class GetExamFileNameJsonCommand final : public DeviceComplexJsonCommand { private: Usb::ByteCommandQueue* const m_commandQueue; Usb::UsbDeviceCommandSet* const m_usbDeviceCommands; public: GetExamFileNameJsonCommand(std::shared_ptr description, Usb::UsbDeviceManager* const usbDeviceManager) : DeviceComplexJsonCommand(description) , m_commandQueue(usbDeviceManager->getCommandQueue()) , m_usbDeviceCommands(usbDeviceManager->getCommandSet()) { } GetExamFileNameJsonCommand(Usb::UsbDeviceManager* const usbDeviceManager) : GetExamFileNameJsonCommand(createCommandDescription(), usbDeviceManager) { } public: static std::shared_ptr createCommandDescription() { return std::make_shared("GetExamFileName", std::make_shared(), std::make_shared(std::vector>{ std::make_shared("FileName", Net::WebApi::EJsonCommandUnitType::String) }) ); } std::shared_ptr execute(std::shared_ptr launchedCommandInfo) override { uid = launchedCommandInfo->uid; std::shared_ptr commandData = std::make_shared>(m_commandQueue->generateUid(), static_cast(DeviceComplexCommands::EEeramVersion9Address::AuxExamInfo), sizeof(uint16_t)); std::shared_ptr command = std::dynamic_pointer_cast(m_usbDeviceCommands->createComplexCommand("GetDataFromEeprom", commandData)); m_executedCommands[command.get()] = command; connect(command.get(), &DeviceComplexCommands::GetDataFromEepromCommand::answerIsReady, this, &GetExamFileNameJsonCommand::handleReadInfoMapLengthIsReady); return executeCommand(command); } private slots: void handleReadInfoMapLengthIsReady(Usb::ComplexCommand* command, std::vector data, std::shared_ptr status) { removeExecutedCommand(command); if (status->status != Usb::DeviceCommand::EStatus::OK) { emit answerIsReady(this, QJsonDocument(), DeviceWebApi::JsonCommandStatusCreator::create(status)); return; } uint16_t infoMapLength = Common::ByteArrayProvider::getWordFromArray(data, 0); std::shared_ptr readInfoMapCommandData = std::make_shared>(m_commandQueue->generateUid(), static_cast(DeviceComplexCommands::EEeramVersion9Address::AuxExamInfo) + sizeof(uint16_t), infoMapLength); std::shared_ptr readInfoMapCommand = std::dynamic_pointer_cast(m_usbDeviceCommands->createComplexCommand("GetDataFromEeprom", readInfoMapCommandData)); m_executedCommands[readInfoMapCommand.get()] = readInfoMapCommand; connect(readInfoMapCommand.get(), &DeviceComplexCommands::GetDataFromEepromCommand::answerIsReady, this, &GetExamFileNameJsonCommand::handleReadInfoMapIsReady); std::shared_ptr jsonStatus = executeCommand(readInfoMapCommand); if (jsonStatus->result != Net::WebApi::EJsonCommandStatus::OK) { emit answerIsReady(this, QJsonDocument(), jsonStatus); return; } } void handleReadInfoMapIsReady(Usb::ComplexCommand* command, std::vector data, std::shared_ptr status) { removeExecutedCommand(command); if (status->status != Usb::DeviceCommand::EStatus::OK) { emit answerIsReady(this, QJsonDocument(), DeviceWebApi::JsonCommandStatusCreator::create(status)); return; } DeviceComplexCommands::InfoMap infoMap; infoMap.readFromBuffer(data); if (!infoMap.contains("fname")) { emit answerIsReady(this, QJsonDocument(), DeviceWebApi::JsonCommandStatusCreator::create(Usb::DeviceCommand::EStatus::BAD_DATA)); return; } std::string examFileName = infoMap.getValue("fname"); QJsonObject jsonResult({ {"FileName", QString::fromStdString(examFileName)} }); QJsonDocument answer(jsonResult); emit answerIsReady(this, answer, DeviceWebApi::JsonCommandStatusCreator::create(status)); } }; } // namespace Incart::DeviceWebApi