From f1cb07dddb80d71c8b451f9b02235b258e9ce1f2 Mon Sep 17 00:00:00 2001 From: Son Dinh Date: Wed, 16 Oct 2024 17:02:35 -0500 Subject: [PATCH] Read from dynamic sample store --- src/dds_data.cpp | 203 +++++++++++++++++++++++++++++++++++++++++++++-- src/dds_data.h | 14 +++- 2 files changed, 207 insertions(+), 10 deletions(-) diff --git a/src/dds_data.cpp b/src/dds_data.cpp index 9c0e16e..66353b9 100644 --- a/src/dds_data.cpp +++ b/src/dds_data.cpp @@ -78,15 +78,13 @@ std::shared_ptr 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>& sampleList = m_samples[topicName]; if ((int)index >= sampleList.count()) @@ -112,7 +110,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(); @@ -209,8 +206,200 @@ QVariant CommonData::readValue(const QString& topicName, } // End targetMember type switch return value; +} + +QVariant CommonData::readDynamicMember(const QString& topicName, + const QString& memberName, + unsigned int index) +{ + QVariant value; + QMutexLocker locker(&m_dynamicSamplesMutex); + + if (!m_dynamicSamples.contains(topicName)) { + value = "NULL"; + return value; + } + + const QList& sampleList = m_dynamicSamples[topicName]; + if ((int)index >= sampleList.count()) { + value = "NULL"; + return value; + } + + DDS::DynamicData_var sample = sampleList.at(index); + DDS::DynamicType_var topic_type = sample->type(); + DDS::DynamicTypeMember_var dtm; + DDS::ReturnCode_t rc = topic_type->get_member_by_name(dtm, memberName.toStdString().c_str()); + if (rc != DDS::RETCODE_OK) { + value = "NULL"; + return value; + } + + DDS::MemberDescriptor_var md; + rc = dtm->get_descriptor(md); + if (rc != DDS::RETCODE_OK) { + value = "NULL"; + return value; + } + + const DDS::TypeKind member_tk = md->type()->get_kind(); + const DDS::MemberId id = md->id(); + + bool failed = true; + switch (member_tk) { + case OpenDDS::XTypes::TK_BOOLEAN: + { + CORBA::Boolean tmp; + rc = sample->get_boolean_value(tmp, id); + if (rc == DDS::RETCODE_OK) { + value = tmp; + failed = false; + } + break; + } + case OpenDDS::XTypes::TK_BYTE: + { + CORBA::Octet tmp; + rc = sample->get_byte_value(tmp, id); + if (rc == DDS::RETCODE_OK) { + value = tmp; + failed = false; + } + break; + } + case OpenDDS::XTypes::TK_INT16: + { + CORBA::Short tmp; + rc = sample->get_int16_value(tmp, id); + if (rc == DDS::RETCODE_OK) { + value = tmp; + failed = false; + } + break; + } + case OpenDDS::XTypes::TK_UINT16: + { + CORBA::UShort tmp; + rc = sample->get_uint16_value(tmp, id); + if (rc == DDS::RETCODE_OK) { + value = tmp; + failed = false; + } + break; + } + case OpenDDS::XTypes::TK_INT32: + { + CORBA::Long tmp; + rc = sample->get_int32_value(tmp, id); + if (rc == DDS::RETCODE_OK) { + value = tmp; + failed = false; + } + break; + } + case OpenDDS::XTypes::TK_UINT32: + { + CORBA::UShort tmp; + rc = sample->get_uint16_value(tmp, id); + if (rc == DDS::RETCODE_OK) { + value = tmp; + failed = false; + } + break; + } + case OpenDDS::XTypes::TK_INT64: + { + CORBA::LongLong tmp; + rc = sample->get_int64_value(tmp, id); + if (rc == DDS::RETCODE_OK) { + value = tmp; + failed = false; + } + break; + } + case OpenDDS::XTypes::TK_UINT64: + { + CORBA::ULongLong tmp; + rc = sample->get_uint64_value(tmp, id); + if (rc == DDS::RETCODE_OK) { + value = tmp; + failed = false; + } + break; + } + case OpenDDS::XTypes::TK_FLOAT32: + { + CORBA::Float tmp; + rc = sample->get_float32_value(tmp, id); + if (rc == DDS::RETCODE_OK) { + value = tmp; + failed = false; + } + break; + } + case OpenDDS::XTypes::TK_FLOAT64: + { + CORBA::Double tmp; + rc = sample->get_float64_value(tmp, id); + if (rc == DDS::RETCODE_OK) { + value = tmp; + failed = false; + } + break; + } + case OpenDDS::XTypes::TK_CHAR8: + { + CORBA::Char tmp; + rc = sample->get_char8_value(tmp, id); + if (rc == DDS::RETCODE_OK) { + value = tmp; + failed = false; + } + break; + } + case OpenDDS::XTypes::TK_CHAR16: + { + CORBA::WChar tmp; + rc = sample->get_char16_value(tmp, id); + if (rc == DDS::RETCODE_OK) { + value = tmp; + failed = false; + } + break; + } + case OpenDDS::XTypes::TK_STRING8: + { + char* tmp; + rc = sample->get_string_value(tmp, id); + if (rc == DDS::RETCODE_OK) { + value = tmp; + failed = false; + } + break; + } + case OpenDDS::XTypes::TK_ENUM: + default: + break; + } + + if (failed) { + value = "NULL"; + } + return value; +} + +//------------------------------------------------------------------------------ +QVariant CommonData::readValue(const QString& topicName, + const QString& memberName, + unsigned int index) +{ + const QVariant value = readMember(topicName, memberName, index); + if (value.toString() != "NULL") { + return value; + } -} // End CommonData::readValue + return readDynamicMember(topicName, memberName, index); +} //------------------------------------------------------------------------------ diff --git a/src/dds_data.h b/src/dds_data.h index 69a831f..70c5694 100644 --- a/src/dds_data.h +++ b/src/dds_data.h @@ -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. @@ -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 > > m_samples; + static QMap>> m_samples; /** * @brief Stores the data sample times. @@ -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 > m_dynamicSamples; + static QMap> m_dynamicSamples; /// Mutex for protecting access to m_samples. static QMutex m_sampleMutex;