#pragma once #include "DeviceComplexJsonCommand.h" #include #include #include "UsbDeviceCommandSet.h" #include "JsonCommandFactory.h" #include namespace Incart::DeviceWebApi { class GetCmdListJsonCommand final : public DeviceComplexJsonCommand { private: Net::WebApi::JsonCommandFactory* m_jsonCommands; public: GetCmdListJsonCommand(std::shared_ptr description, Net::WebApi::JsonCommandFactory* jsonCommands) : DeviceComplexJsonCommand(description) , m_jsonCommands(jsonCommands) { } GetCmdListJsonCommand(Net::WebApi::JsonCommandFactory* jsonCommands) : GetCmdListJsonCommand(createCommandDescription(), jsonCommands) { } public: static std::shared_ptr createCommandDescription() { return std::make_shared("GetCmdList", std::make_shared(), std::make_shared(std::vector>{ std::make_shared("CmdList", Net::WebApi::EJsonCommandUnitType::String) }) ); } std::shared_ptr execute(std::shared_ptr launchedCommandInfo) override { uid = launchedCommandInfo->uid; std::vector commands = m_jsonCommands->getDescriptions(); QJsonArray jsonCmdList; for (size_t i = 0; i < commands.size(); ++i) { jsonCmdList.push_back(createJsonObject(commands[i])); } QJsonDocument answer(jsonCmdList); emit answerIsReady(this, answer, std::make_shared()); return std::make_shared(); } private: QJsonObject createJsonObject(const std::string& text) { QJsonObject obj; QJsonDocument doc = QJsonDocument::fromJson(QString::fromStdString(text).toUtf8()); if(!doc.isNull()) { if(doc.isObject()) { obj = doc.object(); } else { std::cout << "createJsonObject error: Document is not an object! text: " << text << std::endl; } } else { std::cout << "createJsonObject error: Invalid JSON! text: " << text << std::endl; } return obj; } }; } // namespace Incart::DeviceWebApi