#pragma once #include "ComplexCommand.h" #include "ByteCommandQueue.h" #include "GetFrequencyCommand.h" #include "ByteArrayProvider.h" #include "UsbDeviceManager.h" namespace Incart::DeviceComplexCommands { class MicroMonGetFrequencyCommand : public GetFrequencyCommand { private: Usb::UsbDeviceManager* const m_usbDeviceManager; public: MicroMonGetFrequencyCommand(uint32_t uid, Usb::UsbDeviceManager* const usbDeviceManager) : GetFrequencyCommand(uid) , m_usbDeviceManager(usbDeviceManager) { } public: std::shared_ptr execute() override { std::vector commandBytes; Usb::ByteCommandQueue* commandQueue = m_usbDeviceManager->getCommandQueue(); std::shared_ptr commandInfo = std::make_shared("ReadFrequency", 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(commandInfo->name, commandInfo->uid, std::move(commandBytes)); connect(command.get(), &Usb::ByteCommand::answerIsReady, this, &MicroMonGetFrequencyCommand::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; uint16_t frequency = 0; if (status != Usb::DeviceCommand::EStatus::OK) { statusInfo = Usb::DeviceCommandStatusCreator::create(answer, status); } else if (answer.size() != 9) { statusInfo = std::make_shared>>(Usb::DeviceCommand::EStatus::WRONG_FRAME_FORMAT, answer); } else { frequency = getFrequency(answer[4]); if (frequency == 0) { statusInfo = std::make_shared>>(Usb::DeviceCommand::EStatus::WRONG_FRAME_FORMAT, answer); } else { statusInfo = std::make_shared(Usb::DeviceCommand::EStatus::OK); } } emit answerIsReady(this, frequency, statusInfo); } private: uint16_t getFrequency(uint8_t code) { switch (code) { case 2: return 250; case 4: return 500; case 8: return 1000; case 16: return 2000; } return 0; } }; } // namespace Incart::DeviceComplexCommands