#pragma once #include #include #include #include "Device7SeriesFrameDecoder.h" #include "Device7SeriesWithADFrameDecoder.h" #include "CableFrameDecoder.h" #include "UsbAdcListener.h" #include "AdcChannelsManager.h" #include "JTerminal.h" #include "AdcViewerMessage.h" #include "FrameParcer.h" #include "EDeviceCommandStatus.h" #include "LsbChannelInfo.h" #include "MicroMonLsbCreators.h" #include "MicroMonCalibrationInfoCreators.h" #include "AdcViewerMessage.h" #include "VisFrameDecoderFactory.h" #include namespace Incart::Usb { class UserAdcChannelsManager final : public AdcChannelsManager { private: using ChannelTypeInfo = DevicesInfo::DeviceChannelTypeInfo; using ChannelType = DevicesInfo::EDeviceChannelType; using InnerChannelTypes = DevicesInfo::InnerDeviceChannelTypes; using ExternChannelTypes = DevicesInfo::ExternDeviceChannelTypes; using ChannelSource = DevicesInfo::EDeviceChannelSourceType; const size_t m_sendBlockSize = 40; FrameParcer m_adcParcer; uint32_t m_errorCount = 0; std::mutex m_mutex; std::vector> m_frameDecoders; AdcViewerMessage m_adcViewerMessage; Common::DateTimeObject m_restartTime; std::unordered_map m_signalsLsbInfo; const DeviceInfo* m_connectedDeviceInfo; public: UserAdcChannelsManager(uint32_t frameBufferSize, UsbAdcListener* const adcListener) : AdcChannelsManager() , m_adcParcer(frameBufferSize) , m_adcViewerMessage(ChannelTypeInfo{ChannelType::Ecg, ChannelSource::Device}, m_sendBlockSize) { connect(adcListener, &UsbAdcListener::signalAnswer, this, &UserAdcChannelsManager::slotAdcAnswer, Qt::QueuedConnection); } public: void setConnectedDeviceInfo(const DeviceInfo& deviceInfo_) override { m_mutex.lock(); // создаем декодеры кадров (предполагается, что один и тот же сигнал не может обрабатываться двумя и более декодерами) m_signalsLsbInfo.clear(); if (deviceInfo_.typeInfo.type == 0) { m_connectedDeviceInfo = nullptr; } else { m_connectedDeviceInfo = &deviceInfo_; } m_restartTime = deviceInfo_.restartTime; m_adcViewerMessage.reset(m_restartTime); m_frameDecoders.clear(); m_frameDecoders = VisFrameDecoderFactory::create(deviceInfo_); if (deviceInfo_.typeInfo.series == DevicesInfo::EDeviceSeries::Series7) { auto ecgChannelInfo = deviceInfo_.typeInfo.getChannelInfo("ecg"); m_signalsLsbInfo[{ChannelType::Ecg, ChannelSource::Device}] = { InnerChannelTypes::Ecg, ecgChannelInfo.maxSignalCount, std::make_shared(), std::make_shared() }; m_signalsLsbInfo[{ChannelType::Reo, ChannelSource::Device}] = { InnerChannelTypes::Reo, 2, std::make_shared(), std::make_shared() }; m_signalsLsbInfo[{ChannelType::Acc, ChannelSource::Device}] = { InnerChannelTypes::Acc, 3, std::make_shared(), std::make_shared() }; m_signalsLsbInfo[{ChannelType::Acc, ChannelSource::Cable}] = { ExternChannelTypes::Acc, 3, std::make_shared(), std::make_shared() }; m_signalsLsbInfo[{ChannelType::Prs, ChannelSource::Device}] = { InnerChannelTypes::Prs, 1, std::make_shared(), std::make_shared() }; m_signalsLsbInfo[{ChannelType::Ton, ChannelSource::Device}] = { InnerChannelTypes::Ton, 1, std::make_shared(), std::make_shared() }; m_signalsLsbInfo[{ChannelType::Oxy, ChannelSource::Device}] = { InnerChannelTypes::Oxy, 3, std::make_shared(), std::make_shared() }; } // связываем декодеры кадров с получателями сигнала std::vector> channelsInfo; for (size_t decoderIndex = 0; decoderIndex < m_frameDecoders.size(); ++decoderIndex) { size_t channelCount = deviceInfo_.typeInfo.channels.size(); std::shared_ptr frameDecoder = m_frameDecoders[decoderIndex]; for (size_t channelIndex = 0; channelIndex < channelCount; ++channelIndex) { auto channelCommonInfo = deviceInfo_.typeInfo.channels[channelIndex]; if (frameDecoder->canHandleChannel(channelCommonInfo.typeInfo)) { std::shared_ptr channelInfo = std::make_shared(); channelInfo->id = channelCommonInfo.typeInfo; channelInfo->name = channelCommonInfo.name; channelInfo->frameDecoder = frameDecoder; channelInfo->blockSize = 0; channelInfo->pointCounter = 0; channelInfo->signalCount = channelCommonInfo.maxSignalCount; channelsInfo.push_back(channelInfo); } } } m_adcViewerMessage.setChannels(channelsInfo); m_mutex.unlock(); } void restart() override { std::cout << __PRETTY_FUNCTION__ << std::endl; m_mutex.lock(); m_adcParcer.restart(); m_adcViewerMessage.reset(m_restartTime); m_signalsLsbInfo.clear(); m_frameDecoders.clear(); m_mutex.unlock(); } virtual std::unordered_map getSignalsLsbInfo() override { return m_signalsLsbInfo; } std::string getChannelName(const DevicesInfo::DeviceChannelTypeInfo& channelTypeInfo_) override { if (m_connectedDeviceInfo == nullptr) { return ""; } size_t channelCount = m_connectedDeviceInfo->typeInfo.channels.size(); for (size_t channelIndex = 0; channelIndex < channelCount; ++channelIndex) { auto& channelCommonInfo = m_connectedDeviceInfo->typeInfo.channels[channelIndex]; if (channelCommonInfo.typeInfo == channelTypeInfo_) { return channelCommonInfo.name; } } return ""; } void setSignalCount(const DevicesInfo::DeviceChannelTypeInfo& channelTypeInfo_, uint8_t signalCount_) override { std::cout << __PRETTY_FUNCTION__ << std::endl; m_mutex.lock(); m_adcViewerMessage.setSignalCount(channelTypeInfo_, signalCount_); m_mutex.unlock(); } public slots: void slotAdcAnswer(std::vector buffer, int32_t status) { if (status != DeviceCommand::EStatus::OK) { return; } for (char b : buffer) { if (m_adcParcer.put(static_cast(b))) { std::vector frameParcerData = m_adcParcer.getResult(); bool decoderIsFound = false; m_mutex.lock(); // send all channels in 1 message pack, cut message packs from stream by selected channel (default: ecg) frequency // in each frame 1 point for (size_t frameDecoderIndex = 0; frameDecoderIndex < m_frameDecoders.size(); ++frameDecoderIndex) { std::shared_ptr frameDecoder = m_frameDecoders[frameDecoderIndex]; if (frameDecoder->exe(frameParcerData)) { decoderIsFound = true; if (frameDecoder->hasButtonLabel()) { std::cout << "Button event" << std::endl; emit eventTransfer(m_adcViewerMessage.getButtonLabelBson(frameDecoder)); } //------ debug output if (frameDecoder->getFrameNumber() % 1000 == 0) { std::string info = std::string(__PRETTY_FUNCTION__); info += std::to_string(frameDecoder->getFrameNumber()) + " " + std::to_string(m_errorCount); Common::JTerminal() << info; } //------- std::vector& channelsData = frameDecoder->getAllData(); bool blockIsReady = false; for (size_t channelIndex = 0; channelIndex < channelsData.size(); ++channelIndex) { auto channelTypeInfo = channelsData[channelIndex].getChannelTypeInfo(); auto& channelData = channelsData[channelIndex].getData(); if (m_adcViewerMessage.addPoint(channelTypeInfo, channelData)) { blockIsReady = true; } } if (blockIsReady) { emit signalTransfer(m_adcViewerMessage.getAdcBson()); m_adcViewerMessage.resetPointCounter(); } break; } } m_mutex.unlock(); if (!decoderIsFound) { m_errorCount++; } } } } }; } // namespace Incart::Usb