81 lines
2.1 KiB
C++
81 lines
2.1 KiB
C++
#pragma once
|
|
|
|
#include "XNMonitorServer_global.h"
|
|
#include "TypeDefine.h"
|
|
#include "TopicManager.h"
|
|
|
|
#define THISUNUSED(x) (void)(x)
|
|
|
|
class XNFramework;
|
|
using XNFrameworkPtr = std::shared_ptr<XNFramework>;
|
|
|
|
class XNMONITORSERVER_EXPORT DataMonitorBase
|
|
{
|
|
public:
|
|
virtual void Initialize(XNFrameworkPtr framework, uint32_t modelId, uint32_t DDS_type) = 0;
|
|
virtual std::unordered_map<std::string, std::string>
|
|
getStringData(std::vector<std::string> varNames) = 0;
|
|
virtual void setDataByString(std::unordered_map<std::string, std::string> data) = 0;
|
|
virtual bool isInitialized() { return _isInitialized; }
|
|
|
|
protected:
|
|
bool _isInitialized = false;
|
|
};
|
|
|
|
template <typename T>
|
|
class XNMONITORSERVER_EXPORT DataMonitorProduct : public T, public DataMonitorBase
|
|
{
|
|
public:
|
|
DataMonitorProduct() : T() {};
|
|
virtual ~DataMonitorProduct()
|
|
{
|
|
try {
|
|
if (auto topicManager = TopicManager::Instance()) {
|
|
topicManager->unregisterSubscriber(T::topic_name);
|
|
topicManager->unregisterPublisher(T::topic_name);
|
|
}
|
|
} catch (...) {
|
|
return;
|
|
}
|
|
_isInitialized = false;
|
|
};
|
|
|
|
virtual void Initialize(XNFrameworkPtr framework, uint32_t modelId, uint32_t DDS_type) override
|
|
{
|
|
THISUNUSED(framework);
|
|
THISUNUSED(modelId);
|
|
THISUNUSED(DDS_type);
|
|
XNDDSErrorCode ret =
|
|
TopicManager::Instance()->registerSubscriber<typename T::DDSPubSubType>(
|
|
T::topic_name,
|
|
std::bind(&DataMonitorProduct::inputDataListener, this, std::placeholders::_1));
|
|
if (ret != XNDDSErrorCode::SUCCESS) {
|
|
return;
|
|
}
|
|
ret = TopicManager::Instance()->registerPublisher<typename T::DDSPubSubType>(
|
|
T::topic_name, this->dataWriter);
|
|
if (ret != XNDDSErrorCode::SUCCESS || this->dataWriter == nullptr) {
|
|
return;
|
|
}
|
|
_isInitialized = true;
|
|
};
|
|
|
|
virtual std::unordered_map<std::string, std::string>
|
|
getStringData(std::vector<std::string> varNames) override
|
|
{
|
|
if (!isInitialized()) {
|
|
return {};
|
|
}
|
|
return T::getStringData(varNames);
|
|
}
|
|
virtual void setDataByString(std::unordered_map<std::string, std::string> data) override
|
|
{
|
|
if (!isInitialized()) {
|
|
return;
|
|
}
|
|
T::setDataByString(data);
|
|
}
|
|
};
|
|
|
|
using DataMonitorBasePtr = std::shared_ptr<DataMonitorBase>;
|