#pragma once #include "WriteEcgModeCommand.h" #include "UsbEventLoopWorker.h" #include "EEcgMode.h" #include "ByteArrayProvider.h" #include "MicroMonEcgModeConverter.h" namespace Incart::DeviceComplexCommands { class MicroMonWriteEcgModeCommand : public WriteEcgModeCommand { private: Usb::UsbDevice::EEcgMode m_mode; Usb::UsbEventLoopWorker* m_usbEventLoopWorker; bool m_isAdcOn = true; public: MicroMonWriteEcgModeCommand(uint32_t uid, Usb::UsbDevice::EEcgMode mode, Usb::UsbEventLoopWorker* usbEventLoopWorker) : WriteEcgModeCommand(uid) , m_mode(mode) , m_usbEventLoopWorker(usbEventLoopWorker) { } public: std::shared_ptr execute() override { Common::JTerminal() << __PRETTY_FUNCTION__ << " mode:" << static_cast(m_mode); m_isAdcOn = m_usbEventLoopWorker->isAdcOn(); Usb::ByteCommandQueue* commandQueue = m_usbEventLoopWorker->getDeviceManager()->getCommandQueue(); std::shared_ptr command = std::make_shared("AdcOff", commandQueue->generateUid(), std::vector{ 0x7e, 0x01, 0x00, 0x07, 0x00, 0xff, 0xff, 0xbd }); connect(command.get(), &Usb::ByteCommand::answerIsReady, this, &MicroMonWriteEcgModeCommand::handleAdcOffAnswerIsReady); commandQueue->enqueue(command); return std::make_shared(Usb::DeviceCommand::EStatus::OK); } private slots: void handleAdcOffAnswerIsReady(std::shared_ptr /*command*/, std::vector answer, int32_t status) { if (status != Usb::DeviceCommand::EStatus::OK) { emit answerIsReady(this, Usb::DeviceCommandStatusCreator::create(answer, status)); return; } Usb::ByteCommandQueue* commandQueue = m_usbEventLoopWorker->getDeviceManager()->getCommandQueue(); std::shared_ptr writeModeCommand = std::make_shared("WriteEcgMode", commandQueue->generateUid(), std::vector{ 0x7e, 0x02, 0x00, 0x10, 0x01, MicroMonEcgModeConverter::toByte(m_mode), 0xff, 0xff, 0xbd }); connect(writeModeCommand.get(), &Usb::ByteCommand::answerIsReady, this, &MicroMonWriteEcgModeCommand::handleWriteModeAnswerIsReady); commandQueue->enqueue(writeModeCommand); } void handleWriteModeAnswerIsReady(std::shared_ptr /*command*/, std::vector answer, int32_t status) { if (status != Usb::DeviceCommand::EStatus::OK) { emit answerIsReady(this, Usb::DeviceCommandStatusCreator::create(answer, status)); return; } if ((answer.size() != 8) || (answer[4] != 0)) { std::cout << __PRETTY_FUNCTION__ << " WRONG_FRAME_FORMAT [" << Common::ByteArrayProvider::packToText(answer) << "]" << std::endl; emit answerIsReady(this, std::make_shared>>(Usb::DeviceCommand::EStatus::WRONG_FRAME_FORMAT, answer)); return; } /* ByteCommand* readEcgModeCommand(new ByteCommand(new uint8_t[9]{ 0x7e, 0x02, 0x00, 0x10, 0x00, 0x00, 0xff, 0xff, 0xbd }, 9)); connect(readEcgModeCommand, &ByteCommand::AnswerIsReady, this, &MicroMonWriteEcgModeCommand::HandleReadModeAnswerIsReady); m_usbEventLoopWorker->GetCommandQueue()->Enqueue(readEcgModeCommand); */ if (m_isAdcOn) { Usb::ByteCommandQueue* commandQueue = m_usbEventLoopWorker->getDeviceManager()->getCommandQueue(); std::shared_ptr command = std::make_shared("AdcOn", commandQueue->generateUid(), std::vector{ 0x7e, 0x01, 0x00, 0x07, 0x01, 0xff, 0xff, 0xbd }); connect(command.get(), &Usb::ByteCommand::answerIsReady, this, &MicroMonWriteEcgModeCommand::handleAdcOnAnswerIsReady); commandQueue->enqueue(command); } else { emit answerIsReady(this, std::make_shared(Usb::DeviceCommand::EStatus::OK)); } } void handleReadModeAnswerIsReady(std::shared_ptr /*command*/, std::vector answer, int32_t status) { std::cout << __PRETTY_FUNCTION__ << " " << static_cast(answer[4]) << std::endl; m_mode = MicroMonEcgModeConverter::toCableMode(answer[4]); m_usbEventLoopWorker->getDeviceManager()->getEcgModeController()->changeEcgConfiguration(m_mode); if (status != Usb::DeviceCommand::EStatus::OK) { emit answerIsReady(this, Usb::DeviceCommandStatusCreator::create(answer, status)); return; } if (m_isAdcOn) { Usb::ByteCommandQueue* commandQueue = m_usbEventLoopWorker->getDeviceManager()->getCommandQueue(); std::shared_ptr commandAdcOn = std::make_shared("AdcOn", commandQueue->generateUid(), std::vector{ 0x7e, 0x01, 0x00, 0x07, 0x01, 0xff, 0xff, 0xbd }); connect(commandAdcOn.get(), &Usb::ByteCommand::answerIsReady, this, &MicroMonWriteEcgModeCommand::handleAdcOnAnswerIsReady); commandQueue->enqueue(commandAdcOn); } else { emit answerIsReady(this, std::make_shared(Usb::DeviceCommand::EStatus::OK)); } } void handleAdcOnAnswerIsReady(std::shared_ptr /*command*/, std::vector answer, int32_t status) { emit answerIsReady(this, Usb::DeviceCommandStatusCreator::create(answer, status)); } }; } // namespace Incart::DeviceComplexCommands