#pragma once #include "DeviceComplexJsonCommand.h" #include "UsbDeviceManager.h" namespace Incart::DeviceWebApi { class TurnVentJsonCommand final : public DeviceComplexJsonCommand { private: Usb::ByteCommandQueue* const m_commandQueue; Usb::UsbDeviceCommandSet* const m_usbDeviceCommands; public: TurnVentJsonCommand(std::shared_ptr description, Usb::UsbDeviceManager* const usbDeviceManager) : DeviceComplexJsonCommand(description) , m_commandQueue(usbDeviceManager->getCommandQueue()) , m_usbDeviceCommands(usbDeviceManager->getCommandSet()) { } TurnVentJsonCommand(Usb::UsbDeviceManager* const usbDeviceManager) : TurnVentJsonCommand(createCommandDescription(), usbDeviceManager) { } public: static std::shared_ptr createCommandDescription() { return std::make_shared("SetVentState", std::make_shared(std::vector>{ std::make_shared("State", Net::WebApi::EJsonCommandUnitType::String) }), std::make_shared() ); } std::shared_ptr execute(std::shared_ptr launchedCommandInfo_) override { std::shared_ptr launchedCommandInfo = std::dynamic_pointer_cast(launchedCommandInfo_); uid = launchedCommandInfo->uid; if (launchedCommandInfo->data.isNull()) { std::cout << __PRETTY_FUNCTION__ << " json object interpretation error" << std::endl; return DeviceWebApi::JsonCommandStatusCreator::create(Usb::DeviceCommand::EStatus::BAD_DATA); } QVariantMap jsonMap = launchedCommandInfo->data.toMap(); QString state = jsonMap["State"].toString(); uint8_t isOn = 0x00; if (state.compare("on", Qt::CaseInsensitive) == 0) { isOn = 0x01; } else if (state.compare("off", Qt::CaseInsensitive) != 0) { return DeviceWebApi::JsonCommandStatusCreator::create(Usb::DeviceCommand::EStatus::BAD_DATA); } std::vector commandBytes; std::shared_ptr commandInfo = std::make_shared("TurnVent", m_commandQueue->generateUid(), std::vector{ isOn }); Usb::DeviceCommand::EStatus answerStatus = m_usbDeviceCommands->createCommandBytes(commandInfo, commandBytes); if (answerStatus != Usb::DeviceCommand::EStatus::OK) { return std::make_shared(Usb::DeviceCommandStatusConverter::toText(answerStatus)); } std::shared_ptr turnVentCommand = std::make_shared(commandInfo->name, commandInfo->uid, std::move(commandBytes)); connect(turnVentCommand.get(), &Usb::ByteCommand::answerIsReady, this, &TurnVentJsonCommand::handleAnswerIsReady); m_commandQueue->enqueue(turnVentCommand); return std::make_shared(); } private slots: void handleAnswerIsReady(std::shared_ptr /*command*/, std::vector answer, int32_t status) { Usb::DeviceCommand::EStatus answerStatus = (Usb::DeviceCommand::EStatus)status; std::cout << (answerStatus == Usb::DeviceCommand::EStatus::OK ? "TurnVent OK" : "TurnVent is not OK") << std::endl; emit answerIsReady(this, QJsonDocument(), DeviceWebApi::JsonCommandStatusCreator::create(answerStatus, answer)); } }; } // namespace Incart::DeviceWebApi