#pragma once #include "DeviceComplexJsonCommand.h" #include "GetTimeCommand.h" #include "ComplexCommandCreateData.h" #include "UsbDeviceManager.h" namespace Incart::DeviceWebApi { class GetTimeJsonCommand final : public DeviceComplexJsonCommand { private: Usb::ByteCommandQueue* const m_commandQueue; Usb::UsbDeviceCommandSet* const m_usbDeviceCommands; public: GetTimeJsonCommand(std::shared_ptr description, Usb::UsbDeviceManager* const usbDeviceManager) : DeviceComplexJsonCommand(description) , m_commandQueue(usbDeviceManager->getCommandQueue()) , m_usbDeviceCommands(usbDeviceManager->getCommandSet()) { } GetTimeJsonCommand(Usb::UsbDeviceManager* const usbDeviceManager) : GetTimeJsonCommand(createCommandDescription(), usbDeviceManager) { } public: static std::shared_ptr createCommandDescription() { return std::make_shared("GetTime", std::make_shared(), std::make_shared(std::vector>{ std::make_shared("Year", Net::WebApi::EJsonCommandUnitType::Int), std::make_shared("Month", Net::WebApi::EJsonCommandUnitType::Int), std::make_shared("Day", Net::WebApi::EJsonCommandUnitType::Int), std::make_shared("Hours", Net::WebApi::EJsonCommandUnitType::Int), std::make_shared("Minutes", Net::WebApi::EJsonCommandUnitType::Int), std::make_shared("Seconds", Net::WebApi::EJsonCommandUnitType::Int) }) ); } std::shared_ptr execute(std::shared_ptr launchedCommandInfo) override { 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(), &Usb::GetTimeCommand::answerIsReady, this, &GetTimeJsonCommand::handleAnswerIsReady); return executeCommand(command); } private slots: void handleAnswerIsReady(Usb::ComplexCommand* command, Common::DateTimeObject dateTime, std::shared_ptr status) { removeExecutedCommand(command); QJsonDocument answer; if (status->status == Usb::DeviceCommand::EStatus::OK) { QJsonObject jsonResult({ {"Year", static_cast(dateTime.date.getYear())}, {"Month", static_cast(dateTime.date.getMonth())}, {"Day", static_cast(dateTime.date.getDay())}, {"Hours", static_cast(dateTime.time.getHours())}, {"Minutes", static_cast(dateTime.time.getMinutes())}, {"Seconds", static_cast(dateTime.time.getSeconds())} }); answer = QJsonDocument(jsonResult); } emit answerIsReady(this, answer, DeviceWebApi::JsonCommandStatusCreator::create(status)); } }; } // namespace Incart::DeviceWebApi