#pragma once #include "UsbDeviceManager.h" #include "ReadCalibrationPointCommand.h" #include "DeviceComplexJsonCommand.h" #include "ReadExternAccCalibrationPointsCommand.h" #include "ComplexCommandCreateData.h" namespace Incart::DeviceWebApi { class ReadExternAccCalibrationJsonCommand final : public DeviceComplexJsonCommand { private: Usb::ByteCommandQueue* const m_commandQueue; Usb::UsbDeviceCommandSet* const m_usbDeviceCommands; Usb::AdcChannelsManager* const m_signalManager; public: ReadExternAccCalibrationJsonCommand(std::shared_ptr description, Usb::UsbDeviceManager* const usbDeviceManager, Usb::AdcChannelsManager* const signalManager) : DeviceComplexJsonCommand(description) , m_commandQueue(usbDeviceManager->getCommandQueue()) , m_usbDeviceCommands(usbDeviceManager->getCommandSet()) , m_signalManager(signalManager) { } ReadExternAccCalibrationJsonCommand(Usb::UsbDeviceManager* const usbDeviceManager, Usb::AdcChannelsManager* const signalManager) : ReadExternAccCalibrationJsonCommand(createCommandDescription(), usbDeviceManager, signalManager) { } public: static std::shared_ptr createCommandDescription() { return std::make_shared("ReadExternAccCalibration", std::make_shared(), std::make_shared() ); } std::shared_ptr execute(std::shared_ptr launchedCommandInfo) override { //Common::JTerminal() << __PRETTY_FUNCTION__; uid = launchedCommandInfo->uid; std::shared_ptr commandData = std::make_shared>(m_commandQueue->generateUid()); std::shared_ptr command = std::dynamic_pointer_cast(m_usbDeviceCommands->createComplexCommand("ReadExternAccCalibrationPoints", commandData)); m_executedCommands[command.get()] = command; connect(command.get(), &DeviceComplexCommands::ReadExternAccCalibrationPointsCommand::answerIsReady, this, &ReadExternAccCalibrationJsonCommand::handleCommandAnswerIsReady); return executeCommand(command); } private slots: void handleCommandAnswerIsReady(Usb::ComplexCommand* command, std::vector calibrationPoints, 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(); auto it = channelsLsbInfo.find(DevicesInfo::DeviceChannelTypeInfo{DevicesInfo::EDeviceChannelType::Acc, DevicesInfo::EDeviceChannelSourceType::Cable}); if (it == channelsLsbInfo.end()) { emit answerIsReady(this, QJsonDocument(), DeviceWebApi::JsonCommandStatusCreator::create(Usb::DeviceCommand::EStatus::NOT_EXIST)); return; } QJsonArray mapArray; QJsonArray clbKArray; QJsonArray clbBArray; auto calibrationInfoCreator = it->second.calibrationInfoCreator; size_t signalCount = it->second.signalCount; 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(DevicesInfo::DeviceChannelTypeInfo{DevicesInfo::EDeviceChannelType::Acc, DevicesInfo::EDeviceChannelSourceType::Cable}); QJsonObject channelInfoObject({ {"Name", QString::fromStdString(channelName)}, {"Map", mapArray}, {"clbK", clbKArray}, {"clbB", clbBArray} }); emit answerIsReady(this, QJsonDocument(channelInfoObject), 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