#pragma once #include "DeviceComplexJsonCommand.h" #include "ReadCableVersionCommand.h" #include "DeviceCommandInfoJsonPacker.h" #include "UsbDeviceManager.h" #include "ComplexCommandCreateData.h" namespace Incart::DeviceWebApi { class ReadCableVersionJsonCommand final : public DeviceComplexJsonCommand { private: Usb::ByteCommandQueue* const m_commandQueue; Usb::UsbDeviceCommandSet* const m_usbDeviceCommands; public: ReadCableVersionJsonCommand(std::shared_ptr description, Usb::UsbDeviceManager* const usbDeviceManager) : DeviceComplexJsonCommand(description) , m_commandQueue(usbDeviceManager->getCommandQueue()) , m_usbDeviceCommands(usbDeviceManager->getCommandSet()) { } ReadCableVersionJsonCommand(Usb::UsbDeviceManager* const usbDeviceManager) : ReadCableVersionJsonCommand(createCommandDescription(), usbDeviceManager) { } public: static std::shared_ptr createCommandDescription() { return std::make_shared("ReadCableVersion", std::make_shared(), std::make_shared(std::vector>{ std::make_shared("HardwareVersion", Net::WebApi::EJsonCommandUnitType::Int), std::make_shared("SoftwareVersion", Net::WebApi::EJsonCommandUnitType::Int), std::make_shared("Uid", 12, std::make_shared(Net::WebApi::EJsonCommandUnitType::String)), std::make_shared("UidCrc", Net::WebApi::EJsonCommandUnitType::Int) }) ); } std::shared_ptr execute(std::shared_ptr launchedCommandInfo) override { //Common::JTerminal() << __PRETTY_FUNCTION__; uid = launchedCommandInfo->uid; std::shared_ptr commandData = std::make_shared>(m_commandQueue->generateUid()); std::shared_ptr command = std::dynamic_pointer_cast(m_usbDeviceCommands->createComplexCommand(launchedCommandInfo->name, commandData)); m_executedCommands[command.get()] = command; connect(command.get(), &DeviceComplexCommands::ReadCableVersionCommand::answerIsReady, this, &ReadCableVersionJsonCommand::handleAnswerIsReady); return executeCommand(command); } private slots: void handleAnswerIsReady(Usb::ComplexCommand* command, DeviceComplexCommands::CableVersion cableVersion, std::shared_ptr statusInfo) { removeExecutedCommand(command); QJsonDocument answer; if (statusInfo->status == Usb::DeviceCommand::EStatus::OK) { answer = packCableVersionToJson(cableVersion); } emit answerIsReady(this, answer, DeviceWebApi::JsonCommandStatusCreator::create(statusInfo)); } private: QJsonDocument packCableVersionToJson(const DeviceComplexCommands::CableVersion& cableVersion) { QJsonObject out({ {"HardwareVersion", static_cast(cableVersion.HardwareVersion)}, {"SoftwareVersion", static_cast(cableVersion.SoftwareVersion)}, {"Uid", Usb::DeviceCommandInfoJsonPacker::pack(cableVersion.Uid)}, {"UidCrc", static_cast(cableVersion.UidCrc)} }); return QJsonDocument(out); } }; } // namespace Incart::DeviceWebApi