#pragma once #include "ByteCommandQueue.h" #include "WriteCableInfoCommand.h" #include "CableInfo.h" namespace Incart::DeviceComplexCommands { class MicroMonWriteCableInfoCommand final : public WriteCableInfoCommand { Q_OBJECT private: Usb::CableInfo m_cableInfo; Usb::ByteCommandQueue* const m_commandQueue; public: MicroMonWriteCableInfoCommand(uint32_t uid, const Usb::CableInfo& cableInfo, Usb::ByteCommandQueue* const commandQueue) : WriteCableInfoCommand(uid) , m_cableInfo(cableInfo) , m_commandQueue(commandQueue) { } public: std::shared_ptr execute() override { std::cout << __PRETTY_FUNCTION__ << " CableNumber=" << m_cableInfo.CableNumber << std::endl; uint8_t day = 0xff; uint8_t month = 0xff; uint8_t year = 0xff; if (!m_cableInfo.DateOfCreate.isNull()) { day = byteToHexDec(static_cast(m_cableInfo.DateOfCreate.day())); month = byteToHexDec(static_cast(m_cableInfo.DateOfCreate.month())); year = byteToHexDec(static_cast(m_cableInfo.DateOfCreate.year() - 2000)); } std::vector commandData{ 0x7e, 0x0a, 0x00, 0x1e, 0x07, static_cast(m_cableInfo.CableRegime), static_cast(m_cableInfo.CableType), /* CableNumber */0, 0, 0, 0, day, month, year, 0xff, 0xff, 0xbd }; std::vector messageData(commandData); Common::ByteArrayProvider::setDWordToArray(m_cableInfo.CableNumber, &commandData[0], 7); std::shared_ptr command = std::make_shared("WriteMainCableInfo", m_commandQueue->generateUid(), std::move(commandData), 4000); int messageSize = 17; std::cout << __PRETTY_FUNCTION__ << " messageSize = " << std::dec << messageSize << " message=" << std::hex; for (int i = 0; i < messageSize; i++) { std::cout << " 0x" << static_cast(messageData[i]); } std::cout << std::endl; connect(command.get(), &Usb::ByteCommand::answerIsReady, this, &MicroMonWriteCableInfoCommand::handleMainCableInfoAnswerIsReady); m_commandQueue->enqueue(command); return std::make_shared(Usb::DeviceCommand::EStatus::OK); } private slots: void handleMainCableInfoAnswerIsReady(std::shared_ptr /*command*/, std::vector answer, int32_t status) { std::cout << __PRETTY_FUNCTION__ << std::endl; if (status != Usb::DeviceCommand::EStatus::OK) { emit answerIsReady(this, Usb::DeviceCommandStatusCreator::create(answer, status)); return; } std::shared_ptr setupCounterCommand = std::make_shared("WriteSetupCounter", m_commandQueue->generateUid(), std::vector{ 0x7e, 0x05, 0x00, 0x1e, 0x06, static_cast(m_cableInfo.SetupCounter & 0x000000ff), static_cast(m_cableInfo.SetupCounter & 0x0000ff00), static_cast(m_cableInfo.SetupCounter & 0x00ff0000), static_cast(m_cableInfo.SetupCounter & 0xff000000), 0xff, 0xff, 0xbd }, 4000); connect(setupCounterCommand.get(), &Usb::ByteCommand::answerIsReady, this, &MicroMonWriteCableInfoCommand::handleSetupCounterAnswerIsReady); m_commandQueue->enqueue(setupCounterCommand); } void handleSetupCounterAnswerIsReady(std::shared_ptr /*command*/, std::vector answer, int32_t status) { std::cout << __PRETTY_FUNCTION__ << std::endl; if (status != Usb::DeviceCommand::EStatus::OK) { emit answerIsReady(this, Usb::DeviceCommandStatusCreator::create(answer, status)); return; } std::shared_ptr cableResetCommand = std::make_shared("ResetCable", m_commandQueue->generateUid(), std::vector{ 0x7e, 0x01, 0x00, 0x1e, 0x0b, 0xff, 0xff, 0xbd }, 4000); connect(cableResetCommand.get(), &Usb::ByteCommand::answerIsReady, this, &MicroMonWriteCableInfoCommand::handleCableResetAnswerIsReady); m_commandQueue->enqueue(cableResetCommand); } void handleCableResetAnswerIsReady(std::shared_ptr /*command*/, std::vector answer, int32_t status) { std::cout << __PRETTY_FUNCTION__ << std::endl; if (status != Usb::DeviceCommand::EStatus::OK) { emit answerIsReady(this, Usb::DeviceCommandStatusCreator::create(answer, status)); } else { // ждем пока прибор запишет информацию в кабель и перезапустит работу с ним QTimer::singleShot(2000, this, [this, answer, status](){ emit answerIsReady(this, Usb::DeviceCommandStatusCreator::create(answer, status)); }); } } private: uint8_t byteToHexDec(uint8_t value) { uint8_t lowByte = static_cast(value % 10); uint8_t highByte = static_cast(value / 10); return static_cast(lowByte + (highByte << 4)); } }; } // namespace Incart::DeviceComplexCommands