Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Value from Dynamic Sample Store #43

Merged
merged 5 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 201 additions & 7 deletions src/dds_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QMutexLocker>

#include <dds/DCPS/Service_Participant.h>
#include <dds/DCPS/XTypes/Utils.h>

#include <tao/AnyTypeCode/Any.h>

Expand Down Expand Up @@ -78,15 +79,13 @@ std::shared_ptr<TopicInfo> CommonData::getTopicInfo(const QString& topicName)
return topicInfo;
}

//------------------------------------------------------------------------------
QVariant CommonData::readValue(const QString& topicName,
const QString& memberName,
const unsigned int& index)
QVariant CommonData::readMember(const QString& topicName,
const QString& memberName,
unsigned int index)
{
QVariant value;
QMutexLocker locker(&m_sampleMutex);


// Make sure the index is valid
QList<std::shared_ptr<OpenDynamicData>>& sampleList = m_samples[topicName];
if ((int)index >= sampleList.count())
Expand All @@ -112,7 +111,6 @@ QVariant CommonData::readValue(const QString& topicName,
return value;
}


// Store the value into a QVariant
// The tmpValue may seem redundant, but it's very helpful for debug
CORBA::TCKind type = targetMember->getKind();
Expand Down Expand Up @@ -209,8 +207,204 @@ QVariant CommonData::readValue(const QString& topicName,
} // End targetMember type switch

return value;
}

QVariant CommonData::readDynamicMember(const QString& topicName,
const QString& memberName,
unsigned int index)
{
const QVariant error;
QMutexLocker locker(&m_dynamicSamplesMutex);

if (!m_dynamicSamples.contains(topicName)) {
return error;
}

const QList<DDS::DynamicData_var>& sampleList = m_dynamicSamples[topicName];
if (static_cast<int>(index) >= sampleList.count()) {
return error;
}

DDS::DynamicData_var sample = sampleList.at(index);
DDS::DynamicType_var topic_type = sample->type();
OpenDDS::XTypes::MemberPath member_path;
if (member_path.resolve_string_path(topic_type, memberName.toStdString()) != DDS::RETCODE_OK) {
return error;
}

// The direct parent dynamic data of this member
DDS::DynamicData_var parent_data;

// The Id of this member within the direct parent type
DDS::MemberId id;

if (member_path.get_member_from_data(sample, parent_data, id) != DDS::RETCODE_OK) {
return error;
}

DDS::DynamicType_var parent_type = parent_data->type();
DDS::DynamicTypeMember_var dtm;
if (parent_type->get_member(dtm, id) != DDS::RETCODE_OK) {
return error;
}

DDS::MemberDescriptor_var md;
if (dtm->get_descriptor(md) != DDS::RETCODE_OK) {
return error;
}

const DDS::TypeKind member_tk = md->type()->get_kind();
DDS::ReturnCode_t rc = DDS::RETCODE_OK;

switch (member_tk) {
case OpenDDS::XTypes::TK_BOOLEAN:
{
CORBA::Boolean tmp;
rc = parent_data->get_boolean_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
return tmp;
}
break;
}
case OpenDDS::XTypes::TK_BYTE:
{
CORBA::Octet tmp;
rc = parent_data->get_byte_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
return tmp;
}
break;
}
case OpenDDS::XTypes::TK_INT16:
{
CORBA::Short tmp;
rc = parent_data->get_int16_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
return tmp;
}
break;
}
case OpenDDS::XTypes::TK_UINT16:
{
CORBA::UShort tmp;
rc = parent_data->get_uint16_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
return tmp;
}
break;
}
case OpenDDS::XTypes::TK_INT32:
{
CORBA::Long tmp;
rc = parent_data->get_int32_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
return tmp;
}
break;
}
case OpenDDS::XTypes::TK_UINT32:
{
CORBA::ULong tmp;
rc = parent_data->get_uint32_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
return tmp;
}
break;
}
case OpenDDS::XTypes::TK_INT64:
{
CORBA::LongLong tmp;
rc = parent_data->get_int64_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
return static_cast<qlonglong>(tmp);
}
break;
}
case OpenDDS::XTypes::TK_UINT64:
{
CORBA::ULongLong tmp;
rc = parent_data->get_uint64_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
return static_cast<qulonglong>(tmp);
}
break;
}
case OpenDDS::XTypes::TK_FLOAT32:
{
CORBA::Float tmp;
rc = parent_data->get_float32_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
return tmp;
}
break;
}
case OpenDDS::XTypes::TK_FLOAT64:
{
CORBA::Double tmp;
rc = parent_data->get_float64_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
return tmp;
}
break;
}
case OpenDDS::XTypes::TK_CHAR8:
{
CORBA::Char tmp;
rc = parent_data->get_char8_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
return tmp;
}
break;
}
case OpenDDS::XTypes::TK_CHAR16:
{
CORBA::WChar tmp;
rc = parent_data->get_char16_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
return tmp;
}
break;
}
case OpenDDS::XTypes::TK_STRING8:
{
CORBA::String_var tmp;
rc = parent_data->get_string_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
return tmp.in();
}
break;
}
case OpenDDS::XTypes::TK_ENUM:
{
CORBA::Long tmp;
rc = parent_data->get_int32_value(tmp, id);
if (rc == DDS::RETCODE_OK) {
DDS::String8_var name;
rc = OpenDDS::XTypes::get_enumerator_name(name, tmp, md->type());
if (rc == DDS::RETCODE_OK) {
return name.in();
}
}
break;
}
default:
break;
}

return error;
}

} // End CommonData::readValue
//------------------------------------------------------------------------------
QVariant CommonData::readValue(const QString& topicName,
const QString& memberName,
unsigned int index)
{
const QVariant value = readDynamicMember(topicName, memberName, index);
if (!value.isNull()) {
return value;
}
return readMember(topicName, memberName, index);
}


//------------------------------------------------------------------------------
Expand Down
14 changes: 11 additions & 3 deletions src/dds_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class CommonData
*/
static QVariant readValue(const QString& topicName,
const QString& memberName,
const unsigned int& index = 0);
unsigned int index = 0);

/**
* @brief Delete all data samples for a specified topic.
Expand Down Expand Up @@ -293,12 +293,20 @@ class CommonData

private:

static QVariant readMember(const QString& topicName,
const QString& memberName,
unsigned int index = 0);

static QVariant readDynamicMember(const QString& topicName,
const QString& memberName,
unsigned int index = 0);

/**
* @brief Stores the data samples from DDS.
* @details The key is the topic name and the value is the data sample. The
* first sample is always the latest and the last sample is last.
*/
static QMap<QString, QList<std::shared_ptr<OpenDynamicData> > > m_samples;
static QMap<QString, QList<std::shared_ptr<OpenDynamicData>>> m_samples;

/**
* @brief Stores the data sample times.
Expand All @@ -316,7 +324,7 @@ class CommonData

/// Store list of DynamicData objects for each topic.
/// We are storing the timestamps for these samples also in m_sampleTimes.
static QMap<QString, QList<DDS::DynamicData_var> > m_dynamicSamples;
static QMap<QString, QList<DDS::DynamicData_var>> m_dynamicSamples;

/// Mutex for protecting access to m_samples.
static QMutex m_sampleMutex;
Expand Down
Loading