#pragma once #include "DeviceComplexJsonCommand.h" #include "ReadInnerChannelsLsbCommand.h" #include "UsbDeviceManager.h" #include "AdcChannelsManager.h" #include "ComplexCommandCreateMultidata.h" #include "ReadInnerChannelsCalibrationCommand.h" namespace Incart::DeviceWebApi { class ReadInnerChannelsCalibrationJsonCommand final : public DeviceComplexJsonCommand { private: Usb::ByteCommandQueue* const m_commandQueue; Usb::UsbDeviceCommandSet* const m_deviceCommands; Usb::AdcChannelsManager* const m_signalManager; public: ReadInnerChannelsCalibrationJsonCommand(std::shared_ptr description, Usb::UsbDeviceManager* const usbDeviceManager, Usb::AdcChannelsManager* const signalManager) : DeviceComplexJsonCommand(description) , m_commandQueue(usbDeviceManager->getCommandQueue()) , m_deviceCommands(usbDeviceManager->getCommandSet()) , m_signalManager(signalManager) { } ReadInnerChannelsCalibrationJsonCommand(Usb::UsbDeviceManager* const usbDeviceManager, Usb::AdcChannelsManager* const signalManager) : ReadInnerChannelsCalibrationJsonCommand(createCommandDescription(), usbDeviceManager, signalManager) { } public: static std::shared_ptr createCommandDescription() { return std::make_shared("ReadInnerChannelsCalibration", std::make_shared(), std::make_shared() ); } 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_deviceCommands->createComplexCommand("ReadInnerChannelsCalibration", commandData)); m_executedCommands[command.get()] = command; connect(command.get(), &DeviceComplexCommands::ReadInnerChannelsCalibrationCommand::answerIsReady, this, &ReadInnerChannelsCalibrationJsonCommand::handleCommandAnswerIsReady); return executeCommand(command); } private slots: void handleCommandAnswerIsReady(Usb::ComplexCommand* command, std::vector> calibrationInfo, std::shared_ptr status) { std::string commandName = command->getName(); removeExecutedCommand(command); if (status->status != Usb::DeviceCommand::EStatus::OK) { emit answerIsReady(this, QJsonDocument(), DeviceWebApi::JsonCommandStatusCreator::create(status)); return; } std::unordered_map channelsLsbInfo = m_signalManager->getSignalsLsbInfo(); /* int k = 0; for (auto it = channelsLsbInfo.begin(); it != channelsLsbInfo.end(); ++it,++k) { std::cout << "channelsLsbInfo[" << k << "] " << static_cast(it->first.type) << " " << static_cast(it->first.sourceType) << " | " << static_cast(it->second.channelType) << std::endl; } */ QJsonArray channelInfoArray; for (auto it = calibrationInfo.begin(); it != calibrationInfo.end(); ++it) { uint8_t channelType = (*it)->getChannelType(); auto channelLsbInfo = getLsbInfo(channelType, channelsLsbInfo); if (channelLsbInfo.first.type != DevicesInfo::EDeviceChannelType::Unknown) { QJsonArray mapArray; QJsonArray clbKArray; QJsonArray clbBArray; auto calibrationInfoCreator = channelLsbInfo.second.calibrationInfoCreator; size_t signalCount = channelLsbInfo.second.signalCount; std::vector& calibrationPoints = (*it)->getCalibrationPoints(); for (size_t signalIndex = 0; signalIndex < signalCount; ++signalIndex) { Usb::CalibrationInfo calibrationInfo = calibrationInfoCreator->createCalibrationInfo(static_cast(signalIndex), calibrationPoints); mapArray.append(static_cast(signalIndex)); clbKArray.append(static_cast(calibrationInfo.k)); clbBArray.append(static_cast(calibrationInfo.b)); } std::string channelName = m_signalManager->getChannelName(channelLsbInfo.first); QJsonObject channelInfoObject({ {"Name", QString::fromStdString(channelName)}, {"Map", mapArray}, {"clbK", clbKArray}, {"clbB", clbBArray} }); channelInfoArray.append(channelInfoObject); } } QJsonObject calibInfoObject({{"Channels", channelInfoArray}}); emit answerIsReady(this, QJsonDocument(calibInfoObject), DeviceWebApi::JsonCommandStatusCreator::create(status)); } private: std::pair getLsbInfo(uint8_t channelType_, const std::unordered_map& channelsLsbInfo) { for (auto it = channelsLsbInfo.begin(); it != channelsLsbInfo.end(); ++it) { if ((it->second.channelType == channelType_) && (it->first.source == DevicesInfo::EDeviceChannelSourceType::Device)) { return std::pair(it->first, it->second); } } return std::pair({DevicesInfo::EDeviceChannelType::Unknown, DevicesInfo::EDeviceChannelSourceType::Unknown}, {}); } }; } // namespace Incart::DeviceWebApi