#pragma once #include "DeviceComplexJsonCommand.h" #include "ReadCableInfoCommand.h" #include "UsbDeviceManager.h" #include "ComplexCommandCreateData.h" namespace Incart::DeviceWebApi { class ReadCableInfoJsonCommand final : public DeviceComplexJsonCommand { private: Usb::ByteCommandQueue* const m_commandQueue; Usb::UsbDeviceCommandSet* const m_usbDeviceCommands; public: ReadCableInfoJsonCommand(std::shared_ptr description, Usb::UsbDeviceManager* const usbDeviceManager) : DeviceComplexJsonCommand(description) , m_commandQueue(usbDeviceManager->getCommandQueue()) , m_usbDeviceCommands(usbDeviceManager->getCommandSet()) { } ReadCableInfoJsonCommand(Usb::UsbDeviceManager* const usbDeviceManager) : ReadCableInfoJsonCommand(createCommandDescription(), usbDeviceManager) { } public: static std::shared_ptr createCommandDescription() { return std::make_shared("ReadCableInfo", std::make_shared(), std::make_shared(std::vector>{ std::make_shared("Version", Net::WebApi::EJsonCommandUnitType::Int), std::make_shared("CableRegime", Net::WebApi::EJsonCommandUnitType::Int), std::make_shared("CableType", Net::WebApi::EJsonCommandUnitType::Int), std::make_shared("CableNumber", Net::WebApi::EJsonCommandUnitType::Int), std::make_shared("SetupCounter", Net::WebApi::EJsonCommandUnitType::Int), std::make_shared("DateOfCreate", Net::WebApi::EJsonCommandUnitType::String), std::make_shared("BeginUsageDate", Net::WebApi::EJsonCommandUnitType::String), std::make_shared("LastInstallDate", Net::WebApi::EJsonCommandUnitType::String) }) ); } 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::ReadCableInfoCommand::answerIsReady, this, &ReadCableInfoJsonCommand::handleAnswerIsReady); return executeCommand(command); } private slots: void handleAnswerIsReady(Usb::ComplexCommand* command, Usb::CableInfo cableInfo, std::shared_ptr status) { removeExecutedCommand(command); QJsonDocument answer; if (status->status == Usb::DeviceCommand::EStatus::OK) { answer = packCableInfoToJson(cableInfo); } emit answerIsReady(this, answer, DeviceWebApi::JsonCommandStatusCreator::create(status)); } private: QJsonDocument packCableInfoToJson(const Usb::CableInfo& cableInfo) { QJsonObject out({ {"Version", static_cast(cableInfo.Version)}, {"CableRegime", static_cast(cableInfo.CableRegime)}, {"CableType", static_cast(cableInfo.CableType)}, {"CableNumber", static_cast(cableInfo.CableNumber)}, {"SetupCounter", static_cast(cableInfo.SetupCounter)}, {"DateOfCreate", packDate(cableInfo.DateOfCreate)}, {"BeginUsageDate", packDate(cableInfo.BeginUsageDate)}, {"LastInstallDate", packDate(cableInfo.LastInstallDate)} }); return QJsonDocument(out); } QString packDate(QDate date) { return QString("%1-%2-%3").arg(date.year(), 4, 10, QChar('0')).arg(date.month(), 2, 10, QChar('0')).arg(date.day(), 2, 10, QChar('0')); } }; } // namespace Incart::DeviceWebApi