#pragma once #include "ComplexCommand.h" #include "ByteCommandQueue.h" #include "GetAddressCommand.h" #include "ByteArrayProvider.h" #include "UsbDeviceManager.h" namespace Incart::DeviceComplexCommands { class MicroMonGetAddressCommand : public GetAddressCommand { private: Usb::UsbDeviceManager* const m_usbDeviceManager; std::string m_byteCommandName; public: MicroMonGetAddressCommand(uint32_t uid, const std::string& name, const std::string& byteCommandName, Usb::UsbDeviceManager* const usbDeviceManager) : GetAddressCommand(name, uid) , m_usbDeviceManager(usbDeviceManager), m_byteCommandName(byteCommandName) { } public: std::shared_ptr execute() override { std::vector commandBytes; Usb::ByteCommandQueue* commandQueue = m_usbDeviceManager->getCommandQueue(); std::shared_ptr commandInfo = std::make_shared(m_byteCommandName, commandQueue->generateUid(), std::vector()); Usb::DeviceCommand::EStatus status = m_usbDeviceManager->getCommandSet()->createCommandBytes(commandInfo, commandBytes); if (status != Usb::DeviceCommand::EStatus::OK) { return Usb::DeviceCommandStatusCreator::create({}, status); } std::shared_ptr command = std::make_shared(m_byteCommandName, commandInfo->uid, std::move(commandBytes)); connect(command.get(), &Usb::ByteCommand::answerIsReady, this, &MicroMonGetAddressCommand::handleAnswerIsReady); commandQueue->enqueue(command); return std::make_shared(Usb::DeviceCommand::EStatus::OK); } private slots: void handleAnswerIsReady(std::shared_ptr /*command*/, std::vector answer, int32_t status) { std::shared_ptr statusInfo; uint64_t address = 0; if (status != Usb::DeviceCommand::EStatus::OK) { statusInfo = Usb::DeviceCommandStatusCreator::create(answer, status); } else if (answer.size() < 15) { statusInfo = std::make_shared>>(Usb::DeviceCommand::EStatus::WRONG_FRAME_FORMAT, answer); } else { address = Common::ByteArrayProvider::ulongFromArray(answer, 4); statusInfo = std::make_shared(Usb::DeviceCommand::EStatus::OK); } emit answerIsReady(this, address, statusInfo); } }; } // namespace Incart::DeviceComplexCommands